Use the Command Line

Use the Command Line

Put your mouse down for a second, pop open a terminal window, and fall in love with the shell all over again.

If you are used to Windows or Mac desktops, the command line might seem like a foreign thing to you. Typing commands into a window might seem, well, arcane. But even though Linux has really progressed on the desktop, there's still a lot of power you can wield at the command line. If this is your first time with a terminal, this hack will guide you through some command-line basics.

Throughout this book, you'll find a number of places where you'll need to prefix commands with sudo. The sudo command [Hack #67] allows you to temporarily execute a command with different user privileges and is frequently used when you need to add or remove software [Hack #54] from the command line.

The first step is to launch a terminal. Click ApplicationsTerminal to start the default GNOME Terminal program.

Navigate the Filesystem

Now that the terminal program is open, you can navigate the filesystem. By default, terminals will open into your home directory, so one thing you might want to do is see what files are currently in your home directory. The ls command displays all the files in the directory you specify (or in the current directory if you don't list a directory):

[email protected]:~$ ls
[email protected]:~$ ls Desktop/
screenshot1.png  screenshot2.png

The first command lists all of the files in the home directory. In this case, only the Desktop directory exists. The second example lists the contents of the Desktop directory, where there are two screenshot images.

To change to a different directory, use the cd command followed by the directory to change to:

[email protected]:~$ cd Desktop/
[email protected]:~/Desktop$ ls
screenshot1.png  screenshot2.png

Notice that the terminal prompt changed in the second line to show that you are currently in the Desktop directory. You can also use the pwd command to see where you currently are:

[email protected]:~/Desktop$ pwd

The ~ symbol is shorthand in Linux for your user's home directory. If you type cd ~ you will automatically change back to your home directory. It saves you from having to type out cd /home/ username.

Rename and Delete Files and Directories

To create a directory from the command line, type the mkdir command followed by the name of the directory to create:

[email protected]:~$ mkdir test
[email protected]:~$ ls
Desktop  test

Use the mv command to move a file or directory to a different directory, or to rename it in its current directory. To rename the test directory you created to testing, you can type:

[email protected]:~$ mv test testing
[email protected]:~$ ls
Desktop  testing

If you wanted to move the testing directory inside the Desktop directory, you would just specify the Desktop directory as the second argument:

[email protected]:~$ mv testing Desktop/
[email protected]:~$ ls Desktop/
screenshot1.png  screenshot2.png  testing

The rm command removes files, and rmdir removes directories. Just use the commands followed by the files or directories to remove, respectively:

[email protected]:~$ rm Desktop/screenshot1.png Desktop/screenshot2.png
[email protected]:~$ ls Desktop/
[email protected]:~$ rmdir Desktop/testing/
[email protected]:~$ ls Desktop/
[email protected]:~$

You can also remove a directory and all files and directories inside of it by running rm -r followed by the name of the directory.

Be careful when you recursively delete a directory with this command that you do in fact want to remove all of the files within. Once removed via the command line, there's no trash bin to retrieve them from.

File Globs and Tab Completion

There are two major time-savers when dealing with long files on the command line: file globs and tab completion. File globs are symbols you can use as wildcards in the place of a filename. You can substitute the ? symbol for any single character in a filename, and * for any number of characters in a filename. For instance, say you had three files: foo, bar, and baz. If you wanted to delete both bar and baz, you would type:

[email protected]:$ rm ba?

The ? matches both r and the z at the end of the filename. If you wanted to remove all files that started with the letter b, you would type:

[email protected]:$ rm b*

Tab completion is another time-saver on the command line. If you start to type a command and then hit the Tab key, the shell will automatically attempt to complete the name of the command for you. In the case that more than one command matches what you have typed so far, hit Tab an extra time, and you will be shown all of the options that match:

[email protected]:~$ gnome-cups-<Tab><Tab>
gnome-cups-add      gnome-cups-icon     gnome-cups-manager

Tab completion also works for files and directory names. Just type the first part of the filename and hit Tab, and the shell will fill out the rest for you.

Once you are finished with a terminal, you can close the window like any other window, or, alternatively, you can type exit on the command line.

 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows