preface

Linux can be said to be a necessary skill for front-end and back-end developers. So, AH Mu majored in Linux operating system + embedded system in university, although she did not engage in the development of operating system after graduation. But I stayed on the Internet and kept up the front and back end development, which involved server deployment, log analysis and statistics, ViM editing, and so on. Basic Linux commands can be said that we must master, or we will not pass some interviews. So ten thousand words summary Linux practical basic command, small partners collect, every day must see must knock. πŸ˜„ πŸ˜„ πŸ˜„

Because there are many categories of basic command division, I will first make up a brain map:

1, help the command

1.1 Bash Built-in Commands

Help is a very simple command and is not used very often. Because help can only get help from Shell built-in commands, but in Linux the vast majority of commands are external commands, help is of limited use. The help command can only be used for internal commands, not external commands.

The basic information of the help command is as follows. β‘  Command name: help β‘‘ English original meaning: help β‘’ Location: Shell built-in commands β‘£ Execution permission: all users β‘€ Function description: Displays help for Shell built-in commandsCopy the code

# help [Linux built-in command]

➜  ~ help cd
dhsafkdsjfakjsdgjkasdbkjdsb
fasdfdsfdsagsdgsdgsdgasdg
➜  ~ help | less
➜  ~ help | grep read
Copy the code

1.2 help command

The –help command can only be used to query external Linux commands. When you contact with a new command and do not know its attributes, you can use the –help command to view the help information about a command. This method is very simple, and the output help information is basically a brief version of the man command.

# XXX –help

➜ ~ ls -- help help information - check the ls command ls: illegal option -- - the usage: ls [- ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file]... ➜ ~ mkdir --help -- View the help information about the mkdir command mkdir: illegal option -- usage: mkdir [-pv] [-m mode] directory... ➜ ~ grep --help -- Check the help information of the grep command usage: grep [-abcDEFGHhIiJLlmnOoqRSsUVvwxZ] [-A num] [-B num] [-C[num]] [-e pattern] [-f file] [--binary-files=value] [--color=when] [--context[=num]] [--directories=action] [--label] [--line-buffered] [--null] [pattern] [file ...]Copy the code

1.3 man command

The man command can be used to query Linux built-in commands and Linux external commands. And man queries more content than the other two commands. After opening the manual, we can scroll up and down through PGUP and PGDN or up and down keys. We can press Q to exit the current page. It’s a command that we use very, very often.

Man XXX — XXX is the name of the command to view

➜ ~ man ls -- Check the help Manual of ls command ls (1) BSD General Commands Manual ls (1) NAME ls -- list directory contents SYNOPSIS ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1] [file ...]  DESCRIPTION For each operand that names a file of a type other than directory, ls displays its name as well as any requested, associated information. For each operand that names a file of type directory, ls displays the names of files contained within that directory, as well as any requested, associated information. If no operands are given, the contents of the current directory are displayed. If more than one operand is given, non-directory operands are displayed first; directory and non-directory oper- ands are sorted separately and in lexicographical order. The following options are available: -@ Display extended attribute keys and sizes in long (-l) output. -1 (The numeric digit ``one''.) Force output to be one entry per line. This is the default when output is not to a terminal. -A List all entries except for . and ... Always set for the super-user. -a Include directory entries whose names begin with a dot (.) . -B Force printing of non-printable characters (as defined by ctype(3) and current locale settings) in file names as \xxx, where xxx is the numeric value of the character in octal.Copy the code

From the above command we can see that the top left corner displays “LS(1)”, where “LS” indicates the manual name and “(1)” indicates that the manual is located in section 1. There are several chapters in the MAN manual:

We do it with Standard commands, System calls, Library functions, Special devices, and File formats Miscellaneous 8 Administrative Commands 9 others (Linux specific), which is used to store documentation for kernel routinesCopy the code
  • Internal commands: are part of the shell program. They are loaded into memory when the system starts up and are resident in memory, so the execution speed of internal commands is relatively fast.
  • External commands: file system commands, which are not part of the shell program, are programs outside the shell program. When used, they need to be read from the hard disk into memory, so the speed is relatively slow.

Distinguish internal commands from external commands: Use the type command to distinguish between internal commands and external commands.

➜ ~ type vim -- external command vim is /usr/bin/vim ➜ ~ type PWD -- internal command PWD is a shell builtin -- PWD is the shell of a built-in command ➜ ~ type CD -- Internal command CD is a shell builtin -- CD is a shell with built-in commandsCopy the code

2. Switch paths and view them

2.1 the CD command

Absolute is the command we must use; The change directory command is used to change the current working directory.

CD [dirName] where dirName can be absolute path or relative path. If the directory name is omitted, it is converted to the user’s home directory.

Note: ~ also stands for home directory,. Stands for current directory,.. Indicates the directory at the next level above the current directory location.

CD /usr/local To switch to the specified directory
➜ ~ CD/usr/local/var/weixin/amu, switch to the/usr/local/var/weixin/am directory ➜ amu git: (master) βœ— PWD - view the switch after the directory path /usr/local/var/weixin/amuCopy the code
CD ~ Switch to the home directory of the current user
➜ amu git:(master) qualify CD ~ ➜ ~Copy the code
CD – Switches to the directory you last accessed
➜ ~ CD - / usr/local/var/weixin/amuCopy the code
cd .. Switch to the directory of the previous level
➜ amu git:(master) qualify CD.. -- Switch to the upper directory ➜ weixin ➜ weixin CD.. /.. /.. / -- switch to ➜ /usr PWD /usrCopy the code
CD. Keep the current directory unchanged
➜ weixin PWD /usr/local/var/weixin ➜ weixin cd. ➜ weixin PWD /usr/local/var/weixinCopy the code
CD Command description summary
The CD command meaning
cd Switch to the current user’s home directory
cd ~ Switch to the current user’s home directory
cd – Switch to last accessed directory (back and forth between last two operations)
cd .. Switch to the previous directory
cd . Leave the current directory unchanged

2.2 the PWD command

It is still absolutely the order we must use; The :(print work directory) command is used to view the complete path of the current working directory.

