Linux implementation deletes all but a file or a folder _linux

Source: Internet
Author: User

For example, a directory has 1,2,3,4,5 these five files, now I need to delete all except 2 files, then I can use

Find. ! -name 2-exec rm-f {} \;

Of course, you can also cooperate with the-maxdepth to more accurate delete.

The following shows that the scope of the deletion is limited to the current directory.

Find. -maxdepth 1! -name 2-exec rm-f {} \;

Below provides a detailed use of the Find command under Linux

Searches the directory structure for files and performs the specified action. This command provides a considerable number of lookup conditions and is powerful.

Syntax: Find [start directory] look for conditional actions

Description: The Find command starts from the specified starting directory, recursively searches its subdirectories, finds the files that satisfy the search criteria, and takes related actions. This command provides a search condition that can be a composite condition consisting of a logical operator not, and, or. The meaning of the logical operator and, or, not is:

(1) and: logic and, expressed as "a" in the command, is the default option for the system, indicating that the condition is satisfied only if the given condition is satisfied.

For example:

Find–name ' tmp ' –xtype c-user ' inin '

% This command looks for all three files that are satisfied with the given condition

(2) or: logic or, in the command, "-O". This operator indicates that a condition is satisfied if one is satisfied in the given condition.

For example:

Find–name ' tmp ' –o–name ' mina* '

% This command queries all files named ' tmp ' or matching ' mina* '.

(3) Not: illogical, in command "! Said This operator represents the lookup of a file that does not meet the given criteria.

For example:

Find! –name ' tmp '

% This command queries all files for which the filename is not ' tmp '.

It should be explained that when you use a lot of logical options, you can enclose these options in parentheses. To avoid a misunderstanding of the parentheses by she ll itself, you need to add the escape character "\" before the word number to remove the meaning of the parentheses.

Cases:

Find \ (–name ' tmp ' –xtype c-user ' inin ')

The options for this command have the following meanings:

First, the n values in each of the following options can have three input methods, assuming N is 20, then:

+20 means after 20 (21,22,23 etc.)
-20 indicates 20 ago (19,18,17 etc.)
20 says it's exactly 20.

Find with name and file attributes.

-name ' string ' finds the file name matches all the files for the given string, and the wildcard character * 、?、 [] is available within the string.

-lname ' string ' finds the filename matches all the symbolic link files for the given string, and the wildcard character * 、?、 [] is available within the string.

-gid N finds all files that belong to a user group with an ID number of n.

-uid N finds all files belonging to the user with ID number n.

-group ' string ' looks for all files that belong to the given string named by the user group.

-user ' string ' finds all files that belong to a given string of user names.

-empty find directories or files of size 0.

-path ' string ' finds all files that match the given string, and the wildcard character * 、?、 [] is available within the string.

-perm permissions to find files and directories with the specified permissions, the representation of the permission can be as 711,644.

-size n[bckw] looks for files of the specified file size, and the characters after n indicate units, the default is B, which represents a 512-byte block.

-type x finds a file of type X, and X is one of the following characters:

B Block Device file
C Character device file
D Catalog Files
P Named pipe (FIFO)
F Normal File
L Symbolic Link file (Symbolic links)
s socket file

-xtype x is basically the same as-type, but finds only symbolic link files.

Find time as a condition

-amin N finds all files that have been accessed before n minutes.
-atime N finds all files that have been accessed before n days.
-cmin N finds all files where the file state was modified before n minutes.
-ctime N finds all files where the file state was modified before n days.
-mmin N finds all files where the contents of the file were modified before n minutes.
-mtime N finds all files where the contents of the file were modified before n days.

Actions that you can perform

The-exec command name {} Executes the given Linux command on a qualifying file without asking the user if the command needs to be executed.

{} indicates that the command's argument is the found file; the end of the command must end with "\;"

The-OK command name {} Executes the given Linux command for a qualifying file, unlike exec,

It asks the user if the command needs to be executed.

-ls lists all the files found in detail.

-fprintf FileName writes the file name found to the specified file.

-print Displays the file name found on the standard output device.

-PRINTF format format Please refer to the C language book.

Instruction instance:

Find. -Name ' main* '-exec more {} \;

% finds all files in the current directory that start with main and displays the contents of those files.

Find. \ (-Name A.out-o-name ' *.O ' \) >-atime +7-exec rm {} \;

% deletes a. Out or *.O file that has not been accessed in the current directory for all weeks.

% of the "." in command. Represents the current directory, at which point find will start from the current directory,

Find files in their subdirectories, one by one, that meet the criteria that you specify later.

% "\ (" and "\)" represent parentheses (), where "\" is called an escape character. The reason why this is written is because

For the Shell, (and) has a different meaning, not the purpose for which the combination condition is used.

% "-name a.out" means to find a file named a.out;

% "-name ' *.o '" means a file to find all names ending with. O.

The-O representation logic or (or) between these two-name finds a file whose name is A.out or whose name ends with. O.

% find the file in the current directory and its subdirectories, and then make a judgment to see the last access time

Whether 7 days ago (condition-atime +7), if so, execute command RM (-exec rm {} \;) on the file.

where {} is the name of the qualifying file that is currently found, \ is required by the syntax.

% the last of the first line in the above command is a continuation line character. When the command is too long to write on one line,

You can enter a \, and then the system will display a, indicating that the user continues to enter the command.

The above Linux implementation in addition to a file or a folder of all the deletion is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

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.