How to delete all files except a file under Linux

  
                

Delete files under Linux can be completed using the rm command. Then, which command should be used to delete other files except the specified file? The following small series introduces the command to delete files other than the specified file.

First, delete files and folders under Linux common commands as follows:

Delete files: rm file

Delete folder: rm -rf dir

Note that rmdir can only delete empty folders.

Second, delete all files except the development file (clip)?

1. Method 1, the more troublesome thing is:

Copy the files you need to keep to other folders, then delete the directory, and then move back the files you want to keep.

mv keep . . /#保持文件(夹) keep

rm -rf * # Delete all files in the current folder

mv . . /keep. /# Move the original things back

2, method 2, need to be done in the current folder:

rm -rf ! (keep) # Delete all files except keep files

rm -rf ! (keep1

Copyright © Windows knowledge All Rights Reserved