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.
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.
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.
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.
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.
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:
And you can use multiple pipes in a single line:
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):