delete folders with the same name as the parent folders recursively in bash?
up vote
0
down vote
favorite
So some file copying went wrong in one of my machines and I'm wasting a ludicrous amount of space by having a copy of each folder inside of the parent with the same name, and i'd want to delete them all. Example:
/mnt/test/files/foo
_ /mnt/test/files/foo/file1 (etc)
|__ /mnt/test/files/foo/foo
_ /mnt/test/files/foo/foo/file1 (etc)
|_ /mnt/files/foo/foo2
_ /mnt/files/foo/foo2/file1 (etc)
|_ /mnt/files/foo/foo2/foo2
_ /mnt/files/foo/foo2/foo2/file1 (etc)
so obviously i want to delete /mnt/files/foo/foo
, /mnt/files/foo/foo2/foo2
and its contents (and so on) entirely and stop wasting space. What would be a good way to script it in bash?
Thanks in advance.
linux bash scripting terminal
New contributor
add a comment |
up vote
0
down vote
favorite
So some file copying went wrong in one of my machines and I'm wasting a ludicrous amount of space by having a copy of each folder inside of the parent with the same name, and i'd want to delete them all. Example:
/mnt/test/files/foo
_ /mnt/test/files/foo/file1 (etc)
|__ /mnt/test/files/foo/foo
_ /mnt/test/files/foo/foo/file1 (etc)
|_ /mnt/files/foo/foo2
_ /mnt/files/foo/foo2/file1 (etc)
|_ /mnt/files/foo/foo2/foo2
_ /mnt/files/foo/foo2/foo2/file1 (etc)
so obviously i want to delete /mnt/files/foo/foo
, /mnt/files/foo/foo2/foo2
and its contents (and so on) entirely and stop wasting space. What would be a good way to script it in bash?
Thanks in advance.
linux bash scripting terminal
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
So some file copying went wrong in one of my machines and I'm wasting a ludicrous amount of space by having a copy of each folder inside of the parent with the same name, and i'd want to delete them all. Example:
/mnt/test/files/foo
_ /mnt/test/files/foo/file1 (etc)
|__ /mnt/test/files/foo/foo
_ /mnt/test/files/foo/foo/file1 (etc)
|_ /mnt/files/foo/foo2
_ /mnt/files/foo/foo2/file1 (etc)
|_ /mnt/files/foo/foo2/foo2
_ /mnt/files/foo/foo2/foo2/file1 (etc)
so obviously i want to delete /mnt/files/foo/foo
, /mnt/files/foo/foo2/foo2
and its contents (and so on) entirely and stop wasting space. What would be a good way to script it in bash?
Thanks in advance.
linux bash scripting terminal
New contributor
So some file copying went wrong in one of my machines and I'm wasting a ludicrous amount of space by having a copy of each folder inside of the parent with the same name, and i'd want to delete them all. Example:
/mnt/test/files/foo
_ /mnt/test/files/foo/file1 (etc)
|__ /mnt/test/files/foo/foo
_ /mnt/test/files/foo/foo/file1 (etc)
|_ /mnt/files/foo/foo2
_ /mnt/files/foo/foo2/file1 (etc)
|_ /mnt/files/foo/foo2/foo2
_ /mnt/files/foo/foo2/foo2/file1 (etc)
so obviously i want to delete /mnt/files/foo/foo
, /mnt/files/foo/foo2/foo2
and its contents (and so on) entirely and stop wasting space. What would be a good way to script it in bash?
Thanks in advance.
linux bash scripting terminal
linux bash scripting terminal
New contributor
New contributor
New contributor
asked 2 days ago
Webodan
31
31
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
If your find
supports the -regex
predicate, you can list the directories with:
find . -type d -regex '.*/([^/]*)/1' -prune -print
to remove them, you can change -print
to:
-exec rm -rf {} +
But be sure to check the list first so you don't delete any files you need.
Nice! this worked. Thank you!
– Webodan
2 days ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
If your find
supports the -regex
predicate, you can list the directories with:
find . -type d -regex '.*/([^/]*)/1' -prune -print
to remove them, you can change -print
to:
-exec rm -rf {} +
But be sure to check the list first so you don't delete any files you need.
Nice! this worked. Thank you!
– Webodan
2 days ago
add a comment |
up vote
1
down vote
accepted
If your find
supports the -regex
predicate, you can list the directories with:
find . -type d -regex '.*/([^/]*)/1' -prune -print
to remove them, you can change -print
to:
-exec rm -rf {} +
But be sure to check the list first so you don't delete any files you need.
Nice! this worked. Thank you!
– Webodan
2 days ago
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If your find
supports the -regex
predicate, you can list the directories with:
find . -type d -regex '.*/([^/]*)/1' -prune -print
to remove them, you can change -print
to:
-exec rm -rf {} +
But be sure to check the list first so you don't delete any files you need.
If your find
supports the -regex
predicate, you can list the directories with:
find . -type d -regex '.*/([^/]*)/1' -prune -print
to remove them, you can change -print
to:
-exec rm -rf {} +
But be sure to check the list first so you don't delete any files you need.
edited 2 days ago
Stéphane Chazelas
294k54552894
294k54552894
answered 2 days ago
choroba
25.8k44470
25.8k44470
Nice! this worked. Thank you!
– Webodan
2 days ago
add a comment |
Nice! this worked. Thank you!
– Webodan
2 days ago
Nice! this worked. Thank you!
– Webodan
2 days ago
Nice! this worked. Thank you!
– Webodan
2 days ago
add a comment |
Webodan is a new contributor. Be nice, and check out our Code of Conduct.
Webodan is a new contributor. Be nice, and check out our Code of Conduct.
Webodan is a new contributor. Be nice, and check out our Code of Conduct.
Webodan 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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482752%2fdelete-folders-with-the-same-name-as-the-parent-folders-recursively-in-bash%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