在本教程中,我们将列出各种实用的 Linux 命令,仅供有经验的 Linux 用户作为参考指南。默认情况下,并非所有 Linux 命令在您的系统上都可用,因此请考虑在使用前安装相关软件包。
本《Linux 命令实用指南》可能会列出您已经知道但记不住使用语法的 Linux 命令,也可能会介绍一些新的 Linux 命令来提高您的 Linux 命令行效率。请注意,本指南不会教您如何使用 Linux 命令,因为它依赖于您的经验来更改下面的 Linux 命令语法以满足您的需求。
在本教程中您将学习:

备份和压缩
Command |
Description |
tar -c scripts/ | bzip2 -9 > scripts.tar.bz2 |
This linux command will use tar and bzip2 to compress scripts directory with a maximum compression |
dd if=/dev/sda1 | gzip -c9 > /media/usb/sda1.dd.gz |
Backup and compress partition /dev/sda1 to a local file sda1.dd.gz |
cat /media/usb/sda1.dd.gz | gzip -d | dd of=/dev/sdX1 |
Restore a compressed /dev/sda1 partition backup from a file to a /dev/sdX1 partition |
dd bs=1M if=/dev/sda | gzip -c9 | ssh user@linuxconfig.org 'dd of=sda.dd.gz' |
Make a compressed backup of a hard drive /dev/sda and create a remote copy using ssh |
find /etc/ -name '*.conf' | tar -c --files-from=- | bzip2 -9 > system_confs.tar.bz2 |
Find and compress all configuration files ( *.conf ) located in /etc/ directory into a file called system_confs.tar.bz2 |
dd if=/dev/sdb of=my.mbr bs=466 count=1 |
Backup and store a Master Boot Record of hard drive /dev/sdb into a file my.mbr |
dd if=my.mbr of=/dev/sdX bs=466 count=1 |
Restore a Master Boot Record from my.mbr file to hard drive /dev/sdX |
wget --mirror https://example.com |
Create a complete mirror of a remote website with wget command |
tar cvjf etc_$(date +%Y%m%d).tar.bz2 /etc/ |
Create an archive of /etc/ directory using tar command and compress it with bzip2. Compressed file will contain a current date within a filename. |
tar xvjf etc.tar.bz2 |
Uncompress a bzip2 archive etc.tar.bz2 |
find /var/www/ -name '*.gif' | xargs cp -va --target-directory=/tmp/gifs |
Find all GIF files ( *.gif ) in /var/www/ and copy them to /tmp/gifs directory. |
ssh user@linuxconfig.org '( mysqldump --password='pass' data > data.sql )' |
Remotely create a mysql database backup of data database into remote file data.sql |
split -b 1000m linux-commands.iso |
Split a file linux-commands.iso into 1GB files. This will produce xaa, xab, xac.. files each of max size 1GB. Can be handy when working with FAT32 filesystem. See below on how to restore split file. |
cat xa* > linux-commands.iso |
Restore a split file back into linux-commands.iso. See above on how to split file. |
搜索文件系统
Command |
Description |
find /opt -name 'pass*' -or -size +1000k |
Find all files within /opt directory where file name start with pass or file size is 1000k or more. Feel free to use other boolean operators like AND and NOT. |
locate -r '[^/]*\.conf' |
Search index and locate all files with *.conf extension. You may need to run updatedb first. |
find /home/lilo/ -type f ! -perm 755 |
Search for all files in /home/lilo which do not have permissions 755 |
find /home/lilo/ -type f -perm 777 |
Search for all files in /home/lilo with a permissions 777 |
ls -ltr |
List all files in a current directory sorted by access/creation time |
find /tmp/ -mmin -20 |
Find all files within /tmp created within last 20 minutes |
find /tmp -iname file -exec chmod 777 {} \; |
search for a file named file ( case insensitive ) and change its permissions to 777 |
find /var/log/ -size 8k |
Search for files int /var/log with size of 8k |
find / * -perm +6000 -type f -exec ls -ld {} \; > setuid.txt |
Create a list setuid.txt containing names of all binary files with setuid and setguid |
联网
Command |
Description |
curlftpfs ftp-user:ftp-pass@remote.ftp.com /mnt/my_ftp/ |
Mount remote ftp server to a local filesystem /mnt/my_ftp/ |
ssh user@ssh-server.com '( cd /tmp/ && touch ssh_file.txt )' |
Execute commands remotely using ssh. |
ssh user@ssh-server.com '( cat /etc/passwd )' > /tmp/passwd |
create a local copy of remote /etc/passwd |
airodump-ng -c 6 -w data-capture wlan0 |
sniffing wireless network packets using wlan0 wireless interface |
macchanger -r eth0 |
Create a fake MAC address for an eth0 network interface |
ssh -L 4500:127.0.0.1:23 linuxconfig.org |
Create a ssh tunnel for telnet using local port 4500 |
ssh -L 8025:mail.sample.org:25 mail.sample.org |
Tunnel traffic from a local system port 8025 to port mail.sample.org on port 25 |
lsof -i tcp:22 |
Displays a service which uses port 22 |
ethtool eth0 |
Show status of eth0 network interface |
iwlist wlan0 scanning |
Scan for available wireless networks using wlan0 interface |
netstat -ant |
List all TCP ports on the system |
netstat -tupl |
List all available services on the system |
ip route add default via 10.10.10.10 |
Set a default route via 10.10.10.10 |
算术和转换
Command |
Description |
echo $((0xFFF)) |
Convert hexadecimal number ( in this case FFF ) to decimal using shell expansion. |
echo $((8#44)) |
Convert octal number ( in this case 44 ) to decimal using shell expansion. |
echo "obase=16; ibase=10; 555;" | bc |
Convert decimal number ( in this case 555 ) to hexadecimal using shell expansion. |
echo "obase=8; ibase=10; 64;" | bc |
Convert decimal number ( in this case 64 ) to octal using shell expansion. |
echo "obase=16; ibase=8; 255;" | bc |
Convert octal number ( in this case 255 ) to hexadecimal using shell expansion. |
echo "3447.2 * 343.61" | bc |
Multiply a number. For addition and subtraction use “+” and “-” instead of “*” |
echo "scale=10; 100 / 3" | bc |
Divide number with a floating point precision 10 |
units -t '13miles' 'km' |
Convert miles to kilometers ( eg. 13 miles ) |
units -t '10.5inches' 'cm' |
Convert inches to centimeters ( eg. 10.5 inches ) |
units -t '78344352ms' 'hour' |
Convert milliseconds to hours |
文本操作
Command |
Description |
dd if=commands.txt of=commands.new conv=lcase |
Convert all characters from Uppercase to Lowercase This will not alter a source file, but create new file called commands.new |
rename 's/\.sh$/.bash/' *.sh |
Rename all files in a current working directory with extension *.sh to *.bash |
rename 's/^/new_/' *.conf |
Add a prefix new_ to all files in a current working directory with extension *.conf |
grep -v ^\# /etc/ntp.conf | grep . |
Show only uncommented lines within a configuration file as well as ingnore empty lines. |
ls | grep " " | while read -r f; do mv "$f" `echo $f | tr ' ' '_'`; done |
Remove space from all filenames in a current working directory |
ls | while read -r f; do mv "$f" `echo $f | tr '[A-Z]' '[a-z]'`; done |
Change all filenames in a current directory from uppercase to lowercase. |
日期和时间
Command |
Description |
date -ud@1244763573 |
Convert an epoch time to Coordinated Universal Time |
date -d "Dec 23 18:10:02 EST 2010" +%s |
Convert date to an epoch time |
echo 'wget -c http://linux.com/distro.iso' | at 03:00 |
Download an iso image at 3AM. -c allows you to continue download in case of lost network connection. |
date -d '2 Feb 2013' +%A |
What day would be/was 2 Feb 2013? …Saturday |
units -t '10 days + 6 hours + 26 minutes + 59 seconds' 'seconds' |
Convert time to seconds |
多媒体
Command |
Description |
wodim --devices |
Get a burner block device filename |
cdrecord -v blank=all dev=/dev/scd0 |
Erase / full blank your cd-rw. Note: use wodim –devices to get your block device file name. |
cdrecord -v blank=fast dev=/dev/scd0 |
Erase fast your cd-rw. Note: use wodim –devices to get your block device file name. |
fmpeg -i out.wav -acodec libmp3lame out.mp3 |
Convert WAV audio format to MP3 |
normalize-mp3 *.mp3 |
Normalize a volume for all your MP3 audio files to reduce sudden volume spikes between tracks. |
cat file1.mp3 file2.mp3 > out.mp3 |
Join all MP3 audio files into a single track. |
sox file1.wav file2.wav file3.wav out.wav |
Join all wav audio files into a single track. |
for i in $( ls ); do ffmpeg -i $i $i.wav; done |
Convert all MP3 or AC3 audio files into WAV format. |
normalize-audio -m *.wav |
Normalize a volume for all your WAV audio files to reduce sudden volume spikes between tracks. |
cdrecord -v -nofix -eject dev='/dev/scd0' -audio -pad *.wav |
Burn all WAV audio files on a CD using device /dev/scd0 |
cdrecord -v -fix -eject dev='/dev/scd0' |
Close a CD session using /dev/scd0 burning device. |
ffmpeg -f x11grab -s xga -r 25 -i :0 -sameq screen.mpg |
Record a video of your screen into a screen.mpg video file. |
for i in $( ls *.jpg ); do convert -resize 25% $i new_$i; done |
Resize all images in your current directory ( in this case images with extension *jpg ) to a 25% of the original size. |
mkisofs -o /tmp/cd.iso /path/to/your/files/ |
Create an ISO image from files in /path/to/your/files/ |
wodim -eject -tao speed=0 dev=/dev/scd0 -v -data /my/image.iso |
Burn an ISO image using wodim and /dev/scd0 burning device. |
mount -t iso9660 /path/to/iso/file.iso /mnt/iso -o loop |
Mount ISO image to a /mnt/iso directory. |
xrandr --output VGA --auto |
Clone a video output to yout VGA port. Useful for presentations. Use xrandr with no arguments to see whether VGA is connected to a projector. |
arecord -d 10 /tmp/out.wav |
Test your microphone. |
磁盘使用和管理
Command |
Description |
time dd if=/dev/hdb of=/dev/null bs=1024k |
Non-destructive hard drive speed and size test. Replace /dev/hdb with your hard drive. |
du -m --max-depth 1 | sort -rn | head -11 |
Get a directory size of all directories in a current working directory, sort them and show first 10 largest. Note: the first directory is a parent directory. |
du -s * | sort -k1,1rn | head |
Display top 10 largest files or directories in a current working directory. |
dd if=/dev/zero of=/sp bs=10000 count=10000; mkswap /sp; swapon /sp |
Create a file /sp with size of 100MB, generate swap signature and include /sp file into overall system’s swap memory. This will add another 100MB to your system’s swap. |
dpkg-query -Wf='${Installed-Size;10}\t${Package}\n' | sort -k1,1rn |
DEB package management only. Show all installed packages and sort them from largest to smallest. |
rpm -q -a --qf '%10{SIZE}\t%{NAME}\n' | sort -k1,1rn |
RPM package management only. Show all installed packages and sort them from largest to smallest. |
head -c 100000000 /dev/urandom > file.data |
Create a file.data with a random data and approximately with 100MB in size. |
dd bs=1 seek=2TB if=/dev/null of=~/large-file |
Create a 2TB ~/large-file taking no space. |
df -h . |
Information about free space for a partition located under your current working directory. |
硬件信息
Command |
Description |
biosdecode |
Retrieve BIOS information. |
dmidecode -s bios-vendor |
Retrieve your BIOS vendor |
dmidecode --type baseboard |
Retrieve information about your motherboard |
ls -la /dev/disk/by-id/usb-* |
USB disk device files. NOTE: USB disk must be plug-in. May not work on all systems. |
hdparm -I /dev/sdx |
Hard drive model of /dev/sdx. |
hdparm -tT /dev/sdx |
Hard drive speed. NOTE: this test disregards a filesystem. |
hddtemp /dev/sda |
Check temperature of /dev/sda hard drive |
lspci | grep VGA |
Get information about your graphic card |
dmidecode --type 4 |
Retrieve your processor information. Also try cat /proc/cpuinfo |
x86info -a 2> /dev/null | grep Connector | uniq |
Retrieve a processor socket type. For this to work you need to have a x86info command available. Try install x86info package. |
dmidecode -t 17 |
Detect number of RAM slots used, their speed and size. Also try: lshw -C memory -short |
cat /dev/sndstat |
Check your sound card settings and module in use. |
powersave -b |
Get a battery information. |
free -m |
Check system’s free memory. This includes swap memory. Alternatives are: top, cat /proc/meminfo |
fdisk -l | grep GB |
Check a size of all hard drives including USB. |
提示与技巧
Command |
Description |
head -c 4 /dev/urandom | mimencode |
Generate 8 random characters. NOTE: mimencode is part of metamail package |
echo "DISPLAY=$DISPLAY xmessage -center 'abc'" | at "NOW +1hour" |
Display a GUI message in the center of your screen in hour from now. |
:(){ :|:& };: |
Fork Bomb. Simple way to crash your system. |
ccrypt mypasswords.txt |
Encrypt a file. |
ccdecrypt mypasswords.txt.cpt |
Decrypt a previous encrypted file with ccrypt. |
结束语
当您需要快速复习时,请随时参考此备忘单。这里的目标是在尝试记住某个命令时为您节省尽可能多的时间。
每个用户都应该知道的另外两个命令是 man 命令和 apropos 命令。了解这两个使用起来非常简单的命令将允许您查找某些命令附带的所有选项。 apropos
还可以很好地用作手动搜索实用程序,因此您无需经常离开终端。