Common Linux Shell Command

1. concrete shell commands can use to query their specific usage manual by entering help or man command.
Essentially corresponding to the terminal 2. On the / dev / tty devices on Linux, linux is through the multi-user login different / dev / tty device to complete, linux provided by default six pure command line interface '' terminal '' ( accurate to say that there should be six virtual console) to allow users to log in, you can switch by ctrl + alt + F1 ~ F6 on a physical system. After switching to one of the terminal wants to switch back to the graphical interface, you can press ctrl + alt + F7 to complete.
3. command line prompt $ represent the average user, # represents the root user.
4. A few tips:

  • Tab key: command completion, parameter
  • Ctrl + C: Force Quit current program
  • Ctrl + Z: the current program in the background, enter the command fg towed back to the front desk
  • Ctrl + A: the cursor input line begins, corresponds to the home button
  • Ctrl + E: Move the cursor input end of the line, the equivalent of the End key
  • In the terminal switches to root account login: sudo su exit enter: exit

5.linux in common wildcard

  • * Matches zero or more characters
  • ? Matches any one character
  • Any single character [list] matches the list
  • [! List] matches any single character except the characters in the list
  • [C1-c2] c1-c2 in matching any single character, such as: [0-9] [az]
  • {Strin1, string2, ...} or matching string1 string2 (or more) which is a string
  • {C1 ... c2} c1-c2 in all matching strings, such as {1 ... 10}, such as the use of wildcards batch create 5 files touch file {1 ... 5} .txt

6. commonly used shell commands summary

(1) pwd (printing working directory): Displays the current directory

  • Format: pwd [parameters]
  • Optional parameters:
  • -P display physical address, the default value
  • When the connection path -L directory, shows the connection path

(2) wc (word count): number used to calculate, using the instructions may calculate wc byte file number, the number of words or lines, without specifying the file name or file name given "-", the instruction wc reads data from the standard input device.

  • Format: wc [options] [file]
  • Optional parameters:
  • -c displays only a few bytes
  • -l displays only the number of lines
  • -w display only words
  • -m only count the number of characters
  • -L print maximum number of trekking
  • For example, the number of command statistics / bin directory: ls / bin | wc -l

(3) cd (change directory): change the current directory to the specified directory

  • Format: cd [directory name]
  • Several special parameters:
  • /: Root directory system
  • .: The current system directory
  • ...: The current directory of the parent directory
  • ~: The current user's home directory, i.e., / home / cdl directory
  • -: The last directory

(4) ls (list): list the corresponding directory listing

  • Format: ls [options] [directory name]
  • Optional parameters are:
  • -a: lists all files in the directory
  • -l: In addition to the file name, print detailed information including file permissions, size, etc.
  • -d: directory, a folder can be displayed, it does not show specific documents under
  • -h: an easily understood manner listed file size, 1k = 1048
  • -t: chronologically
  • Directory name defaults to the current directory, such as lists / home folder details of all files and directories
  • -to ls / home
  • Easy to understand the formats listed / home directory to the size "m" at the beginning of the file directory all
  • cd / home ls * m -lh

(5) mkdir (make directory): Create a directory name

  • Format: mkdir [options] [directory name]
  • Optional parameters:
  • -m: mode, set permissions
  • -p: parents, create multiple directories
  • -v: verbose, each time you create a new directory will display information
  • For example, to create a plurality of directories, creating and displaying the specific information mkdir -vp temp / text
  • 777 permission to create a directory mkdir -m 777 temp

(6) touch: for the existing file time stamp is updated to the current system time (default), their data will be retained intact; to create a new, empty file

  • Format: touch [options] [file]
  • Optional parameters:
  • -a: Change the access time only
  • -c: Do not create file
  • -d <date> / - t <date>: specified time
  • -m: change only change time
  • For example, create a file text1 touch text1

(7) rm (remove): delete the files in a directory or a directory for links to files, only delete links

  • Format: rm [options] [file or directory name]
  • Optional parameters:
  • -f: force, ignoring the file does not exist, do not give tips
  • -i: interactive, interactive deleted
  • -v: verbose, detailed display step
  • -r: recursive delete
  • For example, to delete all files with a .log suffix, asking each rm -i * .log before deleting
  • Delete / var / log / httpd / access directory and under which it is a file, folder: rm -rf / var / log / httpd / access

