(Transfer) A complete collection of common Linux commands (very complete!!!)

system message


  • arch Displays the processor architecture of the machine
  • uname -m shows the processor architecture of the machine
  • uname -r shows the kernel version in use
  • dmidecode -q show hardware system components - (SMBIOS/DMI)
  • hdparm -i /dev/hda lists the architectural properties of a disk
  • hdparm -tT /dev/sda performs a test read on the disk
  • cat /proc/cpuinfo display CPU info information
  • cat /proc/interrupts shows interrupts
  • cat /proc/meminfo check memory usage
  • cat /proc/swaps shows which swaps are used
  • cat /proc/version shows the version of the kernel
  • cat /proc/net/dev displays network adapters and statistics
  • cat /proc/mounts shows mounted filesystems
  • lspci -tv list PCI devices
  • lsusb -tv show usb devices
  • date display system date
  • cal 2007 displays the calendar for 2007
  • date 041217002007.00 Set date and time - month day hour minute year.second
  • clock -w save time modification to BIOS

Shutdown (shutdown, restart and logout of the system)


  • shutdown -h now shut down the system
  • init 0 shut down the system
  • telinit 0 shut down the system
  • shutdown -h hours:minutes & Shut down the system according to the scheduled time
  • shutdown -c cancels shutting down the system at a scheduled time
  • shutdown -r now restart
  • reboot reboot
  • logout

