Common Linux Commands
A practical reference of the most common Linux commands — file management, viewing, system info, networking, packages, permissions, and archiving — with examples and safety notes.
The Linux command line is a text interface to your computer. Often called the shell, terminal, console, or prompt, it can appear complex at first. But being able to copy and paste commands, combined with the power and flexibility the command line offers, makes it essential when following instructions online — including many on this site.
New to the shell? Prefix any command below with
man(for example,man ls) to read its full manual, and pressqto exit.
File and Directory Management
-
ls- List directory contents1 2 3 4
ls ls -l ls -lh ls -la
-
cd- Change directory1 2 3 4
cd /path/to/directory cd .. cd ~ cd -
-
pwd- Print working directory1
pwd -
mkdir- Create a new directory1 2
mkdir directory_name mkdir -p /path/to/nested/directory
-
rmdir- Remove an empty directory1
rmdir directory_name -
rm- Remove files or directories1 2 3 4
rm file_name rm -i file_name rm -r directory_name rm -rf directory_name
-
cp- Copy files or directories1 2 3
cp source_file destination_file cp -i source_file destination_file cp -r source_directory destination_directory
-
mv- Move or rename files or directories1 2 3
mv source_file destination_file mv -i source_file destination_file mv old_directory_name new_directory_name
-
touch- Create an empty file or update its timestamp1
touch file_name -
ln- Create links between files1
ln -s /path/to/target link_name
-
find- Search for files in a directory hierarchy1 2 3 4
find /path/to/search -name "file_name" find /path -type f -name "*.log" find /path -type d -name "cache" find . -name "*.tmp" -delete
-
tree- Display directories as a tree1 2 3
tree tree -L 2 tree -a
There is no recycle bin on the command line.
rm -rfdeletes permanently and without confirmation. Double-check the path before running it, and never runrm -rfwith a wildcard or a variable you haven’t verified (rm -rf $DIR/becomesrm -rf /if$DIRis empty).
cpandmvoverwrite the destination silently. Add-ito be prompted before replacing an existing file. The same applies tofind ... -delete— test the match withfind ... -printfirst.
File Viewing and Editing
-
cat- Concatenate and display file content1 2
cat file_name cat file1 file2 > combined.txt
-
less- View file content one screen at a time1
less file_name
-
more- View file content one screen at a time1
more file_name
-
head- Display the first lines of a file1 2
head file_name head -n 20 file_name
-
tail- Display the last lines of a file1 2 3
tail file_name tail -n 20 file_name tail -f /var/log/syslog
-
grep- Search text using patterns1 2 3 4
grep "pattern" file_name grep -i "pattern" file_name grep -n "pattern" file_name grep -r "pattern" /path/to/search
-
wc- Count lines, words, and bytes1 2
wc file_name wc -l file_name
-
nano- Simple text editor in the terminal1
nano file_name
-
viorvim- Advanced text editor in the terminal1 2
vi file_name vim file_name
tail -ffollows a file as it grows, which is the quickest way to watch a log in real time. PressCtrl+Cto stop.
Stuck in
vim? PressEsc, then type:wqto save and quit or:q!to quit without saving. Ifcaton a binary file scrambles your terminal, runresetto restore it.
System Information and Management
-
top- Display real-time system information1
top
-
ps- Display information about active processes1 2 3
ps ps aux ps aux | grep process_name -
df- Display disk space usage1 2
df df -h
-
du- Estimate file space usage1 2 3
du du -h du -sh directory_name
-
free- Display memory usage1 2
free free -h -
uptime- Show how long the system has been running1
uptime -
uname- Display system information1 2
uname uname -a
-
systemctl- Manage services on systemd distributions1 2 3 4 5
systemctl status service_name systemctl start service_name systemctl stop service_name systemctl restart service_name systemctl enable service_name
Add
-htodf,du, andfreefor human-readable sizes (KB/MB/GB instead of raw blocks). Intop, pressqto quit;htopis a friendlier alternative if it’s installed.
systemctl start,stop,restart, andenablechange system state and requiresudo.statusis read-only and safe to run as a normal user.
Networking
-
ping- Send ICMP ECHO_REQUEST to network hosts1 2
ping host_name_or_ip ping -c 4 host_name_or_ip -
ifconfig- Configure a network interface1
ifconfig
-
ip- Show/manipulate routing, devices, and tunnels1 2 3 4
ip address ip a ip link ip route -
netstat- Network statistics1 2
netstat netstat -tuln -
ss- Investigate sockets1 2
ss ss -tuln -
scp- Securely copy files between hosts1 2
scp source_file user@remote_host:/path/to/destination scp -r source_directory user@remote_host:/path/to/destination -
wget- Non-interactive network downloader1 2
wget url wget -O output_name url -
curl- Transfer data from or to a server1 2 3
curl url curl -O url curl -L url
-
lsof- List open files, useful to check ports1
lsof -i :port_number -
kill- Terminate a process by PID1 2
kill pid kill -9 pid
ifconfigandnetstatare deprecated on modern distributions and may not be installed. Useipin place ofifconfigandssin place ofnetstat.
kill -9sendsSIGKILL, which the process cannot catch or clean up after — it can leave temp files or corrupt state behind. Try a plainkill(SIGTERM) first and only escalate to-9if the process ignores it.
Package Management
-
apt-get- APT package handling utility (Debian/Ubuntu)1 2 3
sudo apt-get update sudo apt-get install package_name sudo apt-get remove package_name
-
yum- Package manager (Red Hat/CentOS)1 2 3
sudo yum update sudo yum install package_name sudo yum remove package_name
-
dnf- Package manager (Fedora)1 2 3
sudo dnf update sudo dnf install package_name sudo dnf remove package_name
Run the
updatecommand before installing so you pull the latest package versions. On recent Debian/Ubuntu systems,aptis the modern front end and can replaceapt-getin these examples.
Permissions and Ownership
-
sudo- Run a command as the superuser1
sudo command -
chmod- Change file modes or Access Control Lists1 2 3
chmod 755 file_name chmod +x script.sh chmod -R 755 directory_name
-
chown- Change file owner and group1 2
chown user_name:group_name file_name chown -R user_name:group_name directory_name
Avoid
chmod 777— it makes a file writable and executable by everyone, which is a common security hole. Grant the least access that works (often644for files,755for directories and scripts).
Recursive changes (
-R) apply to every file underneath the target. Runningchmod -Rorchown -Ron the wrong directory — for example a system path like/or/etc— can render the system unbootable. Confirm the path first.
Archiving and Compression
-
tar- Store, list, or extract files in an archive1 2 3 4
tar -cvf archive_name.tar directory_name tar -xvf archive_name.tar tar -czvf archive_name.tar.gz directory_name tar -xzvf archive_name.tar.gz
-
zip- Package and compress files1 2
zip archive_name.zip file1 file2 zip -r archive_name.zip directory_name -
unzip- Extract files from a ZIP archive1 2
unzip archive_name.zip unzip archive_name.zip -d /path/to/destination
The
tarflags are easier to remember as a mnemonic: create, extract, gzip compression, verbose output, and file (the archive name, which must come last). So “create a gzipped archive verbosely from a file” is-czvf.
Miscellaneous
-
echo- Display a line of text1 2
echo "Hello, World!" echo $HOME
-
date- Display or set the system date and time1 2
date date -s "2024-07-31 12:34:56"
-
whoami- Print the current user name1
whoami -
which- Locate a command’s executable1
which command_name
-
man- Display the manual for a command1
man command_name
-
history- Display the command history1
history -
clear- Clear the terminal screen1
clear
date -ssets the clock manually, requires root, and will fight an NTP time-sync service if one is running. On most systems, let NTP manage the time instead.
Speed up recall: press
Ctrl+Rto search your history interactively, and type!!to rerun the previous command. Avoid pasting passwords or tokens on the command line, since they are saved inhistoryin plain text.