Essential Linux Commands
A solid understanding of command-line tools is essential for working effectively in Linux. This guide covers the most important commands that every Linux user should know, organized by category.
File and Directory Operations
ls List Directory Contents
The ls command lists files and directories in the current directory or a specified location.
Tips:
- Use
ls -lato combine detailed listing with showing hidden files - Use
ls -ltto sort files by time (newest first) - Use
ls -lSto sort files by size (largest first)
cd Change Directory
The cd command changes your current working directory.
Tips:
- Use tab completion to avoid typing full path names
cd ~is a shortcut to your home directorycd ~/Documentstakes you to the Documents folder in your home directory
pwd Print Working Directory
The pwd command displays the full path of your current directory.
mkdir Make Directory
The mkdir command creates a new directory.
cp Copy Files and Directories
The cp command copies files and directories.
Tips:
- Use
cp -ifor interactive mode (prompts before overwriting) - Use
cp -vfor verbose output to see what's being copied
mv Move/Rename Files and Directories
The mv command moves files and directories or renames them.
Tips:
- Use
mv -ifor interactive mode to prevent accidental overwrites - The mv command works the same way for directories as it does for files
rm Remove Files and Directories
The rm command deletes files and directories. Use with caution!
Warning:
- Files deleted with
rmcannot be easily recovered - Use
rm -ifor interactive mode (asks for confirmation) - Be extremely careful with
rm -rfas it removes directories recursively without prompting
touch Create Empty Files
The touch command creates empty files or updates timestamps of existing files.
File Content Operations
cat Concatenate and Display Files
The cat command displays the contents of files or combines multiple files.
less View Files with Pagination
The less command allows you to view file contents page by page, with searching capabilities.
Navigation in less:
- Space or Page Down: Move forward one page
- b or Page Up: Move back one page
- g: Go to beginning of file
- G: Go to end of file
- /pattern: Search forward for "pattern"
- q: Quit
head Display Beginning of Files
The head command displays the first part of files.
tail Display End of Files
The tail command displays the last part of files.
Tips:
- The
-foption is extremely useful for watching log files in real-time
grep Search Text
The grep command searches for patterns in text.
Tips:
- Combine with pipes for powerful text processing:
cat file.txt | grep "search" - Use
grep -vto invert the match (show lines that don't match)
System Information
df Display Disk Space Usage
The df command shows disk space usage.
du Display Directory Space Usage
The du command shows directory space usage.
top Display System Processes
The top command provides a dynamic real-time view of running system processes.
Keyboard shortcuts in top:
- k: Kill a process (prompts for PID)
- r: Renice a process (change priority)
- q: Quit
- M: Sort by memory usage
- P: Sort by CPU usage
ps List Processes
The ps command shows information about active processes.
Command Combinations
Linux commands become even more powerful when combined. Here are some useful combinations:
| Command | Description |
|---|---|
find . -name "*.txt" | xargs grep "pattern" |
Find all text files containing "pattern" |
ps aux | grep firefox |
Find Firefox processes |
ls -la | grep "^d" |
List only directories |
cat /var/log/syslog | tail -n 50 |
Show last 50 lines of syslog |
du -sh */ | sort -hr |
Sort directories by size (largest first) |