Linux Commands
Navigation Commands
pwd - Print Working Directory
bash
pwdDisplays the full path of the current working directory.
ls - List Directory Contents
bash
ls # Basic listing
ls -l # Long format listing
ls -a # Show hidden files
ls -la # Long listing including hidden files
ls -lh # Long listing with human-readable file sizescd - Change Directory
bash
cd /path/to/directory # Change to specific directory
cd ~ # Change to home directory
cd .. # Move up one directory
cd - # Return to previous directoryFile Operations
touch - Create Empty Files
bash
touch file.txt # Create a new file or update timestamp
touch file1.txt file2.txt # Create multiple filesmkdir - Create Directories
bash
mkdir directory # Create a new directory
mkdir -p dir1/dir2/dir3 # Create nested directoriescp - Copy Files and Directories
bash
cp source.txt destination.txt # Copy a file
cp -r source_dir destination_dir # Copy directories recursivelymv - Move or Rename Files
bash
mv old_name.txt new_name.txt # Rename a file
mv file.txt /path/to/destination/ # Move a filerm - Remove Files and Directories
bash
rm file.txt # Delete a file
rm -i file.txt # Interactive deletion (prompts before deleting)
rm -r directory # Delete directory and contents recursively
rm -rf directory # Force delete directory without prompts (use with caution!)File Viewing Commands
cat - Concatenate and Display File Contents
bash
cat file.txt # Display entire file content
cat file1.txt file2.txt # Display multiple filesless - View File Content with Pagination
bash
less file.txt # View file with paginationUse arrow keys to navigate, press q to exit.
head - Display Beginning of File
bash
head file.txt # Display first 10 lines
head -n 20 file.txt # Display first 20 linestail - Display End of File
bash
tail file.txt # Display last 10 lines
tail -n 20 file.txt # Display last 20 lines
tail -f log.txt # Follow a file in real-time (useful for logs)grep - Search Text Patterns
bash
grep "pattern" file.txt # Search for pattern in file
grep -i "pattern" file.txt # Case-insensitive search
grep -r "pattern" directory/ # Recursive search in directorySystem Information Commands
uname - Print System Information
bash
uname -a # All system information
uname -s # Kernel name
uname -r # Kernel releasedf - Disk Space Usage
bash
df # Show disk space usage
df -h # Human-readable formatfree - Memory Usage
bash
free # Show memory usage
free -h # Human-readable formattop - Process Monitoring
bash
top # Dynamic real-time view of processesPress q to exit.
ps - Process Status
bash
ps # Show current processes
ps aux # Show all processes in detailUser Management Commands
whoami - Print Current User
bash
whoami # Display current usernamesu - Switch User
bash
su username # Switch to another user
su - # Switch to root user (with environment)sudo - Execute Command as Superuser
bash
sudo command # Execute command with superuser privileges
sudo -i # Start a shell as rootpasswd - Change Password
bash
passwd # Change your password
sudo passwd username # Change another user's password (as admin)File Permissions
chmod - Change File Permissions
bash
chmod 755 file.txt # Change permissions using octal notation
chmod u+x script.sh # Add execute permission for user
chmod -R 755 directory # Change permissions recursivelychown - Change File Owner
bash
chown user:group file.txt # Change owner and group
chown -R user directory # Change recursivelyNetwork Commands
ping - Test Network Connectivity
bash
ping google.com # Send ICMP echo requests
ping -c 4 google.com # Send only 4 packetsifconfig/ip - Network Interface Configuration
bash
ifconfig # Show network interfaces
ip addr show # Modern equivalentnetstat - Network Statistics
bash
netstat -tuln # Show listening portsssh - Secure Shell
bash
ssh username@hostname # Connect to remote hostArchiving and Compression
tar - Archive Files
bash
tar -cvf archive.tar directory/ # Create archive
tar -xvf archive.tar # Extract archive
tar -zcvf archive.tar.gz directory/ # Create compressed archive
tar -zxvf archive.tar.gz # Extract compressed archivezip/unzip - Zip Files
bash
zip -r archive.zip directory/ # Create zip archive
unzip archive.zip # Extract zip archivePackage Management
Debian/Ubuntu (apt)
bash
apt update # Update package lists
apt upgrade # Upgrade installed packages
apt install package_name # Install packages
apt remove package_name # Remove packagesRed Hat/Fedora (dnf/yum)
bash
dnf update # Update packages
dnf install package_name # Install packages
dnf remove package_name # Remove packagesRedirects and Pipes
Redirecting Output
bash
command > file.txt # Redirect output to file (overwrite)
command >> file.txt # Append output to file
command 2> error.log # Redirect errors to filePipes
bash
command1 | command2 # Pipe output of command1 to command2
ls -la | grep "pattern" # List files and filter with grepPractice Exercises
- Navigate to your home directory and create a new directory called "linux_practice"
- Create five empty files inside this directory named file1.txt through file5.txt
- Create a text file with some content using a command
- List all files, including hidden ones, in long format
- Rename file1.txt to first_file.txt
- Copy file2.txt to backup_file2.txt
- Move file3.txt to a subdirectory
- Delete file4.txt
- Check how much disk space is available on your system
- Find all files in your home directory that contain the word "linux"
In the next sections, we'll dive deeper into the Linux file system, permissions, and process management.