files and directories


  • cd /home into the '/home' directory'
  • cd ... Return to the previous directory
  • cd …/… Return to the previous two directories
  • cd into your home directory
  • cd ~user1 enters the personal home directory
  • cd - return to last directory
  • pwd show working path
  • ls View files in a directory
  • ls -F View files in a directory
  • ls -l displays detailed information about files and directories
  • ls -a show hidden files
  • ls [0-9] displays filenames and directory names containing numbers
  • tree Displays the tree structure of files and directories starting from the root directory
  • lstree displays the tree structure of files and directories starting from the root directory
  • mkdir dir1 creates a directory called 'dir1'
  • mkdir dir1 dir2 creates two directories at the same time
  • mkdir -p /tmp/dir1/dir2 creates a directory tree
  • rm -f file1 Delete a file called 'file1''
  • rmdir dir1 removes a directory called 'dir1'
  • rm -rf dir1 Delete a directory called 'dir1' and delete its contents
  • rm -rf dir1 dir2 delete both directories and their contents
  • mv dir1 new_dir rename/move a directory
  • cp file1 file2 copies a file
  • cp dir/* . Copy all files in a directory to the current working directory
  • cp -a /tmp/dir1 . Copy a directory to the current working directory
  • cp -a dir1 dir2 copy a directory
  • ln -s file1 lnk1 creates a soft link pointing to a file or directory
  • ln file1 lnk1 creates a physical link to a file or directory
  • touch -t 0712250000 file1 Modify the timestamp of a file or directory - (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)

file search


  • find / -name file1 starts from '/' to enter the root file system to search for files and directories
  • find / -user user1 searches for files and directories belonging to user 'user1'
  • find /home/user1 -name *.bin searches for files ending with '.bin' in directory '/home/user1'
  • find /usr/bin -type f -atime +100 Search for executable files that have not been used in the past 100 days
  • find /usr/bin -type f -mtime -10 Search for files created or modified within 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 such as CD-ROMs and shortcuts
  • locate *.ps finds files ending in '.ps' - run 'updatedb' first
  • whereis halt shows the location of a binary, source or man
  • which halt displays the full path to a binary or executable

mount a filesystem


  • mount /dev/hda2 /mnt/hda2 mounts a disk called hda2 - make sure the directory '/mnt/hda2' already exists
  • umount /dev/hda2 Unmount a disk called hda2 - first exit from the mount point '/mnt/hda2'
  • fuser -km /mnt/hda2 Force unmount when device is busy
  • umount -n /mnt/hda2 runs the unmount without writing the /etc/mtab file - useful when the file is read-only or when the 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 file system
  • mount /dev/sda1 /mnt/usbdisk mount a usb shortcut or flash device
  • mount -t smbfs -o username=user,password=pass //WinClient/share /mnt/share mount a windows network share

disk space


  • df -h displays a list of mounted partitions
  • ls -lSr |more Arrange files and directories by size
  • du -sh dir1 Estimate the disk space used by the directory 'dir1'
  • du -sk * | sort -rn Display the size of files and directories in order based on the size of the capacity
  • rpm -q -a --qf '%10{SIZE}t%{NAME}n' | sort -k1,1n Display the space used by the installed rpm packages in turn based on size (fedora, redhat-like systems)
  • dpkg-query -W -f=’ I n s t a l l e d − S i z e ; 10 t {Installed-Size;10}t InstalledSize;1 0 t {Package}n' | sort -k1,1n show space used by installed deb packages by size (ubuntu, debian-like systems)

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 the "admin" user group
  • useradd user1 creates a new user
  • userdel -r user1 delete a user ('-r' exclude home directory)
  • usermod -c "User FTP" -g system -d /ftp/user1 -s /bin/nologin user1 Modify user attributes
  • passwd Change password
  • passwd user1 Modify a user's password (only root execution is allowed)
  • chage -E 2005-12-31 user1 Set the expiration date of the user password
  • pwck checks '/etc/passwd' for file format and syntax corrections and existing users
  • grpck checks '/etc/passwd' for file format and syntax corrections and existing groups
  • newgrp group_name Login into a new group to change the default group for newly created files

File Permissions - Use '+' to set permissions, '-' to cancel


  • ls -lh show permissions
  • ls /tmp | pr -T5 -W$COLUMNS Divide the terminal into 5 columns for display
  • chmod ugo+rwx directory1 Set the directory owner (u), group (g) and others (o) to read (r), write (w) and execute (x) permissions
  • chmod go-rwx directory1 delete group (g) and others (o) read, write and execute permissions on the directory
  • chown user1 file1 Change the owner attribute of a file
  • chown -R user1 directory1 Change the owner attribute of a directory and change the attributes of all files in the directory at the same time
  • chgrp group1 file1 change the group of the file
  • chown user1:group1 file1 Change the owner and group attributes of a file
  • find / -perm -u+s lists all files in a system that use SUID control
  • chmod u+s /bin/file1 sets the SUID bit of a binary file - the user running the file is also given the same permissions as the owner
  • chmod us /bin/file1 disable SUID bit for a binary file
  • chmod g+s /home/public Set a directory's SGID bit - like SUID, but for directories
  • chmod gs /home/public disables the SGID bit for a directory
  • chmod o+t /home/public Set a file's STIKY bit - allow only legal owners to delete files
  • chmod ot /home/public disables the STIKY bit for a directory

File special attributes - use '+' to set permissions, '-' to cancel


  • chattr +a file1 only allows reading and writing files in append mode
  • chattr +c file1 allows this file to be automatically compressed/decompressed by the kernel
  • chattr +d file1 When doing a file system backup, the dump program will ignore this file
  • chattr +i file1 set to an immutable file that cannot be deleted, modified, renamed or linked
  • chattr +s file1 allows a file to be safely deleted
  • chattr +S file1 Once the application program writes to this file, the system immediately writes the modified result to the disk
  • chattr +u file1 If the file is deleted, the system will allow you to restore the deleted file later
  • lsattr displays special attributes

Pack and compress files


  • bunzip2 file1.bz2 unzip a file called 'file1.bz2'
  • bzip2 file1 compresses a file called 'file1'
  • gunzip file1.gz decompresses a file called 'file1.gz'
  • gzip file1 Compress a file called 'file1'
  • gzip -9 file1 maximum compression
  • rar a file1.rar test_file creates a package called 'file1.rar'
  • rar a file1.rar file1 file2 dir1 compress 'file1', 'file2' and directory 'dir1' at the same time
  • unrar x file1.rar decompress rar package
  • tar -cvf archive.tar file1 creates an uncompressed tarball
  • tar -cvf archive.tar file1 file2 dir1 Create an archive containing 'file1', 'file2' and 'dir1'
  • tar -tf archive.tar displays the contents of a package
  • tar -xvf archive.tar releases a package
  • tar -xvf archive.tar -C /tmp release the compressed package to the /tmp directory
  • tar -cvfj archive.tar.bz2 dir1 Create a compressed package in bzip2 format
  • tar -jxvf archive.tar.bz2 decompress a compressed package in bzip2 format
  • tar -cvfz archive.tar.gz dir1 Create a compressed package in gzip format
  • tar -zxvf archive.tar.gz decompresses a compressed package in gzip format
  • zip file1.zip file1 creates a compressed package in zip format
  • zip -r file1.zip file1 file2 dir1 Compress several files and directories into a compressed package in zip format at the same time
  • unzip file1.zip Unzip a compressed package in zip format

RPM package - (Fedora, Redhat and similar systems)


  • rpm -ivh package.rpm install an rpm package
  • rpm -ivh --nodeeps package.rpm install an rpm package and ignore dependency warnings
  • rpm -U package.rpm updates an rpm package without changing its configuration file
  • rpm -F package.rpm update a confirmed installed rpm package
  • rpm -e package_name.rpm delete a rpm package
  • rpm -qa displays all installed rpm packages in the system
  • rpm -qa | grep httpd displays all rpm packages with "httpd" in their name
  • rpm -qi package_name Get specific information about an installed package
  • rpm -qg "System Environment/Daemons" displays the rpm package of a component
  • rpm -ql package_name displays a list of files provided by an installed rpm package
  • rpm -qc package_name displays a list of configuration files provided by an installed rpm package
  • rpm -q package_name --whatrequires displays a list of dependencies with an rpm package
  • rpm -q package_name --whatprovides shows the volume occupied by an rpm package
  • rpm -q package_name --scripts show scripts executed during installation/removal
  • rpm -q package_name --changelog displays the 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 displays a list of files provided by an rpm package that has not been installed
  • rpm --import /media/cdrom/RPM-GPG-KEY import public key digital certificate
  • rpm --checksig package.rpm confirm the integrity of an rpm package
  • rpm -qa gpg-pubkey Confirm the integrity of all installed rpm packages
  • rpm -V package_name Check file size, permission, type, owner, group, MD5 check and last modification time
  • rpm -Va checks all installed rpm packages on the system - use with care
  • rpm -Vp package.rpm Confirm that an rpm package is not installed
  • rpm2cpio package.rpm | cpio --extract --make-directories bin Run executables from an rpm package
  • rpm -ivh /usr/src/redhat/RPMS/ arch/package.rpm installs a built package from an rpm source
  • rpmbuild --rebuild package_name.src.rpm build an rpm package from an rpm source

YUM Package Upgrader - (Fedora, RedHat and similar)


  • yum install package_name download and install an rpm package
  • yum localinstall package_name.rpm will install an rpm package, using your own software repository to resolve all dependencies for you
  • yum update package_name.rpm updates all installed rpm packages in the current system
  • yum update package_name updates an rpm package
  • Yum remove package_name deletes an rpm package
  • yum list lists all packages installed in the current system
  • yum search package_name searches for packages in the rpm repository
  • yum clean packages to clean up rpm cache and delete downloaded packages
  • yum clean headers delete all header files
  • yum clean all removes all cached packages and header files

DEB package (Debian, Ubuntu and similar)


  • dpkg -i package.deb install/update a deb package
  • dpkg -r package_name removes a deb package from the system
  • dpkg -l displays all deb packages installed in the system
  • dpkg -l | grep httpd displays all deb packages with "httpd" in their name
  • dpkg -s package_name Get information about a particular package already installed on the system
  • dpkg -L package_name displays a list of files provided by a deb package already installed in the system
  • dpkg --contents package.deb display a list of files provided by a package that is not yet installed
  • 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 installs/updates a deb package
  • apt-cdrom install package_name install/update a deb package from CD
  • apt-get update upgrades packages in the list
  • apt-get upgrade upgrades all installed software
  • apt-get remove package_name removes a deb package from the system
  • apt-get check confirms that the dependent software warehouse is correct
  • apt-get clean cleans the cache from downloaded packages
  • apt-cache search searched-package returns the package name containing the searched string

view file content


  • cat file1 looks forward through the contents of the file starting from the first byte
  • tac file1 view the contents of a file in reverse, starting from the last line
  • more file1 view the contents of a long file
  • less file1 similar to the 'more' command, but it allows reverse operations on files as well as forward operations
  • head -2 file1 View the first two lines of a file
  • tail -2 file1 View the last two lines of a file
  • tail -f /var/log/messages View what is added to a file in real time

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 Combine the detailed text of a file and write the introduction into a new file
  • cat file1 | command( sed, grep, awk, grep, etc…) >> result.txt Combine the detailed text of a file and write the introduction into an existing file
  • grep Aug /var/log/messages looks for the keyword "Aug" in the file '/var/log/messages'
  • grep ^Aug /var/log/messages Find words starting with "Aug" in the file '/var/log/messages'
  • grep [0-9] /var/log/messages selects all lines containing numbers in the '/var/log/messages' file
  • grep Aug -R /var/log/* Search for the string "Aug" in the directory '/var/log' and subsequent directories
  • sed 's/stringa1/stringa2/g' example.txt Replace "string1" in the example.txt file with "string2"
  • sed '/^$/d' example.txt removes all blank lines from the example.txt file
  • sed '/ *#/d; /^$/d' example.txt removes all comments and blank lines from the example.txt file
  • echo 'esempio' | tr '[:lower:]' '[:upper:]' Merge the contents of the upper and lower cells
  • sed -e '1d' result.txt excludes the first line from the file example.txt
  • sed -n '/stringa1/p' View only lines containing the word "string1"
  • sed -e 's/ *$//' example.txt deletes the last blank character of each line
  • sed -e 's/stringa1//g' example.txt removes only the word "string1" from the document and keeps the rest
  • sed -n '1,5p;5q' example.txt View the content from the first line to the fifth line
  • sed -n '5p;5q' example.txt View line 5
  • sed -e 's/00*/0/g' example.txt replaces multiple zeros with a single zero
  • cat -n file1 indicates the number of lines in the file
  • cat example.txt | awk 'NR%2==1' deletes all even-numbered lines in the example.txt file
  • echo abc | awk '{print $1}' View the first column of a line
  • echo abc | awk '{print $1,$3}' View the first and third columns of a line
  • paste file1 file2 Merge the contents of two files or two columns
  • paste -d '+' file1 file2 Merge the contents of two files or two columns, separated by "+" in the middle
  • sort file1 file2 Sort the contents of two files
  • sort file1 file2 | uniq Take out the union of two files (only one copy of duplicate lines is kept)
  • sort file1 file2 | uniq -u remove intersection, leave other lines
  • sort file1 file2 | uniq -d Take out the intersection of two files (leave only files that exist in both files)
  • comm -1 file1 file2 Compare the contents of two files and delete only the contents contained in 'file1'
  • comm -2 file1 file2 Compare the contents of two files and delete only the contents contained in 'file2'
  • comm -3 file1 file2 Compare the contents of two files and delete only the parts common to both files

