A Bash Cheat Sheet

By Lewis Fogden, Ben Carley, Mon 02 January 2017, in category Linux



First of all, merry christmas and a happy new year to you all. To welcome you into 2017, and rekindle our brains after NYE, we thought we'd write down a few Bash commands that might have slipped the mind in a handy cheat sheet. Please add your own in the comments, and we'll try to keep this list updated as we find more.


Remember who you are


And where you are


Creating Things

Create a new, blank file x.txt

touch x.txt

Write a quick note

echo 'What was I doing again' > x.txt

Create a new directory, x

mkdir x

Counting Things

List the files in the current directory 1 per line (aliased to l in my .bash_profile)

ls -1

Count all the files in the current directory, including directories

ls | wc -l

Or not including directories or symlinks

find . -type f -maxdepth 1 | wc -l

Reading Things

Print a file, x, with line numbers

cat -n x

Look at the top 10 lines of file x

head -n 10 x

Or the bottom

tail -n 10 x

or follow the tail of a file being written to

tail -f x

Altering File Properties

Rename all of the files of a given type (JSON in this case) in the current directory sequentially with n digits (6 below)

let a=0
for i in *.json; do new=$(printf "%06d.json" "$a"); mv -- "$i" "$new"; let a=a+1; done

Change permissions of file x to 0700

chmod 0700 x

Change ownership of the file x to user

chown user x

Transferring Things

Transfer a local directory to a remote location with an ssh alias

scp -r local_dir_path ssh_alias:remote_dir_path

Sync a directory on a remote server with the files in your local working directory

rsync --ignore-existing * ssh_alias:remote_dir_path

Removing Things

Delete file x

rm x

For destroying all the evidence

rm -rf x

System and Services

Restart a service, x

sudo service x restart

Re-read a service x's config files while remaining running

sudo service x reload

Get the disk space available in human readable format

df -h

Get the # of CPUs on your server

lscpu | grep '^CPU(s)'

Running Scripts

Run a script in the background, ignoring hangup signals and writing to a log file

nohup python script.py > logfile_to_write_to.log &

Searching for Things

Look for progress with a given id, n

ps -elf | grep n

Search for the pattern pat in all of the JSON files in the current directory, printing the n lines after each match

grep pat *.json -A n

Search for pattern pat recursively in the current directory

grep -nr pat .

Recursively search directory x for files with naming pattern pat

find x -name pat

Print the filepath of program x

which x

List the hosts in an Ansible playbook

ansible-playbook playbook.yml --list-hosts


Log in to a local PostgreSQL instance as user, activating database db, with backslash command queries generated. List databases and relations in current db.

psql -h localhost db user -E

Log in to the mongo shell on port p as user user and find collections in database db

mongo --port p --username user
use db

And Remember

If you're really struggling to get back into the swing of it after the break, remember to follow the classic software development process.


That or have a peak at

man x

Further Reading

And check out these top 50 lists for a more thorough look at useful Bash commands