943,563 Members | Top Members by Rank

Ad:
Sep 11th, 2008
0

bulk rename files

Expand Post »
Hi,
I just need to know a simple command to bulk renaming.
I have hundreds of .DEB files. I want to remove 4%3a from all file names.

example :
old name :
ark-kde4_4%3a4.0.3-0ubuntu4_i386.deb
new name :
ark-kde4_4.0.3-0ubuntu4_i386.deb

I know there is a "rename" command but I don't know it's use.

Thank you
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bimaljr is offline Offline
51 posts
since Oct 2006
Sep 11th, 2008
0

Re: bulk rename files

Usually we use the "mv" (move) command to rename a file. Try something like this!

#!/bin/sh

for FILE in $(find . -type f -name '*4%3a*'); do
  NEWNAME=$(echo $FILE|sed s/'4%3a'//)
  mv -v $FILE $NEWNAME
done

If the files are all in one directory, you can run this from in that directory and it should get all of them. It's very basic, so if you run into any problems let us know. It worked on a test run with files formatted like your example.

Hope this helps!
-G
Reputation Points: 46
Solved Threads: 28
Junior Poster
Gromit is offline Offline
183 posts
since Sep 2008
Sep 11th, 2008
0

Re: bulk rename files

Forgot to add comments!

#!/bin/sh

# first we need a list to work from.  the
# "for" statement will loop through the results
# of our "find" command, using each result in turn
# as "$FILE".  The find command that I used will
# recursive look in subdirectories as well.  You can
# fix that with a flag, or you could use something
# like `ls |grep '4%3a'`
for FILE in $(find . -type f -name '*4%3a*'); do

  # here we use sed to get rid of the unwanted string
  # and store it as "$NEWNAME"
  NEWNAME=$(echo $FILE|sed s/'4%3a'//)

  # here we do the actual work!
  mv -v $FILE $NEWNAME

done
Reputation Points: 46
Solved Threads: 28
Junior Poster
Gromit is offline Offline
183 posts
since Sep 2008
Sep 11th, 2008
0

Re: bulk rename files

How to use this ? Sorry I don't know.
Because it's not a single line command.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bimaljr is offline Offline
51 posts
since Oct 2006
Sep 11th, 2008
1

Re: bulk rename files

Hmm... well, technically you could run it all on one line like this:

for FILE in $(find . -type f -name '*4%3a*'); do NEWNAME=$(echo $FILE|sed s/'4%3a'//); mv -v $FILE $NEWNAME; done
but that's kind of ugly :-P

What we have here is called a shell script. What you'll probably want to do is paste the script from above into a text file. Name that text file "myrename.sh" or something easy to remember. Copy it into your ~/bin directory, or if this is a temporary project, save it in the directory where all those files live, and execute it like:

$ sh /path/to/myrename.sh
I hope that's not too confusing.
-G
Reputation Points: 46
Solved Threads: 28
Junior Poster
Gromit is offline Offline
183 posts
since Sep 2008
Sep 12th, 2008
0

Re: bulk rename files

Thank you..
It works as you say.. It's perfectly working

Thanks again
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bimaljr is offline Offline
51 posts
since Oct 2006
Sep 16th, 2008
0

Re: bulk rename files

Click to Expand / Collapse  Quote originally posted by bimaljr ...
Hi,
I just need to know a simple command to bulk renaming.
I have hundreds of .DEB files. I want to remove 4%3a from all file names.

example :
old name :
ark-kde4_4%3a4.0.3-0ubuntu4_i386.deb
new name :
ark-kde4_4.0.3-0ubuntu4_i386.deb

I know there is a "rename" command but I don't know it's use.

Thank you
you can use the script here
eg usage
# ./script.sh -s "4%3a" -e "" -d "*.deb" # display all files to be renamed
# ./script.sh -s "4%3a" -e "" -d "*.deb"  #remove -d to do actual rename
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006
Sep 18th, 2008
0

Re: bulk rename files

Thank you ghostdog74,
This one is also userfull
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
bimaljr is offline Offline
51 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in IT Professionals' Lounge Forum Timeline: Tools/methods to speed up web build process...?
Next Thread in IT Professionals' Lounge Forum Timeline: Targeted email marketing & its benefits





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC