I would like to remove the whole content of a folder, apart from one file. The problem is can't copy the file to another folder and then back to the first folder, because then the application doesn't accepts the file, so the one file must stay in the folder.
Exists there a command like I described?
Best Answer
I would go about it like this:
for this to work with folders, you have to replace the
delete
with anexec rm -r
This breaks down to
find
= find.
= in current working directory, you can replace.
with a path, like~/Documents/
!
= not-name
= name<filename>
= file-exec
= executerm -rv
= verbose remove{}
= results of the find\;
= close the executionFind all files and folders in your working folder, that do not have the name
filename
and then execute the remove command on the results.You should do a dry run without the delete option, first:
This command gives you a list of all files and folders in your working directory that are not named
<filename>
. Adding-delete
or-exec rm -rv {} \;
, will delete this files and folder (theexec
version).You could also use
for folders add
-r
Somehow I feel more comfortable using the
find
option. Mainly because you can do a dry run. Either way I would start with a dummy folder and try both options.