How to Use the Command Line

A Second Lesson

The article How to Use the Command Line, A First Lesson briefly describes the command line, explains why it is important, and tells how to access it. It also introduces what are probably the three most basic commands on Linux and other Unix-like operating systems: pwd (which shows the directory in which the user is presently working), ls (which lists the files and directories) and cd (which changes directories). This lesson introduces six commonly used commands for creating, removing, copying and moving files and directories.

The touch Command

The touch command is the easiest way to create new, empty files. Its syntax is

touch [option] file_name(s)

No options are required for basic file creation. Thus, for example, to create a new file named file1 within the current directory (i.e., the directory in which the user is currently working), all that is necessary is to type the following command and then press the ENTER key:

touch file1

Any number of files can be created simultaneously with touch. Each name must be separated by at least one space. Thus, example, the following would create three files named file2, file3, file4:

touch file2 file3 file4

The existence of these new files can easily be confirmed by using the ls command and seeing if they are included in its output. For example, the following would provide the names of all of the files and directories in the current directory:


A useful feature of touch is that, in contrast to some commands, it does not automatically overwrite (i.e., erase the contents of) existing files that have the same names as new files being created.

The mkdir Command

The mkdir command is is used to create new directories (which are also referred to as folders in some operating systems). Its syntax is

mkdir [option] directory_name(s)

As is the case with touch, mkdir is usually used without any of its few options. directory_name is the name of any directory that the user is asking mkdir to create. Any number of directories can be created simultaneously. Thus, for example, the following would create three directories in the current directory with the names dir1, dir2 and dir3:

mkdir dir1 dir2 dir3

If a directory name provided as an argument (i.e., input) to mkdir is the same as that of an existing directory or file in the same directory in which the user is asking mkdir to create the new directory, mkdir will return a warning message such as mkdir: cannot create directory `dir_1': File exists and will not create a directory with that name. However, it will then continue to create directories for any other names provided as arguments.

The rm Command

The rm command is often the most efficient way to remove files and directories. Its syntax is

rm [option] file_name(s)

Thus, for example, to delete file1, the following would be used:

rm file1

rm can delete any number of files simultaneously. Thus, for example, the following would remove both file2 and file3:

rm file2 file3

This can become tedious if it is desired to delete a large number of files, such as all of the files within a directory. A much more efficient (but also more dangerous) way to do this is to use wildcards, which can be used to represent anything. For example, the star (*) character is a wildcard that can represent any string (i.e., any sequence of characters). Therefore, the following would delete every file in the current directory:

rm *

And the following would remove everything in the directory dir1:

rm dir1/*

As another illustration of the great versatility that wildcards add to the command line, the following would delete all files in the current directory that have a file name extension of .html but would leave all others intact:

rm *.html

As a safety measure, rm does not delete directories by default. This is important because once deleted, it is extremely difficult or impossible to recover deleted data on Unix-like operating systems (in contrast to the Microsoft Windows operating systems). In order to delete directories, it is necessary to use the -r option or the -R option (they are the same). This option recursively removes directories, along with all of their contents, that are included in the argument list; that is, the specified directories will first be emptied of any subdirectories (including their subdirectories and files, etc.) and files and then removed.

The rmdir Command

The rmdir command provides another way to remove empty directories. Its syntax is:

rmdir [option] directory_names

When used without any options, rm will delete any empty directories whose names are supplied as arguments. Thus, for example, the following command would remove two empty directories named dir4 and dir5 that are located in the current directory:

rmdir dir4 dir5

The ability to remove only empty directories is a built-in safeguard that helps prevent the accidental loss of data. In contrast to the rm command, there is no -r option, at least on the GNU version that is standard on Linux. Thus, if a user wants to remove an entire directory structure, it is usually most efficient to use rm with its -r option rather than trying to first remove the contents of each directory, its subdirectories, etc.

The cp Command

The cp command is used to copy files and directories. The copies become independent of the originals (i.e., a subsequent change in one will not affect the other).

cp's basic syntax is

cp [options] name new_name

If a file with the same name as that assigned to the copy of a file (or a directory with the same name as that assigned to the copy of a directory) already exists, it will be overwritten. By default, cp only copies files and not directories.

When a copy is made of a file or directory, the copy must have a different name than the original if it is to be placed in the same directory as the original. However, the copy can have the same name if it is made in a different directory. Thus, for example, a file named file4 which resides in the current directory could be copied with the same name into another directory, such as into /home/john/, as follows:

cp file4 /home/john/file4

Any number of files can be simultaneously copied into another directory by listing their names followed by the name of the directory. cp is an intelligent command and knows to do this when only the final argument is a directory. The files copied into the directory will all have the same names as the originals. Thus, for example, the following would copy the files named file5, file6 and file7 into a directory named dir6:

cp file5 file6 file7 dir6

cp's -r option, which can also be written with an upper case -R, allows directories including all of their contents to be copied. Thus, for example, the following command would make a copy of an existing directory called dir7, inclusive of all it contents (i.e., files, subdirectories, their subdirectories, etc.), called dir8:

cp -r dir7 dir8

The mv Command

The mv command is used to rename and move files and directories. Its basic syntax is:

mv [options] source target

If the target file or directory is located in the same directory as the source file or directory, then the source file or directory can only be renamed. If both are in different directories, then the source file or directory is moved to a new directory, in which it can keep its original name or be assigned a new name.

Thus, for example, to rename a file called file8 to file9, both in the current directory, the following would be used:

mv file8 file9

The following would move a file named file10, without changing its name, from the current directory to an existing subdirectory of the current directory named dir9:

mv file10 dir9/file10

Detailed information (including all options) about mv as well as all of the other commands discussed above can be obtained by using their --help option. Thus, for example, to obtain more information about the mv command, the following would be used:

mv --help

Likewise, information about the current version of each command can be obtained by using its --version option.

Created April 16, 2006.
Copyright © 2006 The Linux Information Project. All Rights Reserved.