Getting started with the Linux command line interface

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:

ls -l

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.

cd

Change directory
To go up a directory:

cd ..

To go to your home directory:

cd ~

To go down into the test directory:

cd test

To go to the /var/logs directory:

cd /var/logs

And if you’re lost then the pwd command will tell you where you are

pwd

Show the present working directory

whoami

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.

ifconfig

Displays information about your network interfaces.
Useful for: finding your IP address, MAC address

grep

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

mv

Move a file

mv i_am_here.txt another_directory/now_i_am_over_here.txt

cp

Copy a file

cp lovelyfile copyoflovelyfile

rm

Delete a file

rm rubbishfile.txt

cat

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

tail

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

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?

wget

Retreive a URL (eg web page)
Download a file to the current directory:

wget http://www.somebodieswebserver/an_image.jpg
Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter

How to see what’s going on in your mysql database (linux)

You can see every SQL command which your mysql database receives.

In your mysql interface you can set where the log file will be written to and then turn on the logging option (change log filename as applicable):
set global general_log_file = ‘/var/log/mysql.log’;
set global general_log = ‘ON’;

(to see the value of these variables type:
show global variables where variable_name like ‘general_log%’;
in your mysql interface)

Now use:
tail -f /var/log/mysql.log

and you’ll see every new SQL statement which the database receives.

And of course to turn it all off again type this in the :
set global general_log = ‘OFF’;

Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter

How to follow what’s going on in your Apache server logs (linux)

At the command line type:

tail -f /var/log/apache2/access.log

As apache requests are received you’ll see information about them appear on screen.

Can’t find the log file?
Try this:

find /var -name access.log 2>/dev/null

This searches for the file “access.log” in the /var directory and sub-directories. The “2>..” on the end is a really neat trick which directs all error messages to /dev/null so you don’t get messages about permission denied on directory traversal.

If this doesn’t find the file then it’s probably not in /var so try:

find / -name access.log 2>/dev/null

Which will search from /

Combine the find command with the tail command

tail -f `find /var -name access.log 2>/dev/null`

Note the wierd backtick character around the find.

Filtering the results
If you’re looking for something in particular then pipe the results through grep to filter on a string:

tail -f /var/log/apache2/access.log | grep login.php
Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter