Organize Files and Directories with mkdir, cp, mv, and rm
In this step, you will learn how to organize your files and directories effectively using mkdir (make directory), cp (copy), mv (move/rename), and rm (remove) commands. These commands are crucial for maintaining a clean and structured file system.
First, ensure you are in your ~/project directory.
cd ~/project
Verify your current location:
pwd
The output should be /home/labex/project.
mkdir: Creating Directories
You've already used mkdir to create a single directory. Now, let's explore more advanced mkdir options.
You can create multiple directories at once by listing them as arguments:
mkdir reports presentations
Verify their creation:
ls
You should see documents, presentations, and reports listed.
The mkdir command's -p (parents) option is very useful for creating any missing parent directories for the requested destination. This prevents errors if you try to create a subdirectory within a non-existent parent directory.
Let's create a nested directory structure: projects/alpha/docs.
mkdir -p projects/alpha/docs
Now, use ls -R to see the newly created nested structure:
ls -R projects
You should see:
projects:
alpha
projects/alpha:
docs
projects/alpha/docs:
cp: Copying Files and Directories
The cp command copies files and directories. When copying a file, it creates a duplicate either in the current directory or in a different specified directory.
Let's copy report.txt from documents to the reports directory.
cp documents/report.txt reports/
Verify the copy by listing the contents of the reports directory:
ls reports
You should see report.txt in the reports directory.
If a file with the same name exists in the target directory, cp will overwrite it by default.
To copy a directory and its contents, you must use the -r (recursive) option. By default, cp ignores directories if -r is not specified.
Let's copy the entire documents directory into projects/alpha/.
cp -r documents projects/alpha/
Verify the recursive copy:
ls -R projects/alpha/documents
You should see the contents of the documents directory (including notes.txt, drafts, and drafts/draft_v1.txt) now duplicated inside projects/alpha/documents.
projects/alpha/documents:
drafts notes.txt report.txt
projects/alpha/documents/drafts:
draft_v1.txt
mv: Moving and Renaming Files and Directories
The mv command moves files from one location to another. It can also be used to rename files or directories. 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 content of the files that are moved remain unchanged.
Let's rename notes.txt in the documents directory to meeting_notes.txt.
mv documents/notes.txt documents/meeting_notes.txt
Verify the rename:
ls documents
You should now see meeting_notes.txt instead of notes.txt.
Now, let's move report.txt from the reports directory into documents/drafts.
mv reports/report.txt documents/drafts/
Verify the move:
ls reports
The reports directory should now be empty.
ls documents/drafts
You should see draft_v1.txt and report.txt in the documents/drafts directory.
The -v option for mv displays a detailed output of the command operations, which can be helpful for confirmation.
mv -v documents/meeting_notes.txt documents/final_notes.txt
You will see output like:
renamed 'documents/meeting_notes.txt' -> 'documents/final_notes.txt'
rm: Removing Files and Directories
The rm command removes files. Be careful with rm, as deleted files are typically not recoverable from the command line.
Let's create a temporary file to practice rm.
touch temp_file.txt
Now, remove temp_file.txt:
rm temp_file.txt
Verify its removal:
ls
temp_file.txt should no longer be listed.
By default, rm does not remove directories. If you try to remove a non-empty directory without the correct option, you will get an error.
rm presentations
You will see an error:
rm: cannot remove 'presentations': Is a directory
To remove directories and their contents, you must use the -r (recursive) option. The rm -r command traverses each subdirectory first and individually removes their files before removing each directory.
Let's remove the presentations directory and its contents (which is currently empty, but -r is still required for directories).
rm -r presentations
Verify its removal:
ls
presentations should no longer be listed.
The -i option for rm interactively prompts for confirmation before deleting each file. This is a good safety measure.
Let's create a few more temporary files and then remove them interactively.
touch file1.txt file2.txt
rm -i file1.txt file2.txt
You will be prompted for each file:
rm: remove regular empty file 'file1.txt'? y
rm: remove regular empty file 'file2.txt'? y
Type y and press Enter for each prompt to confirm deletion.
The -f option (force) forces the removal without prompting the user for confirmation. If you specify both -i and -f, -f takes priority. Use -f with extreme caution.
Finally, let's clean up the projects directory.
rm -r projects
Verify that projects is gone:
ls
rmdir: Removing Empty Directories
You can also use the rmdir command to remove empty directories. It will fail if the directory is not empty.
Let's create an empty directory and remove it with rmdir.
mkdir empty_dir
rmdir empty_dir
Verify its removal:
ls
empty_dir should no longer be listed.
If you try to remove a non-empty directory with rmdir, it will fail:
mkdir test_dir
touch test_dir/test_file.txt
rmdir test_dir
You will see an error:
rmdir: failed to remove 'test_dir': Directory not empty
Remember to use rm -r for non-empty directories.
rm -r test_dir