bash - remove all directories (and contents) but not files in pwd
I'd like to remove all directories from the pwd but leave the files in the pwd alone. If the content of my pwd is:
mydir1
mydir2
myfile1
myfile2
then I'd like to be left with just
myfile1
myfile2
I assume that I need to use rm -r -i
Am I correct?
linux bash mingw
add a comment |
I'd like to remove all directories from the pwd but leave the files in the pwd alone. If the content of my pwd is:
mydir1
mydir2
myfile1
myfile2
then I'd like to be left with just
myfile1
myfile2
I assume that I need to use rm -r -i
Am I correct?
linux bash mingw
add a comment |
I'd like to remove all directories from the pwd but leave the files in the pwd alone. If the content of my pwd is:
mydir1
mydir2
myfile1
myfile2
then I'd like to be left with just
myfile1
myfile2
I assume that I need to use rm -r -i
Am I correct?
linux bash mingw
I'd like to remove all directories from the pwd but leave the files in the pwd alone. If the content of my pwd is:
mydir1
mydir2
myfile1
myfile2
then I'd like to be left with just
myfile1
myfile2
I assume that I need to use rm -r -i
Am I correct?
linux bash mingw
linux bash mingw
edited Feb 7 '14 at 22:05
lesmana
12.9k53442
12.9k53442
asked Feb 7 '14 at 15:33
atomh33ls
3245924
3245924
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
No that would give you "missing operand" since you didn't specify anything. Putting a "*" would prompt also for files.
I'd give a try to:
find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} ;
The "mindepth 1" will exclude "." from the results, the "maxdepth 1" will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.
add a comment |
I found this one somewhere:
rm -r */
Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...
1
That will also follow symbolic links, which very probably isn't desired here.
– JdeBP
Feb 7 '14 at 16:43
add a comment |
Use
rm -rf ./*/
That avoids interactive mode an deletes only directories in your local directory.
As JdeBP pointed out on Martin's very similar answer, if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
– Scott
Dec 21 '18 at 7:07
add a comment |
Something like this should work:
find /path -type d -exec rm -rf '{}' ;
-type d looks for only directories
add a comment |
you can also try in this way to delete only all folders not files from any location in linux.
#delete only all dir and don't touch files
#!/bin/bash
for dir in `ls -l | grep ^d | awk '{print $9}'`
do
echo "going to delete $dir " `rm -rf $dir`
done
ls
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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%2fsuperuser.com%2fquestions%2f713741%2fbash-remove-all-directories-and-contents-but-not-files-in-pwd%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
No that would give you "missing operand" since you didn't specify anything. Putting a "*" would prompt also for files.
I'd give a try to:
find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} ;
The "mindepth 1" will exclude "." from the results, the "maxdepth 1" will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.
add a comment |
No that would give you "missing operand" since you didn't specify anything. Putting a "*" would prompt also for files.
I'd give a try to:
find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} ;
The "mindepth 1" will exclude "." from the results, the "maxdepth 1" will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.
add a comment |
No that would give you "missing operand" since you didn't specify anything. Putting a "*" would prompt also for files.
I'd give a try to:
find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} ;
The "mindepth 1" will exclude "." from the results, the "maxdepth 1" will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.
No that would give you "missing operand" since you didn't specify anything. Putting a "*" would prompt also for files.
I'd give a try to:
find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} ;
The "mindepth 1" will exclude "." from the results, the "maxdepth 1" will exclude trying to do under the directories that will anyway get deleted (therefore creating a warning). But in practice you could leave them both out if you agree to have a few "innocent" warnings.
answered Feb 7 '14 at 15:42
fede.evol
1,652175
1,652175
add a comment |
add a comment |
I found this one somewhere:
rm -r */
Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...
1
That will also follow symbolic links, which very probably isn't desired here.
– JdeBP
Feb 7 '14 at 16:43
add a comment |
I found this one somewhere:
rm -r */
Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...
1
That will also follow symbolic links, which very probably isn't desired here.
– JdeBP
Feb 7 '14 at 16:43
add a comment |
I found this one somewhere:
rm -r */
Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...
I found this one somewhere:
rm -r */
Seems the easiest way to go. With your example, you would have to confirm each case, if you have 5 files it's OK, but with bigger file structures an interactive mode is't the way to go... Just as a suggestion, if it's important information, make a backup...
answered Feb 7 '14 at 15:42
Martin
23115
23115
1
That will also follow symbolic links, which very probably isn't desired here.
– JdeBP
Feb 7 '14 at 16:43
add a comment |
1
That will also follow symbolic links, which very probably isn't desired here.
– JdeBP
Feb 7 '14 at 16:43
1
1
That will also follow symbolic links, which very probably isn't desired here.
– JdeBP
Feb 7 '14 at 16:43
That will also follow symbolic links, which very probably isn't desired here.
– JdeBP
Feb 7 '14 at 16:43
add a comment |
Use
rm -rf ./*/
That avoids interactive mode an deletes only directories in your local directory.
As JdeBP pointed out on Martin's very similar answer, if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
– Scott
Dec 21 '18 at 7:07
add a comment |
Use
rm -rf ./*/
That avoids interactive mode an deletes only directories in your local directory.
As JdeBP pointed out on Martin's very similar answer, if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
– Scott
Dec 21 '18 at 7:07
add a comment |
Use
rm -rf ./*/
That avoids interactive mode an deletes only directories in your local directory.
Use
rm -rf ./*/
That avoids interactive mode an deletes only directories in your local directory.
answered Feb 8 '14 at 16:15
WeSee
21612
21612
As JdeBP pointed out on Martin's very similar answer, if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
– Scott
Dec 21 '18 at 7:07
add a comment |
As JdeBP pointed out on Martin's very similar answer, if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
– Scott
Dec 21 '18 at 7:07
As JdeBP pointed out on Martin's very similar answer, if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
– Scott
Dec 21 '18 at 7:07
As JdeBP pointed out on Martin's very similar answer, if the current (top-level) directory contains symbolic links to other directories, they will also be deleted (even if they aren't in or subordinate to the current directory).
– Scott
Dec 21 '18 at 7:07
add a comment |
Something like this should work:
find /path -type d -exec rm -rf '{}' ;
-type d looks for only directories
add a comment |
Something like this should work:
find /path -type d -exec rm -rf '{}' ;
-type d looks for only directories
add a comment |
Something like this should work:
find /path -type d -exec rm -rf '{}' ;
-type d looks for only directories
Something like this should work:
find /path -type d -exec rm -rf '{}' ;
-type d looks for only directories
answered Feb 7 '14 at 15:45
Matthew Williams
4,00182036
4,00182036
add a comment |
add a comment |
you can also try in this way to delete only all folders not files from any location in linux.
#delete only all dir and don't touch files
#!/bin/bash
for dir in `ls -l | grep ^d | awk '{print $9}'`
do
echo "going to delete $dir " `rm -rf $dir`
done
ls
add a comment |
you can also try in this way to delete only all folders not files from any location in linux.
#delete only all dir and don't touch files
#!/bin/bash
for dir in `ls -l | grep ^d | awk '{print $9}'`
do
echo "going to delete $dir " `rm -rf $dir`
done
ls
add a comment |
you can also try in this way to delete only all folders not files from any location in linux.
#delete only all dir and don't touch files
#!/bin/bash
for dir in `ls -l | grep ^d | awk '{print $9}'`
do
echo "going to delete $dir " `rm -rf $dir`
done
ls
you can also try in this way to delete only all folders not files from any location in linux.
#delete only all dir and don't touch files
#!/bin/bash
for dir in `ls -l | grep ^d | awk '{print $9}'`
do
echo "going to delete $dir " `rm -rf $dir`
done
ls
answered Dec 21 '18 at 2:09
linux.cnf
1
1
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fsuperuser.com%2fquestions%2f713741%2fbash-remove-all-directories-and-contents-but-not-files-in-pwd%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