Summary: Organizing commonly used Linux commands, once organized, looks very comfortable and is easy to remember.
Shutdown/Reboot/Logout#
| Common Command | Function |
|---|---|
| shutdown -h now | Shut down immediately |
| shutdown -h 10 | Shut down in 10 minutes |
| shutdown -h 11:00 | Shut down at 11:00 |
| shutdown -h +10 | Schedule shutdown (in 10 minutes) |
| shutdown -c | Cancel scheduled shutdown |
| shutdown -r now | Reboot |
| shutdown -r 10 | Reboot in 10 minutes |
| shutdown -r 11:00 | Scheduled reboot |
| reboot | Reboot |
| init 6 | Reboot |
| init 0 | Shut down immediately |
| telinit 0 | Shut down |
| poweroff | Shut down immediately |
| halt | Shut down |
| sync | Synchronize buffer data to disk |
| logout | Logout from Shell |
System Information and Performance Monitoring#
| Common Command | Function |
|---|---|
| uname -a | View kernel/OS/CPU information |
| uname -r | View kernel version |
| uname -m | View processor architecture |
| arch | View processor architecture |
| hostname | View computer name |
| who | Display currently logged-in users |
| who am i | Display username at login |
| whoami | Display current username |
| cat /proc/version | View Linux version information |
| cat /proc/cpuinfo | View CPU information |
| cat /proc/interrupts | View interrupts |
| cat /proc/loadavg | View system load |
| uptime | View system uptime, user count, load |
| env | View system environment variables |
| lsusb -tv | View system USB device information |
| lspci -tv | View system PCI device information |
| lsmod | View loaded system modules |
| grep MemTotal /proc/meminfo | View total memory |
| grep MemFree /proc/meminfo | View free memory |
| free -m | View memory usage and swap usage |
| date | Display system date and time |
| cal 2021 | Display 2021 calendar |
| top | Dynamically display CPU/memory/process status |
| vmstat 1 20 | Sample system status every 1 second, 20 times |
| iostat | View IO read/write/CPU usage |
| sar -d 1 10 | Query disk performance |
Disk and Partition#
| Common Command | Function |
|---|---|
| fdisk -l | View all disk partitions |
| swapon -s | View all swap partitions |
| df -h | View disk usage and mount points |
| df -hl | Same as above |
| du -sh /dir | View the size of a specific directory |
| du -sk * | sort -rn |
| mount /dev/hda2 /mnt/hda2 | Mount hda2 disk |
| mount -t ntfs /dev/sdc1 /mnt/usbhd1 | Mount with specified filesystem type (e.g., ntfs) |
| mount -o loop xxx.iso /mnt/cdrom | Mount ISO file |
| umount -v /dev/sda1 | Unmount by device name |
| umount -v /mnt/mymnt | Unmount by mount point |
| fuser -km /mnt/hda1 | Force unmount (use with caution) |
User and User Group#
| Common Command | Function |
|---|---|
| useradd legalgeek | Create user |
| userdel -r legalgeek | Delete user |
| usermod -g group_name user_name | Modify user's group |
| usermod -aG group_name user_name | Add user to group |
| usermod -s /bin/ksh -d /home/codepig –g dev legalgeek | Modify legalgeek's login shell, home directory, and group |
| groups test | View groups of test user |
| groupadd group_name | Create user group |
| groupdel group_name | Delete user group |
| groupmod -n new_name old_name | Rename user group |
| su - user_name | Switch to user_name user |
| passwd | Change password |
| passwd legalgeek | Change password for a specific user |
| w | View active users |
| id legalgeek | View information for specified user legalgeek |
| last | View user login logs |
| crontab -l | View current user's scheduled tasks |
| cut -d: -f1 /etc/passwd | View all system users |
| cut -d: -f1 /etc/group | View all system groups |
Network and Process Management#
| Common Command | Function |
|---|---|
| ifconfig | View network interface properties |
| ifconfig eth0 | View configuration of a specific network card |
| route -n | View routing table |
| netstat -lntp | View all listening ports |
| netstat -antp | View established TCP connections |
| netstat -lutp | View TCP/UDP status information |
| ifup eth0 | Enable eth0 network device |
| ifdown eth0 | Disable eth0 network device |
| iptables -L | View iptables rules |
| ifconfig eth0 192.168.1.1 netmask 255.255.255.0 | Configure IP address |
| dhclient eth0 | Enable eth0 in DHCP mode |
| route add -net 0/0 gw Gateway_IP | Configure default gateway |
| route add -net 192.168.0.0 netmask 255.255.0.0 gw 192.168.1.1 | Configure static route to reach network '192.168.0.0/16' |
| route del 0/0 gw Gateway_IP | Delete static route |
| hostname | View hostname |
| host www.baidu.com | Resolve hostname |
| nslookup www.baidu.com | Query DNS records, check domain resolution status |
| ps -ef | View all processes |
| ps -ef | grep legalgeek |
| kill -s name | Kill process with specified name |
| kill -s pid | Kill process with specified pid |
| top | Display process status in real-time |
| vmstat 1 20 | Sample system status every 1 second, 20 times |
| iostat | iostat |
| sar -u 1 10 | Query CPU usage (once every second, 10 times) |
| sar -d 1 10 | Query disk performance |
Common System Service Commands#
| Common Command | Function |
|---|---|
| chkconfig --list | List system services |
| service <service_name> status | View a specific service |
| service <service_name> start | Start a specific service |
| service <service_name> stop | Stop a specific service |
| service <service_name> restart | Restart a specific service |
| systemctl status <service_name> | View a specific service |
| systemctl start <service_name> | Start a specific service |
| systemctl stop <service_name> | Stop a specific service |
| systemctl restart <service_name> | Restart a specific service |
| systemctl enable <service_name> | Disable auto-start |
| systemctl disable <service_name> | Disable auto-start |
File and Directory Operations#
| Common Command | Function |
|---|---|
| cd <directory_name> | Enter a specific directory |
| cd .. | Go to the parent directory |
| cd ../.. | Go back two levels in the directory |
| cd | Enter the home directory |
| cd - | Go back to the previous directory |
| pwd | Display current path |
| ls | View file directory list |
| ls -F | View contents of the directory (show whether it's a file or directory) |
| ls -l | View detailed list of files and directories |
| ls -a | View hidden files |
| ls -lh | View detailed list of files and directories (enhanced readability of file sizes) |
| ls -lSr | View file and directory list (sorted by file size in ascending order) |
| tree | View tree structure of files and directories |
| mkdir <directory_name> | Create directory |
| mkdir dir1 dir2 | Create two directories at once |
| mkdir -p /tmp/dir1/dir2 | Create directory tree |
| rm -f file1 | Delete 'file1' file |
| rmdir dir1 | Delete 'dir1' directory |
| rm -rf dir1 | Delete 'dir1' directory and its contents |
| rm -rf dir1 dir2 | Delete two directories and their contents at once |
| mv old_dir new_dir | Rename/move directory |
| cp file1 file2 | Copy file |
| cp dir/* . | Copy all files from a directory to the current directory |
| cp -a dir1 dir2 | Copy directory |
| cp -a /tmp/dir1 . | Copy a directory to the current directory |
| ln -s file1 link1 | Create a symbolic link to a file/directory |
| ln file1 lnk1 | Create a hard link to a file/directory |
| find / -name file1 | Search for file/directory starting from the root directory |
| find / -user user1 | Search for files/directories owned by user1 |
| find /dir -name *.bin | Search for files with .bin suffix in directory /dir |
| locate | Quickly locate files |
| locate *.mp4 | Find files ending with .mp4 |
| whereis | Show the path of a binary/executable file |
| which | Find a binary file in system directories |
| chmod ugo+rwx dir1 | Set read (r), write (w), and execute (x) permissions for owner (u), group (g), and others (o) on directory |
| chmod go-rwx dir1 | Remove read, write, and execute permissions for group (g) and others (o) on directory |
| chown user1 file1 | Change file owner |
| chown -R user1 dir1 | Change directory owner |
| chgrp group1 file1 | Change file group |
| chown user1 file1 | Change file owner and group |
File Viewing and Processing#
| Common Command | Function |
|---|---|
| cat file1 | View file content |
| cat -n file1 | View content and show line numbers |
| tac file1 | View file content starting from the last line |
| more file1 | more file1 |
| less file1 | Similar to more command, but allows reverse operation |
| head -2 file1 | View the first two lines of the file |
| tail -2 file1 | View the last two lines of the file |
| tail -f /log/msg | Real-time view of content added to the file |
| grep legalgeek hello.txt | Search for the keyword legalgeek in hello.txt |
| grep ^sheep hello.txt | Search for content starting with sheep in hello.txt |
| grep [0-9] hello.txt | Select all lines in hello.txt containing numbers |
| sed 's/s1/s2/g' hello.txt | Replace s1 with s2 in hello.txt |
| sed '/^$/d' hello.txt | Delete all empty lines from hello.txt |
| sed '/ *#/d; /^$/d' hello.txt | Delete all comments and empty lines from hello.txt |
| sed -e '1d' hello.txt | Exclude the first line from hello.txt |
| sed -n '/s1/p' hello.txt | View lines containing the keyword "s1" |
| sed -e 's/ *$//' hello.txt | Remove trailing whitespace from each line |
| sed -e 's/s1//g' hello.txt | Remove only the word s1 from the document and keep the rest |
| sed -n '1,5p;5q' hello.txt | View content from the first line to the 5th line |
| sed -n '5p;5q' hello.txt | View the 5th line |
| paste file1 file2 | Merge content of two files or columns |
| paste -d '+' file1 file2 | Merge content of two files or columns, separated by "+" |
| sort file1 file2 | Sort content of two files |
| comm -1 file1 file2 | Compare content of two files (remove content in 'file1') |
| comm -2 file1 file2 | Compare content of two files (remove content in 'file2') |
| comm -3 file1 file2 | Compare content of two files (remove common parts) |
Packaging and Unpacking#
| Common Command | Function |
|---|---|
| zip xxx.zip file | Compress to zip package |
| zip -r xxx.zip file1 file2 dir1 | Compress multiple files and directories into zip package |
| unzip xxx.zip | Unzip zip package |
| tar -cvf xxx.tar file | Create uncompressed tar package |
| tar -cvf xxx.tar file1 file2 dir1 | Create tar package from multiple files and directories |
| tar -tf xxx.tar | View contents of tar package |
| tar -xvf xxx.tar | Unzip tar package |
| tar -xvf xxx.tar -C /dir | Unzip tar package to specified directory |
| tar -cvfj xxx.tar.bz2 dir | Create bz2 compressed package |
| tar -jxvf xxx.tar.bz2 | Unzip bz2 compressed package |
| tar -cvfz xxx.tar.gz dir | Create gzip compressed package |
| tar -zxvf xxx.tar.gz | Unzip gzip compressed package |
| bunzip2 xxx.bz2 | Unzip bz2 compressed package |
| bzip2 filename | Compress file |
| gunzip xxx.gz | Unzip gzip compressed package |
| gzip filename | Compress file |
| gzip -9 filename | Maximum compression |
RPM Package Management Commands#
| Common Command | Function |
|---|---|
| rpm -qa | View installed rpm packages |
| rpm -q pkg_name | Query a specific rpm package |
| rpm -q --whatprovides xxx | Show which package provides xxx functionality |
| rpm -q --whatrequires xxx | Show which package depends on xxx functionality |
| rpm -q --changelog xxx | Show changelog of xxx package |
| rpm -qi pkg_name | View detailed information of a package |
| rpm -qd pkg_name | Query documentation provided by a package |
| rpm -qc pkg_name | View configuration files provided by installed rpm package |
| rpm -ql pkg_name | View files installed by a package |
| rpm -qf filename | View which package a file belongs to |
| rpm -qR pkg_name | Query package dependencies |
| rpm -ivh xxx.rpm | Install rpm package |
| rpm -ivh --test xxx.rpm | Test install rpm package |
| rpm -ivh --nodeps xxx.rpm | Install rpm package ignoring dependencies |
| rpm -e xxx | Uninstall package |
| rpm -Fvh pkg_name | Upgrade an already installed rpm package |
| rpm -Uvh pkg_name | Upgrade rpm package (install if not installed) |
| rpm -V pkg_name | Verify detailed information of RPM package |
YUM Package Management Commands#
| Common Command | Function |
|---|---|
| yum repolist enabled | Show available source repositories |
| yum search pkg_name | Search for software package |
| yum install pkg_name | Download and install software package |
| yum install --downloadonly pkg_name | Download without installing |
| yum list | Show all packages |
| yum list installed | View currently installed packages |
| yum list updates | View updatable package list |
| yum check-update | View upgradable software packages |
| yum update | Update all software packages |
| yum update pkg_name | Upgrade specified software package |
| yum deplist pkg_name | List package dependencies |
| yum remove pkg_name | Remove software package |
| yum clean all | Clear cache |
| yum clean packages | Clear cached packages |
| yum clean headers | Clear cached headers |
DPKG Package Management Commands#
| Common Command | Function |
|---|---|
| dpkg -c xxx.deb | List contents of deb package |
| dpkg -i xxx.deb | Install/upgrade deb package |
| dpkg -r pkg_name | Remove deb package |
| dpkg -P pkg_name | Remove deb package (without configuration) |
| dpkg -l | View installed deb packages |
| dpkg -l pkg_name | Show general information of package |
| dpkg -L pkg_name | View files installed by deb package |
| dpkg -s pkg_name | View detailed information of package |
| dpkg –unpack xxx.deb | Unpack contents of deb package |
APT Software Tools#
| Common Command | Function |
|---|---|
| apt-cache search pkg_name | Search for software package |
| apt-cache show pkg_name | Get overview information of package |
| apt-get install pkg_name | Install/upgrade software package |
| apt-get purge pkg_name | Uninstall software (including configuration) |
| apt-get remove pkg_name | Uninstall software (excluding configuration) |
| apt-get update | Update package index information |
| apt-get upgrade | Update installed software packages |
| apt-get clean | Clean cache |