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
Navigating the Filesystem
pwd ā Print Working Directory
pwd
# /home/usernamecd ā 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 directoryls ā 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 filesUnderstanding 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
EOFCopying 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/timestampsMoving 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 JPGsDeleting 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-restoreWARNING:
rm -rf /orrm -rf *with wrong path can destroy your entire system. Always double-check before usingrm -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 contentsViewing 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 filesless / 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 = quithead / 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 lineswc ā 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 countFile 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/ # RecursiveFinding 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 lineswhich / whereis ā Find Commands
which python # Path to executable
which -a python # All paths
whereis bash # Binary, source, manual locations
type ls # What type of commandDisk 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 subdirectoriesExercises
- Create a directory structure:
project/{src,tests,docs,config} - Create 5 text files with different content, then combine them into one
- Find all files larger than 1MB in your home directory
- Change permissions on a script to make it executable
- Use
tail -fto monitor a log file in real-time
Next: Pipes and Redirection ā connect commands and control output!