Files

Find files recursively containing string

Find inside /home/micz/ all*.tpl` files containing ‘visualchart-create.php’

grep -rnw '/home/micz/' --include \*.tpl -e 'visualchart-create.php'

Find largest files

find . -xdev -type f -size +50M -print | xargs ls -lh | sort -k5,5 -h -r

Will find all items larger than 50MB in current directory and subdirectories - and sort them by size.

Find files by name recursively

find /var/www/html/ -name "*2018-11-08*"
find . -type f -name '*2018-11-08*' -print
find . -name "foo*"

Remove files recursively ending with tilde

Find first, to be sure your request doesn’t delete more than you want!!!

find . -type f -name '*.*~' -print

If you agree with this list, only then delete…!

find . -type f -name '*.*~' -delete

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

FILE CONTENT

Delete the first x characters on every line in a text file

sed 's/^.\{10\}//' file.txt > newfile.txt