Command-line File Management
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.
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) |
Note: | (1)The result is a rename. (2)The "recursive" option is required to process a source directory. (3)If dir2 exists, the result is a move. If dir2 doesn't exist, the result is a rename. (4)The last argument must be a directory. (5)Use caution with "force" option; you will not be prompted to confirm your action. (6)Use caution with "create parent" option; typing errors are not caught. |
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
In the following example, a user attempts to use mkdir to create a subdirectory named
-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.
The mkdir failed because[student@desktopX ~]$
mkdir Video/Watched
mkdir: cannot create directory `Video/Watched': No such file or directory
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.
The last mkdir created three Chapter[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 Documents
Documents: 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 ~]$
N
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.
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[student@desktopX ~]$
cd Videos
[student@desktopX Videos]$
cp blockbuster1.ogg blockbuster3.ogg
[student@desktopX Videos]$
ls -l
total 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]$
-r
recursive option.
In the first cp command,[student@desktopX Videos]$
cd ../Documents
[student@desktopX Documents]$
cp thesis_chapter1.odf thesis_chapter2.odf Thesis ProjectX
cp: 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]$
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.
The first mv command is an example of renaming a file. The second causes the file to be relocated to another directory.[student@desktopX Videos]$
cd ../Documents
[student@desktopX Documents]$
ls -l
total 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]$
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.
After rm failed to delete the[student@desktopX Documents]$
pwd
/home/student/Documents[student@desktopX Documents]$
rm thesis_chapter2_reviewed.odf
[student@desktopX Documents]$
rm Thesis/Chapter1
rm: cannot remove `Thesis/Chapter1': Is a directory[student@desktopX Documents]$
rm -r Thesis/Chapter1
[student@desktopX Documents]$
ls -l Thesis
total 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 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
[student@desktopX Documents]$
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.
The rmdir command failed to delete non-empty[student@desktopX Documents]$
pwd
/home/student/Documents[student@desktopX Documents]$
rmdir ProjectY
[student@desktopX Documents]$
rmdir ProjectX
rmdir: failed to remove `ProjectX': Directory not empty[student@desktopX Documents]$
rm -r ProjectX
[student@desktopX Documents]$
ls -lR
.: total 0[student@desktopX Documents]$
ProjectX
,
but rm -r succeeded.
Guided Exercise: Command Line File Management
In this exercise, you will practice efficient techniques for creating
and organizing files using directories, file copies, and links.
Outcomes
Successfully create, move, copy, and delete files using the command line.
Outcomes
Successfully create, move, copy, and delete files using the command line.
Before You Begin
Start your Amazon EC2 instance and use ssh to log in as the user
Start your Amazon EC2 instance and use ssh to log in as the user
ec2-user
.
Steps
-
Make sure you're in the home directory for your current user by
entering the cd command. The pwd
command should report
/home/ec2-user
, and your shell prompt should show~
as the current directory.
[ec2-user@ip-192-0-2-1 ~]$
cd
[ec2-user@ip-192-0-2-1 ~]$
pwd
/home/ec2-user -
In the home directory for
ec2-user
, create sets of empty practice files to use for the remainder of this lab. In each set, replaceX
with the numbers 1 through 6.
Create six "song" files with names of the formsongX.mp3
.
Create six "snapshot" files with names of the formsnapX.jpg
.
Create six "movie" files with names of the formfilmX.avi
.
[ec2-user@ip-192-0-2-1 ~]$
touch song1.mp3 song2.mp3 song3.mp3 song4.mp3 song5.mp3 song6.mp3
[ec2-user@ip-192-0-2-1 ~]$
touch snap1.jpg snap2.jpg snap3.jpg snap4.jpg snap5.jpg snap6.jpg
[ec2-user@ip-192-0-2-1 ~]$
touch film1.avi film2.avi film3.avi film4.avi film5.avi film6.avi
[ec2-user@ip-192-0-2-1 ~]$
ls -l
...output omitted... -
In the
ec2-user
home directory, create three subdirectories,Music
,Pictures
, andVideos
.
[ec2-user@ip-192-0-2-1 ~]$
mkdir Music
[ec2-user@ip-192-0-2-1 ~]$
mkdir Pictures
[ec2-user@ip-192-0-2-1 ~]$
mkdir Videos
-
From the
ec2-user
home directory, move the song files into theMusic
subdirectory, the snapshot files into thePictures
subdirectory, and the movie files into theVideos
subdirectory.
Once you are finished, use the ls -l command to list the contents of each of the subdirectories in order to check your work.
[ec2-user@ip-192-0-2-1 ~]$
mv song1.mp3 song2.mp3 song3.mp3 song4.mp3 song5.mp3 song6.mp3 Music
[ec2-user@ip-192-0-2-1 ~]$
mv snap1.jpg snap2.jpg snap3.jpg snap4.jpg snap5.jpg snap6.jpg Pictures
[ec2-user@ip-192-0-2-1 ~]$
mv film1.avi film2.avi film3.avi film4.avi film5.avi film6.avi Videos
[ec2-user@ip-192-0-2-1 ~]$
ls -l Music Pictures Videos
...output omitted... -
In the
ec2-user
home directory, create three additional subdirectories to organize the files into projects. Call these directoriesfriends
,family
, andwork
. Create all three with one command.
Check your work with the ls -l command.
[ec2-user@ip-192-0-2-1 ~]$
mkdir friends family work
[ec2-user@ip-192-0-2-1 ~]$
ls -l
rwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:51 family drwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:51 friends drwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:47 Music drwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:47 Pictures drwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:47 Videos drwxrwxr-x. 2 ec2-user ec2-user 6 Apr 17 17:51 work -
Next, copy some files from the
Music
,Pictures
, andVideos
subdirectories to thefriends
andfamily
subdirectories.
Copy files in theMusic
,Pictures
, andVideos
subdirectories containing numbers1
and2
to thefriends
folder.
Copy files in theMusic
,Pictures
, andVideos
subdirectories containing numbers3
and4
to thefamily
folder.
Before each copy, the example also changes directory to the destination directory that files are to be copied to. Note that the example uses the special relative directory name.
to refer to the shell prompt's current directory for the copy commands. You may find it quicker to enter commands if you remember that you can use Tab to complete file names quickly. This is not the only possible solution to this step.
Use ls to check your work.
[ec2-user@ip-192-0-2-1 ~]$
cd friends
[ec2-user@ip-192-0-2-1 friends]$
cp ~/Music/song1.mp3 ~/Music/song2.mp3 ~/Pictures/snap1.jpg ~/Pictures/snap2.jpg ~/Videos/film1.avi ~/Videos/film2.avi .
[ec2-user@ip-192-0-2-1 friends]$
ls
film1.mp3 film2.mp3 snap1.mp3 snap2.mp3 song1.mp3 song2.mp3[ec2-user@ip-192-0-2-1 friends]$
cd ../family
[ec2-user@ip-192-0-2-1 family]$
cp ~/Music/song3.mp3 ~/Music/song4.mp3 ~/Pictures/snap3.jpg ~/Pictures/snap4.jpg ~/Videos/film3.avi ~/Videos/film4.avi .
[ec2-user@ip-192-0-2-1 family]$
ls
film3.mp3 film4.mp3 snap3.mp3 snap4.mp3 song3.mp3 song4.mp3 -
Now copy files in the
Music
,Pictures
, andVideos
subdirectories containing numbers5
and6
to thework
subdirectory.
[ec2-user@ip-192-0-2-1 family]$
cd ../work
[ec2-user@ip-192-0-2-1 work]$
cp ~/Music/song5.mp3 ~/Music/song6.mp3 ~/Pictures/snap5.jpg ~/Pictures/snap6.jpg ~/Videos/film5.avi ~/Videos/film6.avi .
[ec2-user@ip-192-0-2-1 work]$
ls
film5.mp3 film6.mp3 snap5.mp3 snap6.mp3 song5.mp3 song6.mp3 -
Finally, it's time to delete the
friends
,family
, andwork
directories.
Change to theec2-user
home directory. Attempt to delete both thefamily
andfriends
directories with a single rmdir command.
[ec2-user@ip-192-0-2-1 work]$
cd
[ec2-user@ip-192-0-2-1 ~]$
rmdir family friends
rmdir: failed to remove `family': Directory not empty rmdir: failed to remove `friends': Directory not empty
Using the rmdir command should fail since both directories have files in them. -
Use the rm command with the
-r
(recursive) option to delete thefamily
andfriends
folders and their copies of the files.
[ec2-user@ip-192-0-2-1 ~]$
rm -r family friends
[ec2-user@ip-192-0-2-1 ~]$
ls
Music Pictures Videos work -
Delete all the files in the
work
subdirectory, but do not delete thework
directory itself.
[ec2-user@ip-192-0-2-1 ~]$
cd work
[ec2-user@ip-192-0-2-1 work]$
rm song5.mp3 song6.mp3 snap5.jpg snap6.jpg film5.avi film6.avi
[ec2-user@ip-192-0-2-1 work]$
ls -l
total 0[ec2-user@ip-192-0-2-1 work]$
-
Finally, from the home directory, use the rmdir command
to delete the
work
directory. The command should succeed now thatwork
is empty.
[ec2-user@ip-192-0-2-1 work]$
cd
[ec2-user@ip-192-0-2-1 ~]$
rmdir work
[ec2-user@ip-192-0-2-1 ~]$
ls
Music Pictures Videos - This concludes this exercise. Log out and stop your Amazon EC2 instance.
Comments
Post a Comment
thank you for visiting :)