Basic Linux Command Line Tips

Learning your way around the command line is essential if you are developing with PHP (or Python, or a myriad of other languages most at home on Linux) and interacting with your server(s) with anything other than FTP and basic control panels. Here are some tips and tricks I've found useful.

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 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.


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.


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).


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!