Create, delete, copy, and move files and directories

1. Understand and Use Essential Tools

πŸ“˜Red Hat Certified System Administrator (RHCSA – EX200)


In Linux, managing files and directories is one of the most important skills for the RHCSA exam. Every Linux system administrator must know how to create, remove, copy, and move files and directories quickly using the command line. This is not just theory – you’ll need to perform these tasks in the exam.


1. Creating Files

In Linux, you can create files in several ways:

a) Using touch

  • Syntax:
touch filename
  • What it does: Creates an empty file if it doesn’t exist. If it already exists, it updates the file’s timestamp.
  • Example:
touch server.log

This creates an empty file called server.log in your current directory.

b) Using echo

  • Syntax:
echo "Some text" > filename
  • What it does: Creates a file and writes text into it.
  • Example:
echo "Server started at 10:00 AM" > server.log

Now, server.log contains that line.

c) Using cat

  • Syntax:
cat > filename
  • What it does: Lets you type multiple lines into a file until you press Ctrl+D.
  • Example:
cat > users.txt
john
mary
alice
Ctrl+D

Now, users.txt has three lines.


2. Creating Directories

Directories are containers for files.

a) Using mkdir

  • Syntax:
mkdir directory_name
  • Example:
mkdir backups

Creates a directory called backups.

b) Creating multiple directories

  • Syntax:
mkdir dir1 dir2 dir3
  • Example:
mkdir logs temp scripts

Creates logs, temp, and scripts directories at once.

c) Creating nested directories

  • Syntax:
mkdir -p parent/child/grandchild
  • Example:
mkdir -p /home/itteam/projects/2026

Creates projects inside /home/itteam and a subdirectory 2026 in it, even if projects doesn’t exist yet.


3. Deleting Files

Linux provides the rm command to delete files:

a) Delete a single file

rm filename
  • Example:
rm old_server.log

Removes the old_server.log file.

b) Delete multiple files

rm file1 file2 file3
  • Example:
rm temp1.txt temp2.txt temp3.txt

Removes all three files at once.

c) Delete files interactively

  • Option: -i asks before deleting each file.
rm -i temp.txt

⚠️ Warning: There’s no β€œRecycle Bin” in Linux. Once deleted with rm, the file is gone unless you have a backup.


4. Deleting Directories

Use rmdir or rm -r:

a) Using rmdir

  • Syntax:
rmdir directory_name
  • Works only if the directory is empty.
  • Example:
rmdir old_backups

b) Using rm -r

  • Syntax:
rm -r directory_name
  • Deletes the directory and everything inside it.
  • Example:
rm -r temp

c) Safe deletion with confirmation

rm -ri temp

Asks before deleting each file or subdirectory.


5. Copying Files

The cp command copies files or directories.

a) Copy a single file

  • Syntax:
cp source_file destination_file
  • Example:
cp server.log server_backup.log

Creates a copy of server.log called server_backup.log.

b) Copy to a directory

  • Syntax:
cp file_name directory_name/
  • Example:
cp server.log backups/

Copies server.log into the backups directory.

c) Copy multiple files

cp file1 file2 file3 destination_directory/

d) Copy directories

  • Must use -r for recursive copying.
cp -r source_directory destination_directory
  • Example:
cp -r projects/ /home/itteam/backups/

Copies the entire projects folder, including all files and subfolders, to backups.


6. Moving and Renaming Files

The mv command is used for moving or renaming files/directories.

a) Move a file

  • Syntax:
mv source_file destination_directory/
  • Example:
mv server.log backups/

Moves server.log into backups.

b) Rename a file

  • Syntax:
mv old_name new_name
  • Example:
mv server.log server_old.log

Renames the file without moving it.

c) Move multiple files

mv file1 file2 file3 destination_directory/

d) Move directories

  • Works the same as files:
mv old_directory/ new_location/

7. Useful Options for File and Directory Management

CommandOptionPurpose
rm-iAsk before deleting
rm-fForce deletion, ignore warnings
rm-rRecursive, for directories
cp-iAsk before overwriting files
cp-rCopy directories recursively
mv-iAsk before overwriting
mkdir-pCreate parent directories if not exist

8. Exam Tips

  • Remember -r for directories: In Linux, you cannot copy or delete a folder with cp or rm without -r.
  • Interactive mode is your friend: Use -i in the exam to avoid mistakes.
  • Files vs Directories: Commands like rm and mv work on both, but directory operations often need -r.
  • Practice paths: Know absolute vs relative paths:
    • Absolute: /home/itteam/backups/file.txt
    • Relative: ../backups/file.txt
  • Do not rely on GUI: The RHCSA exam is command-line only.

9. Quick Command Reference

TaskCommand
Create empty filetouch file.txt
Create file with contentecho "text" > file.txt
Create directorymkdir dir
Create nested directoriesmkdir -p parent/child
Delete filerm file.txt
Delete empty directoryrmdir dir
Delete directory and contentsrm -r dir
Copy filecp file.txt backup.txt
Copy file to directorycp file.txt /backup/
Copy directorycp -r dir /backup/
Move filemv file.txt /backup/
Rename filemv old.txt new.txt

This covers everything you need to handle files and directories for the RHCSA exam. If you can confidently create, delete, copy, and move files and directories with all the options mentioned above, you will be fully prepared for this section.

Buy Me a Coffee