bulk rename files

Reply

Join Date: Oct 2006
Posts: 42
Reputation: bimaljr is an unknown quantity at this point 
Solved Threads: 0
bimaljr bimaljr is offline Offline
Light Poster

bulk rename files

 
0
  #1
Sep 11th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: bulk rename files

 
0
  #2
Sep 11th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: bulk rename files

 
0
  #3
Sep 11th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: bimaljr is an unknown quantity at this point 
Solved Threads: 0
bimaljr bimaljr is offline Offline
Light Poster

Re: bulk rename files

 
0
  #4
Sep 11th, 2008
How to use this ? Sorry I don't know.
Because it's not a single line command.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 61
Reputation: Gromit is an unknown quantity at this point 
Solved Threads: 7
Gromit's Avatar
Gromit Gromit is offline Offline
Junior Poster in Training

Re: bulk rename files

 
1
  #5
Sep 11th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: bimaljr is an unknown quantity at this point 
Solved Threads: 0
bimaljr bimaljr is offline Offline
Light Poster

Re: bulk rename files

 
0
  #6
Sep 12th, 2008
Thank you..
It works as you say.. It's perfectly working

Thanks again
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: bulk rename files

 
0
  #7
Sep 16th, 2008
Originally Posted by bimaljr View 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
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 42
Reputation: bimaljr is an unknown quantity at this point 
Solved Threads: 0
bimaljr bimaljr is offline Offline
Light Poster

Re: bulk rename files

 
0
  #8
Sep 18th, 2008
Thank you ghostdog74,
This one is also userfull
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the IT Professionals' Lounge Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC