Delete Files other than a specified file in linux

Source: Internet
Author: User
Tags grep regular expression


Delete Files other than specified files in linux 1. Common commands for deleting files and folders in Linux: delete files: rm file www.2cto.com delete folders: rm-rf dir, rmdir can only delete empty folders. Ii. Delete all files other than the specified files (folders? 1. method 1. It is troublesome to copy the files to be retained to another folder, delete the directory, and move the files to be retained back. Mv keep .. /# keep the file (folder) keeprm-rf * # delete all files in the current folder .. /keep. /# Move the original things back 2. method 2, which must be carried out in the current folder: rm-rf! (Keep) # delete all files other than the keep file rm-rf! (Keep1 | keep2) # delete all files other than keep1 and keep2. 3. Use grep and xargs in the current folder to process file names: ls | grep-v keep | xargs rm # delete all files other than the keep file Description: ls first obtains the names of all current files and folders, grep-v keep, perform grep regular match to search for keep. The-v parameter determines that the result is not matching, that is, all files except keep are called, then, xargs is used to obtain parameters from the standard input and pass them to the subsequent commands. Here, the command is rm, and then rm deletes the selected file. Benefit: The grep regular expression is used to match the file name. Multiple files can be retained at a time for more accurate processing. 4. Method 4: Use the find command instead of ls to improve method 3 so that you can process the files in the folder: find. /test/| grep-v keep | xargs rm # Delete the description of all files other than the keep files in the current test folder. Use grep instead of find-name to select the name, because finding is difficult to select a name and does not support regular expressions, the specified file name cannot be excluded. 5. Method 5: directly use the find command to delete other files: find. /-name' [^ k] [^ e] [^ e] [^ p] * '-exec rm-rf {}\; # delete files other than keep. Find./-name' [^ k] [^ e] [^ e] [^ p] * '| xargs rm-rf # delete files other than keep. Recommended! Note: the code in the second line above is more efficient, because when multiple files are deleted-exec will start multiple processes for processing, and xargs will start a rm process for processing.

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.