- Creating Files & Directories
- Copying Files & Directories
- Moving & Renaming
- Deleting Files & Directories
- Viewing File Contents
- Finding Files
- Links β Hard & Symbolic
- Wildcards & Globbing
- File Metadata
- Practice Exercises
touch newfile.txt # Create empty file (or update timestamp)
touch file1.txt file2.txt # Create multiple files at once
touch /tmp/test-{1..10}.txt # Create test-1.txt through test-10.txt
echo "Hello" > hello.txt # Create file with content (overwrites)
echo "World" >> hello.txt # Append to file
cat > notes.txt << EOF # Multi-line content using heredoc
Line 1: This is my notes file
Line 2: Learning Linux is fun
EOF
printf "Name: %s\nAge: %d\n" "Sovon" 25 > info.txtmkdir mydir # Create a single directory
mkdir dir1 dir2 dir3 # Create multiple directories
mkdir -p parent/child/grandchild # Create nested directories (-p = parents)
mkdir -pv project/{src,docs,tests} # Create project structure (-v = verbose)# Create a complete project structure in one command
mkdir -pv myproject/{src/{main,utils},docs,tests,config}cp file.txt backup.txt # Copy file
cp file.txt /tmp/ # Copy to another directory
cp file.txt /tmp/newname.txt # Copy with new name
cp -i file.txt backup.txt # Prompt before overwrite (-i = interactive)
cp -v file.txt backup.txt # Show what's being done (-v = verbose)
cp -p file.txt backup.txt # Preserve timestamps & permissions
# Copy directories (MUST use -r)
cp -r mydir/ mydir-backup/ # Copy directory recursively
cp -rv /etc/nginx/ /tmp/nginx-bak/ # Copy with verbose output
cp -a source/ dest/ # Archive mode (preserves everything: -r + -p + links)π¨ Common Mistake: Forgetting
-rwhen copying directories will give an error.
# Move files
mv file.txt /tmp/ # Move to /tmp
mv file.txt /tmp/newname.txt # Move and rename
mv *.log /var/log/ # Move all .log files
# Rename (same directory = rename)
mv oldname.txt newname.txt # Rename a file
mv old-dir/ new-dir/ # Rename a directory
# Safety options
mv -i source dest # Prompt before overwrite
mv -v source dest # Verbose output
mv -n source dest # Never overwrite (no-clobber)
# Move multiple files to a directory
mv file1.txt file2.txt file3.txt /backup/π‘ Tip:
mvworks on both files and directories β no-rflag needed (unlikecp).
rm file.txt # Delete a file (permanent!)
rm -i file.txt # Prompt before deleting
rm -v file.txt # Verbose
rm file1.txt file2.txt # Delete multiple files
rm *.tmp # Delete all .tmp files
# Delete directories
rm -r mydir/ # Delete directory and contents recursively
rm -ri mydir/ # Recursive with confirmation for each file
rm -rf mydir/ # Force delete (no prompts, no errors)π¨ DANGER ZONE:
rm -rf /will destroy your entire system. There is NO recycle bin, NO undo. Always double-check before usingrm -rf.
rmdir emptydir # Only works if directory is empty
rmdir -p parent/child/grandchild # Remove nested empty directories# Use trash-cli instead of rm
sudo apt install trash-cli
trash-put file.txt # Move to trash
trash-list # List trashed files
trash-restore # Restore a file
trash-empty # Empty the trash| Command | Use Case | Memory Usage |
|---|---|---|
cat |
Small files | Loads entire file |
less |
Large files | Loads page by page |
more |
Large files | Older, less features |
head |
First N lines | Minimal |
tail |
Last N lines | Minimal |
nl |
With line numbers | Loads entire file |
# cat β concatenate and display
cat file.txt # Display entire file
cat -n file.txt # Show with line numbers
cat -A file.txt # Show hidden characters (tabs, line endings)
cat file1.txt file2.txt > merged.txt # Concatenate files
# less β paginated viewing (most useful for large files)
less /var/log/syslog
# Navigation: Space=next page, b=back, /=search, q=quit, g=top, G=bottom
# head β first lines
head file.txt # First 10 lines (default)
head -n 5 file.txt # First 5 lines
head -c 100 file.txt # First 100 bytes
# tail β last lines
tail file.txt # Last 10 lines (default)
tail -n 20 file.txt # Last 20 lines
tail -f /var/log/syslog # Follow file in real-time (great for logs!)
tail -F /var/log/syslog # Follow even if file is rotated
# nl β number lines
nl file.txt # Number non-empty lines
nl -ba file.txt # Number ALL lines
# Other viewers
tac file.txt # cat in reverse (last line first)
rev file.txt # Reverse each line
wc file.txt # Count lines, words, characters
wc -l file.txt # Count lines only# Basic syntax: find [path] [criteria] [action]
# Find by name
find / -name "passwd" # Exact name (case-sensitive)
find / -iname "readme.md" # Case-insensitive
find /home -name "*.txt" # All .txt files
find . -name "*.log" -o -name "*.tmp" # .log OR .tmp files
# Find by type
find /var -type f # Files only
find /var -type d # Directories only
find /dev -type l # Symbolic links only
# Find by size
find / -size +100M # Larger than 100 MB
find / -size -1k # Smaller than 1 KB
find . -size 0 # Empty files
find /var -type f -empty # Empty files
# Find by time
find / -mtime -7 # Modified in last 7 days
find / -atime +30 # Accessed more than 30 days ago
find / -mmin -60 # Modified in last 60 minutes
find / -newer reference.txt # Newer than reference file
# Find by permissions
find / -perm 777 # Exact permission 777
find / -perm -u=x # User has execute permission
find /tmp -writable # Writable by current user
# Find by owner
find /home -user sovon # Owned by user
find / -group docker # Owned by group
# Actions on found files
find . -name "*.tmp" -delete # Delete found files
find . -name "*.sh" -exec chmod +x {} \; # Make all .sh files executable
find . -name "*.log" -exec gzip {} \; # Compress all log files
find . -type f -name "*.txt" -exec grep -l "error" {} \; # Find files containing "error"# Uses a pre-built database (faster than find, but not real-time)
sudo apt install mlocate
sudo updatedb # Update the database
locate passwd # Find all files with "passwd" in the path
locate -i readme # Case-insensitive search
locate -c "*.conf" # Count matches
locate --regex "\.conf$" # Use regexwhich python3 # Full path to executable
whereis ls # Binary, source, and man page locations
type cd # Show if built-in, alias, or binaryA symlink is a pointer to another file β like a shortcut.
ln -s /path/to/original /path/to/link
# Example
ln -s /var/log/syslog ~/syslog-shortcut
ls -l ~/syslog-shortcut
# lrwxrwxrwx 1 sovon sovon 15 Feb 22 10:00 syslog-shortcut -> /var/log/syslogA hard link is a direct reference to the same data on disk.
ln original.txt hardlink.txt
# Both point to the same inode (data on disk)
ls -li original.txt hardlink.txt
# 12345 -rw-r--r-- 2 sovon sovon 100 Feb 22 10:00 original.txt
# 12345 -rw-r--r-- 2 sovon sovon 100 Feb 22 10:00 hardlink.txt| Feature | Symbolic Link | Hard Link |
|---|---|---|
| Can link to directories | β Yes | β No |
| Can cross filesystems | β Yes | β No |
| Breaks if target deleted | β Yes (dangling link) | β No (data remains) |
| Has its own inode | β Yes | β No (shares inode) |
| Shows as link in ls -l | β
l prefix |
β Regular - prefix |
Wildcards (globbing patterns) let you match multiple files:
| Pattern | Matches | Example |
|---|---|---|
* |
Any characters (zero or more) | *.txt β all .txt files |
? |
Any single character | file?.txt β file1.txt, fileA.txt |
[abc] |
Any one of a, b, or c | file[123].txt β file1.txt, file2.txt |
[a-z] |
Any character in range | file[a-z].txt β filea.txt, fileb.txt |
[!abc] |
NOT a, b, or c | file[!0-9].txt β fileA.txt, not file1.txt |
{a,b,c} |
Brace expansion | file.{txt,md} β file.txt, file.md |
# Examples
ls *.conf # All .conf files
ls image?.png # image1.png, imageA.png, etc.
ls log-[0-9][0-9].txt # log-01.txt through log-99.txt
cp *.{jpg,png,gif} /images/ # Copy all image files
rm temp* # Delete all files starting with "temp"# File type and info
file document.pdf # PDF document, version 1.4
file script.sh # ASCII text, Bash script
# Detailed file status
stat myfile.txt
# Shows: size, blocks, inode, permissions, timestamps, etc.
# Disk usage
du -sh mydir/ # Total size of directory
du -sh * # Size of each item in current directory
du -ah mydir/ | sort -rh | head -10 # Top 10 largest files
# Disk free space
df -h # Free space on all mounted filesystems
df -h /home # Free space on /home partition- Create a directory structure:
project/{src,docs,tests,build} - Create 5 files:
touch project/src/file{1..5}.py - Copy the
srcdirectory tosrc-backup - Rename
project/docstoproject/documentation - Find all
.pyfiles in your home directory:find ~ -name "*.py" - Symlink: Create a link from
~/quick-accesstoproject/ - View: Use
head,tail, andlesson/etc/passwd - Wildcards: List all files in
/etcthat end with.conf - Size: Find files larger than 10 MB in
/var:find /var -size +10M - Clean up: Delete all
.tmpfiles in/tmp
β Previous: Filesystem Hierarchy Β· π Home Β· Next: Users, Groups & Permissions β