Feb. 23, 2011, 4:12 a.m.
posted by void
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.
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 Desktop [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.
[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 /home/greenfly/Desktop
Rename and Delete Files and Directories
[email protected]:~$ mkdir test [email protected]:~$ ls Desktop test
[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
[email protected]:~$ rm Desktop/screenshot1.png Desktop/screenshot2.png [email protected]:~$ ls Desktop/ testing [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.
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.