rename: Ultimate Guide to Batch File Renaming in Linux
Unlock the power of batch file renaming in Linux with the rename utility. Explore a comprehensive collection of commands for renaming files and directories. Learn how to remove parentheses, replace spaces with underscores, convert case, change extensions, add prefixes, and much more.
Remove all Parentheses from file/folder names
rename '(' '' *
rename ')' '' *
Rename all spaces to underscore
for file in *' '*; do
mv -- "$file" "${file// /_}";
done
Rename all files with spaces to underscore
for file in *; do
mv -- "$file" "${file// /_}";
done
Rename uppercase to lowercase
for i in *; do
mv $i `echo $i | tr 'A-Z' 'a-z'`;
done
for i in $( ls | grep [A-Z] ); do
mv -i $i `echo $i | tr 'A-Z' 'a-z'`;
done
Rename all uppercase to lowercase
rename 'y/A-Z/a-z/' *
Remove .sh file extension for files in the current directory
rename 's/\.sh//' ./*
Remove all spaces from all files in the current folder
rename 's/ //g' *
Rename multiple files from .php to .html
rename s/ .php/ .html/ *.html
Replace spaces in filenames with underscores
rename 's/ /_/g' *
Batch rename extension of all files in a folder, in the example from .txt to .md
rename *.JPG *.jpg
Renames all files in the current directory to contain no space characters
rename 's/ /_/g' *
Batch file suffix renaming
rename -n "s/-.*//" *
Rename all files containing 'foo' with 'bar'
rename foo bar directory/filename
rename foo bar filename
Batch rename extension of all files in a folder, from .txt to .md
rename .txt .md *.txt
Rename all files containing 'foo' with 'bar'
rename 's/foo/bar/g' ./*
Batch rename extension of all files in a folder, from .txt to .md
rename 's/.txt/.md/i' *
Convert single digits to double digits
rename 's/\d+/sprintf("%02d",$&)/e' $@
Replace spaces in filenames
rename "s/ *//g" *.jpg
Rename multiple files in one go
rename 's/.xls/.ods/g' *.xls
Convert filenames in the current directory to lowercase
rename 'y/A-Z/a-z/' *
Title Case Files
rename 's/\b((?!(a|of|that|to)\b)[a-z]+)/\u$1/g' *
Convert uppercase files to lowercase
rename 'y/A-Z/a-z/' *
Change the extension of a filename
rename .JPG .jpg *.JPG
Replace strings in filenames
rename 's/foo/bar/g' foobar
Clean up poorly named TV shows
rename -v 's/.*[s,S](\d{2}).*[e,E](\d{2}).*\.avi/SHOWNAME\ S$1E$2.avi/' poorly.named.file.s01e01.avi
Yet Another Rename (bash function)
rename(){
txtToReplace=${1} ;
replacementTxt=${2} ;
shift 2 ;
files=${@} ;
for file in $files ; do
mv ${file} ${file/${txtToReplace}/${replacementTxt}} ;
done
}
Convert spaces in filenames to underscores
rename 'y/ /_/' *
Rename all .jpeg and .JPG files to have .jpg extension
rename 's/\.jpe?g$/.jpg/i' *
Replace spaces in a filename with hyphens
rename 's/ /-/g' *
Replace spaces in filenames with underscores
rename 'y/ /_/' *
Copy/move a bunch of files to dot files and back
rename s/^/./ *
Rename all .jpeg and .JPG files to .jpg
rename 's/\.jpeg/\.jpg/' *.jpeg; rename 's/\.JPG/\.jpg/' *.JPG
Rename files in batch
rename 's/^hospital\.php\?loc=(\d{4})$/hospital_$1/' hospital.php*
Batch rename extension of all files in a folder, from .txt to .md
rename 's/\.txt$/\.md$/i' *
Title Case Files
rename 's/(^|[\s\(\)\[\]_-])([a-z])/$1\u$2/g' *
rename 's/\b([a-z])/\u$1/g' *
Add prefix onto filenames
rename 's/^/prefix/' *
Tracklist replace backspace with '-'
rename 's/ /-/g' *.mp3
Change file extension
rename .oldextension .newextension *.oldextension
Numeric zero padding file rename
rename 's/\d+/sprintf("%04d",$&)/e' *.jpg
Adding Prefix to File name
rename 's/^/PREFIX/g' *
Adding Prefix to File name
rename 's/^/CS749__/' *.pdf
Renames multiple files that match the pattern
rename 's/foo/bar/g' *
Space to underscore across a single directory
rename ' ' '_' *
Replace all the spaces in all the filenames in the current directory and including directories with underscores
rename "s/ /_/g" * .*
Adding leading zeros to a filename (1.jpg -> 001.jpg)
rename.ul "" 00 ?.jpg; rename "" 0 ??.jpg;
Replace Space In Filenames With Underscore
rename 's/ /_/g' *
Rename with a regular expression and leading zeros
rename 's/result_([0-9]+)_([0-9]+)_([0-9]+)\.json\.txt/sprintf("%d%02d%02d.txt",$3,$2,$1)/ge' result_*.txt