Common Linux commands & usage tips and common linux commands
# Viewing operating system information #
Command:Cat/etc/issue
Comments: view the linux release version, which is applicable to suse, rethat, and centos.
Command:Cat/proc/version
Comments: more information, mainly kernel version, no specific release version information
Command:Uanme-
Comments: Mainly system information, kernel version, etc. The uname-I command is easier to view the number of system digits.
Command:File/bin/bash or file/bin/cat
Comment: the easiest and most intuitive way to view system digits
# Viewing CPU information #
Command:Cat/proc/cpuinfo
Comments: You can view the CPU model, core parameters, and so on, or view the number of CPUs (this can also be easily viewed by top and entering 1)
# Memory-related #
Command:Free-m
Comments: view memory usage and swap zone usage, one of the most common commands
Command:Sync
Comment: refresh cache to disk
Command:Echo 1>/proc/sys/vm/drop_caches
Comment: Clear pagecache
Command:Echo 2>/proc/sys/vm/drop_caches
Comment: Clear dentries & inodes
Command:Echo 3>/proc/sys/vm/drop_caches
Comment: Clear pagecache, dentries, inodes
Generally, the preceding command is used in combination with sync (synchronize data before clearing ).
Command:Top
Comment: Check the system load. Here we will focus on the input M, which can be sorted by memory usage to quickly find the process with the highest memory consumption.
# Disk information #
Command:Df
Comment: view disk storage information, df-h is the most commonly used
Command:Du
Comment: view the disk usage of a directory. You can go to the file to find out the space-consuming directory killer, du-sh(View the storage usage of all subdirectories and files in the current directory.), du-hs/home/xx/(View the space occupied by a directory,Sub-directories can be traversed), du-s/usr/local/| Sort-nr | less (sort by storage size in descending order, because the h parameter is not used because the sorting is no matter the Unit), there are also a variety of users to explore
# File-related #
Command:Ls
Comments: One of the most common skills. Sort (sort by default): ls-lt (descending time), ls-lt | tac (ascending time), and ls-lS (descending size );
Command:Ll
Comments: One of the most common features, ll will show hidden files, ls-l functions are similar (ll is the alias of ls-l), the difference is not to show hidden files, other parameters are basically the same as those of ls.
Command:Tail, head, sed, cat, more, less, grep, vi
Comments: for various command to view text content, Ren Jun chooses to use tail and head more
Command:Zip
Comments: zip package. Common usage (specify the file name and packaging directory): zip-r xx.zip test/* (-r is a recursive subdirectory)
Command:Unzip
Comments: Decompress the zip package. Common usage: unzip xxx.zip (decompress to the current directory), unzip xxx.zip-d dirxx (decompress to dirxx)
Command:Tar
Comments: unpackage | package, tar-cvf/tmp/etc.tar/etc (c-package, package all the files in the/etc directory into/tmp/etc.tar, only package, do not compress), tar-zcvf/tmp/etc.tar.gz/etc (pack all the files in the/etc directory into/tmp/etc.tar and compress them with gzip ), tar-ztvf/tmp/etc.tar.gz (t-check the compressed package file, whether to add z based on whether gzip compression is used), tar-zxvf/tmp/etc.tar.gz (x-Unpack, whether to add z to follow up whether gzip compression is used );
Command:Grep
Comment: SEARCH Command, one of the most commonly used, talk about some skills, such as search contains multiple keywords: grep-E "key1 | key2" xxx.txt or grep "key1 | key2" xxx.txt, exclude Keyword: grep-v key xxx.txt; find the row starting with abc: grep "^ abc" xxx.txt;
Command:Awk
Comments: very powerful commands (it seems that grep can do everything awk can do), data processing is a must-have technique, General Usage: awk [-F filed-separator] "commands" file.
Eg: awk-F "," '{print $1}' xx.txt (if-F is not specified, spaces are used for separation by default. The print column starts from 1 and 0 indicates the entire row, multiple print rows are separated by commas (,). For example, print $1, $2, print-line feed, and printf-do not line feed );
Generally, several major parts: BEGIN, subject, END, eg: awk 'begin {AB = 1 ;}{ AB + = 1; print $1} END {print "total =" AB }'
Awk combined with logical operation processing, eg: cat Zhenai_Sms_Status.log.2015-04-08 | awk-F' # ''BEGIN {a = 0; B = 0} {if ($5 =" M2: 0045 ") {a + = 1; print $5} else if ($5 =" UNDELIV ") {B + = 1; print \ $5 }}end {print "totala =" a "totalb =" B} '. Most applications use this mode.
Command:Find
Comment: find Files. Common Mode: find/dir/xxx-name "xxx ".
Several useful filter parameters:
Filter data file time:-atime + |-days,-mtime + |-days,-ctime + |-days,-amin + |-minutes, -mmin + |-minute,-cmin + |-minute, eg: find/tmp-mtime 2;
Filter by file size:-size + |-2 M-size 2 M (equal to 2 M),-size + 2 M (greater than 2 M ), -size-2 M (less than 2 M), eg: find/tmp-size + 2 M;
Command:Join
Comment: The core is to find the intersection of files (marked by column, the first column by default, separated by space by default, specify the delimiter with-t), the two files must be sorted first according to the same rule.
The simplest usage: join file1.txt file2.txt (use the first column as the join field and merge all columns of the two files ).
Specify the join column: join-j 1 file1.txt file2.txt (associated with the first column of each file ).
Specify join columns: join-1 2-2 3 file1.txt file2.txt (join with Column 2nd of the first file and column 3rd of the Second file ).
Specify the output column: join-o 1.1-o 1.2-o 1.3-o 2.1-o 2.2-o 2.3-e 'null'-a 1 f1.txt f2.txt (-o indicates the output, -a indicates that non-matching is also output. Replace it with-e 'null' to specify the character null ).
Output is not correlated: join-v 1 file1.txt file2.txt.
Command:Sort
Comment: Sort text by a column. Several common parameters:-t: Specify the delimiter,-u: weight,-k specifies the sort column, -r in descending order (Ascending by default), and-n in descending order (by character by default ).
General Usage: sort xxx.txt; sort-t', 'xxx.txt; sort-u xxx.txt sort-k2 xxx.txt...
Command:Tr
Comment: it seems that it is more often used for replacement (generally in the form of a single character ).
Use Cases:
Cat file.txt | tr [a-z] [A-Z]> new_file
Cat file | tr [0-9] [a-j]> new_file (replace 0-9 with a-j)
Cat file | tr-d "Snail il"> new_file (delete the snake string)
Command:Sed
Comments: replacement and deletion of characters (more powerful than tr ).
Use Cases:
Sed's/test/mytest/G' file.txt (replace test with mytest in the entire row. If there is no g tag, only the first matched test in each line is replaced with mytest ).
Sed '2d 'fiel.txt (Delete the second line ).
Sed '2, $ d' example (delete all rows from the second row to the end of the example file ).
Sed '/test/'d example (delete all rows containing test in the example file ).
There are a lot...
# Account related #
Command:Usradd
Comment: add an account. Generally, use useradd-m username-d/home/username (with the home directory created) and passwd to change the password.
Command:Userdel
Comment: delete a user. Generally, userdel xxx is used directly.
# Permission-related #
Command:Chmod
Comments: commonly used commands: use the format chmod [who] [opt] [mode] File/directory name, and add-R to represent recursive subdirectories.
Who: u (file owner); g (users in the same group); o (other users); a (All Users)
Opt operation: + (add permission);-(delete permission); = (assign permission)
Mode: r (readable); w (writable); x (executable)
-Rw --- (600)-only the owner has read and write permissions.
-Rw-r-(644)-only the owner has read and write permissions, while the owner and other users have only read permissions.
-Rwx -- (700)-only the owner has read, write, and execution permissions.
-Rwxr-xr-x (755)-the owner has the read, write, and execution permissions, while the owner and other users have only the read and execution permissions.
-Rwx-x (711)-the owner has the read, write, and execution permissions, while the owner and other users have only the execution permissions.
-Rw-(666)-all users have file read and write permissions. This approach is not advisable.
-Rwxrwxrwx (777)-all users have read, write, and execute permissions. Less desirable
Command:Chown
Comment: It is generally used to modify the owner of a file/folder (only the root account can be used).-R indicates recursive subdirectories.
General Usage: chown-R username dir (change the owner of dir to username)
Command:Mongodo
Comment: the root account is available and the open permission is easy to use. It specifies the command required to run the root permission through sudo without a password. Generally, the root password is not required.
General Usage: edit the new line, username ALL = (ALL) NOPASSWD: ALL (in this way, username can run the command using sudo without a password)
# Scheduled task related #
Command:Ll/var/spool/cron/tabs
Comment: Check the scheduled tasks of all users, so you do not need to check the scheduled tasks by account.