A A short survival guide to Linux command line

People who are not used to the command line interface may find it difficult to get things done on a Linux computer. The basic command-line interface has been the same for more than 30 years. Hence there are plenty of online tutorials about how to use each and every command. This short guide is only a starter.

A.1 Some keystrokes

This is a summary of the most commonly used keystrokes.

  • CTRL-C: Stop the currently running program

  • CTRL-D: Finish this session (close the terminal window)

  • CTRL-L: Clear screen

  • TAB : At command prompt, attempt an autocomplete on what is already written. This is useful with e.g. long file names: Type the beginning of the name, and then [TAB].

  • Up and down arrows: At command prompt, suggests previous commands from the history. This is useful for repeating something just done. Editing of previous commands is possible as well, so it’s also good for doing almost the same as a previous command.

  • space: When computer displays something with a terminal pager, [space] means “page down”.

  • q: “Quit”. In page-by-page display, “q” is used to quit this mode.

A.2 Getting help

Nobody really remembers all the flags and options. There are two common ways to get some more help. One way is the “man” command, and the second way is the help flag.

For example, in order to know more about the “ls” command (list files in current directory):

$ man ls

Note that the ’$’ sign is the command prompt, which the computer prints out to say it’s ready for a command. Usually the prompt is longer, and it includes some information about the user and the current directory.

The manual page is shown with a terminal pager. Use [space], arrow keys, Page Up and Page Down to navigate, and ’q’ to quit.

For a short summary about how to run the command, use the --help flag. Some commands respond to -h or -help (with a single dash). Other commands print the help information when the syntax is incorrect. It’s a matter of trial and error. For the ls command:

$ ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort.

Mandatory arguments to long options are mandatory for short options too.
 -a, --all        do not ignore entries starting with .
 -A, --almost-all    do not list implied . and ..
    --author       with -l, print the author of each file

...

(and it goes on)

A.3 Showing and editing files

If the file is expected to be short (or if it’s OK to use the terminal window’s scrollbar), displaying its content on the console can be done with:

$ cat filename

Longer files require a terminal pager:

$ less filename

As for editing text files, there are a lot of editors to choose from. The most popular (but not necessarily the easiest to start off with) are emacs (and XEmacs) as well as vi. The vi editor is difficult to learn, but it’s always available and always works.

The recommended simple GUI editor is gedit, or xed, whichever is available. It can be started through the desktop menus or from the command line:

$ gedit filename &

The ’&’ at the end means that the command should be executed “in the background”. Or simply put, the next command prompt appears before the command has completed. GUI applications are best started like this.

The ’filename’ in these examples can be a path as well, of course. For example, to view the system’s main log file:

# less /var/log/syslog

To jump to the end of the file, press shift-G, while “less” is running.

Note that the log files are accessible only by the root user on some computers.

A.4 The root user

All Linux computers have a user with the name “root” which has ID 0. This user is also known as the superuser. The special thing about this user is that it’s allowed to do everything. Every other user has limitations on accessing files and resources. Not all operations are allowed for every user. These limitations are not imposed on the root user.

This is not just an issue of privacy on a multi-user computer. Being allowed to do everything includes deleting all data on the hard disk with simple command at shell prompt. It includes several other ways to mistakenly delete data, make the computer useless in general, or make the system vulnerable to attacks. The basic assumption in a Linux system is that whoever is the root user, knows what he or she is doing. The computer doesn’t ask root are-you-sure questions.

Working as root is necessary for system maintenance, including software installation. The trick to not messing things up is thinking before pressing ENTER, and being sure the command was typed exactly as required. Following installation instructions exactly is usually safe. Don’t make any modifications without understanding what they’re doing exactly. If the same command can be repeated as a user that isn’t root (possibly involving other files), try it out to see what happens as that user.

Because of the dangers of being root, the common way to run a command as root is with the sudo command. For example, view the main log file:

$ sudo less /var/log/syslog

This doesn’t always work, because the system needs to be configured to allow the user to use “sudo”. The system requires the user’s password (not the root password).

The second method is to type “su” and start a session in which every command is given as root. This is convenient when several tasks need to be done as root, but it also means there is a bigger chance of forgetting being root, and write the wrong thing without thinking. Keep root sessions short.

$ su
Password:
# less /var/log/syslog

This time, the root password is required.

The change in the shell prompt indicates the change of identity from a regular user to root. In case of doubt, type “whoami” to obtain your current user name.

On some systems, sudo works for the relevant user, but it may still be desired to invoke a session as root. If “su” can’t be used (mainly because the root password isn’t known) the simple alternative is:

$ sudo su
#

A.5 Selected commands

And finally, here are a few commonly used Linux commands.

A few file manipulation commands (it’s possibly better to use GUI tools for this):

  • cp – Copy file or files.

  • rm – Remove file or files.

  • mv – Move file or files.

  • rmdir – Remove directory.

And some which are recommended to know in general:

  • ls – List all files in the current directory (or another directory, when specified). “ls -l” lists the files with their attributes.

  • lspci – List all PCI (and PCIe) devices on the bus. Useful for checking if Xillybus has been detected as a PCIe peripheral. Also try lspci -v, lspci -vv and lspci -n.

  • lsusb – List all USB devices on the bus. Useful for checking if XillyUSB has been detected as a peripheral. Also try lsusb -v and lsusb -vv.

  • cd – Change directory

  • pwd – Show current directory

  • cat – Send the file (or files) to standard output. Or use standard input, if no argument is given. The original purpose of this command was to concatenate files, but it ended up as the Swiss knife for plain input and output from and to files.

  • man – Show the manual page of a command. Also try “man -a” (sometimes there’s more than one manual page for a command).

  • less – terminal pager. Shows a file or data from standard input page by page. See above. Also used to display a long output of a command. For example:

    $                ls                            -l                     |                      less
    
  • head – Show the beginning of a file

  • tail – Show the end of a file. Or even better, with the -f flag: show the end + new lines as they arrive. Good for log files, for example (as root):

    #                  tail                                 -f                          /var/log/syslog
    
  • diff – compare two text files. If it says nothing, files are identical.

  • cmp – compare two binary files. If it says nothing, files are identical.

  • hexdump – Show the content of a file in a neat format. Flags -v and -C are preferred.

  • df – Show the mounted disks and how much space there is left in each. Even better, “df -h”

  • make – Attempt to build (run a compilation of) a project, following the rules in the Makefile

  • gcc – The GNU C compiler.

  • ps – Get a list of running processes. “ps a”, “ps au” and “ps aux” will supply different amounts of information.

And a couple of advanced commands:

  • grep – Search for a textual pattern in a file or standard input. The pattern is a regular expression, but if it’s just text, then it searches for the string. For example, search for the word “xillybus”, as a case insensitive string, in the main log file, and show the output page after page:

    #       grep       -i      xillybus                    /var/log/syslog          |          less
    
  • find – Find a file. It has a complicated argument syntax, but it can find files depending on their name, age, type or anything you can think of. See the man page.