(8) mv (move): to move or change the file name, commonly used to back up files or directories

  • Format: mv [options] [source file or directory] [destination file or directory]
  • Optional parameters:
  • -b: front back, covered back up
  • -f: force, forced cover
  • -i: interactive, asking to overwrite
  • -u: update, the target file already exists, update the source file is newer
  • -t: target, moving a plurality of source files to a directory, the target directory former case, the source file after
  • For example, the file moves to the next a.txt test1 directory, if the file exists, Ask before overwrite Overwrite mv -i a.txt test1 (test1 directory already exists, otherwise it will execute renaming)
  • sudo mv opencv-3.3.1/ opencv-3.3.1-dev

(9) cp (copy): copy a file or directory

  • Format: cp [options] [source] [directory]
  • Optional parameters:
  • -t: target, target directory, the target directory former case, the source file after
  • -f: force, force replication
  • -i: interactive, asking whether to copy
  • -u: update, the target file already exists, update the source file is newer
  • -n: no-clobber, do not overwrite existing files
  • -s: symbolic-link, create a symbolic link to the source file, rather than copying files
  • -r: Copy the folder
  • For example, to copy all the files in the directory to test1 test2 directory, ask cp -i test1 front cover / * test2

(10) cat (concatenate): standard input file or in combination to the standard output, used to display the contents of a file or file connection, the reverse command file contents tac

  • Format: cat [option] [file]
  • Optional parameters:
  • -A:show all
  • -b: number of non-empty output lines
  • -n: row-number all output lines
  • -s: a plurality of blank lines is converted into a blank character
  • For example, after the contents of the file a.log with line numbers b.log input to this document, a plurality of blank lines into a converter output line cat -ns a.log> b.log

The number of rows in the document: (11) nl (number of lines)

  • Format: nl [options] [file]
  • Optional parameters:
  • -ba: blank line also lists the line number, similar to cat -n
  • -bt: Blank lines are not listed line number, default value
  • Digit line number field of: -w
  • -n ln: line number displayed in its own field of far left
  • -n rn: line number in the far right column of its own display, without 0
  • -n rz: The line number in the far right column of his own show, plus 0
  • For example, the contents of the file after a.log display with a line number, line number plus 0 in the right of the screen displays the line number field accounted for a median of 3
  • nl -n rz -w 3 a.log

(12) more: Functions like the cat, cat contents of the file from top to bottom, more command page after page display for easy reading, press the spacebar to turn down, press the b (back) key to display the previous page, key = output current line number, q to quit more. In addition you can also search string

  • Format: more [options] [file]
  • Optional parameters:
  • + N: n-th line from the beginning to show
  • -n: screen size is defined as the n rows
  • + / Pattern: in the file before displaying the search string pattern, the first two lines of the character string from beginning to show
  • For example, to find the file from the first row a.log "g" character string occurs, and starts the display output from the first two lines there, a predetermined number of lines per screen 5
  • more -5 +/g a.log

(13) less: to display files and output tools, very powerful

  • Format: less [options] [file]
  • Optional parameters:
  • -f: Forced Open
  • -i: ignore case
  • -N: display the line number in each column
  • -s: display continuous line empty behavior
  • Common operations:
  • / String: down search string
  • ? String: up search string
  • For example, the display of the content file a.log, the search string "hello", the following command can be less a.log / hello
  • less the difference between cat and more:
  • cat command displays the contents of the entire file, used alone without the paging function. So more often with the use of command, cat command there is the ability to combine multiple files into one file function.
  • more Function: make the picture paused while showing full one, then press the spacebar to continue to display the next page, or press q to exit the display
  • less Function: less command similar to the more command, you can also use the browser files of more than one. The difference is less command in addition press the spacebar down display the documents, you can also use the arrow keys to scroll through the file. When you want to browse the end, as long as the less command prompt: The following press q key. In fact, these three commands in addition to the cat command has a function merge document, the rest is similar to the function, but differ from the browsing habits and show the way.

