(Linux shell) Chapter 2-joy of command (1), linuxshell

Source: Internet
Author: User
Tags linux shell commands

(Linux shell) Chapter 2-joy of command (1), linuxshell

This article is from my personal blog: (linux shell) Chapter 2 -- joy of command (1)

In the previous chapter, we described some syntaxes that need to be paid attention to in linux shell. Next we will begin to understand Common commands of linux shell. Let's go...

  1. Cat command:

    Cat itself indicates concatenate ). The cat command has some common parameters, such as-n and-s. We will introduce them one by one:

    If you want to quickly view the content of a text file, you can use the cat command:

    Cat file

    If you want to view multiple files at a time, add the file path to the file.

    Cat file1 file2 file3

    To display the row number of a text file, use the-n parameter:

    Cat-n file or cat-n file1 file2 file3

    If you want to remove empty lines from the text, use the-s parameter:

    Cat-s file or cat-s file1 file2 file3, of course, here you can also use the tr command to benefit from the blank line: cat file | tr-s ''; this tr command means to treat multiple line breaks as one line break.

  2. Find command:

    The find command traverses down the File hierarchy, matches the files that meet the conditions, and performs corresponding operations.

    Basic Syntax:

    Find.-print # print all file paths in the current directory with paths

The find command also has many parameters. Let's take a look at the usage of some common parameters:

1). Search by file name or regular expression

Find.-name "*. sh"-print # find all files matching the end of sh in the current directory and print them to the terminal.


-Name also has a corresponding-iname to ignore the case sensitivity of keywords.

Find.-iname "*. sh"-print

2). Matching by path

Find.-path "* test *"-print # starting from the current directory, find all paths whose path prefix is test and print the files under these directories.

3). Regular Expression-based matching-regex

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

4). Negative parameter!

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

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

5). Search the directory in depth-maxdepth and-mindepth.

We can use this command to limit the depth of the find downward search.

Find.-maxdepth 1-type f-print # find a file in the current directory, that is, it does not traverse down


-Maxdepth and-mindepth should appear as the third parameter of find. If they appear as the fourth or later parameter, the efficiency of find may be affected, because it has to perform some unnecessary checks.

6). Search for-type based on the file type.

Find.-type d-print # Only list all directories

Find.-type f-print # Only list all objects

Find.-type l-print # Only list all links

7). Search by file time-mtime (modification time)-atime (access time)-ctime (Change Time). Time +,-indicates the time before and after

Find.-type f-atime-7-print # print all files accessed in the last 7 days

-Amin (access time),-mmin (modification time),-cmin (Change Time)

Find.-type f-amin + 7-print # print all files whose access time exceeds 7 minutes

Find.-type f-newer file. sh-print # find all files that have been modified for a longer time than file. sh.

8), search-size Based on file size

Find. -type f-size + 2 k # search for all files larger than 2 kb. In addition to k, you can also use B -- block, c -- byte, w -- word, M -- MB, G -- Kyrgyzstan

Find.-type f-size 2 k # files larger than or equal to 2 kb

Find.-type f-size-2 k # files smaller than 2 kb

9). Matching operation

Find.-name "*. sh"-delete # delete all matched files (usage)

10). Matching of file permissions and ownership -- perm

Find.-type f-perm 644-print # print the file with the permission of 644

11). execute commands or actions in combination with find

The find command can be combined with other commands with the help of 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 used in combination with the-exec option. For each matching file, {} is replaced with the corresponding file name. In the above command, first find all the 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 First detects all sh files, and then adds the contents of the files to the text files.


Linux shell commands

You don't know how to answer this question. If you really want to fully understand it, find a book. ABS is a good book.

Linux Shell commands are not commonly used, especially for common users, such as cd, mv, rm, etc, first, master the commonly used commands (generally, these commands will be listed in any book about Linux). All Linux commands have parameters. First, do not master them completely. First, simply do it.

I have a software running in LINUX. I need to enter commands in shell, but I cannot find the SHELL?

Enter the command env to view the shell red tag you are using. Switch the chsh-s/bin/bsh command to the shell of ash. Switch the chsh-s/bin/csh command to tcsh. We usually use chsh-s/bin/sh bash. If you enter the command prompt "command not find", it is your current user's PATH environment variable setting problem. Enter su-and then enter the root user password to obtain the ROOT user's permissions and environment, you can use it.




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.