Linux命令总结

1. Basic Commands

uname -m Show machine processor architecture
uname -r Show kernel version in use
dmidecode -q Show hardware system components
(SMBIOS / DMI) hdparm -i /dev/hda List a disk's architecture characteristics
hdparm -tT /dev/sda Perform a test read operation on a disk
arch Show machine processor architecture
uname -m Show machine processor architecture
uname -r Show kernel version in use
dmidecode -q Show hardware system components - (SMBIOS / DMI)
hdparm -i /dev/hda List a disk's architecture characteristics
hdparm -tT /dev/sda Perform a test read operation on a disk
cat /proc/cpuinfo Display CPU info
cat /proc/interrupts Display interrupts
cat /proc/meminfo Verify memory usage
cat /proc/swaps Show which swap is being used
cat /proc/version Display kernel version
cat /proc/net/dev Display network adapters and statistics
cat /proc/mounts Display mounted filesystems
lspci -tv List PCI devices
lsusb -tv Display USB devices
date Display system date
cal 2007 Display 2007 calendar
date 041217002007.00 Set date and time - month day hour minute year.second
clock -w Save time modification to BIOS

2. Shutdown

shutdown -h now Shutdown system (1)
init 0 Shutdown system (2)
telinit 0 Shutdown system (3)
shutdown -h hours:minutes & Schedule system shutdown
shutdown -c Cancel scheduled system shutdown
shutdown -r now Reboot (1)
reboot Reboot (2)
logout Logout

3. Files and Directories

