Remove files that start with but don't contain
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm trying to remove a lot of files at once but need to be specific as to not remove any of the files I actually need.
I have a ton of corrupt files that start master- but there are valid files that start with master-2018
So, I want to do something like
rm -rf master-* --exclude master-2018*
Is that I need possible?
linux filenames rm
New contributor
add a comment |Â
up vote
1
down vote
favorite
I'm trying to remove a lot of files at once but need to be specific as to not remove any of the files I actually need.
I have a ton of corrupt files that start master- but there are valid files that start with master-2018
So, I want to do something like
rm -rf master-* --exclude master-2018*
Is that I need possible?
linux filenames rm
New contributor
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to remove a lot of files at once but need to be specific as to not remove any of the files I actually need.
I have a ton of corrupt files that start master- but there are valid files that start with master-2018
So, I want to do something like
rm -rf master-* --exclude master-2018*
Is that I need possible?
linux filenames rm
New contributor
I'm trying to remove a lot of files at once but need to be specific as to not remove any of the files I actually need.
I have a ton of corrupt files that start master- but there are valid files that start with master-2018
So, I want to do something like
rm -rf master-* --exclude master-2018*
Is that I need possible?
linux filenames rm
linux filenames rm
New contributor
New contributor
edited 1 hour ago
Jeff Schaller
35.2k952115
35.2k952115
New contributor
asked 1 hour ago
Dan James Palmer
1084
1084
New contributor
New contributor
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
3
down vote
accepted
Yes you can use more than one pattern with find
:
$ find -name 'master*' ! -name 'master-2018*' -print0 |
xargs -0 echo rm -fr
(remove the echo
if you're satisfied with the dry run)
You should add a -maxdepth 1
predicate just after find
if you only want ro remove files from the current directory, ie master-1991
but no subdir/master-1991
.
but the OP is usingrm -rf
,-delete
will only work if the dir is empty.
â mosvy
1 hour ago
I'll give this a go and report back, thanks for your help
â Dan James Palmer
56 mins ago
This worked great, thank you
â Dan James Palmer
29 mins ago
add a comment |Â
up vote
1
down vote
In bash:
shopt -s extglob
echo rm master-!(2018*)
Remove the echo
if it looks correct.
The above uses bash's extended globbing facility to match files that start with master-
but who do not have 2018
immediately following, then followed by anything (*
).
add a comment |Â
up vote
0
down vote
Use find:
find . ! -name "master-2018*" -delete
Can you specify more than one? Like find . ! -name "master-201801*" "master-201802" -delete
â Dan James Palmer
1 hour ago
there are other files in the same folder that start with things like test-2018 and find-2018. I don't want to delete any of these. It's strictly files that start master- but not master-2018*
â Dan James Palmer
1 hour ago
that's broken -- it will try to delete all files that don't start withmaster-2018
, including directories and those that do not start withmaster
.
â mosvy
1 hour ago
add a comment |Â
up vote
0
down vote
If all files you want to delete has pattern like master-YYYY*, you can use those patterns:
rm -rf master-???[0-79]*
rm -rf master-??[02-9]*
rm -rf master-?[1-9]*
rm -rf master-[013-9]*
The goal is to ommit digits from a year number, so on first place after master- we need to ommit (don't remove) digit 2, on 2nd place digit 0, on 3rd place digit 1, and on 4th place - digit 8
I tried it just a minute before, and is enough to run only first two of them.
Second method:
You can move master-2018 to another dir, e.g. /tmp,
then remove everything with master-*, and move back your master-2018 from tmp.
mkdir /tmp/backup
mv -r master-2018* /tmp/backup
rm -rf master-*
mv -r /tmp/backup/* .
New contributor
1
Ahh so, rm -rf master-????[2019-9999]* would remove all files from master-2019* to master-9999*?
â Dan James Palmer
1 hour ago
No, it not works this way. You can not specify numbers in [ ] braces. Only digits. You specify which digits will be places in the place of [xxxxxxx]. So max possibilities is range from 0 to 9 like this [0-9] or this [0123456789]. This can replace only one character from file name.rob@vps:mast$ ls
master-1990 master-1994 master-2006 master-2010 master-2014 master-2018
master-1991 master-1995 master-2007 master-2009 master-2013 master-2017
rob@vps:mast$ rm master-[2000-2019]*
rob@vps:mast$ ls
empty directory`
â Robert Zabkiewicz
1 hour ago
1
none of your commands will remove a file with the namemaster-of-puppets
â mosvy
1 hour ago
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Yes you can use more than one pattern with find
:
$ find -name 'master*' ! -name 'master-2018*' -print0 |
xargs -0 echo rm -fr
(remove the echo
if you're satisfied with the dry run)
You should add a -maxdepth 1
predicate just after find
if you only want ro remove files from the current directory, ie master-1991
but no subdir/master-1991
.
but the OP is usingrm -rf
,-delete
will only work if the dir is empty.
â mosvy
1 hour ago
I'll give this a go and report back, thanks for your help
â Dan James Palmer
56 mins ago
This worked great, thank you
â Dan James Palmer
29 mins ago
add a comment |Â
up vote
3
down vote
accepted
Yes you can use more than one pattern with find
:
$ find -name 'master*' ! -name 'master-2018*' -print0 |
xargs -0 echo rm -fr
(remove the echo
if you're satisfied with the dry run)
You should add a -maxdepth 1
predicate just after find
if you only want ro remove files from the current directory, ie master-1991
but no subdir/master-1991
.
but the OP is usingrm -rf
,-delete
will only work if the dir is empty.
â mosvy
1 hour ago
I'll give this a go and report back, thanks for your help
â Dan James Palmer
56 mins ago
This worked great, thank you
â Dan James Palmer
29 mins ago
add a comment |Â
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Yes you can use more than one pattern with find
:
$ find -name 'master*' ! -name 'master-2018*' -print0 |
xargs -0 echo rm -fr
(remove the echo
if you're satisfied with the dry run)
You should add a -maxdepth 1
predicate just after find
if you only want ro remove files from the current directory, ie master-1991
but no subdir/master-1991
.
Yes you can use more than one pattern with find
:
$ find -name 'master*' ! -name 'master-2018*' -print0 |
xargs -0 echo rm -fr
(remove the echo
if you're satisfied with the dry run)
You should add a -maxdepth 1
predicate just after find
if you only want ro remove files from the current directory, ie master-1991
but no subdir/master-1991
.
edited 51 mins ago
answered 1 hour ago
mosvy
3,770120
3,770120
but the OP is usingrm -rf
,-delete
will only work if the dir is empty.
â mosvy
1 hour ago
I'll give this a go and report back, thanks for your help
â Dan James Palmer
56 mins ago
This worked great, thank you
â Dan James Palmer
29 mins ago
add a comment |Â
but the OP is usingrm -rf
,-delete
will only work if the dir is empty.
â mosvy
1 hour ago
I'll give this a go and report back, thanks for your help
â Dan James Palmer
56 mins ago
This worked great, thank you
â Dan James Palmer
29 mins ago
but the OP is using
rm -rf
, -delete
will only work if the dir is empty.â mosvy
1 hour ago
but the OP is using
rm -rf
, -delete
will only work if the dir is empty.â mosvy
1 hour ago
I'll give this a go and report back, thanks for your help
â Dan James Palmer
56 mins ago
I'll give this a go and report back, thanks for your help
â Dan James Palmer
56 mins ago
This worked great, thank you
â Dan James Palmer
29 mins ago
This worked great, thank you
â Dan James Palmer
29 mins ago
add a comment |Â
up vote
1
down vote
In bash:
shopt -s extglob
echo rm master-!(2018*)
Remove the echo
if it looks correct.
The above uses bash's extended globbing facility to match files that start with master-
but who do not have 2018
immediately following, then followed by anything (*
).
add a comment |Â
up vote
1
down vote
In bash:
shopt -s extglob
echo rm master-!(2018*)
Remove the echo
if it looks correct.
The above uses bash's extended globbing facility to match files that start with master-
but who do not have 2018
immediately following, then followed by anything (*
).
add a comment |Â
up vote
1
down vote
up vote
1
down vote
In bash:
shopt -s extglob
echo rm master-!(2018*)
Remove the echo
if it looks correct.
The above uses bash's extended globbing facility to match files that start with master-
but who do not have 2018
immediately following, then followed by anything (*
).
In bash:
shopt -s extglob
echo rm master-!(2018*)
Remove the echo
if it looks correct.
The above uses bash's extended globbing facility to match files that start with master-
but who do not have 2018
immediately following, then followed by anything (*
).
answered 1 hour ago
Jeff Schaller
35.2k952115
35.2k952115
add a comment |Â
add a comment |Â
up vote
0
down vote
Use find:
find . ! -name "master-2018*" -delete
Can you specify more than one? Like find . ! -name "master-201801*" "master-201802" -delete
â Dan James Palmer
1 hour ago
there are other files in the same folder that start with things like test-2018 and find-2018. I don't want to delete any of these. It's strictly files that start master- but not master-2018*
â Dan James Palmer
1 hour ago
that's broken -- it will try to delete all files that don't start withmaster-2018
, including directories and those that do not start withmaster
.
â mosvy
1 hour ago
add a comment |Â
up vote
0
down vote
Use find:
find . ! -name "master-2018*" -delete
Can you specify more than one? Like find . ! -name "master-201801*" "master-201802" -delete
â Dan James Palmer
1 hour ago
there are other files in the same folder that start with things like test-2018 and find-2018. I don't want to delete any of these. It's strictly files that start master- but not master-2018*
â Dan James Palmer
1 hour ago
that's broken -- it will try to delete all files that don't start withmaster-2018
, including directories and those that do not start withmaster
.
â mosvy
1 hour ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Use find:
find . ! -name "master-2018*" -delete
Use find:
find . ! -name "master-2018*" -delete
answered 1 hour ago
Ipor Sircer
9,8711921
9,8711921
Can you specify more than one? Like find . ! -name "master-201801*" "master-201802" -delete
â Dan James Palmer
1 hour ago
there are other files in the same folder that start with things like test-2018 and find-2018. I don't want to delete any of these. It's strictly files that start master- but not master-2018*
â Dan James Palmer
1 hour ago
that's broken -- it will try to delete all files that don't start withmaster-2018
, including directories and those that do not start withmaster
.
â mosvy
1 hour ago
add a comment |Â
Can you specify more than one? Like find . ! -name "master-201801*" "master-201802" -delete
â Dan James Palmer
1 hour ago
there are other files in the same folder that start with things like test-2018 and find-2018. I don't want to delete any of these. It's strictly files that start master- but not master-2018*
â Dan James Palmer
1 hour ago
that's broken -- it will try to delete all files that don't start withmaster-2018
, including directories and those that do not start withmaster
.
â mosvy
1 hour ago
Can you specify more than one? Like find . ! -name "master-201801*" "master-201802" -delete
â Dan James Palmer
1 hour ago
Can you specify more than one? Like find . ! -name "master-201801*" "master-201802" -delete
â Dan James Palmer
1 hour ago
there are other files in the same folder that start with things like test-2018 and find-2018. I don't want to delete any of these. It's strictly files that start master- but not master-2018*
â Dan James Palmer
1 hour ago
there are other files in the same folder that start with things like test-2018 and find-2018. I don't want to delete any of these. It's strictly files that start master- but not master-2018*
â Dan James Palmer
1 hour ago
that's broken -- it will try to delete all files that don't start with
master-2018
, including directories and those that do not start with master
.â mosvy
1 hour ago
that's broken -- it will try to delete all files that don't start with
master-2018
, including directories and those that do not start with master
.â mosvy
1 hour ago
add a comment |Â
up vote
0
down vote
If all files you want to delete has pattern like master-YYYY*, you can use those patterns:
rm -rf master-???[0-79]*
rm -rf master-??[02-9]*
rm -rf master-?[1-9]*
rm -rf master-[013-9]*
The goal is to ommit digits from a year number, so on first place after master- we need to ommit (don't remove) digit 2, on 2nd place digit 0, on 3rd place digit 1, and on 4th place - digit 8
I tried it just a minute before, and is enough to run only first two of them.
Second method:
You can move master-2018 to another dir, e.g. /tmp,
then remove everything with master-*, and move back your master-2018 from tmp.
mkdir /tmp/backup
mv -r master-2018* /tmp/backup
rm -rf master-*
mv -r /tmp/backup/* .
New contributor
1
Ahh so, rm -rf master-????[2019-9999]* would remove all files from master-2019* to master-9999*?
â Dan James Palmer
1 hour ago
No, it not works this way. You can not specify numbers in [ ] braces. Only digits. You specify which digits will be places in the place of [xxxxxxx]. So max possibilities is range from 0 to 9 like this [0-9] or this [0123456789]. This can replace only one character from file name.rob@vps:mast$ ls
master-1990 master-1994 master-2006 master-2010 master-2014 master-2018
master-1991 master-1995 master-2007 master-2009 master-2013 master-2017
rob@vps:mast$ rm master-[2000-2019]*
rob@vps:mast$ ls
empty directory`
â Robert Zabkiewicz
1 hour ago
1
none of your commands will remove a file with the namemaster-of-puppets
â mosvy
1 hour ago
add a comment |Â
up vote
0
down vote
If all files you want to delete has pattern like master-YYYY*, you can use those patterns:
rm -rf master-???[0-79]*
rm -rf master-??[02-9]*
rm -rf master-?[1-9]*
rm -rf master-[013-9]*
The goal is to ommit digits from a year number, so on first place after master- we need to ommit (don't remove) digit 2, on 2nd place digit 0, on 3rd place digit 1, and on 4th place - digit 8
I tried it just a minute before, and is enough to run only first two of them.
Second method:
You can move master-2018 to another dir, e.g. /tmp,
then remove everything with master-*, and move back your master-2018 from tmp.
mkdir /tmp/backup
mv -r master-2018* /tmp/backup
rm -rf master-*
mv -r /tmp/backup/* .
New contributor
1
Ahh so, rm -rf master-????[2019-9999]* would remove all files from master-2019* to master-9999*?
â Dan James Palmer
1 hour ago
No, it not works this way. You can not specify numbers in [ ] braces. Only digits. You specify which digits will be places in the place of [xxxxxxx]. So max possibilities is range from 0 to 9 like this [0-9] or this [0123456789]. This can replace only one character from file name.rob@vps:mast$ ls
master-1990 master-1994 master-2006 master-2010 master-2014 master-2018
master-1991 master-1995 master-2007 master-2009 master-2013 master-2017
rob@vps:mast$ rm master-[2000-2019]*
rob@vps:mast$ ls
empty directory`
â Robert Zabkiewicz
1 hour ago
1
none of your commands will remove a file with the namemaster-of-puppets
â mosvy
1 hour ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If all files you want to delete has pattern like master-YYYY*, you can use those patterns:
rm -rf master-???[0-79]*
rm -rf master-??[02-9]*
rm -rf master-?[1-9]*
rm -rf master-[013-9]*
The goal is to ommit digits from a year number, so on first place after master- we need to ommit (don't remove) digit 2, on 2nd place digit 0, on 3rd place digit 1, and on 4th place - digit 8
I tried it just a minute before, and is enough to run only first two of them.
Second method:
You can move master-2018 to another dir, e.g. /tmp,
then remove everything with master-*, and move back your master-2018 from tmp.
mkdir /tmp/backup
mv -r master-2018* /tmp/backup
rm -rf master-*
mv -r /tmp/backup/* .
New contributor
If all files you want to delete has pattern like master-YYYY*, you can use those patterns:
rm -rf master-???[0-79]*
rm -rf master-??[02-9]*
rm -rf master-?[1-9]*
rm -rf master-[013-9]*
The goal is to ommit digits from a year number, so on first place after master- we need to ommit (don't remove) digit 2, on 2nd place digit 0, on 3rd place digit 1, and on 4th place - digit 8
I tried it just a minute before, and is enough to run only first two of them.
Second method:
You can move master-2018 to another dir, e.g. /tmp,
then remove everything with master-*, and move back your master-2018 from tmp.
mkdir /tmp/backup
mv -r master-2018* /tmp/backup
rm -rf master-*
mv -r /tmp/backup/* .
New contributor
edited 1 hour ago
New contributor
answered 1 hour ago
Robert Zabkiewicz
11
11
New contributor
New contributor
1
Ahh so, rm -rf master-????[2019-9999]* would remove all files from master-2019* to master-9999*?
â Dan James Palmer
1 hour ago
No, it not works this way. You can not specify numbers in [ ] braces. Only digits. You specify which digits will be places in the place of [xxxxxxx]. So max possibilities is range from 0 to 9 like this [0-9] or this [0123456789]. This can replace only one character from file name.rob@vps:mast$ ls
master-1990 master-1994 master-2006 master-2010 master-2014 master-2018
master-1991 master-1995 master-2007 master-2009 master-2013 master-2017
rob@vps:mast$ rm master-[2000-2019]*
rob@vps:mast$ ls
empty directory`
â Robert Zabkiewicz
1 hour ago
1
none of your commands will remove a file with the namemaster-of-puppets
â mosvy
1 hour ago
add a comment |Â
1
Ahh so, rm -rf master-????[2019-9999]* would remove all files from master-2019* to master-9999*?
â Dan James Palmer
1 hour ago
No, it not works this way. You can not specify numbers in [ ] braces. Only digits. You specify which digits will be places in the place of [xxxxxxx]. So max possibilities is range from 0 to 9 like this [0-9] or this [0123456789]. This can replace only one character from file name.rob@vps:mast$ ls
master-1990 master-1994 master-2006 master-2010 master-2014 master-2018
master-1991 master-1995 master-2007 master-2009 master-2013 master-2017
rob@vps:mast$ rm master-[2000-2019]*
rob@vps:mast$ ls
empty directory`
â Robert Zabkiewicz
1 hour ago
1
none of your commands will remove a file with the namemaster-of-puppets
â mosvy
1 hour ago
1
1
Ahh so, rm -rf master-????[2019-9999]* would remove all files from master-2019* to master-9999*?
â Dan James Palmer
1 hour ago
Ahh so, rm -rf master-????[2019-9999]* would remove all files from master-2019* to master-9999*?
â Dan James Palmer
1 hour ago
No, it not works this way. You can not specify numbers in [ ] braces. Only digits. You specify which digits will be places in the place of [xxxxxxx]. So max possibilities is range from 0 to 9 like this [0-9] or this [0123456789]. This can replace only one character from file name.
rob@vps:mast$ ls
master-1990 master-1994 master-2006 master-2010 master-2014 master-2018
master-1991 master-1995 master-2007 master-2009 master-2013 master-2017
rob@vps:mast$ rm master-[2000-2019]*
rob@vps:mast$ ls
empty directory`â Robert Zabkiewicz
1 hour ago
No, it not works this way. You can not specify numbers in [ ] braces. Only digits. You specify which digits will be places in the place of [xxxxxxx]. So max possibilities is range from 0 to 9 like this [0-9] or this [0123456789]. This can replace only one character from file name.
rob@vps:mast$ ls
master-1990 master-1994 master-2006 master-2010 master-2014 master-2018
master-1991 master-1995 master-2007 master-2009 master-2013 master-2017
rob@vps:mast$ rm master-[2000-2019]*
rob@vps:mast$ ls
empty directory`â Robert Zabkiewicz
1 hour ago
1
1
none of your commands will remove a file with the name
master-of-puppets
â mosvy
1 hour ago
none of your commands will remove a file with the name
master-of-puppets
â mosvy
1 hour ago
add a comment |Â
Dan James Palmer is a new contributor. Be nice, and check out our Code of Conduct.
Dan James Palmer is a new contributor. Be nice, and check out our Code of Conduct.
Dan James Palmer is a new contributor. Be nice, and check out our Code of Conduct.
Dan James Palmer is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f480241%2fremove-files-that-start-with-but-dont-contain%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password