This chapter introduces the fundamental commands for creating and deleting files and directories, which are essential for managing your workspace on the Linux command line.
1. Creating Files (touch)
The touch command is primarily used to change the access and modification timestamps of a file. However, its most common use is to create a new, empty file instantly.
Execution Examples
To create a single empty file named report.txt in the current directory:
touch report.txt
To create multiple files in one command:
touch index.html style.css script.js
2. Creating Directories (mkdir)
The mkdir (make directory) command is used to create one or more new directories.
Execution Examples
To create a new directory named assets:
mkdir assets
To create a new directory named js inside the existing assets directory:
mkdir assets/js
Creating Nested Directories (-p)
If you need to create a directory structure where the parent directory does not yet exist, you must use the -p (parents) flag.
To create the docs/2025/q1 path, even if docs or 2025 don’t exist:
mkdir -p docs/2025/q1
3. Removing Files (rm)
The rm (remove) command is used to permanently delete files. Unlike operating systems with a Recycle Bin or Trash folder, deleting files with rm is generally irreversible.
Execution Examples
To remove a single file named temp.log:
rm temp.log
To remove multiple files:
rm file1.txt file2.txt
4. Removing Directories (rmdir and rm -r)
Linux provides two methods for directory removal, depending on whether the directory is empty.
A. Removing Empty Directories (rmdir)
The rmdir (remove directory) command can only delete directories that are completely empty.
rmdir empty_folder
B. Removing Non-Empty Directories (rm -r)
To delete a directory and all of its contents (files and subdirectories), you must use the rm command with the -r (recursive) flag. This is the most powerful and dangerous deletion command.
To remove the backups directory and everything inside it:
rm -r backups
Safely Removing with Confirmation (-i)
To prevent accidental deletion, you can add the -i (interactive) flag, which prompts you for confirmation before deleting each item.
rm -ri large_folder
Extreme Caution: Never run
rm -rf /or similar commands, as this could permanently delete the entire operating system, and the system often does not prompt for confirmation with the-f(force) flag.
