Skip to content

Linux Commands

Essential Linux command line operations.

File Operations

COMMANDDESCRIPTION
lsList directory contents
ls -laList all files with details
ls -lhList with human-readable sizes
cd /pathChange directory
pwdPrint working directory
mkdir dirCreate directory
mkdir -p path/to/dirCreate nested directories
rmdir dirRemove empty directory
rm fileRemove file
rm -r dirRemove directory and contents
rm -rf dirForce remove directory
cp file1 file2Copy file
cp -r dir1 dir2Copy directory
mv file1 file2Move/rename file
touch fileCreate empty file

File Content

COMMANDDESCRIPTION
cat fileDisplay file content
less fileView file with paging
head -n 10 fileView first 10 lines
tail -n 10 fileView last 10 lines
tail -f fileFollow file (log watching)
grep pattern fileSearch in file
grep -r pattern dirSearch recursively
grep -i pattern fileCase-insensitive search
grep -v pattern fileInvert match
grep -n pattern fileShow line numbers
wc -l fileCount lines
wc -w fileCount words
wc -c fileCount characters
sort fileSort lines
sort -r fileReverse sort
sort -n fileNumeric sort
uniq fileRemove duplicate lines

File Permissions

COMMANDDESCRIPTION
chmod 755 fileSet permissions (rwxr-xr-x)
chmod +x script.shMake script executable
chmod -R 755 dirRecursive permission change
chown user:group fileChange owner
chown -R user:group dirRecursive owner change
chgrp group fileChange group

Permission Codes

CODEPERMISSION
7rwx (read, write, execute)
6rw- (read, write)
5r-x (read, execute)
4r-- (read only)
0--- (no permission)

Process Management

COMMANDDESCRIPTION
ps auxList all processes
ps -efList processes in full format
topDisplay system processes
htopInteractive process viewer
kill PIDTerminate process
kill -9 PIDForce kill process
killall nameKill all processes by name
pkill patternKill processes matching pattern
pgrep patternFind process IDs
nohup command &Run command in background
jobsList background jobs
bg %1Background job
fg %1Foreground job

System Information

COMMANDDESCRIPTION
uname -aSystem information
uname -rKernel version
hostnameHostname
df -hDisk space usage
du -sh dirDirectory size
free -hMemory usage
uptimeSystem uptime
whoWho is logged in
wWho is logged in and what they're doing
dateCurrent date/time
calCalendar
dmesgKernel ring buffer messages

Network

COMMANDDESCRIPTION
ip addrShow IP addresses
ip linkShow network interfaces
ifconfigShow network config (deprecated)
ping hostPing host
traceroute hostTrace route to host
nslookup hostDNS lookup
dig hostDNS lookup (more detailed)
netstat -tulnList listening ports
ss -tulnList listening sockets
lsof -i :portList processes using port
curl URLTransfer data from URL
wget URLDownload file from URL
hostname -IShow IP address

Disk & Filesystem

COMMANDDESCRIPTION
df -hDisk space (human-readable)
df -iDisk inode usage
du -sh *Size of all files/directories
du -h --max-depth=1Size of top-level directories
mountShow mounted filesystems
mount /dev/sdb1 /mntMount filesystem
umount /mntUnmount filesystem
fdisk -lList disk partitions
lsblkList block devices
mkfs.ext4 /dev/sdb1Format partition

Archive & Compress

COMMANDDESCRIPTION
tar -cvf archive.tar filesCreate tar archive
tar -xvf archive.tarExtract tar archive
tar -czvf archive.tar.gz filesCreate tar.gz
tar -xzvf archive.tar.gzExtract tar.gz
zip -r archive.zip filesCreate zip archive
unzip archive.zipExtract zip archive
gzip fileCompress with gzip
gunzip file.gzDecompress gzip

Find

COMMANDDESCRIPTION
find . -name "pattern"Find by name
find . -type fFind files only
find . -type dFind directories only
find . -size +100MFind files > 100MB
find . -mtime -7Find modified in last 7 days
find . -perm 755Find with specific permissions
find . -exec command {} \;Execute command on results

User Management

COMMANDDESCRIPTION
whoamiCurrent user
idUser and group IDs
whoLogged in users
useradd usernameCreate user
userdel usernameDelete user
usermod -aG group userAdd user to group
passwd usernameChange password
groupsShow user groups
sudo -iSwitch to root

Environment Variables

COMMANDDESCRIPTION
envDisplay environment variables
export VAR=valueSet environment variable
echo $VARDisplay variable value
unset VARUnset variable
source ~/.bashrcReload bash configuration

Package Management

Debian/Ubuntu

COMMANDDESCRIPTION
apt updateUpdate package list
apt upgradeUpgrade packages
apt install packageInstall package
apt remove packageRemove package
apt search packageSearch for package
apt show packageShow package info

RHEL/CentOS

COMMANDDESCRIPTION
yum updateUpdate packages
yum install packageInstall package
yum remove packageRemove package
yum search packageSearch for package

Systemd Services

COMMANDDESCRIPTION
systemctl start serviceStart service
systemctl stop serviceStop service
systemctl restart serviceRestart service
systemctl status serviceShow service status
systemctl enable serviceEnable at boot
systemctl disable serviceDisable at boot
systemctl list-unitsList all units
journalctl -u serviceView service logs

SSH

COMMANDDESCRIPTION
ssh user@hostConnect to remote host
ssh -p port user@hostConnect on specific port
ssh-keygen -t ed25519Generate SSH key
ssh-copy-id user@hostCopy SSH key to host
scp file user@host:/pathCopy file to remote

Useful Commands

Find large files

bash
find . -type f -size +100M -exec ls -lh {} \;

Find old files

bash
find . -type f -mtime +30

Count files in directory

bash
find . -type f | wc -l

Monitor file changes

bash
watch -n 1 ls -lh

Process using most CPU

bash
ps aux --sort=-%cpu | head -n 10

Process using most memory

bash
ps aux --sort=-%mem | head -n 10

Listen on port

bash
nc -l 8080

Download with progress

bash
wget --progress=bar:force URL

Parallelize downloads

bash
aria2c -x 16 -s 16 URL

Best Practices

  • Use tab completion
  • Use man for command documentation
  • Use aliases for frequently used commands
  • Use sudo carefully
  • Regularly update packages
  • Monitor disk space
  • Use log files for troubleshooting
  • Keep backups of important files
  • Use screen or tmux for long-running sessions
  • Use version control for configuration files

TIP

Use history | grep "command" to find previously used commands.

Released under MIT License.