A command to delete all files except a file in the Linux shell __linux

Source: Internet
Author: User

Linux anti-select Delete file

The easiest way is to # shopt-s Extglob (open Extglob mode)
# RM-FR! (File1)
If you want to exclude multiple, you can do this:
# RM-RF! (File1|file2)
LINUXRM Delete Other file method totals outside the specified fileFirst, Linux delete files and folders Common commands are as follows: Delete files: RM file
Delete folder: Rm-rf dir
It should be noted that rmdir can only delete empty folders. Delete all documents except the development document (folder).
1, Method 1, the more troublesome approach is:
Copy the file you want to keep to another folder, and then delete the directory, and then return the move that needs to be retained. MV Keep. /#保留文件 (clip) Keep
RM-RF * #删除当前文件夹里的所有文件 mv. /keep./#将原来的东西移动回来
2, Method 2, you need to do in the current folder:
Rm-rf! (keep) #删除keep文件之外的所有文件
Rm-rf! (Keep1 | keep2) #删除keep1和keep2文件之外的所有文件
3, Method 3, the current folder in conjunction with grep and Xargs to handle file name: ls | Grep-v Keep | Xargs RM #删除keep文件之外的所有文件
Description: LS first to get the name of all files and folders, Grep-v Keep, grep regular match lookup keep,-v parameters determine the result is the result of matching results, that is, to the keep of all file names, and then xargs to obtain parameters from the standard input and passed to the following command, where the command used is RM, and then RM deletes the previously selected file.
Benefits: Using grep to match the file name with the regular expression, you can keep multiple files at a time for more accurate processing.
4, Method 4, use the Find command instead of LS, improve method 3 to be able to process the file folder:
Find./test/| Grep-v Keep | Xargs RM #删除当前test文件夹中keep文件之外的所有文件
Description, use grep instead of Find-name to select the name, because find is more troublesome to pick the name, not enough support for regular expressions, unable to exclude the specified file name.
5, Method 5, directly using the Find command to delete other files:
Find./-name ' [^k][^e][^e][^p]* '-exec rm-rf {} \; #删除keep以外的其他文件. Find./-name ' [^k][^e][^e][^p]* ' | Xargs RM-RF #删除keep以外的其他文件. Recommended.
Note: The code on the second line above is more efficient because-exec will start multiple processes to process while deleting multiple files, and Xargs will start an RM process to process.
For more information on the efficiency of find execution and the use of xargs-initiated commands, please refer to: http://www.linuxsir.org/main/?q=node/137
Linux File Lookup command Find,xargs Details http://www.linuxsir.org/main/?q=node/137 This article is reproduced from: Http://blog.sina.com.cn/s/blog_ 70ffb5c501011rrk.html

RM removal Removes all remaining files from the specified file (RM reverse deletion)zhou@zhou:~/linuxc/file/test$ ls
1 23sdfwe 88888888 Aabb ag Ghdda mmm
2 3 aaaaaaaa ABC ASDG llllllll wwwww
zhou@zhou:~/linuxc/file/test$

Then I want to delete all the files except the one containing the string AA, which is to leave the AABB,AAAAAAAA, these two files,otherDeletion of all
Here is my command:

zhou@zhou:~/linuxc/file/test$rm ' ls | grep-v ' AA '
zhou@zhou:~/linuxc/file/test$ ls
AAAAAAAA AABB
zhou@zhou:~/linuxc/file/test$
So, it was a success.
Simply explain the command: RM deletes the file specified later
' ls | Grep-v "AA" ' remembers the outside is the inverted quotation mark (The position of the inverted quotation mark is on the left of the number 1 of the standard keyboard, ls: Look at all the files in the current directory, use the grep command to filter the grep-v "AA" is to find the string without "AA".

Overall again: List file names without a "AA" string of files, and then delete them. Ok.
In fact, I did it for a long time because I didn't have much contact with grep before, so the first thing I thought about was usingRegular Table
Tatsu-style, but in the process of doing it suddenly found that grep makes a good thing, so it is used.

The above command removes the file with a "AA" string if I just want to leave the file AA. Very simple

zhou@zhou:~/linuxc/file/test$rm ' ls | grep-v ' ^aa$ '
in front of AA plus ^, followed by $ for Terminator meaning, this is exactly the match.
Well, that's all. I wish I could use this useful command in the future. From: http://blog.sina.com.cn/s/blog_67e34ceb01014930.html
Linux Delete other files http://zhidao.baidu.com/link?url=uvHfrb3kSnM_ 8p5ilhzyc39u0h3md-ncrm3iaygeytlu-zjthnblqo674vulvgtnpincl2nj7wo5vn08n4481_Ask for Linux CentOS I want to delete files in a directory where the file name does not conform to the "*20100330*" rule.

If the directory name is MyTest and is the next level of the current directory, use the following command:

CD./mytest && rm ' ls | Grep-v ' 20100330 ' && CD.

Go to the MyTest directory, delete the file and return to the current directory.

the-v parameter of grep represents a reverse selection.

Generally, you will be prompted to delete files using RM, and if you do not confirm, force the deletion directly, you can use the RM-F parameter.
Other 1 AnswersFind./-type f \! -name "*20100330*"-exec rm-rf {} \;
How to reverse Select a file and delete

http://bbs.csdn.net/topics/390077765

Case: Under a folder I want to delete all files except ABC file, command how to write (Linux)

Find. -maxdepth 1-type f-not-name ' abc '-exec RM ' {} ';

ls | GREP-V ABC | Xargs-i RM-RF {}

Rm-f ' ls | GREP-V ABC '
If there are many files in the case, do not use this method ...
We have to use the 2 floor method ...

MV Abc/tmp
RM *
Mv/tmp/abc.

reverse display of files

Use the LS command to only display files that are not included in the current directory.

LS plus grep filtering mode: Ls-al | Grep-v ' \. ' (LS--ignore=.*-al can also be implemented).

In open Extglob mode (the default is open), LS can also be implemented and more flexible.

Shopt-u Extglob #关闭
Shopt-s Extglob #打开

Ls-al! (*.*)
Ls-al-d! (*.*)

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.