PWD [–help][–version] –help Online help / –version Displays version information

➜ weixin PWD -- If you view the current path, /usr/local/var/weixin is displayedCopy the code

2.3 the ls command

It is still absolutely the order we must use; The list files command is used to print a list of the current directory. You can specify other directories (list files and subdirectories in the current working directory).

Ls [-alrtafr] [name…] –alrtAFR refers to the –name file/directory

Ls Displays files and directories in the current path. If there are files or directories, only the current file or directory is displayed
➜ amu git:(master) qualifying ls Library readme. md docs lua redis ➜ amu git:(master) qualifying ls redis/currentlimiting -- ls PHP leakybuckey.php Redistokenbuckey.php slides.php tokenbuckey.php test.php Displays a list of the contents of the specified directory counter.php leakybuckey.php redistokenbuckey.php slides.phpCopy the code
Ls -a Lists the directoryAll the documents, including"."At the beginning ofHidden files
➜ amu git:(master) qualify ls-a -- those who go onto university can go onto those who qualify. .DS_Store .git .idea Library README.md docs lua redisCopy the code
Ls -l lists the files in addition to the file name:permissions,The owner,The file size,timeAnd other details
➜ amu git:(master) qualify ls-l -- go to those who can go onto the university. Total 16 -- total 16 drwxr-xr-x 3 amu amu 96 4 18 18:23 Library -rw-r--r-- 1 amu amu 5012 5 9 18:00 README.md drwxr-xr-x 3 amu amu 96 4 17 20:12 docs drwxr-xr-x 3 amu amu 96 5 8 23:16 lua drwxr-xr-x 9 amu amu 288 5 7 22:44 redisCopy the code
ls -r Reverse order arrangementTo display the files in reverse order (originally in alphabetical order, default is A, B, C, etc.)
➜ amu git:(master) qualify ls-r redis -- test. PHP queue publis-subscribe hash geohash currentmap.phpCopy the code
Ls -t to fileModify the timeThe sorting
➜ amu git:(master) those who qualify can go onto university and onto those who qualify. Those who qualify can go onto those who qualify. Go to ➜ amu git:(master) those who qualify can go onto university. PHP (May 2, 2021 17:26) Hash (25, 2021 22:56) CurrentString (25, 2021 22:29) queue(25, 2021 22:29) bitmap.php (26, 2021 17:26) Hash (25, 2021 22:56) Queue (25, 2021 22:29)Copy the code
Ls -f appends the listed file names with a symbol; Add “*” to executable files and “/” to directories
➜ amu git:(master) qualify ls -f redis bitmap. PHP currentcurrentgallium/geohash/ hash/ publk-subscribe/queue/ test.phpCopy the code
Ls -r Displays subdirectories recursively if there are files in the directory
➜ amu Git :(Master) Qualify ls-r Library Readme. md docs Lua Redis./Library: Traits./Library/Traits: instance.php./docs: basic-knowledge ./lua: geohash.lua ./redis: Bitmap.php currentlimiting geohash hash publish-subscribe queue test.php ./redis/currentlimiting: Counter.php LeakyBucket.php RedisTokenBucket.php Slide.php TokenBucket.php test.php ./redis/geohash: GeoRedis.php Geohash.php GeohashTest.php test.php ./redis/hash: hash.php lua.php scene.php ./redis/publish-subscribe: Publish.php Subscribe.php ./redis/queue: consume.php listCase.php test.phpCopy the code
Ls -g is similar to -l, but does not list the owner information
➜ amu git:(master) qualify ls -g total 16 drwxr-xr-x 3 admin 96 4 18 18:23 library-rw-r --r-- 1 admin 5012 5 9 18:00 readme.md  drwxr-xr-x 3 admin 96 4 17 20:12 docs drwxr-xr-x 3 admin 96 5 8 23:16 lua drwxr-xr-x 9 admin 288 5 7 22:44 redisCopy the code
Ls-lh is listed in a file format that people can easily understand (e.g. 1K, 1M, 1G)
➜ amu git:(master) qualify ls-lh total 16 drwxr-xr-x 3 amu admin 96B 4 18 18:23 library-rw-r --r-- 1 amu admin 4.9k 5 9 18:00  README.md drwxr-xr-x 3 amu admin 96B 4 17 20:12 docs drwxr-xr-x 3 amu admin 96B 5 8 23:16 lua drwxr-xr-x 9 amu admin 288B 5 7 22:44 redisCopy the code
Ls-lh recursive directory All directories and file details are listed below
➜ amu git:(master) qualify ls-lr total 16 drwxr-xr-x 3 amu admin 96 4 18 18:23 library-rw-r --r-- 1 amu admin 5012 5 9 18:00 README.md drwxr-xr-x 3 amu admin 96 4 17 20:12 docs drwxr-xr-x 3 amu admin 96 5 8 23:16 lua drwxr-xr-x 9 amu admin 288 5  7 22:44 redis ./Library: total 0 drwxr-xr-x 3 amu admin 96 4 18 18:27 Traits ./Library/Traits: total 8 -rw-r--r-- 1 amu admin 1171 4 18 18:27 Instance.php ./docs: total 0 drwxr-xr-x 3 amu admin 96 4 17 20:12 basic-knowledge ./docs/basic-knowledge: Total 24-RW-r --r-- 1 amu admin 12232 4 17 20:12 PHP 小 写 (二).md./lua: total 8 -rw-r--r-- 1 amu admin 3023 5 11 09:54 geohash.lua ./redis: total 24 -rw-r--r-- 1 amu admin 5323 5 2 17:26 Bitmap.php drwxr-xr-x 8 amu admin 256 4 25 22:29 currentlimiting drwxr-xr-x 6 amu admin 192 5 9 16:31 geohash drwxr-xr-x 5 amu admin 160 4 25 22:56 hash drwxr-xr-x 4 amu admin 128 5 4 16:51 publish-subscribe drwxr-xr-x 5 amu admin 160 4 25 22:29 queue -rw-r--r-- 1 amu admin 169 5 3 01:13 test.php ./redis/currentlimiting: total 48 -rw-r--r-- 1 amu admin 1461 4 19 23:00 Counter.php -rw-r--r-- 1 amu admin 1763 4 19 23:17 LeakyBucket.php -rw-r--r-- 1 amu admin 3050 4 19 23:08 RedisTokenBucket.php -rw-r--r-- 1 amu admin 1755 4 18 22:51 Slide.php -rw-r--r-- 1 amu admin 1806 4 19 23:15 TokenBucket.php -rw-r--r-- 1 amu admin 1624 4 25 22:29 test.php ./redis/geohash: total 56 -rw-r--r-- 1 amu admin 2116 5 9 15:52 GeoRedis.php -rw-r--r-- 1 amu admin 6438 5 9 16:31 Geohash.php -rw-r--r--  1 amu admin 10058 5 9 12:51 GeohashTest.php -rw-r--r-- 1 amu admin 874 5 9 16:31 test.php ./redis/hash: total 24 -rw-r--r-- 1 amu admin 557 4 25 22:29 hash.php -rw-r--r-- 1 amu admin 933 4 25 22:50 lua.php -rw-r--r-- 1 amu admin 1834 4 25 22:43 scene.php ./redis/publish-subscribe: total 16 -rw-r--r-- 1 amu admin 667 5 4 16:51 Publish.php -rw-r--r-- 1 amu admin 1465 5 4 16:45 Subscribe.php ./redis/queue: total 32 -rw-r--r-- 1 amu admin 615 4 17 21:31 consume.php -rw-r--r-- 1 amu admin 6807 4 17 21:39 listCase.php -rw-r--r-- 1 amu admin 636 4 25 22:29 test.phpCopy the code
Ls-lt sort by modified time (in reverse order)
➜ amu git:(master) qualify ls-lt total 16-rw-r --r-- 1 amu admin 5012 5 9 18:00 readme. md drwxr-xr-x 3 amu admin 96 5 8 23:16  lua drwxr-xr-x 9 amu admin 288 5 7 22:44 redis drwxr-xr-x 3 amu admin 96 4 18 18:23 Library drwxr-xr-x 3 amu admin 96 4  17 20:12 docsCopy the code
The wildcard meaning
* Represents any number of characters
? The value contains at least one character
[] Indicates that any of the character groups can be matched
[abc] Matches any of a, B, or C
[a-f] Matches any character in the range a to F