(14) head: displaying beginning of the file, the default is the first 10 rows, corresponding to the tail command to display the contents of the file at the end of

  • Format: head [options] [file]
  • Optional parameters:
  • -q: hidden file name
  • -v: Displays the file name
  • -c <number of bytes>: shows the number of bytes
  • -n <line number>: the number of lines, the parameter file is displayed when a negative end of the line
  • The display front 5 and a.log row of content files b.log head -n 5 a.log b.log

(15) which: search for the location of the executable file in the path specified in the PATH variable, generally used to confirm whether the specified system software installation

  • For example, verify the correct installation of gcc, the use of which gcc

(16) whereis: positioning the file, the source code can also be searched, the unusual ability to specify search paths and alternate item search. whereis command to find out very quickly, because it is not aimless mess on disk to find, but the query in a database (/ var / lib / mlocate /). This database is automatically created linux system, contains all the information of the local file and automatically executed every day by updatedb command is updated. It is because of this database to be updated only once a day, could make the search results whereis command sometimes inaccurate, such as the file you just added may not search.

  • Format: whereis [options] [file]
  • Optional parameters:
  • -b: locate the executable file
  • -m: Positioning Help file
  • -s: locate the source code file
  • For example, the path search gcc Help file whereis -m gcc

(17) locate: whereis command and the like, and use the same database. But whereis command can only search for executable files, online help files and source code files, if you want to get a more comprehensive search results, you can use the locate command.

  • Format: locat [options] [search string]
  • Optional parameters:
  • -q: quiet, no error message is displayed
  • -n: n output up display
  • -r: regular expressions as search conditions
  • For example, all files that begin with the sh etc directory search, you can use this command to locate / etc / sh

(18) find: traversing downward in the hierarchy file, matching matching document, and performs a corresponding operation

  • Format: find [path search] [expression]
  • The default path is the current directory, the default expression is -print
  • Optional parameters:
  • -print: to standard output
  • -delete: delete the search file
  • -exec: shell command to execute the file matches the given parameters
  • -name: Find files by name
  • -type: Search by file type, file type commonly b (for block device file), c (character device file), d (directory), f (general document), L (symbolic link)
  • -perm: Find files based on file permissions
  • -user: Owner tab.
  • -mtime -n + n: file changes according to the time to find the file, -n indicates change time is less than n days, + n represents a time change is greater than n days
  • -size + 10k / -10k / 10k: Search greater than / less than / equal file 10k
  • Logical Operators: -add -or -not (): escape character in the shell parentheses characters have a special meaning, so they are used when needed in order to cause to the commonly used \
  • For example, to print the current directory of all the symbolic links ending .txt
  • find . -type l -name “.txt” -print
  • All rights under the current directory is printed php file 777
  • find . -type f -name “.php” -perm 777
  • Print all files in the current directory owned by root
  • find . -type f -user root
  • Print directory permissions, not all files 777 and 664
  • find . -type f (! -perm 777 -and ! -perm 664 )
  • Now you want to download all the c language code file, if a download a lot of trouble, you can find all the c language code file, and then write the contents of these files in a file, download the file
  • find . -name “*.c” -exec cat {} ; > all.c
  • Explanation: {} is a placeholder fact, during the execution of the find command is continually replaced with the file currently found; \ exec command is the end mark, because the back of predetermined commands must -exec; end, however; It has a special meaning in the shell, so here escaped.

(19) xargs: receiving an input can be entered from the standard, and the specific input into a parameter list

  • Format: command | xargs [options] [command]
  • Optional parameters:
  • -n: Specifies the maximum number of parameters per line
  • -d: specify the delimiter
  • 如,echo “nameXnameXnameXname” | xargs -dX -n2
  • Find all current c code files in the directory, the head office of statistics
  • find . -type f -name “*.c” | xargs wc -l
  • Output (py all files in the test2 directory) will find produced as a parameter rm, thus completely removed
  • find test2/ -name “.py” | xargs rm -rf

