Console intro

Introduction

The purpose of this page is to provide new unix/Linux users with a quick start guide to using the command line. It is not a complete reference like the manual pages. It is an attempt at explaining, from a new user’s perspective, common commands and other command line features such as piping and redirection.

Tab Completion

Most unix and Linux command line shells provide tab completion. This means that when typing a command the user can press the tab key a few times to see commands that begin with what was typed. If what has been typed so far is unique, the shell will complete the rest of the command. Also, completion works when specifying paths to files or directories.

To experiment with this feature simply log in to a console or open a command prompt if in X, then type c and press tab a few times. One of the suggested commands will be cd. Longer commands such as firefox will complete themselves when enough characters are typed (on my system typing fir then tab completes to firefox). Similarly, typing cd / and pressing tab a few times will show the directories in the root of the filesystem such as /bin, /boot, and /home. Typing cd /h, then tab produces cd /home.

Commands

cd

cd [path]

Change working directory. Paths that start with / are called absolute paths, paths that start from the current directory (such as ../../stuff are relative paths. Executing cd by itself will change to the current user’s home directory.

cd ..

Change the working directory to the parent directory.

cd ~/[path]

Change the working directory to a path in the user’s home directory. A tilde when used in paths is the same as /home/username (or /root if the current user is the root user).

ls

Lists current directory contents. If the listing is too long to be displayed, pipe it into less (described later).

ls -a

Lists all of the contents of the current directory, including dot files (ie “.bash_profile”) and directories.

ls -l

Lists the detailed current directory contents.

ls -al

Lists detailed current directory contents including dot files and directories.

ls -alh

Lists detailed current directory contents including dot files and directories. This time the file sizes are in megabytes (Human readable format)

ls [file]

Lists that particular file in the directory. The usual options apply.

ls [directory]

Lists the contents of the directory [dir]. The usual options apply.

man

man [command]

Display the information pages on the command [command]. (ie man ls)

cp

cp [source path/source file] [destination path/new file name]

Copy source file to the destination path and give it the new file name. If no new file name is given, just the directory for the file to be copied to, the file will have the same name.

cp -r [source directory] [destination directory]

Copies the source directory recursively (ie copies ALL of its contents) into the destination directory. If the destination directory does not exist, it is created.

mv

mv [source file or directory] [destination directory or new file/directory name]

Moves the source file or directory to the destination directory. When moving files, a new file name can be supplied. This is useful for renaming files and directories.

rm

rm [file]

Deletes (removes) file. No recycle bin here. You delete it, its gone.

rm -rf [directory]

Deletes the contents of a directory recursively then removes the directory itself. This command will not prompt you for any input at all, so DON’T do anything like rm -rf /.

cat

cat [file]

Runs the contents of a file by the terminal. Useful for viewing config files. If the file is very long, pipe the output of cat into less, or just use less on the file directly.

less and more

less [file]

View a file (file pager). Less doesn’t shoot the thing past you in the terminal, but instead displays it so you can scroll through the file using the arrow keys, space bar for forward, ‘b’ for back, and ‘q’ for quit.

more [file]

Essentially like less, works the same more or less.

du

du -h [directory or file]

Outputs the sizes of the files and directories in the directory, ending with the size of directory. The -h puts the sizes in human readable format (kilobytes, megabytes, etc).

du -sh [directory]

Only display the grand total size of the directory.

df

df -h

Outputs the sizes, free space, and used space of all mounted file systems in human readable format.

ln

ln -s [file] [linkname]

Create a symbolic link (linkname) that points to a file. Kind of like Windows shortcuts but much more transparent and useful. You can use relative or absolute paths to the file.

top

top

Displays information about current running processes.

tar

tar zxf [filename ending in tgz or tar.gz]

Unzip (actually gunzip) and untar an archive. Change it to zxvf for the files to fly by the screen, though personally I find this annoying and it takes time if you’re untaring/gunzipping an archive with a large number of files.

tar jxf [filename ending in tar.bz2]

Unzip (bunzip2) and untar an archive.

tar cvf [archive name] [dir or file]

Create a tar archive from a directory or file. Its a good idea to end the archive name with the .tar extension so you know its a tar archive.

gzip

gzip [file]

Compress a file. gzip automatically appends the .gz extension to the resulting file. Also, you must gzip files, not directories; if you want to compress directories, create an archive using tar first.

find

find [directory] -name [filename]

Recursively searches a directory and reports back the path to each occurrence of the filename. Standard wildcards apply. Its a good idea to redirect this command’s output to a file. See below about file redirection. Use . for [directory] to search the current directory.

mount and umount

mount [-t type] /dev/[device] [directory]

Attempts to mount the media in the device to the directory specified. For example, if your cdrom is /dev/hdc and you want to access it an ISO9660 format disk from the directory /mnt/cdrom, do

mount -t iso9660 /dev/hdc /mnt/cdrom

While you’re here it might be a good idea to learn about the file /etc/fstab with man fstab. This file can control who can mount what, store the file system type so you don’t have to use -t all the time, and store the mount directory. Once configured, mounting your CD-ROM could simply be mount /mnt/cdrom.

umount [directory]
or
umount /dev/[device]

Unmounts the device. Either method works. Continuing the above example, I could do umount /dev/hdc or umount /mnt/cdrom.

ps

ps

View the current active processes that are accessible by the current user. Use this to get the process ID (PID) of a program. For all processes use ps -aux.

ps aux | grep [program name]

Search the list of programs and extract the line that contains the program name. This is useful for getting the process ID of a program you want to kill. Read below about piping if you don’t understand the vertical bar between the commands.

dmesg

dmesg

Shows the “kernel ring buffer.” Really just prints kernel messages. You can use this command to print out what occurs during boot (instead of writing down error messages by hand as they fly across the screen. You can also use it to see if kernel modules that are loaded are actually recognizing your hardware.

grep

grep [string] [file]

Search a file for instances of a string. Standard wildcards apply. You can use grep along with pipes (discussed below) to do some very useful things. If you’re looking for a string that could be in any one of a bunch of files that are in directories and subdirectories you can use the -r switch to do a recursive grep.

shutdown

shutdown -r now

Reboots the computer. You might need root permission to do this.

shutdown -h now

Shuts down the computer. Tells Linux to bring the computer down to halt. You might need root permission to do this.

Locked Up Programs

killall [process name]

Kills all instances of a process by name. For instance, to kill your xterms type killall xterm.

kill -9 [PID]

Kill a process by its process ID.

File Permissions

chmod ### [file]

Changes the permissions of a file.

chmod -R ### [directory]

Recursively apply permissions to a directory and all of its contents.

The permissions of a file are given in rwxrwxrwx format, where r means read, w means write, and x means execute. The first set of rwx is the permissions for the owner of the file, the second for other users who are in the file’s group, and the third for all other users. In the above chmod command, each of the # corresponds to owner, group, and others respectively. When the numbers are given, they are interpreted as decimal version of the binary permission bits. The following table describes all of the possibilities for one of the digits.

Digit  Permission
  7       rwx
  6       rw-
  5       r-x
  4       r--
  3       -wx
  2       -w-
  1       --x
  0       ---

So chmod 755 file would change file so that it was readable, writable, and executable by the file owner, and readable and executable for the group and everyone else.

A much more intuitive way to use chmod than the octal method above, is to use characters. There are four types of permission groups: user, group, other, and all. They are “u”, “g”, “o”, and “a” respectively). The main types of file permissions are read, write, execute, set user id, set group id, and sticky. They are “r”, “w”, “x”, “s”, “s”, and “t” respectively. To use these characters with chmod, you start by specifying the group, using a “+” to add permission or a “-” to remove permission, and then specifiying the permissions you want to modify.

So chmod a-rwx file; chmod u+rwx file; chmod go+rx file would change file so that it was readable, writable, and executable by the file owner, and readable and executable for the group and everyone else. The first command removes all permission for everyone (“a” for all), the second gives read/write/execute permission for the file owner (“u” for user), and the third gives group and other permission to read/execute. It sometimes takes more commands to change permissions this way, but non-Unix-gurus don’t always have the octal table memorized.

Pipes

To pipe the output of one program into the input of another program, use the piping symbol | between the commands. For instance, if the directory listing is too long with ls, pipe the program into less with the command

ls | less

You can now use the less commands described above to move about in the file listing. There are a lot more interesting things that can be done with pipes. Its not uncommon to see UNIX gurus doing complex tasks in one command with them.

File Redirection

To redirect the output of a command to a file, use the greater than symbol >. For instance, doing a find using the command above with wildcards can produce a lot of results, which I would either pipe into less or redirect to a file. To search for a file with a ‘.so’ extension in the entire / filesystem, then store the results in the file ‘findso’ execute

find / -name *.so > findso

Then you can view the file in a text editor or with less. Other redirections are available. To append output to a file instead of overwriting the file use >>. The redirections > and >> handle standard output. Use 2> and 2>> to redirect standard error. Both standard output and standard error can be redirected to the same file using command &> file. For more redirection tricks see the Linux documentation project.

Typically, the most useful thing to do is to redirect stderr to stdout, and then direct all of that output to a file:

find / -name *.so 2>&1 > findso

Or pipe to another program such as less:

find / -name *.so 2>&1 | less

Virtual Terminals

When you are running in pure terminal mode (no X) it is sometimes handy to have more than one console to issue commands in. You could, say, burn a CD in one and read man pages for a command in another. To do this, use the keys Ctrl+Alt+ F1 through ever how many virtual terminals your system has defined. You can even switch out of X to a real console using these commands, and then back into X (usually X is F7 or so).

Running multiple programs from Xterm

When you’re running in X, you can start any graphical program from X terminal emulators. Unfortunately, when you do this, you can’t use your xterm until the program has ended. To start a graphical program in X from an Xterm and free the Xterm back up, run the program with an & after it like:

firefox &

You’ll notice when you do this that your Xterm’s prompt comes back. In addition to starting programs like this to free an Xterm, you can run LONG running commands in pure console and free your console back up. For instance, you could do find / -name *.so > findso & so you wouldn’t have to wait for find to finish before continuing work.

Escape Key Combinations

Ctrl+C                  - Kill the current process
Ctrl+D                  - Close standard input
Ctrl+U                  - Delete current line up to cursor
Ctrl+L                  - Clear the screen
Ctrl+V                  - Interpret the following character literally
Ctrl+Z                  - Suspend the current process (bring back with fg)
Ctrl+Alt+Backspace      - Kills the X-window server
Ctrl+Alt+Del            - Three finger salute
Ctrl+Alt+[F1 -F8?]      - Switch virtual terminals

Important Directories

/dev   - Where all the hardware device interface files are stored.
/proc  - Kernel and hardware information.
/etc   - Contains all (most) system config files.
/root  - The root user's home directory.
/usr   - The big directory where most programs will go.
/home  - Contains all other users home directories.
*/sbin - Where system programs like ls, cd, and mount are located.
*/bin  - Usually where programs users or distributions install

Note that here I’m using * to mean any sbin or bin directory, there a /bin and a /usr/bin and a /usr/local/bin, etc. same for sbin