2.4 File/Directory Search Commands

We almost always do this, we often need to look up a forgotten file on Linux; For example: configuration files, log locations, downloaded files, and so on.

Common search query commands
Find # Searches files based on specified query criteria. Which # view the location of the execution file. Whereis # views the executable location and associated files. Locate # Query the file location with the database.Copy the code
Against 2.4.1 find command

Using the find command, you can find files in a specified directory. (1) Any string before the parameter is regarded as the name of the directory to be searched. (2) If no parameter is set, subdirectories and files in the current directory are searched and all subdirectories and files found are displayed.

Find [path] -option [-print] [-exec-ok command] {} \ find [path] -option [-print] [-exec-ok command] {} \

1. Path: indicates the path of the directory searched by the find command. For example, a dot represents the current directory and a slash represents the system root directory. The -print: find command outputs the matched file to standard output. Display "\n" as delimiter, newline. 3. -exec: find executes the shell command given by this parameter on the matched files. The corresponding command is of the form 'command' {} \; Note {} and \; The space between. 4. -ok: Performs the same function as -exec, except that the shell commands given by this parameter are executed in a more secure mode. Before each command is executed, a prompt is given for the user to determine whether to execute. 5, print0: with xargs to "0" as a delimiter *. For example: the find -name 'log' | xargs rm - rf - delete the directory suffix for. The log fileCopy the code

Find Common command options:

-name filename # Search for a file named filename -perm 755 # Search for a file by execution permission -user username # Search for a username by file owner -group groupname Groupname groupname -amin +n # query files accessed in the last n minutes -atime -n +n # query files accessed within n*2 hours -cmin -n +n # On newer files -ctime -n +n # on newer files -n on newer files -n on newer files -n on newer files -n on newer files -n on newer files -n on newer files -n on newer files -n on newer files -n on newer files -n on newer files -n on newer files -n on newer files -n on newer files Type XXX # query the specified file type XXX as follows: β‘  b: block device device file β‘‘ C: character device file β‘’ D: directory β‘£ P: pipe β‘€ f: common file β‘₯ L: symbol link ⑦ s: Socket - size n [c] # check of length n block [or] n bytes of file - # make finding the depth first before entering into subdirectories exhausts the directory - follow # if you meet a symbolic link file, will track link referring to a directory file - prune # ignoredCopy the code

View files ending in.php in the current directory

➜ amu git:(master) qualify find.name '*.php'./redis/hash/hash.php./redis/hash/lua.php./redis/hash/ sect.php ./redis/currentlimiting/RedisTokenBucket.php ./redis/currentlimiting/TokenBucket.php ./redis/currentlimiting/Counter.php  ./redis/currentlimiting/LeakyBucket.php ./redis/currentlimiting/test.php ./redis/currentlimiting/Slide.php ./redis/test.php ./redis/geohash/GeoRedis.php ./redis/geohash/GeohashTest.php ./redis/geohash/test.php ./redis/geohash/Geohash.php ./redis/queue/listCase.php ./redis/queue/test.php ./redis/queue/consume.php ./redis/Bitmap.php ./redis/publish-subscribe/Subscribe.php ./redis/publish-subscribe/Publish.php ./Library/Traits/Instance.phpCopy the code

View the file at the end of.php in the current directory, and the output is displayed as a newline: default newline

➜ amu git:(master) qualify find. -name '*.php' -print./redis/hash/hash.php./redis/hash/lua.php./redis/hash/ sect.php ./redis/currentlimiting/RedisTokenBucket.php ./redis/currentlimiting/TokenBucket.php ./redis/currentlimiting/Counter.php  ./redis/currentlimiting/LeakyBucket.php ./redis/currentlimiting/test.php ./redis/currentlimiting/Slide.php ./redis/test.php ./redis/geohash/GeoRedis.php ./redis/geohash/GeohashTest.php ./redis/geohash/test.php ./redis/geohash/Geohash.php ./redis/queue/listCase.php ./redis/queue/test.php ./redis/queue/consume.php ./redis/Bitmap.php ./redis/publish-subscribe/Subscribe.php ./redis/publish-subscribe/Publish.php ./Library/Traits/Instance.phpCopy the code

