How to create, copy, move, and remove files and directories in Linux

Command line file management

To manage files, you need to be able to create, remove, copy, and move them. You also need to organize them logically into directories, which you also need to be able to create, remove, copy, and move. The following table summarizes some of the most common file management commands. The remainder of this section will discuss ways to use these commands in more detail.

Create a directory mkdir directory
Copy a file cp file new-file
Copy a directory and its contents cp -r directory new-directory
Move or rename a file or directory mv file new-file
Remove a file rm file
Remove a directory containing files rm -r directory
Remove an empty directory rmdir directory

Creating Directories

The mkdir command creates one or more directories or subdirectories. It takes as arguments a list of paths to the directories you want to create. The mkdir command will fail with an error if the directory already exists, or if you are trying to create a subdirectory in a directory that does not exist. The -p (parent) option creates missing parent directories for the requested destination. Use the mkdir -p command with caution, because spelling mistakes can create unintended directories without generating error messages.

In the following example, pretend that you are trying to create a directory in the Videos directory named Watched, but you accidentally left off the letter “s” in Videos in your mkdir command.

[[email protected] ~]$ mkdir Video/Watched
mkdir: cannot create directory `Video/Watched': No such file or directory

The mkdir command failed because Videos was misspelled and the directory Video does not exist. If you had used the mkdir command with the -p option, the directory Video would be created, which was not what you had intended, and the subdirectory Watched would be created in that incorrect directory.

After correctly spelling the Videos parent directory, creating the Watched subdirectory will succeed.

 [[email protected] ~]$ mkdir Videos/Watched
[[email protected] ~]$ ls -R Videos
blockbuster1.ogg blockbuster2.ogg Watched


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.

[[email protected] ~]$ cd Documents
[[email protected] Documents]$ mkdir ProjectX ProjectY
[[email protected] Documents]$ ls
ProjectX ProjectY

Use the mkdir -p command and space-delimited relative paths for each of the subdirectory names to create multiple parent directories with subdirectories.

[[email protected] Documents]$ mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3
[[email protected] Documents]$ cd
[[email protected] ~]$ ls -R Videos Documents
ProjectX  ProjectY  Thesis



Chapter1  Chapter2  Chapter3




blockbuster1.ogg blockbuster2.ogg Watched


The last mkdir command created three ChapterN subdirectories with one command. The -p option created the missing parent directory Thesis.

Copying Files

The cp command copies a file, creating a new file either in the current directory or in a specified directory. It can also copy multiple files to a directory.

WARNING: If the destination file already exists, the cp command overwrites the file.

[[email protected] ~]$ cd Videos/
[[email protected] Videos]$ cp blockbuster1.ogg blockbuster3.ogg
[[email protected] Videos]$ ls -l
total 0
-rw-rw-r--. 1 user user 0 Oct  3 04:47 blockbuster1.ogg
-rw-rw-r--. 1 user user 0 Oct  3 04:47 blockbuster2.ogg
-rw-rw-r--. 1 user user 0 Oct  3 04:47 blockbuster3.ogg
[[email protected] Videos]$

When copying multiple files with one command, the last argument must be a directory. Copied files retain their original names in the new directory. If a file with the same name exists in the target directory, the existing file is overwritten. By default, the cp does not copy directories; it ignores them. In the following example, two directories are listed, Thesis and ProjectX. Only the last argument, ProjectX is valid as a destination. The Thesis directory is ignored.

[[email protected] Videos]$ cd ../Documents
[[email protected] Documents]$ cp thesis_chapter1.odf thesis_chapter2.odf Thesis ProjectX
cp: omitting directory `Thesis'
[[email protected] Documents]$ ls Thesis ProjectX
thesis_chapter1.odf thesis_chapter2.odf

Chapter1  Chapter2  Chapter3

In the first cp command, the Thesis directory failed to copy, but the thesis_chapter1.odf and thesis_chapter2.odf files succeeded. If you want to copy a file to the current working directory, you can use the special . directory:

[[email protected] ~]$ cp /etc/hostname .
[[email protected] ~]$ cat hostname

Use the copy command with the -r (recursive) option, to copy the Thesis directory and its contents to the ProjectX directory.

[[email protected] Documents]$ cp -r Thesis ProjectX
[[email protected] Documents]$ ls -R ProjectX
Thesis thesis_chapter1.odf thesis_chapter2.odf

Chapter1  Chapter2  Chapter3




Moving Files

The mv command moves files from one location to another. If you think of the absolute path to a file as its full name, moving a file is effectively the same as renaming a file. File contents remain unchanged. Use the mv command to rename a file.

[[email protected] Videos]$ cd ../Documents
[[email protected] Documents]$ ls -l thesis*
-rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter1.odf
-rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter2.odf

[[email protected] Documents]$ mv thesis_chapter2.odf thesis_chapter2_reviewed.odf
[[email protected] Documents]$ ls -l thesis*
-rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter1.odf
-rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter2_reviewed.odf

Use the mv command to move a file to a different directory.

[[email protected] Documents]$ ls Thesis/Chapter1
[[email protected] Documents]$
[[email protected] Documents]$ mv thesis_chapter1.odf Thesis/Chapter1
[[email protected] Documents]$ ls Thesis/Chapter1
[[email protected] Documents]$ ls -l thesis*
-rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter2_reviewed.odf

Removing Files and Directories

The rm command removes files. By default, rm will not remove directories that contain files, unless you add the -r or –recursive option.

It is a good idea to verify your current working directory before removing a file or directory.

[[email protected] Documents]$ pwd

Use the rm command to remove a single file from your working directory.

[[email protected] Documents]$ ls -l thesis*
-rw-rw-r--. 1 user user 0 Feb 6 21:16 thesis_chapter2_reviewed.odf

[[email protected] Documents]$ rm thesis_chapter2_reviewed.odf
[[email protected] Documents]$ ls -l thesis*
ls: cannot access 'thesis*': No such file or directory

If you attempt to use the rm command to remove a directory without using the -r option, the command will fail.

[[email protected] Documents]$ rm Thesis/Chapter1
rm: cannot remove `Thesis/Chapter1': Is a directory

Use the rm -r command to remove a subdirectory and its contents.

[[email protected] Documents]$ ls -R Thesis
Chapter1 Chapter2 Chapter3




[[email protected] Documents]$ rm -r Thesis/Chapter1
[[email protected] Documents]$ ls -l Thesis
total 8
drwxrwxr-x. 2 user user 4096 Feb 11 12:47 Chapter2
drwxrwxr-x. 2 user user 4096 Feb 11 12:48 Chapter3

The rm -r command traverses each subdirectory first, individually removing their files before removing each directory. You can use the rm -ri command to interactively prompt for confirmation before deleting. This is essentially the opposite of using the -f option, which forces the removal without prompting the user for confirmation.

[[email protected] Documents]$ rm -ri Thesis
rm: descend into directory `Thesis'? y
rm: descend into directory `Thesis/Chapter2'? y
rm: remove regular empty file `Thesis/Chapter2/thesis_chapter2.odf'? y
rm: remove directory `Thesis/Chapter2'? y
rm: remove directory `Thesis/Chapter3'? y
rm: remove directory `Thesis'? y

WARNING: If you specify both the -i and -f options, the -f option takes priority and you will not be prompted for confirmation before rm deletes files.

In the following example, the rmdir command only removes the directory that is empty. Just like the earlier example, you must use the rm -r command to remove a directory that contains content.

[[email protected] Documents]$ pwd
[[email protected] Documents]$ rmdir ProjectY
[[email protected] Documents]$ rmdir ProjectX
rmdir: failed to remove `ProjectX': Directory not empty
[[email protected] Documents]$ rm -r ProjectX
[[email protected] Documents]$ ls -lR
total 0
[[email protected] Documents]$