(Linux shell) Chapter II-The Joy of command (i)

Source: Internet
Author: User

Article from my personal blog: (Linux shell) Chapter II-The Joy of command (a)

In the previous chapter, we described some of the syntax that some Linux shells need to be aware of. Next we begin to understand the frequently used commands of the Linux shell. Let ' s go ...

    1. Cat command:

      The cat itself represents stitching (concatenate). Cat commands have some frequently used parameters, like-n,-s, and so on, we introduce each of the following:

      Suppose you want to see the contents of a text file at high speed. You can use the Cat command:

      Cat file

      Suppose you want to view multiple file contents at once. You can also add a file path in the back

      Cat File1 file2 File3

      Suppose you want to display the line number of a text file. Then use the parameter-N:

      Cat-n file or cat-n file1 file2 file3

      Suppose you want to remove empty lines from the text. Then use the parameter-s:

      Cat-s file or cat-s file1 file2 file3 Of course, it is also possible to use the TR command to benefit the blank line: Cat file |  Tr-s "; The TR command means that multiple newline characters are treated as a newline.

    2. Find command:

      The Find command iterates down the file hierarchy, matches the qualifying file, and runs the corresponding action.

      The basic syntax:

      Find. -print #打印出带有路径的当前文件夹下的全部文件路径

The Find command also has a lot of references, and we'll look at some of the ways we use them frequently:

1). Match Search by file name or by regular table

Find. -name "*.sh"-print #从当前文件夹查找全部符合以sh结尾的文件 and Print to terminal


-name another corresponding-iname to ignore the uppercase and lowercase keyword

Find. -iname "*.sh"-print

2). Based on path matching

Find. -path "*test*"-print #从当前文件夹開始. The Find path prefix is the full path to test and prints the files under those folders

3). Matching-regex based on the normal form

Find. -regex ". *\.py$"-print

4). Negative number of references!

If you want to find a file that does not include a condition, add it before the type! You can:

Find. ! -name "*.sh"-print

5). Give folder depth search-maxdepth and-mindepth

We can use this command to limit the depth of find down lookup

Find. -maxdepth 1-type F-print #在当前文件夹的当前文件夹查找是文件类型的文件, that is, it does not traverse down when searching


-maxdepth and-mindepth should appear as the third parameter of find, assuming that as a fourth or later occurrence, it might affect the efficiency of find, because it has to do some unnecessary checking.

? ? 6). Search-type based on file type

?    ? ? find. -type D-print #仅仅列出全部的文件夹

?    ? ?

?    ? ? find.-type f-print #仅仅列出全部的文件

?    ? ? find. -type L-print #仅仅列出全部的链接

? 7). Search for-mtime (change time)-atime (time to visit) by file time-ctime (time of variation), time with +,-to indicate before and after time

?    ? ? find. -type f-atime-7-print #打印出在近期7天内被訪问的全部文件

?    ? ?

?    ? -amin (visit time),-mmin (Time to change),-cmin (time of variation)

?    ? ? find. -type f-amin +7-print #打印出訪问时间超过7分钟的全部文件

?    ? ? find. -type f-newer file.sh-print #找出比file. Sh All files with a longer change time

? ? 8), file-size-based search-size

?    ? ? find. -type f-size +2k #查找大于2KB的全部文件, in addition to K, can also use b--blocks, c--bytes, w--words, m--megabytes, g--gigabytes

?    ? ?

?    ? ? find.-type f-size 2k #大于等于2KB的文件

?    ? ? find. -type f-size-2k #小于2KB的文件

? ? 9). Matching Operation

?    ? ? find. -name "*.sh"-delete #删除匹配的全部文件 (very use)

? 10). Matching--perm based on file permissions and full weights

?    ? ? find. -type f-perm 644-print #打印出权限为644的文件

    ? 11). Run command or action with find

?    ? The find command can be combined with other commands with the option-exec,-exec is one of the most powerful features of find.

?    ? ? find. -type f-user root-exec chown Test {} \; In this command, {} is a special string that is used in conjunction with the-EXEC option. For each matching file, {} is replaced with the corresponding file name. In the above command, first find all files that belong to the root user, and then add permissions to the test user.

?    ? ? find. -type f-name "*.sh"-exec cat {} \; > All_sh_files.txt This command is to find out all the sh files first. The contents of the file are then added to the text file.

(Linux shell) Chapter II-The Joy of command (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.