Command-line Tricks

I’m a fan of doing work in the terminal. These are the dozen or so one-liners that I keep around.

Files and directories

Change DOS line endings to Unix line endings

find . -type f -exec dos2unix {} \;

Note: on Ubuntu install with apt-get install dos2unix or on OS X with brew install dos2unix for the above command.

Change the permissions on files to 644

find . -type f -exec chmod 644 {} \;

Change the permissions on sub directories to 755

find . -type d -exec chmod 755 {} \;

Remove all empty sub directories

find . -type d -exec rmdir 2>/dev/null {} \;

Find the total space of all child files and directories

du -cksh *

Empty the content of a file (great for clearing logs)

echo "" > file.txt

Quickly make a backup of a file

cp file.txt{,.bak}

Add a private key to the authentication agent

ssh-add ~/.ssh/id_rsa
ssh-add -L


Get the machine’s external IP address


Serve the current directory as a website on port 8000

python -m SimpleHTTPServer

Remove all installed Ruby Gems

gem list --no-version | xargs gem uninstall -aIx

OS X only Update OS X via the command-line

sudo softwareupdate -i -a

OS X only Update the ‘Open With’ Contextual menu in OS X Finder

/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister -kill -r -domain local -domain system -domain user

Recursively delete .DS_Store files on OS X

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


There are many more at