linux implements deletion of all but one file or folder

  • 2020-05-14 05:45:03
  • OfStack

Let's say I have 1,2,3,4,5 files in a directory, and now I need to delete all the files except 2, so I can use it

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

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

The scope of deletions shown below is limited to the current directory.

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

The detailed use of the find command under Linux is provided below

Searches the directory structure for files and performs the specified action. This command provides quite a few lookup criteria and is quite powerful.

Syntax: find [start directory] looks for conditional operations

Note: the find command, starting from the specified starting directory, recursively searches through its subdirectories, looking for files that meet the search criteria and doing something about them. The search condition provided by this command can be a compound condition with the logical operators not, and, and or. The logical operation operators and, or and not have the following meanings:

(1) and: logic and, represented by "-a" in the command, is the default option of the system, which means that only when all the given bars are satisfied, the search condition is satisfied.

Such as:

En 38en 'find' en 39en 'tmp' en 41en c -user 'inin'

% this command looks for all files that satisfy the three given conditions

(2) or: logic or, in the command with "-o". This operator means that if one of the given conditions is satisfied, the condition is satisfied.

Such as:

find, name 'tmp', o, name 'mina*'

% this command queries all files with the file name 'tmp' or matches 'mina*'.

(3) not: use "! "in the command. Said. This operator represents a search for a file that does not meet the given criteria.

Such as:

find! � name 'tmp'

% this command queries all files whose filenames are not 'tmp'.

Note: when using many logical options, you can enclose them in parentheses. In order to avoid the misunderstanding of the parenthesis caused by She ll itself, the escape character "\" needs to be added before the word number to remove the meaning of the parenthesis.

Ex. :

find \ (hang name 'tmp' hang xtype c-user 'inin' \)

The options of this command have the following meanings:

First, the n value in each of the following options can be entered in three ways, assuming n is 20:

Plus 20 means after 20 (21,22,23, etc.)
-20 means 20 before (19,18,17, etc.)
20 means exactly 20

Look up by name and file properties.

-name 'string' looks for all files whose filenames match the given string. The wildcards *, ? And [].

-lname 'string' looks for all symbolic link files whose filenames match the given string. Wildcards *, ? can be used in the string; And [].

-gid n finds all files belonging to the user group ID number n.

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

-group 'string' finds all files that belong to the user group name given to the string.

-user 'string' finds all the files that belong to the string given by the user name.

-empty looks for directories or files of size 0.

-path 'string' finds all files whose pathnames match the given string. The wildcards *, ? And [].

-perm permissions look for files and directories with the specified permissions, which can be represented as 711,644.

-size n[bckw] finds a file with a specified file size. The characters following n represent units. The default is b, which represents 512-byte blocks.

-type x finds files of type x, x is 1 of the following characters:

b block device file
c character device file
d directory files
p named pipe (FIFO)
f ordinary file
l symbolic link file (symbolic links)
s socket file

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

Time based search

-amin n find all the files that n accessed minutes ago.
-atime n finds all the files that n accessed days ago.
-cmin n finds all files whose file status was modified minutes ago.
-ctime n finds all files whose file status has been modified by n days ago.
-mmin n finds all files whose contents have been modified by n minutes ago.
-mtime n finds all files whose contents have been modified before n day.

Executable actions

-exec command name {} executes the Linux command given to the qualified file without asking the user if he needs to execute the command.

{} indicates that the parameter of the command is the file found; The end of the command must be "\;" The end.

-ok command name {} execute the given Linux command on the qualified file, unlike exec,

It asks the user if he needs to execute the command.

-ls details all files found.

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

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

- for the printf format, please refer to the C language book.

Instruction instance:

find. - name 'main*' - exec more {} \;

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

\(-name a.out - o - name '*.o '\) > - atime +7 - exec rm {} \;

% delete all a.out or *.o files in the current directory that have not been accessed in 1 week.

The ". "in the % command represents the current directory, where find will start,

Look in its subdirectories one by one for files that meet the criteria specified later.

% "\(" and" \) "denote parentheses (), where the" \ "is called an escape character. And the reason why I wrote it this way is because

For Shell, (and) has a different meaning than the one used here for combining conditions.

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

% "-name '*.o'" is to find all files whose names end in.o.

The -o between the two -name represents the logical or (or), that is, look for files with the name a.out or the name ending in.o.

% find finds this file in the current directory and its subdirectories, and then determines when it was last accessed

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

Where {} represents the qualified file name found currently, \; Is what the grammar requires.

% the last \ of line 1 in the above command is a line continuation. When the command is too long to fit on line 1,

You can input 1 \, and then the system will display 1 > , instructing the user to continue typing the command.


Related articles: