If you feel comfortable with file system navigation in linux, it’s time to learn how to create, copy, move, and delete files and directories. File and directory management operations are essential for organizing your data and scripting workflows.
1. mkdir
– Create Directories
Creates new directories.
Basic Usage:
mkdir folder_name
Create Nested Directories (-p
flag)
mkdir -p parent/child/grandchild
Set Permissions While Creating (-m
flag)
mkdir -m 755 secure_folder # Sets rwxr-xr-x
2. touch
– Create Empty Files
Creates files or updates timestamps.
Basic Usage:
touch file.txt
Create Multiple Files
touch file1.txt file2.txt file3.txt
Modify Timestamp Only
touch -t 202501010000 file.txt # Sets timestamp to Jan 1, 2025
3. cp
– Copy Files & Directories
Copies files/dirs with various options.
Copy a File
cp original.txt copy.txt
Copy a Directory (-r
flag)
cp -r dir1 dir2
Preserve Metadata (-a
flag)
cp -a dir1 dir2 # Keeps permissions, timestamps
Interactive Copy (-i
flag)
cp -i file.txt dest/ # Prompts before overwrite
4. mv
– Move or Rename Files/Dirs
Relocates or renames files/dirs.
Move a File
mv file.txt /new/location/
Rename a File
mv oldname.txt newname.txt
Move Multiple Files
mv file1.txt file2.txt destination_dir/
Force Move (-f
flag)
mv -f file.txt dest/ # Overwrites without prompt
5. rm
– Remove Files & Directories
⚠️ Caution: Deleted files are gone forever!
Delete a File
rm file.txt
Delete a Directory (-r
flag)
rm -r dir1
Force Delete (-f
flag)
rm -rf dir1 # No confirmation, dangerous!
Interactive Delete (-i
flag)
rm -ri dir1 # Asks before each deletion
6. rsync
– Advanced File Synchronization
rsync
(Remote Synchronization) is a powerful command-line tool for efficiently transferring and synchronizing files between directories or across networked systems. It’s faster and more versatile than basic tools like cp
or scp
because it only transfers changes (delta updates) rather than copying entire files every time.
Basic File Sync
rsync -av source/ destination/
-a
= Archive mode (preserves permissions, timestamps)-v
= Verbose output
Sync Over SSH
rsync -avz -e ssh user@remote:/path/ /local/path/
-z
= Compress during transfer-e ssh
= Use SSH for secure transfer
Dry Run (--dry-run
flag)
rsync -av --dry-run source/ dest/ # Shows what would happen
Delete Extraneous Files (--delete
flag)
rsync -av --delete source/ dest/ # Removes files in dest not in source
Limit Bandwidth (--bwlimit
)
rsync -av --bwlimit=1000 source/ dest/ # Limits to 1000 KB/s
When to Use rsync
Instead of cp
Use rsync
instead of cp
when you need:
- Efficient transfers (only syncs changes, not entire files)
- Network/remote backups (works over SSH)
- Resume interrupted transfers (continues where it left off)
- Preserved metadata (permissions, timestamps, symlinks)
- Dry-run verification (
--dry-run
checks before real sync)
Use cp
for simple local copies, but rsync
for advanced, reliable, and large-scale file transfers.
Summary Cheat Sheet
Command | Description |
---|---|
mkdir dir | Create directory |
touch file | Create/update file |
cp file1 file2 | Copy file |
cp -r dir1 dir2 | Copy directory |
mv file1 file2 | Move/rename file |
rm file | Delete file |
rm -r dir | Delete directory |
rsync -av src/ dest/ | Sync files efficiently |
Leave a Reply