Character setting and file format conversion


  • dos2unix filedos.txt fileunix.txt converts a text file format from MSDOS to UNIX
  • unix2dos fileunix.txt filedos.txt Convert a text file format from UNIX to MSDOS
  • recode ...HTML < page.txt > page.html converts a text file into html
  • recode -l | more show all allowed conversion formats

file system analysis


  • badblocks -v /dev/hda1 Check for bad blocks on disk hda1
  • fsck /dev/hda1 repair/check the integrity of the linux filesystem on the hda1 disk
  • fsck.ext2 /dev/hda1 Repair/check the integrity of the ext2 filesystem on the hda1 disk
  • e2fsck /dev/hda1 Repair/check the integrity of the ext2 filesystem on the hda1 disk
  • e2fsck -j /dev/hda1 Repair/check the integrity of the ext3 filesystem on the hda1 disk
  • fsck.ext3 /dev/hda1 Repair/check the integrity of the ext3 filesystem on the hda1 disk
  • fsck.vfat /dev/hda1 Repair/check the integrity of the fat file system on the hda1 disk
  • fsck.msdos /dev/hda1 repair/check the integrity of the dos file system on the hda1 disk
  • dosfsck /dev/hda1 repair/check integrity of dos filesystem on disk hda1

initialize a file system


  • mkfs /dev/hda1 creates a file system on the hda1 partition
  • mke2fs /dev/hda1 creates a linux ext2 file system in the hda1 partition
  • mke2fs -j /dev/hda1 Create a linux ext3 (journal) file system in the hda1 partition
  • mkfs -t vfat 32 -F /dev/hda1 Create a FAT32 file system
  • fdformat -n /dev/fd0 format a floppy disk
  • mkswap /dev/hda3 creates a swap file system

