CentOS deletes files other than the specified file

Source: Internet
Author: User

Delete files other than the specified file under Linux   A, delete files and folders under Linux Common commands are as follows: Delete files: RM file  www.2cto.com   Delete folder: Rm-rf dir Note that rmdir Only empty folders can be deleted.   (b) Delete all documents other than the development document (folder)?  1, Method 1, it is troublesome to copy the files that need to be kept to other folders, then delete the directory, and then move back the one that needs to be retained. MV Keep. /#保留文件 (clip) KEEPRM-RF * #删除当前文件夹里的所有文件mv. /keep./#将原来的东西移动回来  2, Method 2, using grep and Xargs in the current folder to process file names: ls | Grep-v Keep | Xargs RM #删除keep文件之外的所有文件说明: LS First gets the name of all current files and folders, Grep-v Keep, the grep regular match lookup keep,-v parameter determines the result to match the results, that is, to the keep of all file names , then Xargs is used to get the parameters from the standard input and passed to the following command, where the command used is RM, and then the file selected by RM is deleted. Benefit: Using grep to match the file name with a regular expression, you can keep multiple files at a time for more accurate processing.  3, Method 3, use the Find command instead of LS, and improve method 3 to be able to handle the files that make up the folder: Find./test/| Grep-v Keep | Xargs rm #删除当前test文件夹中keep文件之外的所有文件说明, use grep instead of Find-name to select the name, because find is troublesome to choose the name, the regular expression support is not enough to exclude the specified file name.  4, Method 4, use the Find command to delete other files directly: 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 initiates multiple processes to process when deleting multiple files, and Xargs initiates an RM process to process them.

CentOS deletes files other than the specified file

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.