According to man rm, you cannot do it but within KDE you can select your exclusions and then toggle the selection before deleting. This doesn’t work for subdirectories.
Explanation:
ls -1 lists dirs and files in the current directory but one per line, which is needed for grep. Then we redirect output to a pipe to grep command. Option -v is for excluding those patterns that matches our regexp (regular expression), and -E is for using extended regexp.
Our regexp ‘dir_to_exclude_1|dir_to_exclude_2|^a*’ matches all filenames or dirnames who contains dir_to_exclude_1 or (that is the meaning of |) dir_to_exclude_2 or those filenames that begins with a (^ means begin of line) and have zero or more characters after a (this is represented by *). But we specified -v before, so those filenames that do not match our regexp are printed.
Finally, we use the command xargs to apply rm -R to those filenames returned by grep.
To make sure your regexp is correct, just omit the last command in pipe
ls -1 | grep -v -E 'dir_to_exclude_1|dir_to_exclude_2|^a*'
before actually deleting anything. It will print all files and dirs that will be deleted in case you use the last part of the command.
You can find more info about regular expressions in a google search, there is a lot of tutorials that you may find useful.
I hope this could be of some help to you and sorry about my english.
According to man rm, you cannot do it but within KDE you can select your
exclusions and then toggle the selection before deleting. This doesn’t
work for subdirectories.
Explanation:
ls -1 lists dirs and files in the current directory but one per line,
which is needed for grep. Then we redirect output to a pipe to grep
command. Option -v is for excluding those patterns that matches our
regexp (regular expression), and -E is for using extended regexp.
Our regexp ‘dir_to_exclude_1|dir_to_exclude_2|^a*’ matches all
filenames or dirnames who contains dir_to_exclude_1 or (that is the
meaning of |) dir_to_exclude_2 or those filenames that begins with a (^
means begin of line) and have zero or more characters after a (this is
represented by *). But we specified -v before, so those filenames that
do not match our regexp are printed.
Finally, we use the command xargs to apply rm -R to those filenames
returned by grep.
To make sure your regexp is correct, just omit the last command in
pipe
Code:
ls -1 | grep -v -E ‘dir_to_exclude_1|dir_to_exclude_2|^a*’
before actually deleting anything. It will print all files and dirs
that will be deleted in case you use the last part of the command.
You can find more info about regular expressions in a google search,
there is a lot of tutorials that you may find useful.
I hope this could be of some help to you and sorry about my english.
adriandelatabla;1899106 Wrote:
> This is the first way I come up to, there is more elegants or efficients
> for sure.
> Move to top dir where dirs to be delete are:
> >
Code:
> >
> Explanation:
> ls -1 lists dirs and files in the current directory but one per line,
> which is needed for grep. Then we redirect output to a pipe to grep
> command. Option -v is for excluding those patterns that matches our
> regexp (regular expression), and -E is for using extended regexp.
>
> Our regexp ‘dir_to_exclude_1|dir_to_exclude_2|^a*’ matches all
> filenames or dirnames who contains dir_to_exclude_1 or (that is the
> meaning of |) dir_to_exclude_2 or those filenames that begins with a
> (^ means begin of line) and have zero or more characters after a (this
> is represented by *). But we specified -v before, so those filenames
> that do not match our regexp are printed.
>
> Finally, we use the command xargs to apply rm -R to those filenames
> returned by grep.
> To make sure your regexp is correct, just omit the last command in
> pipe
> >
Code:
> > ls -1 | grep -v -E ‘dir_to_exclude_1|dir_to_exclude_2|^a*’
> >
> before actually deleting anything. It will print all files and dirs
> that will be deleted in case you use the last part of the command.
>
> You can find more info about regular expressions in a google search,
> there is a lot of tutorials that you may find useful.
> I hope this could be of some help to you and sorry about my english.
WOW. That is incredible! It’s simple, it’s a smart combo of the three
utilities and it works! Thanks!!