(20) grep: a powerful text search tool, you can use a regular expression search text, and print out the matching rows

  • Format: grep [options] pattern [file]
  • Optional parameters:
  • -c: calculating the number of times the search string that is the pattern
  • -i: ignore case
  • -n: Output line number
  • -v: invert selection, print does not match the line
  • -r: recursive search
  • -E: the template style for the common representation extends to the use, means using extended regular expressions
  • -color = auto: add keywords to find the color display
  • -o: only print match, one shown in columns
  • As will appear row root / etc / passwd file is taken out, the color display keyword-parts
  • grep “root” /etc/passwd --color=auto
  • The line of / etc / passwd file does not appear to take root and nologin out
  • grep -v “root” /etc/passwd | grep -v “nologin”
  • File in the directory contains the main current in a recursive search for files (), often used to find certain functions which located the source file
  • grep -r “main()”
  • Regular Expressions
  • Regular expression is a notation is used to identify text mode. To some extent, shell wildcard matching them with the file name and path similar, but larger scale. Regular expressions and wildcards like form and function, but there are big differences between them, especially that some of the special meaning of matching characters, such as * represents 0 or more characters in a wildcard, and regular expression string of 0 or more times prior to matching.
  • E.g., using a dictionary lookup linux system comes with a five-letter word, the third letter is j, the last letter is r
  • grep “^…j.r$” /usr/share/dict/words

(21) cut: a text widget will segmentation columns, each column can specify a separator delimiter. If a row of data contains multiple fields (multi-column), and now want to extract one or more columns in which, this time cut command will be used.

  • Format: cut [options] [filename]
  • Optional parameters:
  • -b: bytes divided units
  • -c: divided in units of characters, the front -C -5 5, then 5 before -c 5-, -c 5 5th, -c 2-5 second to fifth
  • -d: custom delimiter, the default is a tab
  • -f: custom fields, such as a column and three -f 1,3, the first column -f 1, the first three columns -f 1-3
  • -complement: extracting entire text line except -c, -f specified text
  • The extract student.txt cut -f file other columns in the first column 1 -d '' student.txt addition -complement

(22) sed: Stream Editor belong, that is, without the entire file is read into memory when editing a file, you can read line by line, and operations to complete before reading the next line, can take up less memory resources.

  • Format: sed [options] [Operation] [filename]
  • As the contents of the / etc / passwd lists and print line numbers, meanwhile, please delete the line 2-5
  • NL / etc / passwd | But '2,5d'
  • As with grep, sed also supports special meta-characters, to find and replace mode. Except that, sed using regular expressions is enclosed in / between the modes. If the regular expression should separator / to another character, such as O, as long as a character is added before the \, after the regular expressions to keep the character, this character to talk.

(23) paste: Instead of cut command, it will add one or more columns of text in a file, rather than to extract text from a file column

  • Format: paste [options] [filename]
  • Optional parameters:
  • -s: each file merge trip, rather than a separate adhesive
  • -d: custom delimiter, the default is a tab
  • As the content files telephone.txt student.txt and splicing columns, designated delimiter is: paste student.txt telephone.txt -d ':'

(24) tr (translate): often used to change a character, it can be seen as based find and replace operations character. In other words to a character conversion process from one to the other letters of the alphabet. tr replace from standard input, cut, delete characters, and writes the results to standard output.

  • Format: tr [options] SET1 SET2
  • Optional parameters:
  • -d: delete the contents of SET1 match, without replacement
  • For example, convert the input character uppercase to lowercase
  • echo ‘THIS IS CDL’ | tr ‘A-Z’ ‘a-z’
  • For example, the digital character input in the deleted
  • echo ‘THIS 123 IS CDL!’ | tr -d ‘0-9’

(25) sort: for text files and standard input from small to large sort

  • Format: sort [options] [filename]
  • Optional parameters:
  • -n: are sorted based on the length of the string
  • -k: Specifies the sort Keywords
  • -b: Ignore spaces at the beginning of each line when ordering
  • -r: i.e., in reverse order descending
  • -t: custom delimiter, the default is a tab
  • As listed up before use of space directory files 10 / usr / bin / directory
  • ls -l /usr/bin | sort -nr -k 5 | head -n 10

