6:     The Command Line

Introduction

Although MEPIS Linux 7.0 offers a complete set of graphical tools for installing, configuring, and using your system, the command line (also called the console, terminal, BASH, or shell) is still a useful and at times indispensible tool. Here are some common uses:

The default program to run a terminal in a MEPIS Linux 7.0 KDE desktop window is Konsole, which can be found at KMenu --> System --> Terminal Program (Konsole).


First steps

Though console commands can be fairly complex, understanding the command line is just a matter of putting together simple things. To see how easy it can be, open Konsole and try a few basic commands. This will all make more sense if you do it as a tutorial exercise rather than just reading it. Let's start with a simple command: ls, which lists the contents of a directory. The basic command lists the contents of whatever directory you are currently in:

ls

That's a useful command, but it's just a few short columns of names printed across the screen. Suppose we want more information on the files in this directory. We can add a switch to the command to make it print out more information. A switch is a modifier we append to a command to change its behavior. In this case, the switch we want is:

ls -l

As you can see on your own screen if you are following along, this switch provides more detailed information on the files in any directory. Of course, we might want to see the contents of another directory (without going there first). To do this, we add an argument to the command, specifying which file we want to look at. An argument is a value or reference we add to a command to target its operation. In this case:

ls -l /usr/bin/

By giving an argument of /usr/bin, we can list the contents of that directory, rather than the one where we currently are.

There are a lot of files in /usr/bin! It would be nice if we could filter this output so that only entries that contained, say, the word "fire" would be listed. We can do this by piping the output of the ls command into another command, grep. The pipe, or “|” character, is used to send the ouput of one command to the input of another.

The command grep searches for the pattern you give it and returns all matches, so piping the output of the previous command to it filters the output.

ls -l /usr/bin/ | grep fire

Thanks to grep, we only see the lines of the output that contain the word "fire". Finally, suppose we want these results saved in a text file for use at a later time?

When we issue commands, the output is usually directed to the console display; but we can redirect this output somewhere else, such as to a file, using the > (redirect) symbol:

ls -l /usr/bin/ | grep fire > FilesOfFire.txt

This command now instructs your computer to make a detailed list of all the files that contain the word "fire" and to create a text file containing that list. As you can see, the console can be used to perform complex tasks very easily by joining simple commands together in different ways.


Common commands

Here is a list of rudimentary terminal commands. For a complete reference, see the Links and Guides section.

Filesystem navigation
cd /usr/share Changes current directory to the given path: “/usr/share”. With no argument, cd takes you to your home directory.
pwd Prints the current working directory path
lsLists the contents of the current directory. Use the -a switch to show hidden files as well, and the -l switch to show details on all files. Often combined with other terms: lsusb lists all the usb devices, lsmod all the modules, etc.
File management
cp source.file destination.file Copy a file to another filename or location. Use the -R switch ("recursive") to copy entire directories.
mv source.file destination.file Move a file or directory from one location to another. Also used to rename files or directories and to make a backup, for example before changing a critical file such as xorg.conf.
rm deleteme.file Delete a file. Use the -R switch to delete a directory, and the -f switch ("force") if you don't want to be prompted to confirm each deletion.
cat file.txt Prints the contents of a file on the screen. Only use on text files.
grep Find a given string of characters in a given piece of text, and print the entire line it was on. Usually used with a pipe, e.g. cat somefile.txt | grep somestring will display the line from somefile.txt that contains ”somestring”. To find a network usb card, for instance, you could type: lsusb | grep network. The grep command is case sensitive by default, use the -i switch to make it case-insensitive.
most More sophisticated file viewer, which features one screenful at a time, up and down scrolling, text searching, line numbers, and other things. Often used in a pipe, for instance cat somefile.txt | most. NOTE: this replaces less as the default pager in MEPIS Linux 7.0.
Symbols
| The pipe symbol, used to send the output of one command into the input of another.
> The redirect symbol, used to send the output of a command into a file or device. Doubling the redirect symbol will cause the output to be added to the end of an existing file rather than replacing it.
& Adding the ampersand to the end of a command (with a space before it) causes it to run in the background, so that you don't have to wait for it to complete to issue the next command.


Troubleshooting

For most new Linux users, the command line is mainly used as a troubleshooting tool. Terminal commands give quick, detailed information that can be easily pasted into a forum post, search box, or email when seeking help on the web. Here are some common troubleshooting commands:

lspci Shows a quick summary of detected internal hardware devices. If a devices shows as unknown, you usually have a driver issue. The -v switch causes more detailed information to be displayed.
lsusb Lists attached usb devices.
dmesg Shows the system log for the current session (i.e. since you last booted). The output is quite long, and usually this is piped through grep, less (similar to most) or tail (to see what happened most recently). For example, to find potential errors related to your network hardware, try dmesg | grep -i net.
ifconfig Shows the status of currently active network interfaces. See Section 5.7.1.
iwconfig Shows the status of your wireless network interface. See Section 5.7.1.
top Provides a real-time list of running processes and various statistics about them.


Accessing documentation for commands