Linux removes all but a file or a folder

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 accurately delete.

The following surface shows that the deletion is scoped to the current directory only.

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

The next side provides a detailed use of the Find command under Linux

Searches for a file in the directory structure and performs the specified action. This command provides quite a few look-up conditions and is powerful.

Syntax: Find [start directory] Find conditional action

Description: The Find command starts with the specified starting directory, searches its subdirectories recursively, finds the file that satisfies the search criteria, and takes relevant action on it. The search condition provided by this command can be a compound condition consisting of a logical operator not, and, or. The meaning of the logical operator and, or, is:

(1) and: Logic and, in the command with "-a", is the system default option, indicating that only when the given conditions are satisfied, the search condition is satisfied. For example:

Find–name ' tmp ' –xtype c-user ' inin '
% This command looks for all the files that are satisfied with the three given criteria

(2) or: logical OR, denoted by "-O" in the command. The operator indicates that the search condition is satisfied as long as one of the given conditions is satisfied. For example:

Find–name ' tmp ' –o–name ' mina* '
% This command queries the file name ' tmp ' or all files matching ' mina* '.

(3) Not: Logical, in command "! Said The operator indicates that a file was found that did not meet the given criteria. For example:

Find it! –name ' tmp '
% This command queries all files for which the file name is not ' tmp '.

It is necessary to note that these options can be enclosed in parentheses when using a number of logical options. To avoid the She ll itself is misunderstood by the parentheses, it is necessary to add the escape character "\" 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 option in the following column can be entered in three ways, assuming that N is 20:
+20 means after 20 (21,22,23 etc.)
-20 means before 20 (19,18,17, etc.)
20 means it's exactly 20.

Find by name and file attributes.
-name ' string ' Find file name matches all files of the given string, wildcard characters * 、?、 [] in the string.
-lname ' string ' Find file name matches all symbolic link files of the given string, wildcard characters * 、?、 [] are available within the string.
-gid N finds all files belonging to the user group with ID number n.
-uid N finds all files belonging to a user with ID number n.
-group ' string ' finds all the files that belong to the user group named the given string.
The-user ' string ' finds all the files that belong to the user name of the given string.
-empty look for a directory or file of size 0.
The-path ' string ' lookup path name matches all the files given to the string, and the wildcard characters * 、?、 [] are available within the string.
-perm permissions find files and directories with the specified permissions, which can be represented as 711,644.
-size n[bckw] finds files of the specified file size, the characters following n represent units, the default is B, which represents 512-byte blocks.
-type x looks for files of type X, and X is one of the following column characters:
B-Block device files
C-Character device files
D catalog File
P Named Pipes (FIFO)
F Common Files
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-based conditions
-amin N finds all files that were accessed N minutes ago.
-atime N finds all files that were accessed N days ago.
-cmin N finds all files that have been modified for n minutes before the file status.
-ctime N finds all files that have been modified by the file status N days ago.
-mmin N finds all files where the contents of the file were modified before n minutes.
-mtime N finds all files that have been modified by the contents of the file N days ago.

Actions that can be performed
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 argument to the command is the found file ; the end of the command must end with "\;".
-ok command name {} Performs a given Linux command on a qualifying file , unlike exec,
It asks the user if the command needs to be executed.
The-ls lists all the files found in detail.
-fprintf FileName writes the found file name to the specified file.
-print Displays the found file name on the standard output device.
Please refer to the C language book for-printf format format.

instruction Example:

Find. -Name ' main* '-exec more {} \;
% finds all files in the current directory that begin with main and displays the contents of those files.

Find. \ (-Name A.out-o-name ' *.O ' \) >-atime +7-exec rm {} \;
% delete A. out or *.o file that has not been accessed within the current directory for a week.
The "." In the% command Represents the current directory, where find starts at the current directory,
Finds files in its subdirectories, one after the other , that meet the criteria specified later.
% "\ (" and "\)" denotes parentheses (), where "\" is called an escape character. The reason why this is written is because
For the Shell, (and) has a different meaning, rather than the use of the combination condition here.
% "-name a.out" means to find a file named a.out;
% "-name ' *.o '" means to find all files whose names end With. O.
The-o between the two-name represents the logical or (or), which is to find the file whose name is A.out or whose name ends with. O.
% find after this file is found in the current directory and its subdirectories, and then judged to see its last access time
Whether it was 7 days ago (condition-atime +7), and if so, execute the command rm (-exec rm {} \;) on the file.
where {} represents the currently found qualified file name, \; is required by the syntax.
% the last \ is the continuation character of the first line in the preceding command. When the command is too long and the line is not written down ,
You can enter a \, and then the system will display a, indicating that the user continues to enter the command.

Linux removes all but a file or a folder

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.