Essential Bash Commands

Master the essential Bash commands for navigating the filesystem, managing files, viewing content, and working with permissions.

šŸ“– 6 min readšŸ“… 2026-02-10Core Concepts

pwd — Print Working Directory

pwd
# /home/username

cd — Change Directory

cd /home/username/Documents    # Absolute path
cd Documents                   # Relative path
cd ..                          # Parent directory
cd ../..                       # Two levels up
cd ~                           # Home directory
cd                             # Home directory (shortcut)
cd -                           # Previous directory

ls — List Directory Contents

ls                    # Basic listing
ls -l                 # Long format (permissions, size, date)
ls -a                 # Show hidden files (dotfiles)
ls -la                # Long format + hidden files
ls -lh                # Human-readable sizes (KB, MB, GB)
ls -lt                # Sort by modification time
ls -lS                # Sort by size
ls -R                 # Recursive listing
ls -d */              # List only directories
ls *.txt              # List only .txt files

Understanding ls -l Output

-rw-r--r--  1  user  group  4096  Feb 10 14:30  file.txt
│           │  │     │      │     │              │
│           │  │     │      │     │              └─ Filename
│           │  │     │      │     └─ Modification date
│           │  │     │      └─ File size (bytes)
│           │  │     └─ Group owner
│           │  └─ User owner
│           └─ Link count
└─ Permissions (type + rwx for user/group/others)

Working with Files

Creating Files

touch newfile.txt              # Create empty file (or update timestamp)
touch file1.txt file2.txt      # Create multiple files
 
echo "Hello" > file.txt        # Create with content (overwrites)
echo "World" >> file.txt       # Append content
 
cat > notes.txt << EOF         # Create with multi-line content
Line 1
Line 2
Line 3
EOF

Copying Files

cp source.txt destination.txt          # Copy file
cp file.txt /home/user/backup/         # Copy to directory
cp -r source_dir/ destination_dir/     # Copy directory recursively
cp -i file.txt backup.txt             # Interactive (confirm overwrite)
cp -v *.txt /backup/                   # Verbose (show what's copied)
cp -p file.txt backup.txt             # Preserve permissions/timestamps

Moving and Renaming

mv oldname.txt newname.txt          # Rename file
mv file.txt /home/user/Documents/   # Move to directory
mv -i file.txt destination.txt      # Interactive (confirm overwrite)
mv *.jpg ~/Pictures/                # Move all JPGs

Deleting Files

rm file.txt                  # Delete file
rm -i file.txt               # Interactive (confirm)
rm -f file.txt               # Force (no confirmation)
rm -r directory/             # Delete directory recursively
rm -rf directory/            # Force delete directory (DANGEROUS!)
rm *.tmp                     # Delete by pattern
 
# Safe alternative: use trash
# Install: sudo apt install trash-cli
trash-put file.txt
trash-list
trash-restore

WARNING: rm -rf / or rm -rf * with wrong path can destroy your entire system. Always double-check before using rm -rf.

Working with Directories

mkdir newdir                     # Create directory
mkdir -p parent/child/grandchild # Create nested directories
mkdir -v newdir                  # Verbose output
mkdir dir1 dir2 dir3             # Create multiple
 
rmdir emptydir                   # Remove empty directory
rm -r directory/                 # Remove directory with contents

Viewing File Contents

cat — Concatenate and Display

cat file.txt                # Display entire file
cat -n file.txt             # With line numbers
cat file1.txt file2.txt     # Concatenate files

less / more — Paged Viewing

less large_file.txt         # Scroll with arrows, q to quit
more large_file.txt         # Basic paging
 
# In less:
# Space = page down, b = page up
# /pattern = search forward
# ?pattern = search backward
# n = next match, N = previous match
# g = start of file, G = end of file
# q = quit

head / tail — Start and End

head file.txt               # First 10 lines
head -n 20 file.txt         # First 20 lines
head -c 100 file.txt        # First 100 bytes
 
tail file.txt               # Last 10 lines
tail -n 20 file.txt         # Last 20 lines
tail -f logfile.txt         # Follow (live updates)
tail -f -n 50 app.log       # Follow with last 50 lines

wc — Word Count

wc file.txt                 # Lines, words, bytes
wc -l file.txt              # Line count only
wc -w file.txt              # Word count only
wc -c file.txt              # Byte count only
wc -m file.txt              # Character count

File Permissions

Understanding Permissions

rwxr-xr--
│││ │││ │││
│││ │││ └── Others: read (4)
│││ └── Group: read (4) + execute (1) = 5
└── User: read (4) + write (2) + execute (1) = 7

Numeric: 754

chmod — Change Permissions

# Symbolic mode
chmod u+x script.sh          # Add execute for user
chmod g+w file.txt            # Add write for group
chmod o-r file.txt            # Remove read for others
chmod a+r file.txt            # Add read for all
chmod u=rwx,g=rx,o=r file.txt # Set explicitly
 
# Numeric mode
chmod 755 script.sh           # rwxr-xr-x
chmod 644 file.txt            # rw-r--r--
chmod 600 secret.txt          # rw-------
chmod 777 shared.txt          # rwxrwxrwx (avoid!)
 
# Recursive
chmod -R 755 directory/

chown — Change Ownership

sudo chown user file.txt              # Change owner
sudo chown user:group file.txt        # Change owner and group
sudo chown -R user:group directory/   # Recursive

Finding Files

find — Search for Files

find /home -name "*.txt"              # Find by name
find . -name "*.log" -type f          # Files only
find . -type d -name "src"            # Directories only
find . -size +100M                    # Files larger than 100MB
find . -mtime -7                      # Modified in last 7 days
find . -empty                         # Empty files/directories
find . -perm 755                      # By permissions
 
# Find and execute
find . -name "*.tmp" -delete          # Find and delete
find . -name "*.sh" -exec chmod +x {} \;  # Find and chmod
find . -name "*.log" -exec wc -l {} +     # Find and count lines

which / whereis — Find Commands

which python                 # Path to executable
which -a python              # All paths
 
whereis bash                 # Binary, source, manual locations
type ls                      # What type of command

Disk Usage

df -h                        # Filesystem disk space
du -sh /home/user            # Directory size
du -sh * | sort -rh | head   # Largest items in current dir
du -sh */ | sort -rh         # Largest subdirectories

Exercises

  1. Create a directory structure: project/{src,tests,docs,config}
  2. Create 5 text files with different content, then combine them into one
  3. Find all files larger than 1MB in your home directory
  4. Change permissions on a script to make it executable
  5. Use tail -f to monitor a log file in real-time

Next: Pipes and Redirection — connect commands and control output!