
A companion Linux Command Line Cheat Sheet is available for free at Cheatography!
The last year has seen me take on the management of a few Linux servers, and ditch my Windows PCs for Linux too. There's been a lot to learn, and it's not all been fun - sometimes it has been intensely frustrating. However, overall it has been extremely positive. On the rare occasions I have to return to Windows now, I wonder how I ever coped without grep, stdin, stdout, pipes, multiple workspaces ... the list is long.
I'm getting to the stage where I'm comfortable on the command line. I have a vast amount yet to learn, but I can do most of the things I need to do now without looking up commands or file locations. I've also started to explore more tools, and think it would be useful to start to share some of the things I'm finding helpful.
Apologies to those of you with strong command-fu who were expecting cutting edge tricks - this may be too basic for you. I've been using largely Ubuntu, Ubuntu server and Centos 5, though most of these commands should be available (or easily installable) on most modern distros.
Commands to be executed on the command line are prefixed with $. Commands without $ are to be run within applications.
Corrections, improvements and suggestions are all very welcome in the comments below!
screen
screen is a command-line tool to allow multiplexing of terminal windows. In layman's terms, it means you can have multiple terminal "windows" open at once and switch between them. Most importantly, screen allows you to resume a session later, so if you are disconnected from a remote machine, no problem - you reconnect, resume your screen session, and it's like nothing happened. Perfect.
- $ screen - start a screen session.
- $ screen -r - resume a screen session.
- $ screen -list - show your current screen sessions.
- CTRL-A - This key activates commands for screen. Once in screen, you'll need to use this to tell screen to do anything.
- CTRL-A c - create a new instance of terminal.
- CTRL-A n - go to the next instance of terminal.
- CTRL-A p - go to the previous instance of terminal.
- CTRL-A " - show current instances of terminals.
- CTRL-A A (the case matters) - rename the current instance of terminal.
man
I'm including this here not because it's a particularly obscure trick, but because it is so useful and I use it so often. It is invaluable. Simple type "man" then a command to see information about that command.
- $ man grep - Show information about the "grep" command.
tail
Another command which along with its partner head is well known and widely used, but that I find invaluable. tail shows the last 10 lines of any file (and head the first 10). But where it gets good is with the -f argument, which tells tail to "follow" the file. It will then dutifully continue to output anything written to the file until it is stopped. This is very handy for debugging.
- $ tail -f error_log - Show the last 10 lines of error_log and continue to output any new data added to the end of the file.
grep, awk and sed
There three commands, together with the pipe, are the workhorses of the Linux command line. They allow you to manipulate streams of data, like directory lists, log files, test files and other command outputs. They are often used in combination with other commands.
grep is first, and the most-used (at least, for me). It is a text search utility, which searches lines from the input it is given for a pattern and then outputs those lines. awk is a programming language, though personally I've not used it for much more than splitting lines of text into their constituent parts. sed is another text utility which parse input and performs a search and replace on it, before outputting it.
- $ grep -iR "superted" /home/bananaman/ - search Bananaman's home folder recursively and case-insensitively for all files containing "superted".
- $ grep "Panthro" Skeletor.txt - search the file Skeletor.txt for lines containing "Panthro" (case-sensitive).
- $ grep -v "Dogtanian" Muskehounds.txt - an inverted search - search the file Muskehounds.txt for lines NOT containing "Dogtanian" (case-sensitive).
- $ sed 's/Superman/Batman/g' SuperheroLeagueTable.csv - replace all instances of "Superman" with "Batman" in SuperheroLeagueTable.csv and output.
- $ sed -i'.bak' 's/KITT/KARR/g' Stock.txt - Make a backup of Stock.txt as Stock.txt.bak, then replace all instances of "KITT" in Stock.txt with "KARR".
- $ awk 'NR==15' BabyGotBack.txt - Output the 15th line of BabyGotBack.txt.
Where these three have their greatest utility is in combination, using pipes. A pipe - with the symbol | - tells Linux to use the output from the first command as the input to the second. For example:
- $ ls | grep "Penfold" - list files in a folder whose name contains "Penfold".
- $ tail -f error_log | grep "badscript.php" - show any of the last 10 lines of error_log with "badscript.php" in them, and watch the file for new lines with "badscript.php" in them.
- $ ls -al | awk '{print $5}' - List just the sizes of all files in a folder.
- $ ls | sed 's/123/456/g' - list files in a folder and replace "123" with "456" in their names.
And you can use multiple pipes in a single line:
- $ df | grep "/dev/sda1" | awk '{print $4}' - show free space on the /dev/sda1 drive.
- $ vmstat | awk 'NR==3' | awk '{print $3}' - output the third item on the third line of vmstat (the amount of virtual memory in use).
- $ ls -al | awk '{print $8}' | grep "Holiday" | sed 's/^[^.]*\.//g' - list all files in a directory with their attributes, reduce that list to just their names, filter for just those containing "Holiday" and, for those, just show their file extensions (yes, a hugely contrived example).
Shortcuts
And finally, Linux has a remarkable number of shortcuts for things, especially where related to the history of commands run, and the ability, with the alias command, to add even more. Here are a few of the ones I find myself using regularly (and there are plenty more on this cheat sheet for bash shortcuts):
- $ sudo !! - run the previous command with superuser privileges.
- $ cd !$ - the "!$" is a shortcut for the argument used in the last command. So if you create a directory in one like using mkdir, you can type this command to change to that directory without needing to type out the whole directory name. Handy!
- $ ALT-. - (that's ALT Period) in bash and zsh. This works as above, with the added bonus that it will cycle through recent arguments if you press it again, and you can see what the substituted argument is! Thanks to Josh Dick and RandomDude for adding this in the comments.
- $ !cd - re-run the most recent command starting with "cd".
- $ ^save^dave^ - re-run the previous command, replacing the first instance of "save" with "dave".
- $ CTRL-r - search backwards through your commands (great for re-running a recent command).

A companion Linux Command Line Cheat Sheet is available for free at Cheatography!
13 Comments
Improve on 'head' and 'tail' use 'slice' :
#!/bin/bash
function usage() {
echo "Usage: $0 <first line> <last line> <file>"
echo "line numbers start at 1 and are inclusive"
echo "negative numbers are relative to the end of file"
exit 0
}
[ -z "$3" ] && usage
LEN=`wc -l $3 |cut -f 1 -d ' '`
BEG=$1
[ "$BEG" -lt "0" ] && BEG=`expr $LEN + $BEG`
END=$2
[ "$END" -lt "0" ] && END=`expr $LEN + $END`
CTR=`expr $END - $BEG`
CTR=`expr $CTR + 1`
head -$END $3 | tail -$CTR
Replies: #5.
#1, jld, 19 March 2011. Reply to this.
A silly one which I love :
alias hg='history | grep '
Replies: #5.
#2, jld, 19 March 2011. Reply to this.
If you like GNU screen, look into tmux. It's a modern replacement for screen with many additional cool features, and you can easily set it up to use all of the keybindings you're already familiar with in screen. Check out my tmux configuration for ideas:
https://github.com/joshdick/dotfiles/raw/master/.tmux.conf
Also, relating to 'cd !$', bash/zsh both substitute in arguments from the previous command when you press alt+period. This should have the same effect as !$, with the added benefit of you being able to see what's about to be run.
Replies: #5.
#3, Josh Dick, USA, 19 March 2011. Reply to this.
Good post, it's always nice to learn new shell tricks! A couple of tips: instead of !$ I use ALT-. (ALT-dot). It's faster to type and will also cycle over the last commands.
Also, I put this in my inputrc:
$if bash
Space: magic-space
"\e[A": history-search-backward
"\e[B": history-search-forward
$endif
So that instead of using !cd, I can just type cd and the up key to look for commands that start with "cd" - I find it more intuitive. The magic space is... magic!
Replies: #5.
#4, RandomDude, 19 March 2011. Reply to this.
#1 and #2 Thanks! I like aliases on my home pc, but I'm trying to reduce the amount I use them, as working on lots of different machines means that the same aliases aren't always available.
#3 tmux looks good - I'll give that a try. Thanks for the ALT-. trick too, that's great! I've added it to the list.
#4 Thanks for the ALT-. trick too, I've added it above. Magic space looks ... well, magic! Nice, thanks.
#5, DaveChild, United Kingdom, 20 March 2011. Reply to this.
nice post there are definitely a few things i picked up that i will use again!
Replies: #8.
#6, David Jones, United Kingdom, 22 March 2011. Reply to this.
I like this post. My memory needs this information
Replies: #8.
#7, Carlos Fabuel, spain, 22 June 2011. Reply to this.
#6 #7 Thanks :)
#8, DaveChild, United Kingdom, 27 June 2011. Reply to this.
Awesome! I took a class in Linux for a couple of years ago but I lost my book where there was a page with basic commands. Thanks Dave :)
#9, Albin, Sweden, 12 July 2011. Reply to this.
Nice overview of basic commands.
I'm learning Linux for the moment and this post is very useful. I have printed this page and it lies next to my book. Thanks.
#10, Marc, België, 25 July 2011. Reply to this.
Thanks for those tips.
They are really useful.
Keep more coming, dude! :)
#11, rod, brazil, 25 August 2011. Reply to this.
For the screen command, I found "killing" session is useful as well.
screen -X -S #screen_id kill
#12, Roy, 14 September 2011. Reply to this.
Thanks! I needed to refresh some commands in linux. That´s what i was looking.
Best regards!
#13, Tomás de Teresa, Spain, 10 November 2011. Reply to this.