Files

Standard renaming for whitespaces, special chars

# rename all the files, replace whitespaces
find -name "* *" -type f | rename 's/ /_/g'

# rename file names, replace umlaute and sonderzeichen
find -name "*[äöüÄÖÜß]*" -exec rename 's/ä/ae/g;s/ü/ue/g;s/ß/ss/g;s/Ä/Ae/g;s/Ü/Ue/g;s/Ö/Oe/g;s/ö/oe/g' {} \;

# get rid off some other special chars
find -name "*[,\!\:\?]*" -exec rename 's/,/-/g;s/\!/-/g;s/\:/-/g;s/\?/-/g' {} \;

Replace all file endings recursively

I needed to get all images of a site offline for a while. Also the PDF downloads. Here is the script.

  • The following recursively replaces all .jpg file endings with .offpg.
  • The second line does the same with .gif, renaming to .offif, the third line .png to .offng.
  • The forth line looks at the uppercase version and renames .JPG to .offPG.
  • The last line renames .pdf to .offdf.
find . -name "*.jpg" -exec rename -v 's/\.jpg$/\.offpg/i' {} \;
find . -name "*.gif" -exec rename -v 's/\.gif$/\.offif/i' {} \;
find . -name "*.png" -exec rename -v 's/\.png$/\.offng/i' {} \;
find . -name "*.JPG" -exec rename -v 's/\.JPG$/\.offPG/i' {} \;
find . -name "*.pdf" -exec rename -v 's/\.pdf$/\.offdf/i' {} \;

Remove the x characters of a bunch of file names

  • Replace x with the count of chars you want to remove.
  • The -n is for simulating; remove it to get the actual result.
  • Does change folder names, too!
  • Does not change file names recursively in sub folders.

Remove characters at the beginning:

rename -n 's/.{x}(.*)/$1/' *

Remove chars at the end (will change file extension, too):

rename -n "s/.{13}$//" *

Remove file of specific name or file ending in multiple folders

This test run will list all the files that match:

find . -name "folder.conf" -type f

And the following command will delete them (just add -delete)

find . -name "folder.conf" -type f -delete