Uses 'rename' to pad zeros in front of first existing number in each filename. The "--" is not required, but it will prevent errors on filenames which start with "-". You can change the "2d" to any number you want, equaling the total numeric output: aka, 4d = ????, 8d = ????????, etc. I setup a handful of handy functions to this effect (because I couldn't figure out how to insert a var for the value) in the form of 'padnum?', such as: padnum5 () { /usr/bin/rename 's/\d+/sprintf("%05d",$&)/e' -- $@ } Which would change a file "foo-1.txt" to "foo-00001.txt"
Good old bracket expansion :-) For large numbers of files, "rename" will spare you the for-loop, or the find/exec...
I used this when I had a directory of movies from a camera. I wanted to watch a little of each movie, then rename it depending on what was in the movie. This did the trick for me.
Useful if you have a list of images called 1 2 3 4 and so on, you can adapt it to rewrite it as 4 (in this example) 0-padded number. Show Sample Output
Batch rename extension of all files in a folder, in the example from .txt to .md mmv most likely must be installed, but is very powerfull when you want to move/copy/append/link multiple files by wildcard patterns.
FILENAME=nohup.out mv -iv $FILENAME{,.$(stat -c %Y $FILENAME)} does it help ? Show Sample Output
seq allows you to format the output thanks to the -f option. This is very useful if you want to rename your files to the same format in order to be able to easily sort for example:
for i in `seq 1 3 10`; do touch foo$i ;done
And
ls foo* | sort -n
foo1
foo10
foo4
foo7
But:
for i in `seq -f %02g 1 3 10`; do touch foo$i ;done
So
ls foo* | sort -n
foo01
foo04
foo07
foo10
Show Sample Output
Substitute spaces in filename with underscore, it work on the first space encountered.
jhead is a very nice tool to do all sorts of things with photographs, in a batch-oriented way. It has a specific function to rename files based on dates, and the format I used above was just an example. Show Sample Output
Renames all files in a directory named foo to bar. foobar1 gets renamed to barbar1 barfoo2 gets renamed to barbar2 fooobarfoo gets renamed to barobarfoo NOTE: Will break for files with spaces AND new lines AND for an empty expansion of the glob '*'
Note that the -i will not help in a script. Proper error checking is required. Show Sample Output
rename is a really powerfull to, as its name suggests, rename files Show Sample Output
renames Anime Episodes to files, that can be parsed by sonarr & co Show Sample Output
This will change all files ending in .JPG to .jpg and will work with any file extension
Uses vi style search / replace in bash to rename files. Works with regex's too (I use the following a script to fixup / shorten file names): # Remove complete parenthetical/bracket/brace phrases rename 's/\(.*\)//g' * rename 's/\[.*\]//g' * rename 's/\{.*\}//g' * Show Sample Output
All words of the filenames except "a", "of", "that" and "to" are capitalized.
To also match words which begin with a specific string, you can use this:
rename 's/\b((?!hello\b|t)[a-z]+)/\u$1/g' *
This will capitalize all words except "hello" and words beginning with "t".
An entirely shell-based solution (should work on any bourne-style shell), more portable on relying on the rename command, the exact nature of which varies from distro to distro.
Same thing using bash built-in features instead of a sub-shell.
rename file name with fixed length nomeric format pattern Show Sample Output
imagemagick is required Show Sample Output
commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.
Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.
» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10
Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):
Subscribe to the feed for: