How to delete all files except one specific file in Linux?

How to delete all files except one specific file in Linux?

First, go to the test folder: And run the following command: The above command will delete all files in the test folder except file10.txt file. You can also use find command to delete everything but specific one. The following command will delete all files in the current folder (i.e test in our case) except file10.txt.

Is there a way to delete all files at once?

You can copy the files you wanted to keep, and save them in a different location, and then delete the rest of the files or the entire folder. But wait, I know an easiest way to do this. You can remove all files in a older except one specific file or certain type of files in one go with a single line command.

How to delete all files in a test folder?

And run the following command: The above command will delete all files in the test folder except file10.txt file. You can also use find command to delete everything but specific one. The following command will delete all files in the current folder (i.e test in our case) except file10.txt.

How to delete all files and directories except 1.BAT?

If I remove the rd line, it removes all the files, not the directories and keeps the 1.bat file (like I need). But this doesn’t work either. I simply cannot get all the directories and files to be deleted except for the “special folder” and the “1.bat file”.

Is there a way to delete all files in a directory?

BUT this won’t prompt you for confirmation or output what it just deleted. Therefore best to run it without the -delete action first and check that they’re the correct files.

Is there a way to remove a directory without rm-f?

Without -f it won’t force-delete, without -r it won’t recurse and delete directories as well as files. rm -f dirname/* will remove only files without prompting for each file. It will also display “Cannnot remove ‘subdirname’: Is a directory” for each sub directory.