View the file at the end of.php in the current directory. The output is not displayed on a new line

➜ amu git:(master) qualify find. -name '*.php' -print0 ./redis/hash/hash.php./redis/hash/lua.php./redis/hash/scene.php./redis/currentlimiting/RedisTokenBucket.php./redis/curre ntlimiting/TokenBucket.php./redis/currentlimiting/Counter.php./redis/currentlimiting/LeakyBucket.php./redis/currentlimit ing/test.php./redis/currentlimiting/Slide.php./redis/test.php./redis/geohash/GeoRedis.php./redis/geohash/GeohashTest.php ./redis/geohash/test.php./redis/geohash/Geohash.php./redis/queue/listCase.php./redis/queue/test.php./redis/queue/consume .php./redis/Bitmap.php./redis/publish-subscribe/Subscribe.php./redis/publish-subscribe/Publish.php./Library/Traits/Insta nce.php%Copy the code

Query files ending in.php with the -exec parameter and use ll to query more file information

➜ amu git:(master) qualify find. -name '*.php' -exec ls -ll {} \; -rw-r--r-- 1 amu admin 557 4 25 22:29 ./redis/hash/hash.php -rw-r--r-- 1 amu admin 933 4 25 22:50 ./redis/hash/lua.php -rw-r--r-- 1 amu admin 1834 4 25 22:43 ./redis/hash/scene.php -rw-r--r-- 1 amu admin 3050 4 19 23:08 ./redis/currentlimiting/RedisTokenBucket.php -rw-r--r-- 1 amu admin 1806 4 19 23:15 ./redis/currentlimiting/TokenBucket.php -rw-r--r-- 1 amu admin 1461 4 19 23:00 ./redis/currentlimiting/Counter.php -rw-r--r-- 1 amu admin 1763 4 19 23:17 ./redis/currentlimiting/LeakyBucket.php -rw-r--r-- 1 amu admin 1624 4 25 22:29 ./redis/currentlimiting/test.php -rw-r--r-- 1 amu admin 1755 4 18 22:51 ./redis/currentlimiting/Slide.php -rw-r--r-- 1 amu admin 169 5 3 01:13 ./redis/test.php -rw-r--r-- 1 amu admin 2116 5 9 15:52 ./redis/geohash/GeoRedis.php -rw-r--r-- 1  amu admin 10058 5 9 12:51 ./redis/geohash/GeohashTest.php -rw-r--r-- 1 amu admin 874 5 9 16:31 ./redis/geohash/test.php  -rw-r--r-- 1 amu admin 6438 5 9 16:31 ./redis/geohash/Geohash.php -rw-r--r-- 1 amu admin 6807 4 17 21:39 ./redis/queue/listCase.php -rw-r--r-- 1 amu admin 636 4 25 22:29 ./redis/queue/test.php -rw-r--r-- 1 amu admin 615 4 17 21:31 ./redis/queue/consume.php -rw-r--r-- 1 amu admin 5323 5 2 17:26 ./redis/Bitmap.php -rw-r--r-- 1 amu admin 1465 5 4  16:45 ./redis/publish-subscribe/Subscribe.php -rw-r--r-- 1 amu admin 667 5 4 16:51 ./redis/publish-subscribe/Publish.php -rw-r--r-- 1 amu admin 1171 4 18 18:27 ./Library/Traits/Instance.phpCopy the code

View files in the current directory that end in.php and begin with h.*

➜ amu git:(master) qualify find. -name '*.php' -a -name 'h*' -exec ls -ll {} \; -rw-r--r-- 1 amu admin 557 4 25 22:29 ./redis/hash/hash.phpCopy the code

View files in the current directory that end in.php or *.lua

➜ amu git:(master) qualify find. -name "*.php" -o-name "*.lua"./redis/hash/hash.php./redis/hash/lua.php./lua/geohashCopy the code

View files that begin with a capital letter

➜ amu git: (master) βœ—. Find -name "[a-z] *" - print. / redis/currentlimiting RedisTokenBucket. PHP ./redis/currentlimiting/TokenBucket.phpCopy the code

View the file with permission 755

➜ amu git:(master) qualify find./ redis-perm 755-print-exec ls-ll {} \; ./redis/queue total 32 -rw-r--r-- 1 amu admin 615 4 17 21:31 consume.php -rw-r--r-- 1 amu admin 6807 4 17 21:39 listCase.php -rw-r--r-- 1 amu admin 636 4 25 22:29 test.php ./redis/publish-subscribe total 16 -rw-r--r-- 1 amu admin 667 5 4 16:51 Publish.php -rw-r--r-- 1 amu admin 1465 5 4 16:45 Subscribe.phpCopy the code

Query all files in the current directory

➜ amu git:(master) qualify find.-type f./redis/hash/hash.php./redis/hash/lua.php./redis/hash/ sect.phpCopy the code

Example Query all directories in the current directory

➜ amu git:(master) qualify find.-type d./redis/geohash./redis/queue./redis/ publk-subscribe./luaCopy the code

View files between 5K and 100K in the current directory

➜ amu git: (master) βœ— find. - size + 5 k - a - size - 100 k. / redis/geohash GeohashTest. PHP. / redis/geohash geohash. PHP /redis/ bitmap.php./docs/basic-knowledge/PHPCopy the code
Which command 2.4.2

Summary: Searches for the location of a system command in the PATH specified by the PATH variable and returns the first search result. That is, using the which command, you can see whether a system command exists and which location of the command was executed.

-n Specifies the length of the file name. The specified length must be longer than or equal to the longest file name in all files. The -p parameter is the same as the -n parameter, but contains the file path. -w Specifies the width of the output field. -v Displays the version informationCopy the code

