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.

ACTIVITY COMMAND SYNTAX
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.

[user@host ~]$ 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.

 [user@host ~]$ mkdir Videos/Watched
[user@host ~]$ ls -R Videos
Videos/:
blockbuster1.ogg blockbuster2.ogg Watched

Videos/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.

[user@host ~]$ cd Documents
[user@host Documents]$ mkdir ProjectX ProjectY
[user@host 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.

[user@host Documents]$ mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3
[user@host Documents]$ cd
[user@host ~]$ ls -R Videos Documents
Documents:
ProjectX  ProjectY  Thesis

Documents/ProjectX:

Documents/ProjectY:

Documents/Thesis:
Chapter1  Chapter2  Chapter3

Documents/Thesis/Chapter1:

Documents/Thesis/Chapter2:

Documents/Thesis/Chapter3:

Videos:
blockbuster1.ogg blockbuster2.ogg Watched

Videos/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.

[user@host ~]$ cd Videos/
[user@host Videos]$ cp blockbuster1.ogg blockbuster3.ogg
[user@host 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
[user@host 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.

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

Thesis:
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:

[user@host ~]$ cp /etc/hostname .
[user@host ~]$ cat hostname
host.example.com

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


[user@host Documents]$ cp -r Thesis ProjectX
[user@host Documents]$ ls -R ProjectX
ProjectX:
Thesis thesis_chapter1.odf thesis_chapter2.odf

ProjectX/Thesis:
Chapter1  Chapter2  Chapter3

ProjectX/Thesis/Chapter1:

ProjectX/Thesis/Chapter2:
thesis_chapter2.odf

ProjectX/Thesis/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.

[user@host Videos]$ cd ../Documents
[user@host 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

[user@host Documents]$ mv thesis_chapter2.odf thesis_chapter2_reviewed.odf
[user@host 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.

[user@host Documents]$ ls Thesis/Chapter1
[user@host Documents]$
[user@host Documents]$ mv thesis_chapter1.odf Thesis/Chapter1
[user@host Documents]$ ls Thesis/Chapter1
thesis_chapter1.odf
[user@host 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.

[user@host Documents]$ pwd
/home/geek/Documents

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

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

[user@host Documents]$ rm thesis_chapter2_reviewed.odf
[user@host 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.

[user@host Documents]$ rm Thesis/Chapter1
rm: cannot remove `Thesis/Chapter1': Is a directory

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

[user@host Documents]$ ls -R Thesis
Thesis/:
Chapter1 Chapter2 Chapter3

Thesis/Chapter1:
thesis_chapter1.odf

Thesis/Chapter2:
thesis_chapter2.odf

Thesis/Chapter3:

[user@host Documents]$ rm -r Thesis/Chapter1
[user@host 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.

[user@host 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.

[user@host Documents]$ pwd
/home/geek/Documents
[user@host Documents]$ rmdir ProjectY
[user@host Documents]$ rmdir ProjectX
rmdir: failed to remove `ProjectX': Directory not empty
[user@host Documents]$ rm -r ProjectX
[user@host Documents]$ ls -lR
.:
total 0
[user@host Documents]$