Delete files with regular expression
I Tried to delete files that starts with A and ends with 2 numbers but It doesn't do a thing.
What I tried:
rm ^A*[0..9]2$
Where am I wrong?
bash unix regex rm
add a comment |
I Tried to delete files that starts with A and ends with 2 numbers but It doesn't do a thing.
What I tried:
rm ^A*[0..9]2$
Where am I wrong?
bash unix regex rm
add a comment |
I Tried to delete files that starts with A and ends with 2 numbers but It doesn't do a thing.
What I tried:
rm ^A*[0..9]2$
Where am I wrong?
bash unix regex rm
I Tried to delete files that starts with A and ends with 2 numbers but It doesn't do a thing.
What I tried:
rm ^A*[0..9]2$
Where am I wrong?
bash unix regex rm
bash unix regex rm
asked Feb 22 '12 at 18:07
gdorongdoron
2651310
2651310
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You can use the following command to delete all files matching your criteria:
ls | grep -P "^A.*[0-9]{2}$" | xargs -d"n" rm
How it works:
ls
lists all files (one by line since the result is piped).
grep -P "^A.*[0-9]{2}$"
filters the list of files and leaves only those that match the regular expression^A.*[0-9]{2}$
.*
indicates any number of occurrences of.
, where.
is a wildcard matching any character.[0-9]{2}
indicates exactly two occurrences of[0-9]
, that is, any digit.
xargs -d"n" rm
executesrm line
once for everyline
that is piped to it.
Where am I wrong?
For starters, rm
doesn't accept a regular expression as an argument. Besides the wildcard *
, every other character is treated literally.
Also, your regular expression is slightly off. For example, *
means any occurrences of ...
in a regular expression, so A*
matches A
, AA
, etc. and even an empty string.
For more information, visit Regular-Expressions.info.
2
Beware of spaces in file names.
– slhck
Feb 22 '12 at 18:27
1
The-d"n
switch fixes the spaces problem.
– Frg
Feb 22 '12 at 19:14
1
Note - some distros (like Mac OS) don't have agrep -P
(Perl regex).grep -E
may work in this case.
– bluescrubbie
Oct 2 '13 at 20:59
I prefer using-I
withxargs
and always test with non-lethal commands first:xargs -d"n" -I {} echo "{}"
– jozxyqk
Mar 24 '14 at 5:40
1
Parsingls
? See this question which points to this article. Because of the pitfalls you mayrm
what you don't want to.
– Kamil Maciorowski
Nov 15 '16 at 11:57
|
show 2 more comments
Or using find
:
find your-directory/ -name 'A*[0-9][0-9]' -delete
This solution will deal with weird file names.
3
This is a great solution. I prefer it because it is simpler and you can omit the -delete flag at the end first to see if your regex is correct before mass deleting your files.
– JAMESSTONEco
Apr 14 '15 at 21:31
1
Furthermore you have more control on what you delete, for example adding-type f
– Marco Sulla
Jun 8 '16 at 8:12
Can this be used to delete files and folders? It does not work for non empty folders.
– Alex
Sep 9 '17 at 11:46
@Alex nope, the directory must be empty (it wasn't an OP requirement anyway), you can use thexargs
approach withrm -f
.
– cYrus
Sep 9 '17 at 13:54
add a comment |
See the filename expansion section of the bash man page:
rm A*[0-9][0-9]
worked nicely for me.
– Nishanth Matha
Mar 15 '17 at 6:09
This was the simplest, yet complete answer to the question.
– Janac Meena
Oct 17 '17 at 17:22
add a comment |
The solution with regexp is 200 times better, even with that you can see which file will be deleted before using the command, cutting off the final pipe:
ls | grep -P "^A.*[0-9]{2}$"
Then if it's correct just use:
ls | grep -P "^A.*[0-9]{2}$" | xargs -d "n" rm
This is 200 times better because if you work with Unix it's important to know how to use grep. It's very powerful if you know how to use it.
This doesn't seem to add much beyond what Dennis's 4 year old answer already says.
– 8bittree
Nov 16 '16 at 19:48
"200 times" is a pretty specific. Lots of other commands are very powerful too, all you need to do is learn how to use them.
– glenn jackman
Mar 15 '17 at 11:22
add a comment |
This works on my mac:
rm $(ls | grep -e '^A*[0..9]2$')
add a comment |
protected by Pimp Juice IT Jan 9 at 13:58
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the following command to delete all files matching your criteria:
ls | grep -P "^A.*[0-9]{2}$" | xargs -d"n" rm
How it works:
ls
lists all files (one by line since the result is piped).
grep -P "^A.*[0-9]{2}$"
filters the list of files and leaves only those that match the regular expression^A.*[0-9]{2}$
.*
indicates any number of occurrences of.
, where.
is a wildcard matching any character.[0-9]{2}
indicates exactly two occurrences of[0-9]
, that is, any digit.
xargs -d"n" rm
executesrm line
once for everyline
that is piped to it.
Where am I wrong?
For starters, rm
doesn't accept a regular expression as an argument. Besides the wildcard *
, every other character is treated literally.
Also, your regular expression is slightly off. For example, *
means any occurrences of ...
in a regular expression, so A*
matches A
, AA
, etc. and even an empty string.
For more information, visit Regular-Expressions.info.
2
Beware of spaces in file names.
– slhck
Feb 22 '12 at 18:27
1
The-d"n
switch fixes the spaces problem.
– Frg
Feb 22 '12 at 19:14
1
Note - some distros (like Mac OS) don't have agrep -P
(Perl regex).grep -E
may work in this case.
– bluescrubbie
Oct 2 '13 at 20:59
I prefer using-I
withxargs
and always test with non-lethal commands first:xargs -d"n" -I {} echo "{}"
– jozxyqk
Mar 24 '14 at 5:40
1
Parsingls
? See this question which points to this article. Because of the pitfalls you mayrm
what you don't want to.
– Kamil Maciorowski
Nov 15 '16 at 11:57
|
show 2 more comments
You can use the following command to delete all files matching your criteria:
ls | grep -P "^A.*[0-9]{2}$" | xargs -d"n" rm
How it works:
ls
lists all files (one by line since the result is piped).
grep -P "^A.*[0-9]{2}$"
filters the list of files and leaves only those that match the regular expression^A.*[0-9]{2}$
.*
indicates any number of occurrences of.
, where.
is a wildcard matching any character.[0-9]{2}
indicates exactly two occurrences of[0-9]
, that is, any digit.
xargs -d"n" rm
executesrm line
once for everyline
that is piped to it.
Where am I wrong?
For starters, rm
doesn't accept a regular expression as an argument. Besides the wildcard *
, every other character is treated literally.
Also, your regular expression is slightly off. For example, *
means any occurrences of ...
in a regular expression, so A*
matches A
, AA
, etc. and even an empty string.
For more information, visit Regular-Expressions.info.
2
Beware of spaces in file names.
– slhck
Feb 22 '12 at 18:27
1
The-d"n
switch fixes the spaces problem.
– Frg
Feb 22 '12 at 19:14
1
Note - some distros (like Mac OS) don't have agrep -P
(Perl regex).grep -E
may work in this case.
– bluescrubbie
Oct 2 '13 at 20:59
I prefer using-I
withxargs
and always test with non-lethal commands first:xargs -d"n" -I {} echo "{}"
– jozxyqk
Mar 24 '14 at 5:40
1
Parsingls
? See this question which points to this article. Because of the pitfalls you mayrm
what you don't want to.
– Kamil Maciorowski
Nov 15 '16 at 11:57
|
show 2 more comments
You can use the following command to delete all files matching your criteria:
ls | grep -P "^A.*[0-9]{2}$" | xargs -d"n" rm
How it works:
ls
lists all files (one by line since the result is piped).
grep -P "^A.*[0-9]{2}$"
filters the list of files and leaves only those that match the regular expression^A.*[0-9]{2}$
.*
indicates any number of occurrences of.
, where.
is a wildcard matching any character.[0-9]{2}
indicates exactly two occurrences of[0-9]
, that is, any digit.
xargs -d"n" rm
executesrm line
once for everyline
that is piped to it.
Where am I wrong?
For starters, rm
doesn't accept a regular expression as an argument. Besides the wildcard *
, every other character is treated literally.
Also, your regular expression is slightly off. For example, *
means any occurrences of ...
in a regular expression, so A*
matches A
, AA
, etc. and even an empty string.
For more information, visit Regular-Expressions.info.
You can use the following command to delete all files matching your criteria:
ls | grep -P "^A.*[0-9]{2}$" | xargs -d"n" rm
How it works:
ls
lists all files (one by line since the result is piped).
grep -P "^A.*[0-9]{2}$"
filters the list of files and leaves only those that match the regular expression^A.*[0-9]{2}$
.*
indicates any number of occurrences of.
, where.
is a wildcard matching any character.[0-9]{2}
indicates exactly two occurrences of[0-9]
, that is, any digit.
xargs -d"n" rm
executesrm line
once for everyline
that is piped to it.
Where am I wrong?
For starters, rm
doesn't accept a regular expression as an argument. Besides the wildcard *
, every other character is treated literally.
Also, your regular expression is slightly off. For example, *
means any occurrences of ...
in a regular expression, so A*
matches A
, AA
, etc. and even an empty string.
For more information, visit Regular-Expressions.info.
edited Feb 22 '12 at 18:41
answered Feb 22 '12 at 18:19
DennisDennis
40.5k6101136
40.5k6101136
2
Beware of spaces in file names.
– slhck
Feb 22 '12 at 18:27
1
The-d"n
switch fixes the spaces problem.
– Frg
Feb 22 '12 at 19:14
1
Note - some distros (like Mac OS) don't have agrep -P
(Perl regex).grep -E
may work in this case.
– bluescrubbie
Oct 2 '13 at 20:59
I prefer using-I
withxargs
and always test with non-lethal commands first:xargs -d"n" -I {} echo "{}"
– jozxyqk
Mar 24 '14 at 5:40
1
Parsingls
? See this question which points to this article. Because of the pitfalls you mayrm
what you don't want to.
– Kamil Maciorowski
Nov 15 '16 at 11:57
|
show 2 more comments
2
Beware of spaces in file names.
– slhck
Feb 22 '12 at 18:27
1
The-d"n
switch fixes the spaces problem.
– Frg
Feb 22 '12 at 19:14
1
Note - some distros (like Mac OS) don't have agrep -P
(Perl regex).grep -E
may work in this case.
– bluescrubbie
Oct 2 '13 at 20:59
I prefer using-I
withxargs
and always test with non-lethal commands first:xargs -d"n" -I {} echo "{}"
– jozxyqk
Mar 24 '14 at 5:40
1
Parsingls
? See this question which points to this article. Because of the pitfalls you mayrm
what you don't want to.
– Kamil Maciorowski
Nov 15 '16 at 11:57
2
2
Beware of spaces in file names.
– slhck
Feb 22 '12 at 18:27
Beware of spaces in file names.
– slhck
Feb 22 '12 at 18:27
1
1
The
-d"n
switch fixes the spaces problem.– Frg
Feb 22 '12 at 19:14
The
-d"n
switch fixes the spaces problem.– Frg
Feb 22 '12 at 19:14
1
1
Note - some distros (like Mac OS) don't have a
grep -P
(Perl regex). grep -E
may work in this case.– bluescrubbie
Oct 2 '13 at 20:59
Note - some distros (like Mac OS) don't have a
grep -P
(Perl regex). grep -E
may work in this case.– bluescrubbie
Oct 2 '13 at 20:59
I prefer using
-I
with xargs
and always test with non-lethal commands first: xargs -d"n" -I {} echo "{}"
– jozxyqk
Mar 24 '14 at 5:40
I prefer using
-I
with xargs
and always test with non-lethal commands first: xargs -d"n" -I {} echo "{}"
– jozxyqk
Mar 24 '14 at 5:40
1
1
Parsing
ls
? See this question which points to this article. Because of the pitfalls you may rm
what you don't want to.– Kamil Maciorowski
Nov 15 '16 at 11:57
Parsing
ls
? See this question which points to this article. Because of the pitfalls you may rm
what you don't want to.– Kamil Maciorowski
Nov 15 '16 at 11:57
|
show 2 more comments
Or using find
:
find your-directory/ -name 'A*[0-9][0-9]' -delete
This solution will deal with weird file names.
3
This is a great solution. I prefer it because it is simpler and you can omit the -delete flag at the end first to see if your regex is correct before mass deleting your files.
– JAMESSTONEco
Apr 14 '15 at 21:31
1
Furthermore you have more control on what you delete, for example adding-type f
– Marco Sulla
Jun 8 '16 at 8:12
Can this be used to delete files and folders? It does not work for non empty folders.
– Alex
Sep 9 '17 at 11:46
@Alex nope, the directory must be empty (it wasn't an OP requirement anyway), you can use thexargs
approach withrm -f
.
– cYrus
Sep 9 '17 at 13:54
add a comment |
Or using find
:
find your-directory/ -name 'A*[0-9][0-9]' -delete
This solution will deal with weird file names.
3
This is a great solution. I prefer it because it is simpler and you can omit the -delete flag at the end first to see if your regex is correct before mass deleting your files.
– JAMESSTONEco
Apr 14 '15 at 21:31
1
Furthermore you have more control on what you delete, for example adding-type f
– Marco Sulla
Jun 8 '16 at 8:12
Can this be used to delete files and folders? It does not work for non empty folders.
– Alex
Sep 9 '17 at 11:46
@Alex nope, the directory must be empty (it wasn't an OP requirement anyway), you can use thexargs
approach withrm -f
.
– cYrus
Sep 9 '17 at 13:54
add a comment |
Or using find
:
find your-directory/ -name 'A*[0-9][0-9]' -delete
This solution will deal with weird file names.
Or using find
:
find your-directory/ -name 'A*[0-9][0-9]' -delete
This solution will deal with weird file names.
answered Feb 22 '12 at 19:01
cYruscYrus
15.8k55268
15.8k55268
3
This is a great solution. I prefer it because it is simpler and you can omit the -delete flag at the end first to see if your regex is correct before mass deleting your files.
– JAMESSTONEco
Apr 14 '15 at 21:31
1
Furthermore you have more control on what you delete, for example adding-type f
– Marco Sulla
Jun 8 '16 at 8:12
Can this be used to delete files and folders? It does not work for non empty folders.
– Alex
Sep 9 '17 at 11:46
@Alex nope, the directory must be empty (it wasn't an OP requirement anyway), you can use thexargs
approach withrm -f
.
– cYrus
Sep 9 '17 at 13:54
add a comment |
3
This is a great solution. I prefer it because it is simpler and you can omit the -delete flag at the end first to see if your regex is correct before mass deleting your files.
– JAMESSTONEco
Apr 14 '15 at 21:31
1
Furthermore you have more control on what you delete, for example adding-type f
– Marco Sulla
Jun 8 '16 at 8:12
Can this be used to delete files and folders? It does not work for non empty folders.
– Alex
Sep 9 '17 at 11:46
@Alex nope, the directory must be empty (it wasn't an OP requirement anyway), you can use thexargs
approach withrm -f
.
– cYrus
Sep 9 '17 at 13:54
3
3
This is a great solution. I prefer it because it is simpler and you can omit the -delete flag at the end first to see if your regex is correct before mass deleting your files.
– JAMESSTONEco
Apr 14 '15 at 21:31
This is a great solution. I prefer it because it is simpler and you can omit the -delete flag at the end first to see if your regex is correct before mass deleting your files.
– JAMESSTONEco
Apr 14 '15 at 21:31
1
1
Furthermore you have more control on what you delete, for example adding
-type f
– Marco Sulla
Jun 8 '16 at 8:12
Furthermore you have more control on what you delete, for example adding
-type f
– Marco Sulla
Jun 8 '16 at 8:12
Can this be used to delete files and folders? It does not work for non empty folders.
– Alex
Sep 9 '17 at 11:46
Can this be used to delete files and folders? It does not work for non empty folders.
– Alex
Sep 9 '17 at 11:46
@Alex nope, the directory must be empty (it wasn't an OP requirement anyway), you can use the
xargs
approach with rm -f
.– cYrus
Sep 9 '17 at 13:54
@Alex nope, the directory must be empty (it wasn't an OP requirement anyway), you can use the
xargs
approach with rm -f
.– cYrus
Sep 9 '17 at 13:54
add a comment |
See the filename expansion section of the bash man page:
rm A*[0-9][0-9]
worked nicely for me.
– Nishanth Matha
Mar 15 '17 at 6:09
This was the simplest, yet complete answer to the question.
– Janac Meena
Oct 17 '17 at 17:22
add a comment |
See the filename expansion section of the bash man page:
rm A*[0-9][0-9]
worked nicely for me.
– Nishanth Matha
Mar 15 '17 at 6:09
This was the simplest, yet complete answer to the question.
– Janac Meena
Oct 17 '17 at 17:22
add a comment |
See the filename expansion section of the bash man page:
rm A*[0-9][0-9]
See the filename expansion section of the bash man page:
rm A*[0-9][0-9]
answered Feb 22 '12 at 19:46
glenn jackmanglenn jackman
15.7k22644
15.7k22644
worked nicely for me.
– Nishanth Matha
Mar 15 '17 at 6:09
This was the simplest, yet complete answer to the question.
– Janac Meena
Oct 17 '17 at 17:22
add a comment |
worked nicely for me.
– Nishanth Matha
Mar 15 '17 at 6:09
This was the simplest, yet complete answer to the question.
– Janac Meena
Oct 17 '17 at 17:22
worked nicely for me.
– Nishanth Matha
Mar 15 '17 at 6:09
worked nicely for me.
– Nishanth Matha
Mar 15 '17 at 6:09
This was the simplest, yet complete answer to the question.
– Janac Meena
Oct 17 '17 at 17:22
This was the simplest, yet complete answer to the question.
– Janac Meena
Oct 17 '17 at 17:22
add a comment |
The solution with regexp is 200 times better, even with that you can see which file will be deleted before using the command, cutting off the final pipe:
ls | grep -P "^A.*[0-9]{2}$"
Then if it's correct just use:
ls | grep -P "^A.*[0-9]{2}$" | xargs -d "n" rm
This is 200 times better because if you work with Unix it's important to know how to use grep. It's very powerful if you know how to use it.
This doesn't seem to add much beyond what Dennis's 4 year old answer already says.
– 8bittree
Nov 16 '16 at 19:48
"200 times" is a pretty specific. Lots of other commands are very powerful too, all you need to do is learn how to use them.
– glenn jackman
Mar 15 '17 at 11:22
add a comment |
The solution with regexp is 200 times better, even with that you can see which file will be deleted before using the command, cutting off the final pipe:
ls | grep -P "^A.*[0-9]{2}$"
Then if it's correct just use:
ls | grep -P "^A.*[0-9]{2}$" | xargs -d "n" rm
This is 200 times better because if you work with Unix it's important to know how to use grep. It's very powerful if you know how to use it.
This doesn't seem to add much beyond what Dennis's 4 year old answer already says.
– 8bittree
Nov 16 '16 at 19:48
"200 times" is a pretty specific. Lots of other commands are very powerful too, all you need to do is learn how to use them.
– glenn jackman
Mar 15 '17 at 11:22
add a comment |
The solution with regexp is 200 times better, even with that you can see which file will be deleted before using the command, cutting off the final pipe:
ls | grep -P "^A.*[0-9]{2}$"
Then if it's correct just use:
ls | grep -P "^A.*[0-9]{2}$" | xargs -d "n" rm
This is 200 times better because if you work with Unix it's important to know how to use grep. It's very powerful if you know how to use it.
The solution with regexp is 200 times better, even with that you can see which file will be deleted before using the command, cutting off the final pipe:
ls | grep -P "^A.*[0-9]{2}$"
Then if it's correct just use:
ls | grep -P "^A.*[0-9]{2}$" | xargs -d "n" rm
This is 200 times better because if you work with Unix it's important to know how to use grep. It's very powerful if you know how to use it.
edited Nov 11 '16 at 0:14
3498DB
15.7k114762
15.7k114762
answered Nov 10 '16 at 19:25
SalvatoreSalvatore
191
191
This doesn't seem to add much beyond what Dennis's 4 year old answer already says.
– 8bittree
Nov 16 '16 at 19:48
"200 times" is a pretty specific. Lots of other commands are very powerful too, all you need to do is learn how to use them.
– glenn jackman
Mar 15 '17 at 11:22
add a comment |
This doesn't seem to add much beyond what Dennis's 4 year old answer already says.
– 8bittree
Nov 16 '16 at 19:48
"200 times" is a pretty specific. Lots of other commands are very powerful too, all you need to do is learn how to use them.
– glenn jackman
Mar 15 '17 at 11:22
This doesn't seem to add much beyond what Dennis's 4 year old answer already says.
– 8bittree
Nov 16 '16 at 19:48
This doesn't seem to add much beyond what Dennis's 4 year old answer already says.
– 8bittree
Nov 16 '16 at 19:48
"200 times" is a pretty specific. Lots of other commands are very powerful too, all you need to do is learn how to use them.
– glenn jackman
Mar 15 '17 at 11:22
"200 times" is a pretty specific. Lots of other commands are very powerful too, all you need to do is learn how to use them.
– glenn jackman
Mar 15 '17 at 11:22
add a comment |
This works on my mac:
rm $(ls | grep -e '^A*[0..9]2$')
add a comment |
This works on my mac:
rm $(ls | grep -e '^A*[0..9]2$')
add a comment |
This works on my mac:
rm $(ls | grep -e '^A*[0..9]2$')
This works on my mac:
rm $(ls | grep -e '^A*[0..9]2$')
answered Jan 9 at 13:35
RayRay
1
1
add a comment |
add a comment |
protected by Pimp Juice IT Jan 9 at 13:58
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?