A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Linux renaming examples, when filenames have spaces

Do your filenames have spaces? Do you curse bash for making it difficult to loop over them?

Fear not! (and wash that potty mouth out with soap). This loop is all you need:

find . -type f -name "*.mp3" -print0 | while IFS= read -r -d '' file; do ls "$file"; done

# Rename "foo.mp3.mp3" to "foo.mp3"

find . -type f -name "*.mp3" -print0 | while IFS= read -r -d '' file; do file=`basename "$file"`; newfile=$(perl -le'$ARGV[0]=~s/\.mp3//;print$ARGV[0]' "$file"); echo mv "$file" "$newfile"; done


# Add a leading zero to numbered filenames

find . -type f -name "064*" -print0 | while IFS= read -r -d '' file; do file=`basename "$file"`; echo mv "$file" "0$file"; done

# The above commands will print out what they're about to do. Check it is what you expect! Then make this change to run them for real:

Change: echo mv foo bar
To: `mv foo bar`

# ^ Those ` are backticks

# Note the order of files is not sorted