Mastering the command line is one way to save time and avoid clicking endlessly. On Linux or macOS, it lets you search files, edit text, and manage systems from the terminal—no graphical interface needed. No special setup is required, and you can start using it right after you log in. The first few commands are so common they’re in every terminal by default, but understanding all the advanced ones unlocks deeper control over your system.
What You'll Need
- A Linux system (Ubuntu, Fedora, Debian, etc.) or a macOS installation with Terminal open.
- Basic familiarity with your file structure (folders, documents, etc.)—knowing where files live helps.
- No extra tools or installers; your OS includes everything you need.
Step-by-Step: Core Command Line Commands
-
Navigate with `cd` and `ls`
Start by listing files in your current directory.
lsshows all files and folders; usels -lfor a detailed list (permissions, size, owner).Move into a folder with
cd foldername. Change to your home directory withcd ~orcd(without arguments). To go back to the parent folder, usecd ... -
Search for files with `find` or `grep`
Find a file by name (case-sensitive by default):
find ~/Documents -name "filename". For case-insensitive searches, add-iname:find ~/Downloads -iname "*.pdf".Search text inside files without listing them first:
grep "searchterm" file.txt. To search recursively in a folder:grep -r "searchterm" /path/to/folder. -
Create, edit, and delete files/folders
Create a new folder:
mkdir newfolder. Create a file:touch report.txt. Delete a file:rm file.txt. Delete a folder (and its contents):rm -r foldername(use with caution—this removes everything inside too). -
Copy, move, and compress files
Copy a file to another location:
cp source.txt destination/. Move a file (renames it to the destination):mv oldfile.txt newfolder/. Compress a folder into a ZIP:zip archive.zip foldername. Extract a ZIP:unzip archive.zip. -
View and modify text files
Open a file in the terminal (no editor needed):
cat file.txt(displays everything). Usemore file.txtto scroll line-by-line. Edit a file interactively withnano file.txt(pressCtrl+Oto save,Ctrl+Xto exit). For more advanced editing, usevim file.txt(requires practice). -
System updates and troubleshooting
Check for updates:
sudo apt update && sudo apt upgrade -y(Ubuntu/Debian) orbrew update && brew upgrade(macOS). Restart a service (e.g., SSH):sudo systemctl restart sshd(Linux) orsudo launchctl start com.openssh.server(macOS).If a command fails, check for typos and verify permissions. If it’s an error like "Permission denied," use
sudo(but be cautious—it grants root-level access).
Common Mistakes
-
Forgiving typos in commands
Linux/macOS are case-sensitive. For example,
ls≠Ls. Always spell commands exactly as you see them in the manual or help output (man ls). -
Running `sudo` on non-system files
Using `sudo` for files you don’t own can break permissions or lock you out. Always confirm the file’s owner with
ls -lbefore granting root access. -
Ignoring recursive flags
Commands like
grep -rorfind -type fmust include `-r` or `-type` to search all subdirectories. Omitting them limits your search to the current folder. -
Overwriting files without backup
Commands like
mv old.txt new.txtrename the file—not backup it. Usecp old.txt old.txt.backupfirst if you need history.
Tips
-
Use the Tab key
Pressing
Tabauto-completes filenames, commands, and arguments. For example, typingvi+Tabfills in "vim" or "visual mode." -
Pipe commands with `|`
Redirect output from one command to another. For example:
ls | grep ".txt"lists all files and filters only those ending with.txt. Useful for cleaning up search results. -
Learn shortcuts
Common shortcuts:
Ctrl+C: Stop the current command.Ctrl+D: Exit the terminal.Ctrl+Z: Suspend the current process (to bring back, usefgorjobs).
-
Use `history` and `!!`
View previous commands with
history. Press!followed by a number (e.g.,!5) to rerun the 5th command. Typing!!reruns the last command.
Troubleshooting
-
Command not found
Check for typos, ensure the command is in your
PATH(add it temporarily withexport PATH=$PATH:/custom/path), or install it if missing (e.g.,sudo apt install vim). -
Permission denied
If you’re not the owner, use
sudo(last resort), copy the file to your directory first, or ask the owner to grant permissions withchmodorchown. -
Terminal won’t recognize commands
Update your shell (e.g.,
bashorzsh) with:sudo update-alternatives --config editor(Linux) orbrew install zsh && chsh -s /usr/local/bin/zsh(macOS). -
Process hangs or crashes
Kill it with
Ctrl+C. To see running processes, usetoporps aux. Restart the service if appropriate (e.g.,sudo systemctl restart nginx). -
Need to reset the terminal
Close and reopen Terminal; if that doesn’t work, restart your system (most terminal issues are transient).
Frequently Asked Questions
What if my terminal doesn’t recognize `grep` or `zip`?
These tools are usually preinstalled, but if missing, install them with:
sudo apt install grep zip (Linux) or brew install grep zip (macOS). For macOS, you may also need to enable sudo access to Homebrew via sudo sudo (type it twice).
How do I create a hidden file?
Files starting with a dot (e.g., ~/.bashrc) are hidden. Create one by typing:
touch .hiddenfile and use ls -a to see it.
Can I use command line commands on Windows?
Windows 10/11 includes PowerShell, but Linux/macOS commands require a Unix-like environment (e.g., Git Bash, WSL, or a Linux VM). For native Windows, use built-in cmd.exe instead.
Why do some commands fail with "No such file or directory"?
Check the full path to the file or command. Use which commandname to find its location, or pwd to confirm you’re in the right directory. Example: If you’re in /home/user but need /usr/local/bin, use:
sudo ln -s /usr/local/bin/commandname /usr/bin/commandname to symlink it.
How do I search for files by last modified date?
Use:
find . -type f -mtime -7 to find files modified in the last 7 days. Add -iname for case-insensitive searches.
What’s the difference between `cp` and `mv`?
cp copies files to a destination, while mv moves or renames them. To copy a file to another folder, use:
cp file.txt /destination/folder/.
To move a folder, include its contents:
mv foldername newlocation/.
Now you have the tools to navigate, automate, and debug your system with ease. Start with the basics and build up—mastering the command line is a skill that grows with practice.