(26) uniq (unique): used to report or ignore files really duplicates, only the data sequence for row over the use of regular and sort order combination

  • Format: uniqu [options] [filename]
  • Optional parameters:
  • -c: label plus the number appear in front of each row
  • -d: only recurring output lines
  • -u: show only a single row
  • For example, to find all the same command / bin directory and / usr / bin directory
  • ls /bin /usr/bin | sort | unqiu -d

(27) join: two documents used to develop the field the same line of content connected. Find two files, specify the contents of the field the same line and be merged, and then output to the standard output device. Uniqu the same command, commonly used in the sorted data.

  • Format: join [options] file1 file2
  • Optional parameters:
  • -j FIFLD (equivalent to -1 FIFLD -2 FIFLD): two fields of the same matching files
  • As specified in the third field two files match field to connect two files join -1 3 -2 3 c.txt d.txt

(28) common: comparing the text file line by line, display result contains three columns: the first row is only found in the first file; row second column is found only in the second file; third column is the common line of two files, with uniqu, join the same, only with the already sorted data

  • Format: common [options] [file 1] [File 2]
  • Optional parameters:
  • -1: 1 can not output specific file line
  • -2: 2 can not output specific file line
  • -3: can not output lines common to both files
  • For example, compare the contents of two files file2.txt file1.txt and display only the contents of two files, total common -12 file1.txt file2.txt

(29) diff (differential): In the simplest case, to compare two files, if using - instead of the file parameters have to compare the contents from the standard input. diff command based on similarities and differences progressive way, compare text files. If the command specifies compare directory, it will compare the files in a directory with the same file name, without any comparison operations of its subdirectory.

  • Format: diff [options] file
  • Optional parameters:
  • -c: Context Mode
  • -u: Unified mode
  • -a: Progressive Compare
  • Recursively compare files in the directory: -r

(30) patch: is used to change the place of application to the file. It receives the output from the diff program, and is typically used to an older version of the file into a newer version of the file.

  • Use diff / patch combination has two advantages:
  • A diff file is very small, and the entire source tree size comparison
  • A diff file succinctly show the local modifications made to allow the program patches reviewers can quickly assess it.
  • Of course, diff / patch command can work in any text file, not just the source code file
  • Prepare a file for the patch diff command, diff -Naur old_file new_file> diff_file
  • Format: patch [options] patch file
  • Optional parameters:
  • -p num: Ignore layers of folders
  • -E: delete empty files found
  • -R: cancel played patch
  • Such as, generating a diff file file2.txt file1.txt and then apply the patch file command to update file1.txt
  • diff -Naur file1.txt file2.txt > patchdiff.txt
  • patch < patchdiff.txt
  • Cancellation played patch
  • patch -R < patchdiff.txt

(31) df (disk free): Check the disk linux server's file system occupancy

  • Format: df [options] file
  • Optional parameters:
  • -a: lists all file system
  • -h: easy to read display
  • -i: display inode information
  • -T: file system type
  • -t <file system type>: Displays information about the selected disk file system
  • -X <file system type>: Do not display information about the selected disk file system
  • For example, to specify the type of disk df -t ext4

(32) du (disk usage): viewing files and directories use of space

  • Format: du [options] file
  • Optional parameters:
  • -a: show directory size of individual files
  • -b: when display size in byte units
  • -k: When the display size in KB
  • -m: display size in MB
  • -s: show only total, plus the total value of listed last
  • -h: In K, M, G as a unit, to provide readability
  • -c: In addition to a separate directory display size, the display sum of all files and directories
  • For example, each display several files or directories take up the size of disk space, and statistical sum
  • du -ch file1.txt file2.txt
  • In reverse order according to the display space
  • you -h | black -nr | head -n 10

(33) time: a measuring command runtime

  • Format: time command
  • For example, the command date of the run time remains to a local file
  • {time date;} > 1.txt

(34) clear: clear screen

Guess you like

Origin www.cnblogs.com/sx66/p/11885655.html