Which [file…] — File: name of the executable file

Example: ➜ ~ which bash /bin/bash -- #bash Absolute path to the executable ➜ ~ which PWD MAC terminal: PWD: Shell built-in command -- shell built-in command centos terminal: /bin/ PWD -- # PWD Absolute path to the executable program ➜ ~ which CD MAC terminal: CD: Shell built-in command centos Terminal: /usr/bin/which no CD.... Note: you may find that the centos system prompts you to find the CD command. This is because CD is built into bash! ➜ ~ which which MAC terminal: which: Shell Built-in Command centos terminal: Alias which = 'alias | / usr/bin/which - tty - only - read - alias - show - dot - show - tilde'/usr/bin/which note: The command alias is the command alias. The command alias is the command alias. Type which equals the command that followsCopy the code
2.4.3 whereis command

Overview: This directive looks for eligible files in a specific directory; Can only be used to search for program names, such as raw code, binary files, or MAN files. It is very fast because Linux records everything in the system in a database file, and using whereis looks for data in the database rather than traversing the hard disk like the find command does, which is efficient.

-b finds only binary files. -f Does not display the path name before the file name. -m Searches only description files. -s finds only the original code file. -b Specifies the path to search for executable files. -m Specifies the path to search for help files. -s Specifies the path to search for source code files.Copy the code

