If we want to delete all files and directories we use, rm -rf *
.
But what if i want all files and directories be deleted at a shot, except one particular file?
Is there any command for that? rm -rf *
gives the ease of deletion at one shot, but deletes even my favourite file/directory.
Thanks in advance
I don't know of such a program, but I have wanted it in the past for some times. The basic syntax would be:
The program I have in mind has three modes:
-e
)glob
matching (default, like shown in the above example)-r
)It takes the patterns to be excluded from the command line, followed by the separator
--
, followed by the file names. Alternatively, the file names might be read fromstdin
(if the option-s
is given), each on a line.Such a program should not be hard to write, in either C or the Shell Command Language. And it makes a good excercise for learning the Unix basics. When you do it as a shell program, you have to watch for filenames containing whitespace and other special characters, of course.
you need to use regular expression for this. Write a regular expression which selects all other files except the one you need.
This looks tedious, but it is rather safe
rm -rf *
, its results depend on your current directory (which could be/
;-)*
: its expansion is limited by ARGV_MAX.At least in zsh
could be an option, if you only want to preserve one single file.