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' {} \;

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

Website backup

wget

wget -xkrNE -nH {url} -P /path/to/target/folder/
wget -xkrNE -nH http://charlotteeifler.works/ -P /var/www/html/ce/
  • -k after the download is complete, convert the links in the document to make them suitable for local viewing
  • -x create a hierarchy of directories
  • -r recursive
  • -N if the local file has the same timestamp as the server, or a newer one, the remote file will not be re-fetched
  • -E aka --adjust-extension will change the file extension to .html or .css or .xml if the file-type suggests it
  • -nH disable generation of host-prefixed directories
  • -P /path/to/target/folder/ will create target folder is non-existent

The -E option is useful to bypass problems related to .css when there is a ?ver=… at the end.

If you download a Wordpress site, don’t forget to delete the file wp-login.php in the root directory. This file will still point to the source loction.

Note to self… investigate:

  • –no-parent

PDF management

Compress PDF with ghostscript

ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Even smaller -dPDFSETTINGS=/ebook:

ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

Date formatting

$ date --date="20200801"

Result: Sa 1. Aug 00:00:00 CEST 2020

date --date="20200801" +"%A, %B %-d, %Y"

Result: Samstag, August 1, 2020