Whereis [-bfmsu][-b < directory >… [-m < directory >…] [-s < directory >…] [file]…

Example: ➜ ~ whereis grep -- Query all grep related files /usr/bin/grepCopy the code
2.4.4 the locate command

Summary: Locate command and find find file search function is similar, but locate through the update program will be hard disk all files and directory information to establish an index database, in the implementation of loACte directly find the index, the query speed will be faster, index database is generally managed by the operating system, However, you can force the system to modify the index database immediately by issuing an UPDATE directly.

Command parameters: -b, --basename -- matches only the basic name of the pathname -c, --count -- prints only the number found -e, --existing -- prints only the entry of the current existing file -1 -- if so starts safe mode. In safe mode, users do not see files that are not viewable by permissions. This slows things down because Locate has to go to the actual file system to get the permissions for the file. -q, --quiet --quiet mode, -l, --limit, -n limit -- limits the output (or count) to a limit of entries -r, --regexp regexp -- specifies the name of the data store using the basic regular expression -o. -h, --help -- Displays auxiliary help -v, --version -- displays version informationCopy the code

Locate [-d][–help][–version]

Example: ➜ ~ locate PWD /bin/pwd /etc/.pwd.lock /usr/bin/pwdx /usr/include/pwd.hCopy the code

3. The file | directory operations

3.1.1 touch command

Overview: The Linux touch command is used to modify the time properties of a file or directory, including access time and change time. If the file does not exist, the system creates a new file.

-a or --time=atime or --time=access or --time=use Changes the read time record of the file. -c If the destination file does not exist, no file is created. -d specifies the date and time, and can be used in a variety of formats. -f This parameter is ignored and only resolves compatibility issues with the BSD version of the touch directive. -m or --time=mtime or --time=modify Only changes the change time. -r Sets the date and time of the specified document or directory to the same as that of the reference document or directory. -t Uses the specified date and time instead of the current time.Copy the code

Touch [options]… The file…

Example 1: Create a file that does not exist
➜ weixin touch test1.log test2.log -- create ➜ weixin ll total 0 -rw-r--r-- 1 amu admin 0B 5 17 12:57 test1.log -rw-r--r-- 1 amu admin 0B 5 17 12:57 test2.log ➜ weixin touch -c test3.log -- Do not create files that do not exist ➜ weixin ll total 0 -rw-r--r-- 1 amu admin 0B 5 17 12:57 test1.log -rw-r--r-- 1 amu admin 0B 5 17 12:57 test2.logCopy the code
Example 2: Update the timestamp of a file (test1 is the same as test2)
➜ weixin ll total 0 drwxr-xr-x 10 AMu admin 320B 5 9 18:00 AMu -- Before modification, the time is 18:00 -rw-r--r-- 1 amu admin 0B 5 17 12:57 Test1. log -rw-r--r-- 1 amu admin 0B 5 17 12:57 test2.log ➜ weixin touch -r test1.log amu -- Run ➜ weixin ll total 0 Drwxr-xr-x 10 AMu admin 320B 5 17 12:57 AMu -- Modified time 12:57-rw-r --r-- 1 AMu admin 0B 5 17 12:57 test1.log -rw-r--r-- 1 amu admin 0B 5 17 12:57 test2.logCopy the code
Example 3: Specify the timestamp of the file
➜ weixin ll total 0 -rw-r--r-- 1 amu admin 0B 5 17 12:57 test1.log -rw-r--r-- 1 amu admin 0B 5 17 12:57 test2.log -- Unspecified 12:57 ➜ weixin touch -t 202105171305.20 test2.log -- Run the ➜ weixin ll total 0 -rw-r--r-- 1 amu admin 0B 517 12:57 Test1.log -rw-r--r-- 1 amu admin 0B 5 17 13:05 test2.log -- 13:05 after the specified time note: -t time uses the specified time value time as the new value of the corresponding timestamp of the specified file. Format: [[CC]YY]MMDDhhmm[.ss] β‘  CC refers to the first two digits of the year, i.e. "century"; β‘‘ YY is the last two digits of the number of years, i.e. the number of years in a certain century; β‘’ MM is the number of months; DD is the number of days; Hh is the number of hours. Mm is minutes; SS is the number of seconds (0 to 61, which can handle leap seconds); The time component is a time in the time zone specified by the environment variable TZ. The system is restricted after January 1, 1970Copy the code

3.1.2 the mkdir command

Overview: Using the Linux mkdir(full English: make directory) command, you can create a directory with the specified name. The user who creates the directory must have the write permission in the current directory, and the specified directory name cannot be an existing directory in the current directory.

-p Directory does not exist. The system automatically creates non-existent directories. That is, multiple directories can be created at a time. -m Sets permissions (similar to chmod) -v Displays information every time a new directory is createdCopy the code

Mkdir [-pv] dirName

Example 1: Create an empty directory
➜  weixin mkdir test_1
➜  weixin ll
total 0
drwxr-xr-x   2 amu  admin    64B  5 17 21:37 test_1
Copy the code
Example 2: Create multiple directories recursively
➜ weixin mkdir -p test_2/test_22/test_222 -- create test_2 directory, create test_22 directory, ➜ weixin ls -lr total 0 drwxr-xr-x 3 amu admin 96B 5 17 21:39 test_2./test_2: total 0 drwxr-xr-x 3 amu admin 96 5 17 21:39 test_22 ./test_2/test_22: total 0 drwxr-xr-x 2 amu admin 64 5 17 21:39 test_222Copy the code
Example 3: Create a directory with permission 775
➜ weixin mkdir -m 775 test_3 ➜ weixin ll total 0 DRWXRWXR -x 2 amu admin 64B 5 17 21:42 test_3Copy the code
Example 4: Display information when creating a new directory
➜  weixin mkdir -v test_4
mkdir: created directory 'test_4'
Copy the code

3.1.3 the rm command

Overview: Linux rm (full English: remove) This command is used to delete one or more files or directories in a directory. It can also delete all files and subdirectories in a directory. For linked files, only the link is deleted, and the original files remain unchanged.

The command parameters are as follows: -i (--interactive) Interactive deletion. -f (--force) Deletes files one by one without confirmation, even if the original file property is set to read-only. This is equivalent to forcing -r (--recursive) to recursively delete all directories and subdirectories listed in the argumentCopy the code

Rm [-ifr] file…

Example 1: The system asks whether to delete
➜ weixin rm test1.log -- the MAC side is deleted without warning ➜ weixin ls amu test2.log test_1 test_2 test_3 test_4 root@localhost ~]# rm test1.log -- The centos VM terminal prompts rm: Do YOU want to delete the test1.log file? yCopy the code
Example 2: Unable to delete a directory
➜ weixin rm test_1 -- MAC client rm: test_1: is a directory [root@localhost ~]$rm test/ -- centos client rm: cannot remove `test/': Is a directoryCopy the code
Example 3: The system does not prompt you to delete files forcibly
➜  weixin rm -f test2.log
➜  weixin ll
total 0
drwxr-xr-x   2 amu  admin    64B  5 17 21:37 test_1
drwxr-xr-x   3 amu  admin    96B  5 17 21:39 test_2
drwxrwxr-x   2 amu  admin    64B  5 17 21:42 test_3
drwxr-xr-x   2 amu  admin    64B  5 17 21:44 test_4
Copy the code
Example 3: Delete all files without recovery
➜ weixin rm -rf # Note: Any delete operation is a dangerous action, use with cautionCopy the code

3.1.4 mv command

Overview: Linux MV (full: remove) is used to move a file or rename a file or move a file or directory to another location.

Command parameters: -b If a file or directory exists, a backup is automatically created before overwriting. -I If the file or directory has the same name, the system asks whether to overwrite the file or directory. -f force to overwrite the specified file or directory without asking. -n Do not overwrite any existing file or directory. -u Move the file only when the specified file is newer than the target file or the target file does not existCopy the code

[option] Source file or directory Destination file or directory

Example 1: Rename a file
➜ test_1 ll total 0 -rw-r--r-- 1 amu admin 0B 5 17 22:20 test1.log-rw-r --r-- 1 amu admin 0B 5 17 22:20 test2.log ➜ Md ➜ test_1 ll total 0 -rw-r--r-- 1 amu admin 0B 5 17 22:20 test1.md-rw-r --r-- 1 amu admin 0B  5 17 22:20 test2.logCopy the code
Example 2: File movement
➜ test_1 ll total 0 -rw-r--r-- 1 amu admin 0B 5 17 22:20 test1.log-rw-r --r-- 1 amu admin 0B 5 17 22:20 test2.log ➜ Md ➜ test_1 ll total 0 -rw-r--r-- 1 amu admin 0B 5 17 22:20 test1.md-rw-r --r-- 1 amu admin 0B 5 17 22:20 test2.log ➜ test_1 mv test1.md.. /test_2 ➜ test_1 ll total 0 -rw-r--r-- 1 amu admin 0B 5 17 22:20 test2.log ➜ test_1 ll.. /test_2 total 0 -rw-r--r-- 1 amu admin 0B 5 17 22:20 test1.md drwxr-xr-x 3 amu admin 96B 5 17 21:39 test_22Copy the code

3.1.5 cp command

Summary: Linux CP copies a source file to a destination file or multiple source files to a destination directory.

Command parameters: -a This option is usually used when directories are copied. It preserves links, file properties, and all contents in the directory. -d Preserves links when directories are copied. -f overwrites the existing target file without prompting -p In addition to copying the contents of the file, the modification time and access rights are also copied to the new file. -r Copies the directory and all items in the directory. -l Does not copy files, but only generates link filesCopy the code

# syntax format: cp / – rf source file | | to the target file directory

Example 1: Copy a single file to a destination directory. The file does not exist in the destination file
➜ test_1 ll total 0 -rw-r--r-- 1 amu admin 0B 5 17 22:20 test2.log ➜ test_1 cp test2.log.. /test_2 ➜ test_1 ll.. /test_2 total 0 -rw-r--r-- 1 amu admin 0B 5 17 22:35 test2.log # test_2 total 0 -rw-r--r-- 1 amu admin 0B 5 17 22:35 test2.log #Copy the code
Example 1: Copy an entire directory
➜  weixin ll
total 0
drwxr-xr-x   3 amu  admin    96B  5 17 22:27 test_1
drwxr-xr-x   5 amu  admin   160B  5 17 22:35 test_2
➜  weixin cp -a test_2 test_1
➜  weixin ll test_1
total 0
-rw-r--r--  1 amu  admin     0B  5 17 22:20 test2.log
drwxr-xr-x  5 amu  admin   160B  5 17 22:35 test_2
Copy the code

3.1.6 rmdir commands

Overview: Linux rmdir (remove directory) This command deletes one or more subdirectory items from a directory. When deleting a directory, you must have the write permission on the parent directory.

-p Recursively deletes directory dirname. If the parent directory is empty after the subdirectory is deleted, the subdirectory is also deletedCopy the code

Rmdir [-p] dirName

Example 1: Deleting a non-empty directory
➜ weixin tree test_1 test_1 β”œ ─ ─ test2. Log β”” ─ ─ test_2 β”œ ─ ─ test1. Md β”œ ─ ─ test2. Log β”” ─ ─ test_22 β”” ─ ─ test_222 3 directories, 3 files ➜ weixin rmdir test_1 RMdir: test_1: Directory not empty # Note: the rmdir Directory name command cannot directly delete a non-empty DirectoryCopy the code
Example 2: If the parent directory is empty after the subdirectory is deleted, delete it as well
➜ unscied press, unscied press, unscied press, unscied press, unscied press, unscied press, unscied press, unscied press, unscied press, unscied press, unscied press, unscied press, unscied press, unscied press, unscied press, unscied press weixin tree test_1 test_1 [error opening dir] 0 directories, 0 filesCopy the code

3.1.7 the ln command

Overview: Linux ln (Link Files) is a super important command; Its function is to create a synchronous link for a file in another location. When we need in different directories, use the same file, we don’t need to put in every need directory a file must be the same, we just in a fixed directory, put the file, and then in the other directory with the ln command links (link) it can, need not repeat take up disk space.

Functional analysis: The hard link and the soft link are symbolic links. The hard link means that a file can have multiple names, and the soft link means that a special file is generated, which points to the location of another file. The hard link is in the same file system. Soft links, on the other hand, can span different file systemsCopy the code
Hard link description:
  • (1) Hard links exist in the form of file copies; butDoes not take up real space
  • β‘‘ Not allowed to giveHard links are created for directories
  • β‘’ Hard links are only available inSame file SystemTo create
Soft link description:
  • β‘  Soft link, toThe pathExists in the form of. Similar to Windows operating systemA shortcut
  • β‘‘ Soft links canCross file systemHard links do not
  • β‘’ Soft links can be on oneThere is notheThe file namelink
  • β‘£ Soft links can bedirectorylink
Matters needing attention:
  • β‘  The ln command doesEnsure the synchronization of linked files in each place; No matter what you change, the rest of the file willThe same thing happens
  • β‘‘ Links are divided into soft links and hard links; Soft links areLn -s Source file Target file, it will only generate one at the location you selectImage of file.Does not take up disk space; Hard linksLn Source file Target fileIt will generate a file of the same size as the source file in the location you select, regardless of whether the soft link or hard link, the file remainsSynchronous change
  • β‘’ If more than two files or directories are specified when you run the ln command to link a file or directory, and the final directory already exists, all the files or directories specified are copied to this directory. Otherwise, an error message will be displayed
Necessary parameters for command parameters: β‘  -b delete to overwrite previously established links β‘‘ -d Allow super users to create hard links for directories β‘’ -f (force) Enforce the -i interaction mode β‘€ -n treat symbolic links as general directories β‘₯ -s soft links (symbolic links) ⑦ -v Display detailed processing processCopy the code

Ln [parameter] [source file or directory] [destination file or directory]

Example 1: Creating a soft link to a file
➜ test_4ll total 0-rw-r --r-- 1 amu admin 0B 5 18 13:05 log2021. TXT ➜ test_4 ln -s log2021. TXT link2021 ➜ test_4ll total 0 lrwxr-xr-x 1 amu admin 11B 5 18 13:05 link2021 -> log2021.txt -rw-r--r-- 1 amu admin 0B 5 18 13:05 log2021.txt # Note: If log2021. TXT is deleted or lost, link2021 will also be invalidCopy the code
Example 2: Creating a hard link to a file
➜ test_4ll total 0 lrwxr-xr-x 1 amu admin 11B 5 18 13:05 link2021 -> log2021.txt -rw-r--r-- 1 amu admin 0B 5 18 13:05 Log2021. TXT ➜ test_4 ln log2021. TXT ln2021 ➜ test_4 ll total 0 lrwxr-xr-x 1 amu admin 11B 5 18 13:05 link2021 -> Log2021. TXT -rw-r--r-- 2 amu admin 0B 5 18 13:05 ln2021-rw-r --r-- 2 amu admin 0B 5 18 13:05 log2021. TXT Hard links ensure that the properties of ln2021 and log2021. TXT are consistentCopy the code
Example 3: Delete and rebuild the link original file
➜ test_4ll total 0 lrwxr-xr-x 1 AMu admin 11B 5 18 13:05 link2021 -> log2021.txt -rw-r--r-- 2 amu admin 0B 5 18 13:05 Ln2021-rw-r --r-- 2 AMu admin 0B 5 18 13:05 log2021. TXT ➜ test_4 rm -rf log2021. TXT ➜ test_4 ll total 0 lrwxr-xr-x 1 AMu Admin 11B 5 18 13:05 link2021 -> log2021.txt -rw-r--r-- 1 AMu admin 0B 5 18 13:05 ln2021 ➜ test_4 touch log2021.txt ➜ test_4 ll total 0 lrwxr-xr-x 1 amu admin 11B 5 18 13:05 link2021 -> log2021.txt -rw-r--r-- 1 amu admin 0B 5 18 13:05 Ln2021-rw-r --r-- 1 amu admin 0B 5 18 13:17 log2021. TXT ➜ test_4 vim log2021. TXT ➜ test_4 cat log2021. TXT Cat ln2021 ➜ test_4 cat link2021 However, the soft connection file in the centos terminal keeps blinking red, indicating that the source file does not exist. 2 After the source file is rebuilt, the soft connection file is displayed normally. Change the content of the source file, the hard link file is not affected, or retain the empty file content before deletion; The hard link becomes invalid after the source file is deletedCopy the code

3.2.1 Setting File directory PermissionschmodThe command

Overview: Linux chmod (change mode) is used to change access to files or directories on the Linux system. Use it to control access to files or directories.

Function analysis: β‘  File directory access permissions are divided into three types: read (read-only), write (write), execute (execute) β‘‘ file directory call permissions are divided into three types: 3 There are three groups of file and directory access permissions. Each Group has three groups: read/write and execute permissions of the file owner. Read/write and execute permissions of the owner group. Read/write and execute permissions of other system usersCopy the code
Permission Description:
➜ weixin ll-al drwxr-xr-x 10 AMu admin 320B 5 17 12:57 AMU-RW-r --r-- 1 amu admin 0B 5 18 13:05 log2021.txt # β‘  The first character is a line to indicate: a non-directory file; If it starts with d, it indicates that a directory contains 9 characters from the second character to the tenth character. Each group of three characters represents the following: permissions of three groups of users on files or directories. W only write; U (user) indicates the owner of the file/directory. G (group) indicates that the owner belongs to the same group as the owner of the file. O (others) indicates that the owner belongs to other people. β‘‘ -f Do not display error message if the file permission cannot be changed β‘’ -r make the same permission change for all files and subdirectories in the current directory (i.e. change one by one in a recursive way) β‘  R read permission, represented by 4 β‘‘ W write permission, represented by 2 β‘’ X execute permission, represented by 1 β‘£ - delete permission, represented by 0Copy the code
There are two permission setting schemes:
(1) the text set method (symbol) chmod [who] [+ | - | =] [mode] file name + add permissions to the user specified types - delete specify the permissions of the user type = Settings of the specified user permissions Settings, Chmod [mode] = 0; 1 indicates the execution permission. 2 indicates the write permission. 4 indicates the read permission. The owner's permissions are expressed in numbers: the sum of the three permissions bits of the owner; RWX, that is, 4+2+1 = 7, is expressed in numbers: the sum of the number of the permission bit of the user group; Rw -, that is, 4+2+0 = 6 Number of other user permissions: the sum of the number of other user permissions; R minus x, which is 4 plus 0 plus 1 is 5Copy the code

Chmod [-cfvr] [–help] [–version] mode file…

Example 1: Add the execute permission to all user groups of a file
➜ test_4ls-al-rw-r --r-- 1 amu admin 16 5 18 13:18 log2021. TXT ➜ test_4 chmod a+x log2021. TXT ➜ test_4ls-al -rwxr-xr-x 1 amu admin 16 5 18 13:18 log2021. TXTCopy the code
Example 2: Deleting file permissions
➜ test_4ls-la log2021. TXT -rwxr-xr-x 1 amu admin 16 5 18 13:18 log2021. TXT ➜ test_4 chmod a-x log2021. TXT ➜ test_4 ls -ll log2021. TXT -rw-r--r-- 1 amu admin 16 5 18 13:18 log2021. TXTCopy the code
Example 3: Add permissions to all files in a directory and its subdirectories
➜ weixin ls -al test_4 lrwxr-xr-x 1 AMu admin 11 5 18 13:05 link2021 -> log2021.txt -rw-r--r-- 1 amu admin 05 18 13:05 Ln2021-rw-r --r-- 1 amu admin 16 5 18 13:18 log2021. TXT ➜ weixin chmod -r a+x test_4 ➜ weixin ls -al test_4 lrwxr-xr-x 1  amu admin 11 5 18 13:05 link2021 -> log2021.txt -rwxr-xr-x 1 amu admin 0 5 18 13:05 ln2021 -rwxr-xr-x 1 amu admin 16 5 Test_4: assign permissions to all files and subdirectories in test_4: assign permissions to all files and subdirectories in test_4Copy the code

3.3.1 Viewing the File Sizedu -xxThe command

Linux DU (Disk Usage) is used to display the disk usage of each file and directory

Parameter Description:
β‘  -a (-all) Displays the size of an individual file in a directory. β‘‘ -b (-bytes) Displays the size of a directory or file in bytes. β‘’ -C (--total) Displays the size of an individual directory or file in bytes. β‘£ -h (--human-readable) in units of K, M, G, β‘€ -m (--megabytes) in 1MB β‘₯ -s (-- Summarize) only display the total number of ⑦ --max-depth=< directory layers > directories that exceed the specified number of layers and are ignoredCopy the code

Du [-abch] [file]

Example 1: Display space occupied by directories or files
➜ weixin du... /amu 0./test_3 8./test_4 0./test_2 2288Copy the code
Example 2: Display the space occupied by the specified file
➜ weixin du test_4/log2021. TXT 8 test_4/log2021. TXTCopy the code
Example 3: Both files and directories are displayed
➜  weixin du -ah test_4
4.0K	test_4/log2021.txt
  0B	test_4/ln2021
  0B	test_4/test444
  0B	test_4/link2021
4.0K	test_4
Copy the code
Example 4: Sort by disk space occupied
2288    .
2264    ./amu
200     ./amu/redis
56      ./amu/redis/geohash
48      ./amu/redis/currentlimiting
8       ./test_4
Copy the code
Example 5: Output the space used by each subdirectory under the current directory
➜ weixin du-sh --max-depth=1 1K./amu 0./test_4 0./test_3Copy the code

3.3.2 Viewing the File TypefileThe command

Overview: Linux file is used to identify file types

Parameter Description:
β‘  -b list identification results, do not show the name of the file β‘‘ -c detailed display instruction execution process, easy to troubleshoot or analyze the program execution situation β‘’ -f specifies the name of the file, its content has one or more file names, let file in order to identify these files, format for each column of a file nameCopy the code

File [operation] [file or directory…]

Example 1: Show the file type
➜ weixin file log.log log.log: Utf-8 Unicode text ➜ weixin file -b log.log -- do not display file nicknames UTF-8 Unicode text ➜ weixin file -i log.log log.log: Regular file ➜ weixin file -b -i log.log regular fileCopy the code

The final summary

In fact, Linux instructions seem to be quite a lot, but in fact, it is easy to understand the operation of the module. These are just a small part according to the function, remember to use more oh, as the saying goes: “practice makes perfect”; Amu is going to write redis related articles on the basis of; A daily introduction to the use of Linux instructions (frequently used instructions at work). Go β›½ ️ β›½ ️ β›½ ️