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.