SWAP file system


  • mkswap /dev/hda3 creates a swap file system
  • swapon /dev/hda3 enables a new swap filesystem
  • swapon /dev/hda2 /dev/hdb3 enable two swap partitions

backup


  • dump -0aj -f /tmp/home0.bak /home Make a full backup of the '/home' directory
  • dump -1aj -f /tmp/home0.bak /home Make an interactive backup of the '/home' directory
  • restore -if /tmp/home0.bak restore an interactive backup
  • rsync -rogpav --delete /home /tmp to synchronize the directories on both sides
  • rsync -rogpav -e ssh --delete /home ip_address:/tmp rsync via SSH channel
  • rsync -az -e ssh --delete ip_addr:/home/public /home/local Synchronize a remote directory to a local directory via ssh and compression
  • rsync -az -e ssh --delete /home/local ip_addr:/home/public Synchronize local directory to remote directory via ssh and compression
  • dd bs=1M if=/dev/hda | gzip | ssh user@ip_addr 'dd of=hda.gz' Perform an operation of backing up the local disk on the remote host through ssh
  • dd if=/dev/sda of=/tmp/file1 backup disk contents to a file
  • tar -Puf backup.tar /home/user Perform an interactive backup of the '/home/user' directory
  • ( cd /tmp/local/ && tar c . ) | ssh -C user@ip_addr 'cd /home/share/ && tar x -p' Copy a directory content in a remote directory via ssh
  • ( tar c /home ) | ssh -C user@ip_addr 'cd /home/backup-home && tar x -p' Copy a local directory in a remote directory via ssh
  • tar cf - . | (cd /tmp/backup ; tar xf - ) Copy a directory to another place locally, retaining the original permissions and links
  • find /home/user1 -name '*.txt' | xargs cp -av --target-directory=/home/backup/ --parents Find and copy all files ending with '.txt' from one directory to another
  • find /var/log -name '*.log' | tar cv --files-from=- | bzip2 > log.tar.bz2 Find all files ending with '.log' and make a bzip package
  • dd if=/dev/hda of=/dev/fd0 bs=512 count=1 Do an action to copy the content of MBR (Master Boot Record) to the floppy disk
  • dd if=/dev/fd0 of=/dev/hda bs=512 count=1 restore MBR content from backup already saved to floppy

