Skip to content

4. File Operations

4.1 Browsing & Navigation

ls — List Directory Contents

bash
# Basic listing
ls

# Detailed info + show hidden files
ls -la

# Human-readable file sizes
ls -lh

# Sort by time (newest first)
ls -lt

# Sort by size (largest first)
ls -lS

# Recursively list subdirectory contents
ls -R

# Show file type indicators
ls -F
bash
$ ls -lah
total 56K
drwxr-xr-x 5 user user 4.0K Jun 20 10:00 .
drwxr-xr-x 3 root root 4.0K Jun 15 08:30 ..
-rw------- 1 user user  220 Jun 15 08:30 .bash_history
-rw-r--r-- 1 user user 3.7K Jun 15 08:30 .bashrc
drwxr-xr-x 2 user user 4.0K Jun 18 14:20 Documents
drwxr-xr-x 2 user user 4.0K Jun 19 09:15 Downloads
-rw-r--r-- 1 user user  807 Jun 15 08:30 .profile
drwxr-xr-x 2 user user 4.0K Jun 20 10:00 projects
-rw-r--r-- 1 user user   64 Jun 20 09:30 readme.txt

cd — Change Directory

bash
# Navigate to a specific directory
cd /var/log

# Return to the previous directory
cd -

# Go up two levels
cd ../..
bash
$ cd /var/log
$ pwd
/var/log
$ cd -
/home/user
$ pwd
/home/user

4.2 Creating & Deleting

mkdir — Create Directory

bash
# Create a single directory
mkdir myproject

# Create nested directories (-p for recursive creation)
mkdir -p projects/web/frontend/src

# Set permissions at creation time
mkdir -m 700 private_dir

touch — Create Empty File / Update Timestamp

bash
# Create an empty file
touch newfile.txt

# Create multiple files at once
touch file1.txt file2.txt file3.txt

# Batch create (brace expansion)
touch report_{01..12}.csv

cp — Copy Files/Directories

bash
# Copy a file
cp source.txt dest.txt

# Copy to a target directory
cp report.txt /backup/

# Recursively copy a directory (-r or -a)
cp -r myproject/ /backup/

# Preserve permissions and timestamps
cp -a source_dir/ dest_dir/

# Confirm before overwriting
cp -i important.conf /etc/

# Show the copy process
cp -v *.txt /backup/
bash
$ cp -v *.txt /backup/
'file1.txt' -> '/backup/file1.txt'
'file2.txt' -> '/backup/file2.txt'
'readme.txt' -> '/backup/readme.txt'

mv — Move / Rename

bash
# Rename a file
mv oldname.txt newname.txt

# Move a file to a directory
mv report.txt /archive/

# Confirm before overwriting
mv -i file.txt /existing_dir/

# Move multiple files
mv *.log /var/log/backup/

rm — Delete Files/Directories

bash
# Delete a file
rm file.txt

# Confirm before deletion
rm -i *.txt

# Recursively delete a directory and its contents
rm -r old_project/

# Force recursive deletion (use with caution!)
rm -rf /tmp/cache/

# Delete an empty directory
rmdir empty_dir/

⚠️ Note: ⚠️ Fatal warning: rm -rf / will delete your entire system! Never run this command. Does Linux have a recycle bin? Not on the command line! Once deleted, it's gone for good.

4.3 Viewing File Contents

bash
# cat — display all content at once (best for small files)
cat /etc/hostname

# Display with line numbers
cat -n /etc/passwd | head -5

# less — paginated viewing (essential for large files)
less /var/log/syslog
# Press q to quit, /keyword to search, n for next, N for previous

# head — view the beginning of a file (first 10 lines by default)
head -20 /etc/passwd

# tail — view the end of a file
tail -20 /var/log/syslog

# Real-time log updates (-f = follow)
tail -f /var/log/syslog
bash
$ cat -n /etc/passwd | head -5
     1	root:x:0:0:root:/root:/bin/bash
     2	daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
     3	bin:x:2:2:bin:/bin:/usr/sbin/nologin
     4	sys:x:3:3:sys:/dev:/usr/sbin/nologin
     5	sync:x:4:65534:sync:/bin:/bin/sync

4.4 Searching for Files

find — Powerful File Search Tool

bash
# Search by name (case-insensitive)
find / -name "*.conf" 2>/dev/null
find /home -iname "readme*"

# Search by size (files larger than 100M)
find / -size +100M 2>/dev/null

# Search by modification time (modified within 7 days)
find /home/user -mtime -7

# Search by type
find /tmp -type f -name "*.log"

# Search and perform an action (delete logs older than 7 days)
find /var/log -name "*.log" -mtime +7 -exec rm {} \;

# Search and list detailed information
find /home -name "*.txt" -exec ls -lh {} \;
bash
$ find /home/user -name "*.txt" -exec ls -lh {} \;
-rw-r--r-- 1 user user  64 Jun 20 09:30 /home/user/readme.txt
-rw-r--r-- 1 user user 1.2K Jun 19 11:45 /home/user/notes.txt
-rw-r--r-- 1 user user  256 Jun 18 16:30 /home/user/Documents/todo.txt

locate — Fast Search (Database-based)

bash
# Update the database first (requires sudo)
sudo updatedb

# Quick search (much faster than find)
locate nginx.conf

# Limit the number of results
locate -l 5 "*.log"
bash
$ locate nginx.conf
/etc/nginx/nginx.conf
/usr/share/doc/nginx/examples/nginx.conf
/usr/share/man/man5/nginx.conf.5.gz