Command line cheat sheet
Whilst extremely simple stuff, I often find myself (at the end of a 12 hour day) with brain fuzz trying to remember the most simple of commands.
Given how regularly I utilise these commands I figured that I would document them in one place for easy reference. Hopefully this reference is of some use to you too - these commands save me seconds (woop woop !) every time, which I am sure over my software career adds up to many saved days.
Whilst writing this post (gradually over a number of weeks years - I just found this in my drafts and figured I'd post it) I stumbled upon the following post: Advancing in the bash shell. It provides a longer, more in depth, and extremely useful overview explanation of the power of the bash shell. Have a read.
File systems
df -h
outputs all of the mounted file systems, and the amount of available space on each. The -h
option makes the output human readable.
du -sh /folder_name
outputs the disk usage of a particular folder.
Want the size of all files/folders in a directory? du -a --max-depth=1 | sort -n
. Thanks Stack Overflow.
Aliases
On my mac I define aliases in ~/.profile
. On other operating systems you might define them in a .bashrc
file or a .bash_profile
file.
An alias is essentially a command shortener, so for example I could define an alias like alias go-product="cd ~/Development/product-website"
such that I only need to type go-product
to access my development directory for a particular product.
Aliases can not explicitly take parameters, but the can call functions to which they pass parameters.
alias run-tests=runTests
function runTests() {
../vendor/phpunit/phpunit/phpunit unit-tests/$1
}
An alias/function combination (like above) allows me to run my controller unit tests by simply typing run-tests controllers
(from the testing directory).
Listing files
ls
lists files in the current directory but does not include hidden files. ls -a
lists all files.
ls -l
will list the files in a directory - it will show additional information like for example where smbolic links point.
You'll never guess what ls -al
does.
Symbolic links
ln -s
creates a symbolic link.
When for example you create a virtual host configuration file in sites-available
you need to symbolically link to it from sites-enabled
to.. enable it.
If you cd
into the sites-enabled
directory you can create the appropriate link with ln -s ../sites-available/domain.com
- if you do not specify a filename for the link file, it will use the same name.
Going back
If you type cd -
you will be returned to the previous directory that you were in. No more entering complex paths to get back to where you were.
Want to execute a command you executed 12 minutes ago? history | grep searchTerm
will list matching commands. Then just type !commandId
and hit enter.
Other
landscape-sysinfo
will provide you some important information about your server - load, memory/space usage etc.
Packages
screen
- I use it every day. Apparently it is a 'terminal multiplexer'. It basically lets you run multiple terminal 'tabs' on a server. Have your error logs tailed (tail -f error.log
) in one tab, your node backend services in another, mysql in a third etc.
ncdu
- makes doing a deep dive into disk usage easy and pretty.