If you ever used DOS or the command prompt on Windows machines then just forget everything you’ve seen. The linux command line interface (CLI) is amazingly flexible and powerful. Here are some essential commands to get you started:
List files in a directory
The handiest form is:
which gives something like this:
-rw-r--r-- 1 james james 31908 2010-06-02 20:30 exploits_of_a_mom.png
The information here is: files permissions, number of links to this entry (ie files in a sub-directory), the file’s owner, the file’s group, the file size in bytes, date modified and filename.
To go up a directory:
To go to your home directory:
To go down into the test directory:
To go to the /var/logs directory:
And if you’re lost then the pwd command will tell you where you are
Show the present working directory
Tells you which user you are logged in as (I mainly included this because it makes me giggle). Can be useful – if your program is running on a web server and it can’t access a file then this might make things clearer.
Displays information about your network interfaces.
Useful for: finding your IP address, MAC address
Search for a phrase
Search all .php files for the string “add_filter”:
grep add_filter *.php
Same but recurse all sub-directories:
grep -r add_filter *.php
Same but case insensitive (ie pick up “Add_Filter” or “ADD_FILTER” as well):
grep -r -i add_filter *.php
Move a file
mv i_am_here.txt another_directory/now_i_am_over_here.txt
Copy a file
cp lovelyfile copyoflovelyfile
Delete a file
Output a file contents as a stream. Useful when piped with “|” into another command, eg search the file myfile.txt for the string “frogs” (case insensitive):
cat myfile.txt | grep -i frogs
Displays the end of a file (ususally used for following log files). As new lines are added they are displayed.
tail -f myfile
See find and backticks below for useful tricks.
Press Control-C (^C) to leave.
Find a file
As seen in my article on following your apache webserver logs you can find all files called “access-log” in the /var directory:
find /var -name access.log
unfortunately this gives errors for directories which couldn’t be opened (user has no permissions) so this is better:
find /var -name access.log 2>/dev/null
Linux commands give output streams called standard output (given number 1) and errors (given number 2). Here we send the error stream off to the device /dev/null (which basically throws everything away). We could do this:
find /var -name access.log 2>error.txt 1>out.txt
And now the file “out.txt” contains the output, in this case the filename and “error.txt” contains the error output.
Hints and tips: Backticks
You can send the results of one command to another using backticks (`), as in this example from my article on following your apache webserver logs:
tail -f `find /var -name access.log 2>/dev/null`
This does the find command first to get the location of the file “access.log” and then uses that filename for the tail command.
We could even pipe this result through grep to search for a phrase:
tail -f `find /var -name access.log 2>/dev/null` | grep login.php
This will give a realtime display of all requests to “login.php” on your web server. Neat huh?
Retreive a URL (eg web page)
Download a file to the current directory: