Master the essential commands for managing files and directories
Click the green 💻 Terminal button in the bottom-right corner to open an interactive terminal. You can practice all the commands on this page in real-time!
The file system is the foundation of Linux. Understanding how to create, move, copy, and delete files is essential for any Linux user.
mkdir📁 mkdir - Make DirectoryCreates new directories for organizing your files. Essential for building project structures and keeping your system organized.Basic Usage$ mkdir my_folder▸ Creates a new directory named 'my_folder'Create Parent Directories$ mkdir -p path/to/nested/dir▸ Creates all parent directories automaticallyMultiple Directories$ mkdir docs tests src▸ Creates three directories at once)The mkdir command creates directories to organize your files.
mkdirTry it in the interactive terminal:
mkdir my_folder
Expected Output:
Creates a new directory named 'my_folder'
Use the -p flag to create parent directories automatically:
mkdir -p projects/2024/my_app
ls -R projects
Expected Output:
projects/
projects/2024:
my_app/
projects/2024/my_app:
touch📄 touch - Create Empty File or Update TimestampCreates a new empty file or updates the timestamp of an existing file. Quick way to create placeholder files.Create New File$ touch newfile.txt▸ Creates an empty fileUpdate Timestamp$ touch existingfile.txt▸ Updates last modified timeMultiple Files$ touch file1.txt file2.txt file3.txt▸ Creates three empty files)The touch command creates empty files or updates file timestamps.
touchtouch newfile.txt
ls
Expected Output:
newfile.txt
touch file1.txt file2.txt file3.txt
ls
Expected Output:
file1.txt file2.txt file3.txt
rm🗑️ rm - Remove Files or DirectoriesDeletes files or directories permanently. Be careful - there's no undo!Remove File$ rm file.txt▸ Deletes the file permanentlyRemove Directory$ rm -r folder/▸ Recursively deletes directory and contentsForce Remove$ rm -rf temp/▸ Force remove without prompts (use carefully!))The rm command deletes files and directories. Be careful - there's no undo!
touch oldfile.txt
ls
rm oldfile.txt
ls
Expected Output:
(oldfile.txt is no longer listed)
rm -r)mkdir test_dir
touch test_dir/file1.txt
rm -r test_dir
ls
Expected Output:
test_dir is completely removed
rm -f)Use -f to force removal without prompts:
touch unwanted.txt
rm -f unwanted.txt
ls
Expected Output:
(unwanted.txt removed without confirmation)
rmdir📁 rmdir - Remove Empty DirectorySafely removes empty directories only. Won't delete if directory contains files (safer than rm -r).Remove Empty Directory$ rmdir empty_folder▸ Removes the directory if emptySafety Feature$ rmdir folder_with_files▸ Error: directory not empty (safe!))The rmdir command only removes empty directories - a safer alternative to rm -r.
rmdirmkdir empty_folder
rmdir empty_folder
Expected Output:
Directory removed safely
mkdir folder
touch folder/file.txt
rmdir folder
Expected Output:
rmdir: failed to remove 'folder': Directory not empty
This error prevents accidental data loss!
mv↔️ mv - Move or Rename FilesMoves files to a new location or renames them. Can also move directories.Rename File$ mv oldname.txt newname.txt▸ Renames the fileMove File$ mv file.txt /path/to/destination/▸ Moves file to new locationMove with Wildcards$ mv *.txt documents/▸ Moves all .txt files to documents folder)The mv command moves files to a new location or renames them.
touch oldname.txt
mv oldname.txt newname.txt
ls
Expected Output:
newname.txt
touch newname.txt
mkdir documents
mv newname.txt documents/
ls documents/
Expected Output:
newname.txt
touch report1.txt report2.txt code.js
mkdir backups
mv *.txt backups/
ls backups/
Expected Output:
report1.txt report2.txt
cp📋 cp - Copy Files or DirectoriesCreates a copy of files or directories, leaving the original unchanged.Copy File$ cp source.txt destination.txt▸ Creates a copyCopy Directory$ cp -r folder/ backup_folder/▸ Recursively copies entire directoryCopy to Different Location$ cp file.txt /path/to/backup/▸ Copies file to backup directory)The cp command creates copies of files or directories.
touch source.txt
cp source.txt backup.txt
ls
Expected Output:
source.txt backup.txt
cp -r)Use the -r flag to copy entire directories:
mkdir original
touch original/file.txt
cp -r original/ copy_of_original/
ls
Expected Output:
original/ copy_of_original/
Absolute Paths: Start from root (/)
/home/user/documents/file.txt
Relative Paths: Start from current directory
documents/file.txt # In current directory
../other_folder/file.txt # Go up one level, then down
Special Path Symbols:
. - Current directory.. - Parent directory (one level up)~ - Home directory/ - Root directory (top of filesystem)Hidden Files: Files starting with . are hidden
ls # Won't show hidden files
ls -a # Shows ALL files, including hidden
File Extensions: Optional in Linux (not like Windows)
Linux uses file permissions and content to determine file types, not extensions.
Case Sensitivity: Linux is case-sensitive!
File.txt ≠ file.txt ≠ FILE.TXT
Wildcards: Match multiple files
* - Matches any characters? - Matches single character[abc] - Matches one of a, b, or cExercise 1: Build a Project Structure
mkdir -p my_project/src my_project/docs my_project/tests
ls -R my_project
Goal: Create organized project directories
Exercise 2: Organize Files
touch report1.txt report2.txt code.js notes.md
mkdir documents code
mv *.txt documents/
mv *.js code/
ls
Goal: Use wildcards to organize files by type
Exercise 3: Clean Up
mkdir temp_work
touch temp_work/test1.txt temp_work/test2.txt
rm -r temp_work
ls
Goal: Practice safe deletion with rm -r
Exercise 4: Copy Directory Structure
mkdir original
touch original/file.txt
cp -r original/ backup/
ls -R
Goal: Understand recursive copying
Best Practices:
ls before rm to verify filesrmdir for empty directories when possible (safer)rm -rf / (extremely dangerous)If you move a file to a location where a file with the same name already exists, mv will overwrite it without asking!
Now that you can manage files and directories, you're ready to learn about:
Continue to: User & Permission Management →