This lesson will introduce you to the following commands:
These four commands are among the most frequently used Linux commands. They are the basic commands for manipulating both files and directories.
Now, to be frank, some of the tasks performed by these commands are more easily done with a graphical file manager. With a file manager, you can drag and drop a file from one directory to another, cut and paste files, delete files, etc. So why use these old command line programs?
The answer is power and flexibility. While it is easy to perform simple file manipulations with a graphical file manager, complicated tasks can be easier with the command line programs. For example, how would you copy all the HTML files from one directory to another, but only copy files that did not exist in the destination directory or were newer than the versions in the destination directory? Pretty hard with with a file manager. Pretty easy with the command line:
[me@linuxbox me]$ cp -u *.html destination
Before I begin with our commands, I want to talk
about a shell feature that makes these commands so
powerful. Since the shell uses filenames so much,
it provides special characters to help you rapidly
specify groups of filenames. These special
characters are called wildcards. Wildcards
allow you to select filenames based on patterns of
characters. The table below lists the wildcards and
what they select:
Using wildcards, it is possible to construct
very sophisticated selection criteria for
filenames. Here are some examples of patterns and
what they match:
You can use wildcards with any command that accepts filename arguments.
The cp program copies files and directories. In its simplest form, it copies a single file:
[me@linuxbox me]$ cp file1 file2
It can also be used to copy multiple files (and/or directories) to a different directory:
[me@linuxbox me]$ cp file... directory
A note on notation: ... signifies that an item can be repeated one or more times.
Other useful examples of cp and its options include:
The mv command moves or renames files and directories depending on how it is used. It will either move one or more files to a different directory, or it will rename a file or directory. To rename a file, it is used like this:
[me@linuxbox me]$ mv filename1 filename2
To move files (and/or directories) to a different directory:
[me@linuxbox me]$ mv file... directory
Examples of mv and its
The rm command removes (deletes) files and directories.
[me@linuxbox me]$ rm file...
It can also be used to delete directories:
[me@linuxbox me]$ rm -r directory...
Examples of rm and its
Be careful with rm!
Linux does not have an undelete command. Once you delete something with rm, it's gone. You can inflict terrific damage on your system with rm if you are not careful, particularly with wildcards.
Before you use rm with wildcards, try this helpful trick: construct your command using ls instead. By doing this, you can see the effect of your wildcards before you delete files. After you have tested your command with ls, recall the command with the up-arrow key and then substitute rm for ls in the command.
The mkdir command is used to create directories. To use it, you simply type:
[me@linuxbox me]$ mkdir directory...
Using Commands With Wildcards
Since the commands we have covered here accept multiple file and directories names as arguments, you can use wildcards to specify them. Here are a few examples:
© 2000-2015, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved.
Linux® is a registered trademark of Linus Torvalds.