CD


  • cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force Empty a rewritable CD
  • mkisofs /dev/cdrom > cd.iso Create an iso image file of a CD on disk
  • mkisofs /dev/cdrom | gzip > cd_iso.gz Create a compressed CD-ROM iso image file on disk
  • mkisofs -J -allow-leading-dots -R -V “Label CD” -iso-level 4 -o ./cd.iso data_cd Create an iso image file of a directory
  • cdrecord -v dev=/dev/cdrom cd.iso Burn an ISO image file
  • gzip -dc cd_iso.gz | cdrecord dev=/dev/cdrom - burn a compressed ISO image
  • mount -o loop cd.iso /mnt/iso Mount an ISO image file
  • cd-paranoia -B rip tracks from a CD to wav files
  • cd-paranoia - "-3" rip tracks from a CD disc to wav files (parameter -3)
  • cdrecord --scanbus scan the bus to identify scsi channels
  • dd if=/dev/hdc | md5sum Verify the md5sum encoding of a device, such as a CD

Network - (Ethernet and WIFI Wireless)


  • ifconfig eth0 shows the configuration of an ethernet card
  • ifup eth0 enable an 'eth0' network device
  • ifdown eth0 disable an 'eth0' network device
  • ifconfig eth0 192.168.1.1 netmask 255.255.255.0 Control IP address
  • ifconfig eth0 promisc set 'eth0' to promiscuous mode to sniff packets (sniffing)
  • dhclient eth0 enable 'eth0' in dhcp mode
  • route -n show routing table
  • route add -net 0/0 gw IP_Gateway configura 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 IP_gateway remove static route
  • echo “1” > /proc/sys/net/ipv4/ip_forward activate ip routing
  • hostname show hostname of system
  • host www.example.com lookup hostname to resolve name to ip address and viceversa
  • nslookup www.example.com lookup hostname to resolve name to ip address and viceversa
  • ip link show show link status of all interfaces
  • mii-tool eth0 show link status of ‘eth0’
  • ethtool eth0 show statistics of network card ‘eth0’
  • netstat -tup show all active network connections and their PID
  • netstat -tupl show all network services listening on the system and their PID
  • tcpdump tcp port 80 show all HTTP traffic
  • iwlist scan show wireless networks
  • iwconfig eth1 show configuration of a wireless network card
  • hostname show hostname
  • host www.example.com lookup hostname to resolve name to ip address and viceversa
  • nslookup www.example.com lookup hostname to resolve name to ip address and viceversa
  • whois www.example.com lookup on Whois database

For details, please refer to the official sun documentation: Linux commands

おすすめ

転載: blog.csdn.net/weixin_43415201/article/details/112187310