cd /home Enter '/home' directory
cd .. Go back one directory level
cd ../.. Go back two directory levels
cd Go to home directory
cd ~user1 Go to user1's home directory
cd - Go back to previous directory
pwd Show working directory
ls View files in directory
ls -F View files in directory
ls -l Show detailed list of files and directories
ls -a Show hidden files
ls *[0-9]* Show filenames and directory names containing numbers
tree Show tree structure of files and directories starting from root (1)
lstree Show tree structure of files and directories starting from root (2)
mkdir dir1 Create a directory named 'dir1'
mkdir dir1 dir2 Create two directories simultaneously
mkdir -p /tmp/dir1/dir2 Create a directory tree
rm -f file1 Delete a file named 'file1'
rmdir dir1 Delete a directory named 'dir1'
rm -rf dir1 Delete a directory named 'dir1' and its contents
rm -rf dir1 dir2 Delete two directories and their contents simultaneously
mv dir1 new_dir Rename/move a directory
cp file1 file2 Copy a file
cp dir/* . Copy all files in a directory to current working directory
cp -a /tmp/dir1 . Copy a directory to current working directory
cp -a dir1 dir2 Copy a directory
ln -s file1 lnk1 Create a symbolic link to a file or directory
ln file1 lnk1 Create a hard link to a file or directory
touch -t 0712250000 file1 Modify a file or directory's timestamp - (YYMMDDhhmm)
file file1 outputs the mime type of the file as text
iconv -l List known encodings
iconv -f fromEncoding -t toEncoding inputFile > outputFile creates a new from the given input file by assuming it is encoded in fromEncoding and converting it to toEncoding.
find . -maxdepth 1 -name *.jpg -print -exec convert "{}" -resize 80x60 "thumbs/{}" \; batch resize files in the current directory and send them to a thumbnails directory (requires convert from Imagemagick)
find / -name file1 Search for files and directories starting from '/' into root filesystem
find / -user user1 Search for files and directories belonging to user 'user1'
find /home/user1 -name \*.bin Search for files ending with '.bin' in directory '/home/user1'
find /usr/bin -type f -atime +100 Search for executable files not used in the last 100 days
find /usr/bin -type f -mtime -10 Search for files created or modified within the last 10 days
find / -name \*.rpm -exec chmod 755 '{}' \; Search for files ending with '.rpm' and define their permissions
find / -xdev -name \*.rpm Search for files ending with '.rpm', ignoring removable devices like CD-ROMs, flash drives
locate \*.ps Find files ending with '.ps' - run 'updatedb' command first
whereis halt Show location of a binary, source, or man page
which halt Show full path of a binary or executable file

5. Mount a Filesystem

mount /dev/hda2 /mnt/hda2 Mount a disk called hda2 - ensure directory '/mnt/hda2' exists
umount /dev/hda2 Unmount a disk called hda2 - exit from mount point '/mnt/hda2' first
fuser -km /mnt/hda2 Force unmount when device is busy
umount -n /mnt/hda2 Perform unmount operation without writing to /etc/mtab file - useful when filesystem is read-only or disk is full
mount /dev/fd0 /mnt/floppy Mount a floppy disk
mount /dev/cdrom /mnt/cdrom Mount a cdrom or dvdrom
mount /dev/hdc /mnt/cdrecorder Mount a cdrw or dvdrom
mount /dev/hdb /mnt/cdrecorder Mount a cdrw or dvdrom
mount -o loop file.iso /mnt/cdrom Mount a file or ISO image file
mount -t vfat /dev/hda5 /mnt/hda5 Mount a Windows FAT32 filesystem
mount /dev/sda1 /mnt/usbdisk Mount a usb flash drive or storage device
mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share Mount a windows network share

6. Disk Space

df -h Show list of mounted partitions
ls -lSr |more List files and directories sorted by size
du -sh dir1 Estimate disk space used by directory 'dir1'
du -sk * | sort -rn Show size of files and directories sorted by capacity
rpm -q -a --qf '%10{SIZE}t%{NAME}n' | sort -k1,1n Show space used by installed rpm packages sorted by size (fedora, redhat systems)
dpkg-query -W -f='${Installed-Size;10}t${Package}n' | sort -k1,1n Show space used by installed deb packages sorted by size (ubuntu, debian systems)

7. Users and Groups

groupadd group_name Create a new user group
groupdel group_name Delete a user group
groupmod -n new_group_name old_group_name Rename a user group
useradd -c "Name Surname " -g admin -d /home/user1 -s /bin/bash user1 Create a user belonging to "admin" group
useradd user1 Create a new user
userdel -r user1 Delete a user ( '-r' removes home directory)
usermod -c "User FTP" -g system -d /ftp/user1 -s /bin/nologin user1 Modify user attributes
passwd Change password
passwd user1 Change a user's password (root only)
chage -E 2005-12-31 user1 Set user password expiration date
pwck Check '/etc/passwd' file format, syntax, and existing users
grpck Check '/etc/passwd' file format, syntax, and existing groups
newgrp group_name Log into a new group to change default group for newly created files

8. File Permissions Use “+” to set permissions, use “-” to remove

ls -lh Show permissions
ls /tmp | pr -T5 -W$COLUMNS Divide terminal into 5 columns for display
chmod ugo+rwx directory1 Set read (r), write (w), and execute (x) permissions for owner (u), group (g), and others (o) on directory
chmod go-rwx directory1 Remove read, write, execute permissions for group (g) and others (o) on directory
chown user1 file1 Change ownership of a file
chown -R user1 directory1 Change ownership of a directory and all its contents
chgrp group1 file1 Change group of a file
chown user1:group1 file1 Change owner and group of a file
find / -perm -u+s List all files with SUID set in the system
chmod u+s /bin/file1 Set SUID bit on a binary file - user executing the file gets same permissions as owner
chmod u-s /bin/file1 Disable SUID bit on a binary file
chmod g+s /home/public Set SGID bit on a directory - similar to SUID, but for directories
chmod g-s /home/public Disable SGID bit on a directory
chmod o+t /home/public Set sticky bit on a file - only legitimate owner can delete the file
chmod o-t /home/public Disable sticky bit on a directory
chmod +x file_path Add execute permission for owner, group, and others
chmod -x file_path Remove execute permission for owner, group, and others
chmod u+x file_path Add execute permission for owner
chmod g+x file_path Add execute permission for group
chmod o+x file_path Add execute permission for others
chmod ug+x file_path Add execute permission for owner and group
chmod =wx file_path Add write and execute permissions for owner, group, and others; remove read permission
chmod ug=wx file_path Add write and execute permissions for owner and group; remove read permission

9. Special File Attributes Use “+” to set permissions, use “-” to remove

chattr +a file1 Allow file to be opened in append mode only for reading and writing
chattr +c file1 Allow kernel to automatically compress/decompress this file
chattr +d file1 dump program will ignore this file during filesystem backup
chattr +i file1 Make file immutable, cannot be deleted, modified, renamed, or linked
chattr +s file1 Allow file to be securely deleted
chattr +S file1 Make system immediately write modifications to disk after application writes to this file
chattr +u file1 If file is deleted, system allows you to recover it later
lsattr Show special attributes

10. Packing and Compressing Files

bunzip2 file1.bz2 Decompress a file called 'file1.bz2'
bzip2 file1 Compress a file called 'file1'
gunzip file1.gz Decompress a file called 'file1.gz'
gzip file1 Compress a file called 'file1'
gzip -9 file1 Maximum compression
rar a file1.rar test_file Create an archive called 'file1.rar'
rar a file1.rar file1 file2 dir1 Compress 'file1', 'file2' and directory 'dir1' simultaneously
rar x file1.rar Extract rar archive
unrar x file1.rar Extract rar archive
tar -cvf archive.tar file1 Create a non-compressed tarball
tar -cvf archive.tar file1 file2 dir1 Create an archive containing 'file1', 'file2', and 'dir1'
tar -tf archive.tar Show contents of an archive
tar -xvf archive.tar Extract an archive
tar -xvf archive.tar -C /tmp Extract archive to /tmp directory
tar -cvfj archive.tar.bz2 dir1 Create a bzip2 compressed archive
tar -xvfj archive.tar.bz2 Extract a bzip2 compressed archive
tar -cvfz archive.tar.gz dir1 Create a gzip compressed archive
tar -xvfz archive.tar.gz Extract a gzip compressed archive
zip file1.zip file1 Create a zip compressed archive
zip -r file1.zip file1 file2 dir1 Compress several files and directories into a zip archive
unzip file1.zip Extract a zip compressed archive

11. RPM Packages

rpm -ivh package.rpm Install an rpm package
rpm -ivh --nodeeps package.rpm Install an rpm package ignoring dependency warnings
rpm -U package.rpm Update an rpm package without changing its configuration files
rpm -F package.rpm Update an rpm package that is definitely installed
rpm -e package_name.rpm Remove an rpm package
rpm -qa Show all installed rpm packages in the system
rpm -qa | grep httpd Show all rpm packages containing "httpd" in their name
rpm -qi package_name Get specific information about an installed package
rpm -qg "System Environment/Daemons" Show rpm packages of a component
rpm -ql package_name Show file list provided by an installed rpm package
rpm -qc package_name Show configuration file list provided by an installed rpm package
rpm -q package_name --whatrequires Show dependency list for an rpm package
rpm -q package_name --whatprovides Show size occupied by an rpm package
rpm -q package_name --scripts Show scripts executed during installation/removal
rpm -q package_name --changelog Show modification history of an rpm package
rpm -qf /etc/httpd/conf/httpd.conf Confirm which rpm package provides the given file
rpm -qp package.rpm -l Show file list provided by an uninstalled rpm package
rpm --import /media/cdrom/RPM-GPG-KEY Import public key digital certificate
rpm --checksig package.rpm Verify integrity of an rpm package
rpm -qa gpg-pubkey Verify integrity of all installed rpm packages
rpm -V package_name Check file size, permissions, type, owner, group, MD5 check, and last modification time
rpm -Va Check all installed rpm packages in the system - use with caution
rpm -Vp package.rpm Verify an rpm package is not yet installed
rpm2cpio package.rpm | cpio --extract --make-directories *bin* Run executable files from an rpm package
rpm -ivh /usr/src/redhat/RPMS/`arch`/package.rpm Install a built package from an rpm source
rpmbuild --rebuild package_name.src.rpm Build an rpm package from an rpm source

12. YUM Package Updater

yum install package_name Download and install an rpm package
yum localinstall package_name.rpm Install an rpm package using your own software repositories to resolve all dependencies
yum update package_name.rpm Update all installed rpm packages in the current system
yum update package_name Update an rpm package
yum remove package_name Remove an rpm package
yum list List all installed packages in the current system
yum search package_name Search for software packages in rpm repositories
yum clean packages Clean rpm cache delete downloaded packages
yum clean headers Delete all header files
yum clean all Delete all cached packages and header files

13. deb Packages

dpkg -i package.deb Install/update a deb package
dpkg -r package_name Remove a deb package from the system
dpkg -l Show all installed deb packages in the system
dpkg -l | grep httpd Show all deb packages containing "httpd" in their name
dpkg -s package_name Get information about a specific package installed in the system
dpkg -L package_name Show file list provided by an installed deb package
dpkg --contents package.deb Show file list provided by an uninstalled package
dpkg -S /bin/ping Confirm which deb package provides the given file
APT Software Tools (Debian, Ubuntu and similar systems)
apt-get install package_name Install/update a deb package
apt-cdrom install package_name Install/update a deb package from CD
apt-get update Upgrade list of software packages
apt-get upgrade Upgrade all installed software
apt-get remove package_name Remove a deb package from the system
apt-get check Confirm dependency software repositories are correct
apt-get clean Clean cache from downloaded software packages
apt-cache search searched-package Return package names containing the search string

14. View File Contents

cat file1 View file contents from the first byte forward
tac file1 View a file in reverse starting from the last line
more file1 View contents of a long file
less file1 Similar to 'more' command, but allows reverse operations like forward operations in the file
head -2 file1 View first two lines of a file
tail -2 file1 View last two lines of a file
tail -f /var/log/messages Real-time view of content being added to a file

15. Text Processing

cat file1 file2 ... | command <> file1_in.txt_or_file1_out.txt general syntax for text manipulation using PIPE, STDIN and STDOUT
cat file1 | command( sed, grep, awk, grep, etc...) > result.txt Merge detailed description text of a file and write summary to a new file
cat file1 | command( sed, grep, awk, grep, etc...) >> result.txt Merge detailed description text of a file and write summary to an existing file
grep Aug /var/log/messages Find keyword "Aug" in file '/var/log/messages'
grep ^Aug /var/log/messages Find words starting with "Aug" in file '/var/log/messages'
grep [0-9] /var/log/messages Select all lines containing numbers in file '/var/log/messages'
grep Aug -R /var/log/* Search for string "Aug" in directory '/var/log' and subsequent directories
sed 's/stringa1/stringa2