Delete all folders containing files which match pattern
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to delete all subdirectories of my current working directory which contain a rar file.
My first attempt: find -name *.rar -exec rm -r /.. ';'
failed because that is not a valid directory. I tried using dirname
for a more sensible command, but decided to just ask after almost deleting stuff I didn't mean to.
I'm using Cygwin on Windows 7.
find directory rm
add a comment |
I'm trying to delete all subdirectories of my current working directory which contain a rar file.
My first attempt: find -name *.rar -exec rm -r /.. ';'
failed because that is not a valid directory. I tried using dirname
for a more sensible command, but decided to just ask after almost deleting stuff I didn't mean to.
I'm using Cygwin on Windows 7.
find directory rm
add a comment |
I'm trying to delete all subdirectories of my current working directory which contain a rar file.
My first attempt: find -name *.rar -exec rm -r /.. ';'
failed because that is not a valid directory. I tried using dirname
for a more sensible command, but decided to just ask after almost deleting stuff I didn't mean to.
I'm using Cygwin on Windows 7.
find directory rm
I'm trying to delete all subdirectories of my current working directory which contain a rar file.
My first attempt: find -name *.rar -exec rm -r /.. ';'
failed because that is not a valid directory. I tried using dirname
for a more sensible command, but decided to just ask after almost deleting stuff I didn't mean to.
I'm using Cygwin on Windows 7.
find directory rm
find directory rm
edited Mar 9 at 14:19
Rui F Ribeiro
41.9k1483142
41.9k1483142
asked Dec 30 '12 at 21:55
MikeFHayMikeFHay
12815
12815
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
You could do it with a pair of statements.
First, get a list of directories to remove using
find -name *.rar -exec dirname ';' > toremove
Next, cat toremove
to make sure it has the folders you want. Then, pass it to rm -rf
using
sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
Last, rm toremove
.
1
Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line tosed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
– MikeFHay
Dec 31 '12 at 1:27
1
This will not work on files with spaces. Also will break if the current directory contains a*.rar
file due to lack of quoting.
– jordanm
Dec 31 '12 at 3:28
@MikeL: Thanks for the improvement; I replaced the second command with your version.
– cpast
Dec 31 '12 at 3:39
2
@MikeL This is good enough for you because Windows filenames can't contain"
or newlines, but in general, this method is unsafe. There's no convenient way to usexargs
(exceptxargs -0
) with arbitrary file names. It's usually better to usefind -exec
.
– Gilles
Jan 1 '13 at 1:14
add a comment |
With zsh:
rm -rf **/*.rar(:h)
The suffix :h
applies the history expansion modifier h
(“head”) which removes the basename of each match, keeping only the directory part.
Make sure to check that these are really the directories you want to delete! For example, move them to a temporary directory first:
mkdir DELETE
mv **/*.rar(:h) DELETE/
# check that you really want to delete everything in DELETE
rm -r DELETE
add a comment |
You can use bash -c
to perform more advanced operations in and -exec
for find. The problem with using a temp file and cat in combination with xargs
is that it will break if a file contains a space, newline, or tab. The following should work:
find . -type f -name '*.rar' -exec bash -c 'rm -rf "$@%/*"' -- +
Using +
for find with "$@" will execute rm
one time like with xargs
.
add a comment |
Bash 4+ solution:
shopt -s globstar
for f in ./**/*.rar; do rm -rf "$f%/*"; done
add a comment |
You could try something like this:
for dir in */; do # iterate all subdirectories
touch $dir"dummy.rar" # create a "rar"-file...
for file in $dir*.rar; do # ...so this won't break on zero such
rm $file # remove the dummy, and all archives
done
done
OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)
– Emanuel Berg
Jan 1 '13 at 1:32
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f59890%2fdelete-all-folders-containing-files-which-match-pattern%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could do it with a pair of statements.
First, get a list of directories to remove using
find -name *.rar -exec dirname ';' > toremove
Next, cat toremove
to make sure it has the folders you want. Then, pass it to rm -rf
using
sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
Last, rm toremove
.
1
Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line tosed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
– MikeFHay
Dec 31 '12 at 1:27
1
This will not work on files with spaces. Also will break if the current directory contains a*.rar
file due to lack of quoting.
– jordanm
Dec 31 '12 at 3:28
@MikeL: Thanks for the improvement; I replaced the second command with your version.
– cpast
Dec 31 '12 at 3:39
2
@MikeL This is good enough for you because Windows filenames can't contain"
or newlines, but in general, this method is unsafe. There's no convenient way to usexargs
(exceptxargs -0
) with arbitrary file names. It's usually better to usefind -exec
.
– Gilles
Jan 1 '13 at 1:14
add a comment |
You could do it with a pair of statements.
First, get a list of directories to remove using
find -name *.rar -exec dirname ';' > toremove
Next, cat toremove
to make sure it has the folders you want. Then, pass it to rm -rf
using
sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
Last, rm toremove
.
1
Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line tosed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
– MikeFHay
Dec 31 '12 at 1:27
1
This will not work on files with spaces. Also will break if the current directory contains a*.rar
file due to lack of quoting.
– jordanm
Dec 31 '12 at 3:28
@MikeL: Thanks for the improvement; I replaced the second command with your version.
– cpast
Dec 31 '12 at 3:39
2
@MikeL This is good enough for you because Windows filenames can't contain"
or newlines, but in general, this method is unsafe. There's no convenient way to usexargs
(exceptxargs -0
) with arbitrary file names. It's usually better to usefind -exec
.
– Gilles
Jan 1 '13 at 1:14
add a comment |
You could do it with a pair of statements.
First, get a list of directories to remove using
find -name *.rar -exec dirname ';' > toremove
Next, cat toremove
to make sure it has the folders you want. Then, pass it to rm -rf
using
sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
Last, rm toremove
.
You could do it with a pair of statements.
First, get a list of directories to remove using
find -name *.rar -exec dirname ';' > toremove
Next, cat toremove
to make sure it has the folders you want. Then, pass it to rm -rf
using
sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
Last, rm toremove
.
edited Dec 31 '12 at 3:38
answered Dec 31 '12 at 0:11
cpastcpast
27829
27829
1
Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line tosed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
– MikeFHay
Dec 31 '12 at 1:27
1
This will not work on files with spaces. Also will break if the current directory contains a*.rar
file due to lack of quoting.
– jordanm
Dec 31 '12 at 3:28
@MikeL: Thanks for the improvement; I replaced the second command with your version.
– cpast
Dec 31 '12 at 3:39
2
@MikeL This is good enough for you because Windows filenames can't contain"
or newlines, but in general, this method is unsafe. There's no convenient way to usexargs
(exceptxargs -0
) with arbitrary file names. It's usually better to usefind -exec
.
– Gilles
Jan 1 '13 at 1:14
add a comment |
1
Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line tosed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
– MikeFHay
Dec 31 '12 at 1:27
1
This will not work on files with spaces. Also will break if the current directory contains a*.rar
file due to lack of quoting.
– jordanm
Dec 31 '12 at 3:28
@MikeL: Thanks for the improvement; I replaced the second command with your version.
– cpast
Dec 31 '12 at 3:39
2
@MikeL This is good enough for you because Windows filenames can't contain"
or newlines, but in general, this method is unsafe. There's no convenient way to usexargs
(exceptxargs -0
) with arbitrary file names. It's usually better to usefind -exec
.
– Gilles
Jan 1 '13 at 1:14
1
1
Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line to
sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
– MikeFHay
Dec 31 '12 at 1:27
Excellent tip. This still doesn't quite work when the directory names contain spaces. I solved this by swapping the second line to
sed 's/^/"/g' toremove | sed 's/$/"/g' | xargs rm -r
– MikeFHay
Dec 31 '12 at 1:27
1
1
This will not work on files with spaces. Also will break if the current directory contains a
*.rar
file due to lack of quoting.– jordanm
Dec 31 '12 at 3:28
This will not work on files with spaces. Also will break if the current directory contains a
*.rar
file due to lack of quoting.– jordanm
Dec 31 '12 at 3:28
@MikeL: Thanks for the improvement; I replaced the second command with your version.
– cpast
Dec 31 '12 at 3:39
@MikeL: Thanks for the improvement; I replaced the second command with your version.
– cpast
Dec 31 '12 at 3:39
2
2
@MikeL This is good enough for you because Windows filenames can't contain
"
or newlines, but in general, this method is unsafe. There's no convenient way to use xargs
(except xargs -0
) with arbitrary file names. It's usually better to use find -exec
.– Gilles
Jan 1 '13 at 1:14
@MikeL This is good enough for you because Windows filenames can't contain
"
or newlines, but in general, this method is unsafe. There's no convenient way to use xargs
(except xargs -0
) with arbitrary file names. It's usually better to use find -exec
.– Gilles
Jan 1 '13 at 1:14
add a comment |
With zsh:
rm -rf **/*.rar(:h)
The suffix :h
applies the history expansion modifier h
(“head”) which removes the basename of each match, keeping only the directory part.
Make sure to check that these are really the directories you want to delete! For example, move them to a temporary directory first:
mkdir DELETE
mv **/*.rar(:h) DELETE/
# check that you really want to delete everything in DELETE
rm -r DELETE
add a comment |
With zsh:
rm -rf **/*.rar(:h)
The suffix :h
applies the history expansion modifier h
(“head”) which removes the basename of each match, keeping only the directory part.
Make sure to check that these are really the directories you want to delete! For example, move them to a temporary directory first:
mkdir DELETE
mv **/*.rar(:h) DELETE/
# check that you really want to delete everything in DELETE
rm -r DELETE
add a comment |
With zsh:
rm -rf **/*.rar(:h)
The suffix :h
applies the history expansion modifier h
(“head”) which removes the basename of each match, keeping only the directory part.
Make sure to check that these are really the directories you want to delete! For example, move them to a temporary directory first:
mkdir DELETE
mv **/*.rar(:h) DELETE/
# check that you really want to delete everything in DELETE
rm -r DELETE
With zsh:
rm -rf **/*.rar(:h)
The suffix :h
applies the history expansion modifier h
(“head”) which removes the basename of each match, keeping only the directory part.
Make sure to check that these are really the directories you want to delete! For example, move them to a temporary directory first:
mkdir DELETE
mv **/*.rar(:h) DELETE/
# check that you really want to delete everything in DELETE
rm -r DELETE
answered Jan 1 '13 at 1:12
GillesGilles
546k12911111624
546k12911111624
add a comment |
add a comment |
You can use bash -c
to perform more advanced operations in and -exec
for find. The problem with using a temp file and cat in combination with xargs
is that it will break if a file contains a space, newline, or tab. The following should work:
find . -type f -name '*.rar' -exec bash -c 'rm -rf "$@%/*"' -- +
Using +
for find with "$@" will execute rm
one time like with xargs
.
add a comment |
You can use bash -c
to perform more advanced operations in and -exec
for find. The problem with using a temp file and cat in combination with xargs
is that it will break if a file contains a space, newline, or tab. The following should work:
find . -type f -name '*.rar' -exec bash -c 'rm -rf "$@%/*"' -- +
Using +
for find with "$@" will execute rm
one time like with xargs
.
add a comment |
You can use bash -c
to perform more advanced operations in and -exec
for find. The problem with using a temp file and cat in combination with xargs
is that it will break if a file contains a space, newline, or tab. The following should work:
find . -type f -name '*.rar' -exec bash -c 'rm -rf "$@%/*"' -- +
Using +
for find with "$@" will execute rm
one time like with xargs
.
You can use bash -c
to perform more advanced operations in and -exec
for find. The problem with using a temp file and cat in combination with xargs
is that it will break if a file contains a space, newline, or tab. The following should work:
find . -type f -name '*.rar' -exec bash -c 'rm -rf "$@%/*"' -- +
Using +
for find with "$@" will execute rm
one time like with xargs
.
edited Jan 1 '13 at 20:33
answered Dec 31 '12 at 3:31
jordanmjordanm
31.3k38897
31.3k38897
add a comment |
add a comment |
Bash 4+ solution:
shopt -s globstar
for f in ./**/*.rar; do rm -rf "$f%/*"; done
add a comment |
Bash 4+ solution:
shopt -s globstar
for f in ./**/*.rar; do rm -rf "$f%/*"; done
add a comment |
Bash 4+ solution:
shopt -s globstar
for f in ./**/*.rar; do rm -rf "$f%/*"; done
Bash 4+ solution:
shopt -s globstar
for f in ./**/*.rar; do rm -rf "$f%/*"; done
answered May 28 '13 at 20:52
evilsoupevilsoup
4,34421737
4,34421737
add a comment |
add a comment |
You could try something like this:
for dir in */; do # iterate all subdirectories
touch $dir"dummy.rar" # create a "rar"-file...
for file in $dir*.rar; do # ...so this won't break on zero such
rm $file # remove the dummy, and all archives
done
done
OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)
– Emanuel Berg
Jan 1 '13 at 1:32
add a comment |
You could try something like this:
for dir in */; do # iterate all subdirectories
touch $dir"dummy.rar" # create a "rar"-file...
for file in $dir*.rar; do # ...so this won't break on zero such
rm $file # remove the dummy, and all archives
done
done
OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)
– Emanuel Berg
Jan 1 '13 at 1:32
add a comment |
You could try something like this:
for dir in */; do # iterate all subdirectories
touch $dir"dummy.rar" # create a "rar"-file...
for file in $dir*.rar; do # ...so this won't break on zero such
rm $file # remove the dummy, and all archives
done
done
You could try something like this:
for dir in */; do # iterate all subdirectories
touch $dir"dummy.rar" # create a "rar"-file...
for file in $dir*.rar; do # ...so this won't break on zero such
rm $file # remove the dummy, and all archives
done
done
answered Dec 31 '12 at 3:20
Emanuel BergEmanuel Berg
3,75152954
3,75152954
OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)
– Emanuel Berg
Jan 1 '13 at 1:32
add a comment |
OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)
– Emanuel Berg
Jan 1 '13 at 1:32
OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)
– Emanuel Berg
Jan 1 '13 at 1:32
OK, I read @cpast's answer and I see now that you want to remove the subdirectories, not the just the archives. My mistake! (But, isn't that a bit strange behavior?)
– Emanuel Berg
Jan 1 '13 at 1:32
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f59890%2fdelete-all-folders-containing-files-which-match-pattern%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown