Skip to main content

Common Linux Commands

Open a terminal and use this guide to find files, inspect their contents, and answer basic questions about your Linux system. The examples assume Bash on Linux. Replace sample paths with paths on your system when adapting a command.

CommandWhat it does
pwdPrints the full path to your current directory.
ls -lahLists files with details, including hidden entries (-a) and readable sizes (-h, with -l).
cd ~/DownloadsOpens the Downloads directory in your home directory, if it exists.
cd ..Moves up to the parent directory.
cd -Returns to your previous working directory.

An absolute path starts at /, as in /etc/os-release. A relative path starts from your current directory, as in reports/today.txt. Quote paths that contain spaces, such as cd "Project Notes".

Create, copy, and rename files​

mkdir reports
touch notes.txt
cp -i notes.txt notes-backup.txt
mv -i notes-backup.txt archived-notes.txt

mkdir creates a directory. touch creates an empty file or updates the timestamps on an existing file. cp copies a file, and mv moves or renames it. For both commands, -i prompts before overwriting an existing destination.

Use rm -i archived-notes.txt to prompt before removing a file. rm typically bypasses the desktop trash. Use rmdir reports to remove an empty directory.

Read and search text​

cat app.log
less app.log
head -n 5 app.log
tail -n 5 app.log
grep -n 'ERROR' app.log
find . -type f -name '*.log'
  • cat prints the whole file. less lets you scroll through it. Press q to exit.
  • head -n 5 and tail -n 5 show the first and last five lines.
  • grep -n prints each matching line with its line number. For example, 2:ERROR disk full shows a match on line 2.
  • find searches beneath the current directory (.). -type f limits results to regular files. Quoting '*.log' lets find match the pattern in each directory it visits.

Use pipes and redirection​

grep 'ERROR' app.log | wc -l
grep 'ERROR' app.log > errors.log
grep 'WARNING' app.log >> errors.log

The pipe (|) sends the matching lines to wc -l for counting. The > operator writes output to a file and replaces any existing contents. The >> operator appends output. Both operators create the destination file if it is missing.

Inspect and change permissions​

id
ls -l notes.txt
chmod u=rw,go= notes.txt

id displays your user ID and group memberships. ls -l shows a file's permissions and ownership. The chmod example grants the owner read and write permissions and clears permissions for the group and others. Afterward, ls -l notes.txt shows permissions starting with -rw-------.

Check system resources​

QuestionCommandWhat to look for
Which filesystem is running out of space?df -hThe Use% column shows how full each mounted filesystem is.
How much space does this directory use?du -sh .A single human-readable total for the current directory.
How much memory can applications use?free -hThe available column estimates how much RAM is available for new applications without swapping.
Which processes are active?ps -efA snapshot with process IDs (PIDs) and command lines.
What is using CPU or memory right now?topA live view of processes and resource use. Press q to exit.
Which distribution am I running?cat /etc/os-releaseThe distribution name and version.

If you need to stop a process you own, identify its PID with ps -ef, then run kill PID with that number. The default signal, SIGTERM, requests that the process terminate and gives it a chance to clean up. Double-check the PID before sending the signal.

Check basic connectivity​

CommandWhat it tells you
ip -brief addressWhich interfaces are present and which IP addresses they have.
ip routeWhich routes the system knows about, including the default gateway when one is configured.
ping -c 4 example.comWhether the host responds to up to four ICMP echo requests. A firewall can block these requests while normal web traffic still works.
curl -I https://example.comThe HTTP response headers, including the status code, if DNS resolution and the connection succeed. Some servers handle HEAD requests differently from GET requests.

Find help and software​

Use man ls to open the manual, ls --help for a short option summary, and help cd for Bash's built-in commands. Use command -v curl to see whether Bash can find curl. Minimal Linux installations may omit tools such as man, less, or curl.

Package tools vary by distribution. Try apt search curl on Ubuntu or Debian, dnf search curl on Fedora, or pacman -Ss curl on Arch Linux. These commands search package metadata. Installing or removing system packages modifies the machine and generally requires administrator privileges.