Marc Ermshaus’ avatar

Marc Ermshaus

Linkblog

Shell snippets

Published on 14 Nov 2013.

Shell commands I search for all the time.

# Count lines of all *.php and *.phtml files in working dir
find . -name '*.php' -o -name '*.phtml' | xargs wc -l

# Count all files, links and directories in ./dir
for t in files links directories; do echo `find ./dir -type ${t:0:1} | wc -l` $t; done 2> /dev/null

# Set files in ./dir to 0644 and directories to 0755
find ./dir -type d ! -perm 0755 -exec chmod 755 {} \;
find ./dir -type f ! -perm 0644 -exec chmod 644 {} \;
# Branch create
git checkout -b <branch>

# Branch push
git push -u <repo> <branch>

# Branch delete
git branch -D <branch>
git push <repo> --delete <branch>

# git log compressed output
git --no-pager log --pretty=format:"%h%x09%an%x09%ad%x09%s" && echo

# Apply changes from branch without merging
git merge --no-commit --squash branchA
git reset HEAD # to unstage the changes
# Replaces content of user@host:/path/to/dir/ with content of ./dir/
rsync --progress \
      --recursive \
      --delete \
      --perms \
      --quiet \
      ./dir/ user@host:/path/to/dir/

# "Default" rsync call
rsync -avz ./dir/ user@host:/path/to/dir/
# Generates a TAR archive of ./dir and encrypts it with user@example.org's PGP
# key
tar czfp - ./dir | gpg -e -r user@example.org --output "./dir.tar.gz.gpg" -

# Decrypt and extract into ./dir
gpg -d ./dir.tar.gz.gpg | tar xzfp -
# Compares the content of two directories
diff -r --brief ./dir1 ./dir2
# Mount ISOs
mount -o loop disk.iso /mnt/disk
# Open files from directory with default application
ls -b *.mp3 | xargs -n 1 xdg-open
# Batch resize images (by largest side, keeps propotions) and compress for web
mogrify -resize "1200x1200>" -quality 75 *.jpg
# Search for string in files
ag searchstring
grep -r searchstring
# Get dir of script file
SCRIPT_PATH="$(realpath "$0")"
SCRIPT_DIR="$(dirname "${SCRIPT_PATH}")"