This afternoon I needed to rename a bunch of files from one form to another in a command shell… Well technically I didn't need to do it in a shell - but, as sure as there is a hole in my ass, I wasn't gonna go through renaming them all manually!
They needed to go from, for example, add.png to add_32.png. After a little research into commands like printf, awk, bison and so on - I suddenly realized that 'cut' held the key!
for i in *; do j=`echo $i | cut -d . -f 1`; j=$j"_32.png"; mv $i $j; done
Basically, this says "for every file in the folder, cut the filename on all dots and take the first result into variable 'j'. Then append '_32.png' onto the end of the variable. Finally move the original file to the new filename".
I hope this saves someone else a bit of time!
17 Comments
renaming script
Looking for a solution..please any help...
Any scripts, software or work arounds, that will allow renaming of 3 part file which would add a 3 digit value example a folio id. If a change to some of the programs could be made to add an option, example do a search and replace for the next 3 files. Then a list could be used.
Thanks in advance...
Rich
An example
Not sure what you mean but an example might help...
THANKS FOR THE QUICK RESPONSE
Wkflow consists of a folder with up to 96 pages. Each page consists of a 3 part file. These are a handshake format.
Example CustomerA3149R.pg
CustomerA3149R.ch
CustomerA3149R.lh
Need to rename with a folio at the beginning.
Example 003_LHJ3149R.pg
003_LHJ3149R.ch
003_LHJ3149R.lh
Typical renaming programs support a single or group of single files, ie ,,one image file or a .mp3 file.
My problem is i'm looking to be able to handle a 3 part file as shown, utilizing a sequential renaming function but I get hung up on the 3 part factor.
It wants to rename 003,004,005. I wish as stated before to have the program prompt how many files do you want to use the identified sequential number. 1 for normal processing , 2 for a 2 part file or 3 for a 3 part.....hope this better explains.
The other factor is the link between the .pg file must still link the .ch and the .lh
Thanks
Rich
Try this
This should do the trick:
If the prefix is a part of a path, you might use something like this:
That would set prefix to the 3rd part of a path (eg: /x/y/z -> z). Attention to the inversed quotes (`) inside the double quotes.
Or you can use another "for" before to get the prefix from paths/files.
Great! thanks!
Saved me a lot of time! Thank you! By the way, your site's theme is awesome.
More universal script
I just figured out a more universal script...
change searchstring and replacestring for whatever you want to replace. If a file is missing this searchstring, then you will get an error for that file and nothing will happen to it.
oo
WOAH MAGIC! Thanks!!
Thanks
Just a quick note to say I stumbled across this page perhaps a few weeks ago, and the little snippet of code you've posted repeatedly saves me absurd amounts of time. Thanks!
This one works
This one works recursively:
The only change is the initial file gathering, from "*" to "`find`".
The script breaks if the
The script breaks if the folder name contains white space...
renaming multipal recordings.
Hi, i just want to rename multiple rec. which is present in a particular single directory.for EX:
P889158-271-20090908-112524-1252434324.4956.wav
P889158-271-20090908-112558-1252434324.4956.wav
P889158-271-20090908-112625-1252434324.4956.wav
Like this there are 1000 of rec...I have to just rename the first character of those rec. Ex:- abc-271-20090908-112524-1252434324.4956.wav
abc-271-20090908-112558-1252434324.4956.wav
so can u plz help me how can i rename those rec. at once.
The "rename" command also
The "rename" command also does exactly what you are looking for. It batch renames files based using Perl regular expressions. Here is the rename command that accomplishes what you are doing:
rename -v 's/\./_32./g' *
Cheers,
Jason
Try this command: rename -v
Try this command:
rename -v 's!P889158!abc!g' *.wav
If you want to replace everything before the 1st "-" with abc, do the following:
rename -v 's!^.*?-!abc-!g' *.wav
(-v just prints out the changes that were made)
Also if you want to see what the rename command would do without actually renaming the files, use the "-n" flag.
Thanks!
This is exactly what I was looking for, worked like a charm removing IMG_ from a set of images.
Problem
I want to add dates next to the file name. For example: "hello_25November2009.txt" or "hello_25-11-2009.txt", assuming that the original file name is hello.txt
Thanks in advance.
Renaming multiple files in batch
samiLyle says:
> I want to add dates next to the file name. For
> example: "hello_25November2009.txt" or "hello_25-11-2009.txt",
> assuming that the original file name is hello.txt
If someone wants to do this on Windows, here is a quick script.
# Script TimeRenamer.txt # Input argument - folder path var str path # Go to the folder path cd $path # Collect the list of files. var str list, file, time, name find -n "*" $path ($ftype=="f") > $list # Go thru list of files one by one while ($list <> "") do # Get the next file. lex "1" $list > $file # Get the file creation time. af $file ; set $time = $fctime # Get the file's name from the path stex -p "^/^l[" $file > $name # Insert $time before the last dot. sin "^.^l" $time $name # Rename file. system rename ("\""+$file+"\"") ("\""+$name+"\"") doneScript is in biterscripting ( http://www.biterscripting.com ) . Copy and paste the script into a file /Scripts/TimeRenamer.txt, start biterscripting and enter the following command.
script "/Scripts/TimeRenamer.txt" path("/path/to/some/directory")That's it.
sed would have done the trick too
Works also with filenames that contain space when using
echofor the variables:for name in *.png; do new=`echo $name | sed s/'.png'/'_32.png'/`; mv "`echo $name`" "`echo $new`"; donePost new comment