Linux commands are the backbone of shell scripting, system administration, and automation—whether you're a beginner or need to troubleshoot efficiently. With a few core commands, you can quickly find files, edit text, manage processes, and optimize system performance. No prior experience is needed, though familiarity with a terminal is helpful. Basic commands like `ls`, `cd`, and `pwd` form the foundation; more advanced tools like `grep`, `awk`, and `sed` enable powerful text processing and scripting.
What You'll Need
- A Linux system (Ubuntu, Fedora, Debian, or any distro with a terminal) or a Linux virtual machine running in a cloud service (e.g., AWS Lightsail, DigitalOcean).
- Access to the command line (e.g., via SSH if remote) or a pre-installed terminal emulator (default on Linux desktops).
- Basic knowledge of keyboard shortcuts (e.g., tab completion, arrow keys for history).
- A text editor (e.g., `nano`, `vim`, or `gedit`) is optional but useful for writing small scripts.
Step-by-Step: Essential Linux Commands
- Open the Terminal
- On Linux/macOS: Press
Ctrl+Alt+Tor search for "Terminal" in your applications menu. - On Windows with WSL (Windows Subsystem for Linux): Open the integrated terminal or launch a distro-specific terminal (e.g., Ubuntu’s "Ubuntu Terminal").
- If using SSH: Connect via a terminal emulator (e.g., PuTTY, iTerm, or terminal.app) with the command:
ssh username@hostname(Replaceusernameandhostnamewith your details.)
- On Linux/macOS: Press
- Move between directories:
cd ..– Go up one directory level.cd /path/to/directory– Change to a specific directory (e.g.,cd ~/Documents).cd ~– Switch to your home directory.
- List files and folders:
ls– Show current directory contents (default: long listing).ls -l– Long format (permissions, owner, size, timestamp).ls -a– Include hidden files (starting with.).
- Check your current directory:
pwd(Prints the full path, e.g.,/home/username/Desktop.)
- Find files by name (case-sensitive by default):
find /path/to/search -name "filename"(Replacefilenamewith a pattern like*.txtto match all .txt files.) - Use
grepto search text within files:grep "keyword" filename.txt– Search for exact matches.grep -i "keyword" file.log– Case-insensitive search.grep -r "error" /var/log– Recursively search logs in subdirectories.
- View file contents:
cat file.txt– Display entire file (risky for large files).less file.log– Scrollable view (pressqto exit).
- Create a directory:
mkdir new_folder(Usemkdir -p parent/childto create nested folders without errors.) - Move or rename files:
mv file.txt /backup– Move file to a directory.mv old_name.txt new_name.txt– Rename a file.
- Delete files or empty a folder:
rm file.txt– Permanently delete a file.rm -r directory/– Delete a folder and its contents (use with caution!).rmdir empty_folder– Remove an empty folder.
- Copy files:
cp file.txt /backup/(Add-rto copy directories recursively.)
- View running processes:
toporhtop(install withsudo apt install htopif needed). (Pressqto exit.) - Kill a process by PID (Process ID):
kill(Useps aux | grep "process_name"to find the PID first.) - Start a service:
sudo systemctl start service_name(Replaceservice_namewith the actual service, e.g.,nginx.)
- Open a file for editing:
nano edit_me.txt - Key shortcuts:
Ctrl+O– Save changes.Ctrl+X– Exit (unless you have unsaved changes).Ctrl+K– Cut selected text.Ctrl+U– Paste text.
Common Mistakes
- Running commands as root without `sudo`
- Always preface privileged commands with
sudo(e.g.,sudo rm file.txt). Forgottensudocan lock you out of critical files or system settings. - Warning: Avoid usingsudofor non-critical tasks unless absolutely necessary—it grants broad system access. - Deleting directories or files unintentionally
- Use
rm -i(interactive) or double-check withlsbefore running destructive commands likerm -r. - For safety, create backups withcp file.txt backup/before deleting. - Using wildcards incorrectly
- Commands like
ls *.txtmatch all files ending in.txt. Wildcards (e.g.,*,?) are case-sensitive by default. Usels -ito see file IDs for debugging. - Ignoring permission errors
- If you see
Permission denied, the file/directory lacks read/execute permissions. Fix with:chmod +r file.txt– Grant read permissions.chown user:group file.txt– Change ownership (requiressudofor system files).
Tips for Efficiency
- Master tab completion
- Press
Tabto autocomplete filenames, commands, or arguments. PressShift+Tabto see possible matches. - Use aliases for frequent commands
- Create shortcuts in your shell config file (e.g.,
~/.bashrcor~/.zshrc):alias ll='ls -la'(Reload withsource ~/.bashrcafter editing.) - Learn shell history
- View past commands with
history. Edit history with!99(run the 99th command) or!!(repeat the last command). - Pipe commands for data processing
- Chain commands using the pipe (
|):ls | grep ".txt"– List files and filter only .txt files.grep "error" system.log | less– Search logs and view results page-by-page.
- Use aliases for common tasks
- Example: Add this to your shell config to avoid typing
sudo su:alias su='sudo su -'
Troubleshooting
- Command not found
- If you see
bash: command_not_found: command not found, the command doesn’t exist or isn’t installed. Install it with:sudo apt install command_name(Debian/Ubuntu).sudo dnf install command_name(Fedora/RHEL).
- Permission denied for critical files
- If you need to modify system files (e.g.,
/etc/hosts), usesudoor contact your system administrator. Never edit files directly in /var/lib/ or /usr/ without permission. - Terminal freezes or hangs
- Restart the terminal or your system. If the issue persists, check for conflicting processes:
toporhtopto identify resource-hogging apps.- Kill resource-intensive processes with
kill -9.
- Shell prompt is unreadable
- Ensure your terminal supports UTF-8 encoding. Set locale for UTF-8 in your shell config:
export LANG=en_US.UTF-8(Add this to~/.bashrcor~/.zshrc.)
Frequently Asked Questions
How do I create a password-protected archive?
Usetar -czvf archive.tar.gz /path/to/folder to compress a folder into a .tar.gz file. Add password protection with:
sudo tar -czvf - archive.tar.gz | gzip -d - | gpg -c -
(The -c flag creates a new encrypted file; remove it to edit.)
What’s the difference between ls and dir?
On Linux, ls is the primary command for listing files. dir does not exist natively—it’s a Windows/CMD command. Some Linux distributions may include ls aliases for dir, but it’s not standard.
How do I search for a file by size?
Usefind /path -type f -size +100M to find files larger than 100MB. To list sizes in human-readable format:
find /path -type f -exec du -h {} \;
Why can’t I run sudo commands in my home directory?
Permissions for sudo are tied to system-wide files. If you’re locked out, try:
sudo -i to enter the root shell (use sparingly).
If that fails, reboot and check for Permission denied errors in logs (e.g., /var/log/auth.log).
How do I create a symbolic link?
ln -s /target/path /link/path creates a symbolic link. For example:
ln -s /etc/hosts /home/user/hosts-backup
(The link will update if the target changes.)
What’s the quickest way to count lines in a file?
wc -l file.txt counts lines. Add -w for words and -m for characters. For large files, use wc -l < file.txt to avoid loading the file into memory.
Linux commands empower you to automate tasks, debug systems, and optimize workflows—whether you’re managing servers, scripting scripts, or troubleshooting issues. Start with the basics, then explore advanced tools like awk and sed for text manipulation. Always prioritize safety (e.g., use sudo judiciously) and back up critical data before deletions.