008 Create Delete Copy and Move Files and Directories

Tech Tutorial: Understand and Use Essential Tools #

Introduction #

In this tutorial, we will cover essential tools and commands on Red Hat Enterprise Linux (RHEL) for managing files and directories. These basic but crucial skills are part of the Red Hat Certified Systems Administrator (RHCSA) exam objectives. Understanding how to manipulate files and directories from the command line is fundamental for any systems administrator.

Step-by-Step Guide #

1. Creating Files and Directories #

Creating Files #

To create a file in RHEL, you can use the touch command. This command will create an empty file if the specified file does not exist.

touch myfile.txt

This command creates an empty file named myfile.txt in the current directory.

Creating Directories #

To create a directory, you use the mkdir command. For example, to create a directory named mydirectory, you would run:

mkdir mydirectory

To create multiple directories at once or to create nested directories, you can use:

mkdir -p mydirectory/nesteddirectory

This command creates both mydirectory and nesteddirectory within it, even if mydirectory does not yet exist.

2. Deleting Files and Directories #

Deleting Files #

To delete a file, use the rm command. For instance, to delete myfile.txt:

rm myfile.txt

Be cautious with this command, as it does not move the file to a trash bin; it permanently deletes it.

Deleting Directories #

To remove a directory, use rm with the -r option for recursive deletion:

rm -r mydirectory

If you need to force deletion without being asked for confirmation, add the -f option:

rm -rf mydirectory

3. Copying Files and Directories #

Copying Files #

To copy a file, use the cp command. To copy myfile.txt to a new file mycopy.txt:

cp myfile.txt mycopy.txt

Copying Directories #

To copy directories, including their contents, add the -r (recursive) option:

cp -r mydirectory newdirectory

This copies mydirectory and all its contents into newdirectory.

4. Moving Files and Directories #

Moving Files #

The mv command is used to move or rename files. To move myfile.txt to a new directory:

mv myfile.txt mydirectory/

To rename the file during the move:

mv myfile.txt mydirectory/renamedfile.txt

Moving Directories #

Moving directories works similarly to moving files. To move mydirectory into another directory targetdirectory:

mv mydirectory targetdirectory/

This moves the entire mydirectory into targetdirectory.

Detailed Code Examples #

Let’s consider a real-world scenario where you need to organize documents in a project:

  1. Create project directory and subdirectories:

    mkdir -p project/{documents,reports,logs}
    
  2. Create a new document:

    touch project/documents/newdoc.txt
    
  3. Copy the document to reports for sharing:

    cp project/documents/newdoc.txt project/reports/
    
  4. Delete an outdated log file:

    rm project/logs/oldlog.txt
    
  5. Move a completed report to a separate directory:

    mkdir completed_reports
    mv project/reports/completed_report.txt completed_reports/
    

Conclusion #

Mastering file and directory management commands is a cornerstone of system administration on RHEL. This tutorial has equipped you with the knowledge to perform basic operations like creating, copying, moving, and deleting files and directories using the command line. Practice these commands to build your confidence and prepare effectively for the RHCSA exam.