List of Useful Linux Commands

This post contains a collection of useful Linux commands for the Ubuntu distribution. The list is extended every time I find a new handy command. Most of the commands are also applicable to other Linux distributions. Use commands in red with caution.

File System Commands

Description Example
Display Current Working Directory pwd
Change Current Working Directory cd /path/to/directory
Show All Files in Current Directory (Including Hidden Ones) ls -la
Show Sizes of All Files and Directories in Current Directory du -sh *
Show Available Disk Space df -h
Copy File cp /source/file /destination/file
Move or Rename File mv /source/file /destination/file
Create Directory mkdir myDirectory
Delete File rm /path/to/file
Delete Directory Recursively rm -rf /path/to/directory
Create Symbolic Link ln -s /path/to/target /path/to/symlink
Change File Permissions (non-recursive) sudo chmod 644 /path/to/file
Change File Permissions (recursive) sudo chmod -R 644 /path/to/directory
Change Owner of File (non-recursive) sudo chown user:group /my/path
Change Owner of Symbolic Link (non-recursive) sudo chown -h user:group /my/symlink
Change Owner of Files / Directories (recursive) sudo chown -R user:group /my/path
Count Files (recursive) find /my/path -type f | wc -l
List Number of Files (recursive) for Each Subfolder find . -type f | cut -d/ -f2 | sort | uniq -c
Find Files Older Than a Specified Number of Days (non-recursive) find /path/to/directory -maxdepth 1 -mtime +60 -type f -print
Find Files Older Than a Specified Number of Days (recursive) find /path/to/directory -mtime +60 -type f -print
Delete Files Older Than a Specified Number of Days (non-recursive) find /path/to/directory -maxdepth 1 -mtime +60 -type f -delete
Delete Files Older Than a Specified Number of Days (recursive) find /path/to/directory -mtime +60 -type f -delete
Show All Disks and Partitions lsblk
Show File Systems for all Disks and Partitions lsblk -f
Format Partition with File System sudo mkfs -t ext4 /dev/sdf2
Mount Drive sudo mount /dev/sdc1 /mnt/mountpoint/
Unmount Drive sudo umount /dev/sdc1
Mount USB Stick pmount /dev/sdf1
Unmount USB Stick pumount /dev/sdf1
Check File System sudo fsck -p /dev/sdc1
File System Commands

Viewing, Editing and Comparing Files

Description Example
Display File Contents cat /path/to/file
Edit File sudo nano /path/to/file
View End of File less +G /path/to/file
View End of File and Update Automatically tail -f /path/to/file
Compare Files in Two Directories Recursively diff -rq /path/to/dir1 /path/to/dir2
Viewing, Editing and Comparing Files

User Management

Description Example
List All Users cut -d: -f1 /etc/passwd
List All Groups cut -d: -f1 /etc/group
Add New User sudo adduser john
Show Groups a User is Assigned to groups john
Add User to Group usermod -a -G examplegroup john
User Management

Package Management Commands

Description Example
Update Package Index sudo apt update
List Upgradable Packages apt list --upgradable
Upgrade Installed Packages sudo apt upgrade
Install Security Updates sudo unattended-upgrade -v
List All Installed Packages apt list --installed
Show Version of Installed Package apt list <package name>
Show All Versions of a Package apt list -a <package name>
Package Management Commands

Network / Internet / Firewall Commands

Description Example
Check Host Availability ping [IP or hostname]
Download File curl -O [URL]
List Open Ports sudo lsof -i -P -n | grep LISTEN
Open Firewall to Specific Port from Local Subnet sudo ufw allow from 192.168.1.0/24 to any port 22
Delete Firewall Rule by Number sudo ufw status numbered
sudo ufw delete 42
Connect to FTP Server ftp myserver.org
Connect to SFTP Server sftp user@myserver.org
Network and Firewall Commands

Leave a Reply

Your email address will not be published. Required fields are marked *