The absolute minimum you need to know about the Linux command line

This tutorial is intended for to familiarise users and system administrators who are familiar with Microsoft Windows environment with the Linux command line. Linux has a powerful graphical user interface similar to Windows and Macintosh, but much of the power of Linux is only unleashed at the command line. Almost everything is equally applicable to UNIX.

Accessing Linux from Windows

Command Line only

Install PuTTY, choosing "Windows installer for everything except PuTTYtel".

On some versions of Linux you will get better output if you choose Window -> Translation -> Remote character set: UTF-8, and Use Unicode line drawing code points. Now save as Default Settings under Session.

Accessing Linux Graphical User Interface from Windows

Install CygWing/X (be sure to include X11 in installation) or XMing.

Installing Linux on USB Thumb Drive

You can also install Linux on a USB stick, and boot from the USB stick instead of from Windows on your C:. You can still access files from your C:, and you can even install Linux on your C: so that you are given a choice between starting Windows and Linux each time you boot. There are many variants or "distributions" of Linux, for desktop use I favor Ubuntu since it comes with the software to play MPEGs and Flash player sites easily installable from the Ubuntu Software Center.

Logging In

Printing Working Directory (Folder)

You should be at a command line prompt similar to [user@yourhost ~]$ . The ~ is a short cut representing your home directory.

$ pwd
/home/user

On some systems the actual location of your home directory will be different, e.g. on large systems it may be sorted into subdirectories by initial letter /home/u/user. No matter what, ~ always represents your home directory.

Listing the contents of a directory

$ ls /home
user1 user2 user3

List in long format, with owners and date

$ ls -l /home

drwx------. 4 user1 user1  4096 Mar 11 18:37 user1
drwx------. 4 user2 user2  4096 Mar 11 18:37 user2
drwx------. 4 user2 user2  4096 Mar 11 18:37 user3

Files that start with a "." are hidden by default in Linux. List all files in current working directory, in long format, including hidden files:

$ ls -al

total 6
drwx------. 5 wrcourt wrcourt 4096 Mar 24 13:07 .
drwxr-xr-x. 6 root    root    4096 Mar 20 14:27 ..
-rw-------. 1 wrcourt wrcourt  688 Mar 20 19:36 .bash_history
-rw-r--r--. 1 wrcourt wrcourt   18 Jul 18  2013 .bash_logout
-rw-r--r--. 1 wrcourt wrcourt  176 Jul 18  2013 .bash_profile
-rw-r--r--. 1 wrcourt wrcourt  124 Jul 18  2013 .bashrc

The entry "." is a system defined entry that represents the current directory. The entry .. represents the directory above the current one.

Get more information about all the options for the ls command:

$ man ls

Use <space> to advance through help text, <q> to quit.

Make a new directory

$ mkdir foo

Change directory

$ cd foo

Recall recent commands

Press <Up-Arrow> until ls -al command appears, press <Enter>.

$ ls -al

total 2
drwxrwxr-x. 2 user user 4096 Mar 24 13:20 .
drwx------. 6 user user 4096 Mar 24 13:20 ..

Copy a file from another directory into the current directory

$ cp /etc/hosts .

Rename a file

$ mv hosts copy

Edit a text file

The classic text editor on Linux is vi. It is extremely powerful, allowing all sorts of wild cards (regular expressions). It does require a few hours to learn the basics. The variant on Linux is actually Vim, an improved version of the original vi.

Edit a file with vi

$ vi copy

The <Insert>, <Delete> and arrow keys should all work, even though vi tutorials generally assume that they don't. Use <Esc> to get out of insert mode. ZZ to exit.

There are many tutorials for vi. I strongly encourage you to spend a while doing one.

GUI text editor

If you are using Cygwin/X or Linux from a USB stick:

$ gedit copy&

This will not work with PuTTY, since it is purely an SSH (command line) client, not an X server.

Menu driven text editor

If you truly have no time to do a vi tutorial, nano may be installed.

$ nano copy

Menu driven, Ctrl-X to exit.

Viewing text files

Type out file with no pauses:

$ cat copy

Type out file with pauses:

$ less copy

Use <Space> to move forward through file, Ctrl-B to move back, q to quit, / to search for specific text, h for help on more keys.

Type out the beginning of a file:

$ head /etc/services

Type out the end of a file:

$ tail /etc/services

Search for all lines in file /etc/services matching a string:

$ grep nfs /etc/services

$ grep udp /etc/services|less

The | between two commands "pipes" the output of one command in as the input of another command. Unix has a powerful collection of text processing tools - try man grep, man sort, man awk, man find, man xargs, and man perl just to get started.

Remove file

$ rm copy

Remove a directory

If you have been following this tutorial closely, you are in the directory foo. Change back to the home directory first:

$ cd

and remove the directory:

$ rmdir foo

Multi-user, multi-tasking

Linux, like its predecessor UNIX &tm;, is fundamentally multi-user and multi-tasking - Unix was from inception, in the days before GUIs.

Who is currently logged on:

$ who

What processes are running:

$ ps -ef|less

What processes are taking up the most CPU time:

$ top

1 to show separate state of CPUs on multi-CPU/multi-core machines, h for help, On to sort by memory usage.

Further Reading

UNIX For Dummies, John R. Levine and Margaret Levine Young

The Unix Programming Environment, by Brian W. Kernighan and Rob Pike

Search Amazon.com:


Amazon

Kudos & Brickbats

E-mail me at Wrolf Courtney <wrolf@wrolf.net> with any comments or updates.