<< back to Guides
<< back to Guides
File Management in Linux: cp, mv, rm, touch, and ln
This guide covers essential commands for managing files and directories in Linux. These commands help you copy, move, remove, create, and link files effectively.
1. cp - Copy Files and Directories
Description:
The cp command copies files or directories from one location to another.
Syntax:
cp [options] source target
Common Options:
-r : Copy directories recursively.
-i : Prompt before overwriting files.
-u : Copy only if the source file is newer than the destination.
Examples:
- Copy a file:
cp file.txt backup/
- Copy a directory:
cp -r folder/ backup/
- Copy with overwrite confirmation:
cp -i file.txt backup/
2. mv - Move or Rename Files
Description:
The mv
command moves files or directories to a new location or renames them.
Syntax:
mv [options] source target
Common Options:
-i : Prompt before overwriting files.
-u : Move only if the source is newer than the destination.
Examples:
- Rename a file:
mv oldname.txt newname.txt
- Move a file to a directory:
mv file.txt backup/
- Prompt before overwriting:
mv -i file.txt folder/
3. rm - Remove Files and Directories
Description:
The rm
command deletes files or directories.
Syntax:
rm [options] target
Common Options:
-i : Prompt before deleting.
-r : Remove directories and their contents recursively.
-f : Force remove files or directories without prompts.
Examples:
- Remove a file:
rm file.txt
- Remove a directory and its contents:
rm -r folder/
- Force remove a directory:
rm -rf folder/
4. touch - Create or Update Files
Description:
The touch
command creates an empty file or updates the timestamp of an existing file.
Syntax:
touch [options] filename
Options
Besides creating empty files, you can set timestamps with touch:
touch -t 202311160830 file.txt
Examples:
- Create a new empty file:
touch newfile.txt
- Update the timestamp of an existing file:
touch existingfile.txt
5. ln - Create Links
Description:
The ln
command creates links between files. Links can be hard or symbolic (soft).
Syntax:
ln [options] target link_name
Common Options:
-s : Create a symbolic (soft) link.
-f : Force overwrite an existing link.
Examples:
- Create a hard link:
ln original.txt hardlink.txt
- Create a symbolic link:
ln -s /path/to/original.txt symlink.txt
- Overwrite an existing link:
ln -f original.txt newlink.txt
File Management Cheatsheet
Command | Description | Example |
---|---|---|
cp source target |
Copy a file to a new location | cp file.txt backup/ |
cp -r source target |
Copy directories recursively | cp -r folder/ backup/ |
cp -i source target |
Prompt before overwriting files | cp -i file.txt backup/ |
cp -u source target |
Copy only when source file is newer | cp -u file.txt backup/ |
mv source target |
Move or rename a file | mv oldname.txt newname.txt |
mv -i source target |
Prompt before overwriting files | mv -i file.txt folder/ |
mv -u source target |
Move only if the source is newer | mv -u file.txt folder/ |
rm file |
Remove a file | rm file.txt |
rm -i file |
Prompt before deleting | rm -i file.txt |
rm -r directory |
Remove a directory and its contents recursively | rm -r folder/ |
rm -f file |
Force remove a file (ignores nonexistent files) | rm -f file.txt |
rm -rf directory |
Force remove a directory and its contents recursively | rm -rf folder/ |
touch filename |
Create an empty file or update timestamp | touch newfile.txt |
ln target link_name |
Create a hard link | ln original.txt hardlink.txt |
ln -s target link_name |
Create a symbolic (soft) link | ln -s /path/to/original.txt symlink.txt |
ln -f target link_name |
Force overwrite an existing link | ln -f original.txt hardlink.txt |