Creating, copying, moving, and, removing files and directories are common operations for a system administrator. Without options, some commands are used to interact with files, or they can manipulate directories with the appropriate set of options.
Be aware of the options that are used when running a command. The meaning of some options might differ between commands.
The mkdir command creates one or more directories or subdirectories.
It takes as an argument a list of paths to the directories that you want to create.
In the following example, files and directories are organized beneath the /home/user/Documents directory.
Use the mkdir command and a space-delimited list of the directory names to create multiple directories.
[user@host ~]$cd Documents[user@host Documents]$mkdir ProjectX ProjectY ProjectZ[user@host Documents]$lsProjectX ProjectY ProjectZ
If the directory exists, or a parent directory of the directory that you are trying to create does not exist, then the mkdir command fails and it displays an error.
The mkdir command -p (parent) option creates any missing parent directories for the requested destination.
In the following example, the mkdir command creates three Chapter subdirectories with one command.
The N-p option creates the missing Thesis parent directory.
[user@host Documents]$mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3[user@host Documents]$ls -R Thesis/Thesis/: Chapter1 Chapter2 Chapter3 Thesis/Chapter1: Thesis/Chapter2: Thesis/Chapter3:
Use the mkdir command -p option with caution, because spelling mistakes can create unintended directories without generating error messages.
In the following example, imagine that you are trying to create a Watched subdirectory in the Videos directory, but you accidentally omitted the letter "s" in Videos in your mkdir command.
[user@host ~]$mkdir Video/Watchedmkdir: cannot create directoryVideo/Watched: No such file or directory
The mkdir command fails because the Video directory does not exist.
If you had used the mkdir command with the -p option, then the Video directory would be created unintentionally. The Watched subdirectory would be created in that incorrect directory.
The cp command copies a file, and creates a file either in the current directory or in a different specified directory.
[user@host ~]$cd Videos[user@host Videos]$cp blockbuster1.ogg blockbuster3.ogg[user@host Videos]$ls -ltotal 0 -rw-rw-r--. 1 user user 0 Feb 8 16:23 blockbuster1.ogg -rw-rw-r--. 1 user user 0 Feb 8 16:24 blockbuster2.ogg -rw-rw-r--. 1 user user 0 Feb 8 16:34blockbuster3.ogg
You can also use the cp command to copy multiple files to a directory.
In this scenario, the last argument must be a directory.
The copied files retain their original names in the new directory.
If a file with the same name exists in the target directory, then the existing file is overwritten.
By default, the cp command does not copy directories; it ignores them.
In the following example, two directories are listed as arguments, the Thesis and ProjectX directories.
The last argument, the ProjectX directory, is the target and is valid as a destination.
The Thesis argument is ignored by the cp command, because it is intended to be copied and it is a directory.
[user@host Documents]$cp thesis_chapter1.txt thesis_chapter2.txt Thesis ProjectXcp: -r not specified; omitting directory 'Thesis' [user@host Documents]$ls Thesis ProjectXProjectX: thesis_chapter1.txt thesis_chapter2.txt Thesis: Chapter1 Chapter2 Chapter3
The Thesis directory failed to copy, but the copying of the thesis_chapter1.txt and thesis_chapter2.txt files succeeded.
You can copy directories and their contents by using the cp command -r option.
Keep in mind that you can use the . and .. special directories in command combinations.
In the following example, the Thesis directory and its contents are copied to the ProjectY directory.
[user@host Documents]$cd ProjectY[user@host ProjectY]$cp -r ../Thesis/ .[user@host ProjectY]$ls -lR.: total 0 drwxr-xr-x. 5 user user 54 Mar 7 15:08 Thesis ./Thesis: total 0 drwxr-xr-x. 2 user user 6 Mar 7 15:08 Chapter1 drwxr-xr-x. 2 user user 6 Mar 7 15:08 Chapter2 drwxr-xr-x. 2 user user 6 Mar 7 15:08 Chapter3 ./Thesis/Chapter1: total 0 ./Thesis/Chapter2: total 0 ./Thesis/Chapter3: total 0
The mv command moves files from one location to another.
If you think of the absolute path to a file as its full name, then moving a file is effectively the same as renaming a file.
The contents of the files that are moved remain unchanged.
Use the mv command to rename a file.
In the following example, the mv thesis_chapter2.txt command renames the thesis_chapter2.txt file to thesis_chapter2_reviewed.txt in the same directory.
[user@host Documents]$ls -l-rw-r--r--. 1 user user 7100 Mar 7 14:37 thesis_chapter1.txt -rw-r--r--. 1 user user 11431 Mar 7 14:39 thesis_chapter2.txt ...output omitted... [user@host Documents]$mv thesis_chapter2.txt thesis_chapter2_reviewed.txt[user@host Documents]$ls -l-rw-r--r--. 1 user user 7100 Mar 7 14:37 thesis_chapter1.txt -rw-r--r--. 1 user user 11431 Mar 7 14:39 thesis_chapter2_reviewed.txt ...output omitted...
Use the mv command to move a file to a different directory.
In the next example, the thesis_chapter1.txt file is moved from the ~/Documents directory to the ~/Documents/Thesis/Chapter1 directory.
You can use the mv command -v option to display a detailed output of the command operations.
[user@host Documents]$ls Thesis/Chapter1[user@host Documents]$ [user@host Documents]$mv -v thesis_chapter1.txt Thesis/Chapter1renamed 'thesis_chapter1.txt' -> 'Thesis/Chapter1/thesis_chapter1.txt' [user@host Documents]$ls Thesis/Chapter1thesis_chapter1.txt [user@host Documents]$ls -l-rw-r--r--. 1 user user 11431 Mar 7 14:39 thesis_chapter2_reviewed.txt ...output omitted...
The rm command removes files.
By default, rm does not remove directories.
You can use the rm command -r or the --recursive option to enable the rm command to remove directories and their contents.
The rm -r command traverses each subdirectory first, and individually removes their files before removing each directory.
In the following example, the rm command removes the thesis_chapter1.txt file without options, but to remove the Thesis/Chapter1 directory, you must add the -r option.
[user@host Documents]$ls -l Thesis/Chapter1-rw-r--r--. 1 user user 7100 Mar 7 14:37 thesis_chapter1.txt [user@host Documents]$rm Thesis/Chapter1/thesis_chapter1.txt[user@host Documents]$rm Thesis/Chapter1rm: cannot remove 'Thesis/Chapter1': Is a directory [user@host Documents]$rm -r Thesis/Chapter1[user@host Documents]$ls -l Thesisdrwxr-xr-x. 2 user user 6 Mar 7 12:37 Chapter2 drwxr-xr-x. 2 user user 6 Mar 7 12:37 Chapter3
Red Hat Enterprise Linux does not provide a command-line undelete feature, nor a "trash bin" from which you can restore files held for deletion. A trash bin is a component of a desktop environment such as GNOME, but is not used by commands that are run from a shell.
It is a good idea to verify your current working directory before you remove a file or directory by using relative paths.
[user@host Documents]$pwd/home/user/Documents [user@host Documents]$ls -l thesis* -rw-r--r--. 1 user user 11431 Mar 7 14:39 thesis_chapter2_reviewed.txt [user@host Documents]$rm thesis_chapter2_reviewed.txt[user@host Documents]$ls -l thesis* ls: cannot access 'thesis*': No such file or directory
You can use the rm command -i option to interactively prompt for confirmation before deleting.
This option is essentially the opposite of using the rm command -f option, which forces the removal without prompting the user for confirmation.
[user@host Documents]$rm -ri Thesisrm: descend into directory 'Thesis'?yrm: descend into directory 'Thesis/Chapter2'?yrm: remove regular empty file 'Thesis/Chapter2/thesis_chapter2.txt'?yrm: remove directory 'Thesis/Chapter2'?yrm: remove directory 'Thesis/Chapter3'?yrm: remove directory 'Thesis'?y
If you specify both the -i and -f options, then the -f option takes priority and you are not prompted for confirmation before rm removes files.
You can also use the rmdir command to remove empty directories.
Use the rm command -r option to remove non-empty directories.
[user@host Documents]$pwd/home/user/Documents [user@host Documents]$rmdir ProjectZ[user@host Documents]$rmdir ProjectXrmdir: failed to remove 'ProjectX': Directory not empty [user@host Documents]$rm -r ProjectX[user@host Documents]$