Work from the command line to create, move, and delete files and directories.
After completing this section, students should be able to create, copy, link, move, and remove files and subdirectories in various directories.
File management involves creating, deleting, copying, and moving files. Additionally, directories can be created, deleted, copied, and moved to help organize files logically. When working at the command line, file management requires awareness of the current working directory to choose either absolute or relative path syntax as most efficient for the immediate task.
Table 2.2. File management commands
| Activity | Single source (note) | Multiple source (note) | ||||||
|---|---|---|---|---|---|---|---|---|
| Copy file | cp file1 file2 | cp file1 file2 file3 dir (4) | ||||||
| Move file | mv file1 file2 (1) | mv file1 file2 file3 dir (4) | ||||||
| Remove file | rm file1 | rm -f file1 file2 file3 (5) | ||||||
| Create directory | mkdir dir | mkdir -p par1/par2/dir (6) | ||||||
| Copy directory | cp -r dir1 dir2 (2) | cp -r dir1 dir2 dir3 dir4 (4) | ||||||
| Move directory | mv dir1 dir2 (3) | mv dir1 dir2 dir3 dir4 (4) | ||||||
| Remove directory | rm -r dir1 (2) | rm -rf dir1 dir2 dir3 (5) | ||||||
| Remove empty directory | rmdir dir1 | rmdir -p dir1/dir2/dir3 | ||||||
| Note: |
| |||||||
Create directories
The mkdir command creates one or more directories or subdirectories,
generating errors if the file name already exists or when attempting to create a directory
in a parent directory that doesn't exist. The -p parent
option creates missing parent directories for the requested destination.
Be cautious when using mkdir -p, since accidental spelling mistakes
create unintended directories without generating error messages.
In the following example, a user attempts to use mkdir to create a subdirectory named
Watched in the existing Videos directory, but mistypes the directory name.
[student@desktopX ~]$mkdir Video/Watchedmkdir: cannot create directory `Video/Watched': No such file or directory
The mkdir failed because Videos was misspelled and the
directory Video does not exist.
If the user had used mkdir with the -p option, there would be no error
and the user would end up with two directories, Videos and Video,
and the Watched subdirectory would be created in the wrong place.
[student@desktopX ~]$mkdir Videos/Watched[student@desktopX ~]$cd Documents[student@desktopX Documents]$mkdir ProjectX ProjectY[student@desktopX Documents]$mkdir -p Thesis/Chapter1 Thesis/Chapter2 Thesis/Chapter3[student@desktopX Documents]$cd[student@desktopX ~]$ls -R Videos DocumentsDocuments: ProjectX ProjectY Thesis thesis_chapter1.odf thesis_chapter2.odf 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:[student@desktopX ~]$
The last mkdir created three ChapterN
subdirectories with one command.
The -p parent option
created the missing parent directory Thesis.
Copy files
The cp command copies one or more files to become new, independent files. Syntax allows copying an existing file to a new file in the current or another directory, or copying multiple files into another directory. In any destination, new file names must be unique. If the new file name is not unique, the copy command will overwrite the existing file.
[student@desktopX ~]$cd Videos[student@desktopX Videos]$cp blockbuster1.ogg blockbuster3.ogg[student@desktopX Videos]$ls -ltotal 0 -rw-rw-r--. 1 student student 0 Feb 8 16:23 blockbuster1.ogg -rw-rw-r--. 1 student student 0 Feb 8 16:24 blockbuster2.ogg -rw-rw-r--. 1 student student 0 Feb 8 19:02 blockbuster3.ogg drwxrwxr-x. 2 student student 4096 Feb 8 23:35 Watched[student@desktopX 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.
Conflicting file names that exist at a destination may be overwritten.
To protect users from accidentally overwriting directories with contents,
multiple file cp commands ignore directories specified as a source.
Copying non-empty directories, with contents, requires the -r
recursive option.
[student@desktopX Videos]$cd ../Documents[student@desktopX Documents]$cp thesis_chapter1.odf thesis_chapter2.odf Thesis ProjectXcp: omitting directory `Thesis'[student@desktopX Documents]$cp -r Thesis ProjectX[student@desktopX Documents]$cp thesis_chapter2.odf Thesis/Chapter2/[student@desktopX Documents]$ls -R.: ProjectX ProjectY Thesis thesis_chapter1.odf thesis_chapter2.odf ./ProjectX: Thesis thesis_chapter1.odf thesis_chapter2.odf ./ProjectX/Thesis: ./ProjectY: ./Thesis: Chapter1 Chapter2 Chapter3 ./Thesis/Chapter1: ./Thesis/Chapter2: thesis_chapter2.odf ./Thesis/Chapter3:[student@desktopX Documents]$
In the first cp command, Thesis failed to copy,
but thesis_chapter1.odf and thesis_chapter2.odf
succeeded. Using the -r recursive option,
copying Thesis succeeded.
Move files
The mv command renames files in the same directory, or relocates files to a new directory. File contents remain unchanged. Files moved to a different file system require creating a new file by copying the source file, then deleting the source file. Although normally transparent to the user, large files may take noticeably longer to move.
[student@desktopX Videos]$cd ../Documents[student@desktopX Documents]$ls -ltotal 0 -rw-rw-r--. 1 student student 0 Feb 8 16:24 thesis_chapter1.odf -rw-rw-r--. 1 student student 0 Feb 8 16:24 thesis_chapter2.odf[student@desktopX Documents]$mv thesis_chapter2.odf thesis_chapter2_reviewed.odf[student@desktopX Documents]$mv thesis_chapter1.odf Thesis/Chapter1[student@desktopX Documents]$ls -lR.: total 16 drwxrwxr-x. 2 student student 4096 Feb 11 11:58 ProjectX drwxrwxr-x. 2 student student 4096 Feb 11 11:55 ProjectY drwxrwxr-x. 5 student student 4096 Feb 11 11:56 Thesis -rw-rw-r--. 1 student student 0 Feb 11 11:54 thesis_chapter2_reviewed.odf ./ProjectX: total 0 -rw-rw-r--. 1 student student 0 Feb 11 11:58 thesis_chapter1.odf -rw-rw-r--. 1 student student 0 Feb 11 11:58 thesis_chapter2.odf ./ProjectX/Thesis: total 0 ./ProjectY: total 0 ./Thesis: total 12 drwxrwxr-x. 2 student student 4096 Feb 11 11:59 Chapter1 drwxrwxr-x. 2 student student 4096 Feb 11 11:56 Chapter2 drwxrwxr-x. 2 student student 4096 Feb 11 11:56 Chapter3 ./Thesis/Chapter1: total 0 -rw-rw-r--. 1 student student 0 Feb 11 11:54 thesis_chapter1.odf ./Thesis/Chapter2: total 0 -rw-rw-r--. 1 student student 0 Feb 11 11:54 thesis_chapter2.odf ./Thesis/Chapter3: total 0[student@desktopX Documents]$
The first mv command is an example of renaming a file. The second causes the file to be relocated to another directory.
Remove files and directories
Default syntax for rm deletes files, but not directories.
Deleting a directory, and potentially many subdirectories and files below it,
requires the -r recursive option.
There is no command-line undelete feature, nor a trash bin from which to restore.
[student@desktopX Documents]$pwd/home/student/Documents[student@desktopX Documents]$rm thesis_chapter2_reviewed.odf[student@desktopX Documents]$rm Thesis/Chapter1rm: cannot remove `Thesis/Chapter1': Is a directory[student@desktopX Documents]$rm -r Thesis/Chapter1[student@desktopX Documents]$ls -l Thesistotal 8 drwxrwxr-x. 2 student student 4096 Feb 11 12:47 Chapter2 drwxrwxr-x. 2 student student 4096 Feb 11 12:48 Chapter3[student@desktopX Documents]$rm -ri Thesisrm: descend into directory `Thesis'?yrm: descend into directory `Thesis/Chapter2'?yrm: remove regular empty file `Thesis/Chapter2/thesis_chapter2.odf'?yrm: remove directory `Thesis/Chapter2'?yrm: remove directory `Thesis/Chapter3'?yrm: remove directory `Thesis'?y[student@desktopX Documents]$
After rm failed to delete the Chapter1 directory,
the -r recursive option succeeded.
The last rm command parsed into each subdirectory first,
individually deleting contained files before removing each now-empty directory.
Using -i will interactively prompt for each deletion. This is essentially
the opposite of -f which will force the deletion without prompting the user.
The rmdir command deletes directories only if empty. Removed directories cannot be undeleted.
[student@desktopX Documents]$pwd/home/student/Documents[student@desktopX Documents]$rmdir ProjectY[student@desktopX Documents]$rmdir ProjectXrmdir: failed to remove `ProjectX': Directory not empty[student@desktopX Documents]$rm -r ProjectX[student@desktopX Documents]$ls -lR.: total 0[student@desktopX Documents]$
The rmdir command failed to delete non-empty ProjectX,
but rm -r succeeded.
cp(1), mkdir(1), mv(1), rm(1), and rmdir(1) man pages