How do I make “find” exclude the folder it searches in?












9















I'm running the following command:



find /var/www/html/content/processing -type d -mtime +1 -exec rm -rf {} ;


I would like to delete all the folders under the processing folder (the processing folder should never be deleted).



The command is deleting the processing folder as well. How do I limit the script to delete only the folders under that folder?










share|improve this question




















  • 1





    posterity: stackoverflow.com/questions/13525004/…

    – Trevor Boyd Smith
    Sep 20 '16 at 17:37
















9















I'm running the following command:



find /var/www/html/content/processing -type d -mtime +1 -exec rm -rf {} ;


I would like to delete all the folders under the processing folder (the processing folder should never be deleted).



The command is deleting the processing folder as well. How do I limit the script to delete only the folders under that folder?










share|improve this question




















  • 1





    posterity: stackoverflow.com/questions/13525004/…

    – Trevor Boyd Smith
    Sep 20 '16 at 17:37














9












9








9


1






I'm running the following command:



find /var/www/html/content/processing -type d -mtime +1 -exec rm -rf {} ;


I would like to delete all the folders under the processing folder (the processing folder should never be deleted).



The command is deleting the processing folder as well. How do I limit the script to delete only the folders under that folder?










share|improve this question
















I'm running the following command:



find /var/www/html/content/processing -type d -mtime +1 -exec rm -rf {} ;


I would like to delete all the folders under the processing folder (the processing folder should never be deleted).



The command is deleting the processing folder as well. How do I limit the script to delete only the folders under that folder?







linux shell find






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited May 2 '13 at 7:30









slhck

161k47447470




161k47447470










asked May 2 '13 at 7:10









Elad DotanElad Dotan

168126




168126








  • 1





    posterity: stackoverflow.com/questions/13525004/…

    – Trevor Boyd Smith
    Sep 20 '16 at 17:37














  • 1





    posterity: stackoverflow.com/questions/13525004/…

    – Trevor Boyd Smith
    Sep 20 '16 at 17:37








1




1





posterity: stackoverflow.com/questions/13525004/…

– Trevor Boyd Smith
Sep 20 '16 at 17:37





posterity: stackoverflow.com/questions/13525004/…

– Trevor Boyd Smith
Sep 20 '16 at 17:37










3 Answers
3






active

oldest

votes


















22














The easiest way would be to just add -mindepth 1, which will skip the first depth hierarchy and thus leave out your parent directory.



Also, you don't need an extra -exec call to rm, you can just delete the folders directly if they're empty.



find /var/www/html/content/processing -mindepth 1 -type d -mtime +1 -delete


If they're not empty:



find /var/www/html/content/processing -mindepth 1 -type d -mtime +1 -exec rm -rf {} ;


If you're lazy you can also have a wildcard expanded. Since * doesn't include the current directory by default (unless dotglob is set), you could also do:



find /var/www/html/content/processing/* -type d -mtime +1 -delete


However, this would also not include hidden folders, again due to the dotglob option.






share|improve this answer


























  • Hi, I get "find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. " and also "find: cannot delete `/folder/50d82faf0e09e': Directory not empty"

    – Elad Dotan
    May 5 '13 at 7:14











  • Add mindepth before type then. To delete the directories when not empty you can stick with the rm approach you had originally.

    – slhck
    May 5 '13 at 12:25



















4














The problem is that find returns the current directory (.) along with the other directories, so it deletes the processing folder as well as the subdirectories. A quick way to get around that would be to append the option



-not -name .


which stops find from outputting the current directory, and in turn stops it from being deleted.



That would work if you were running the command within the processing directory, so to allow for the fact that you are using an absolute path:



-not -name /var/www/html/content/processing


And the whole command would be:



find /var/www/html/content/processing -type d -mtime +1 -not -name /var/www/html/content/processing -exec rm -rf {} ;





share|improve this answer
























  • The last command is wrong. -name refers to basename, it never matches anything with a slash. You probably need to use -path.

    – Kamil Maciorowski
    Jan 17 at 23:22



















0














Already answered, still I would like to list another approach.



find /var/www/html/content/processing -mindepth 1 -maxdepth 1 -type d



This will exclude top directory (and also sub directories), and now you can apply whatever command that we want to apply over it.



>> Options:

-mindepth 1 : To exclude root directory

-maxdepth 1 : To avoid parsing sub directories. (For particular scenario as questioned, you don't need this).

-type d : List only directory types. This option should come after mindepth maxdepth uses.






share|improve this answer


























  • Why -maxdepth 1? Why -type d twice? How is this different to the accepted answer?

    – Attie
    Oct 12 '18 at 11:17











  • @Attie: Thanks, corrected the mistake. maxdepth is not necessary, but it would be useful if you want to avoid listing sub-directories.

    – prashant
    Nov 5 '18 at 9:27











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f590465%2fhow-do-i-make-find-exclude-the-folder-it-searches-in%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









22














The easiest way would be to just add -mindepth 1, which will skip the first depth hierarchy and thus leave out your parent directory.



Also, you don't need an extra -exec call to rm, you can just delete the folders directly if they're empty.



find /var/www/html/content/processing -mindepth 1 -type d -mtime +1 -delete


If they're not empty:



find /var/www/html/content/processing -mindepth 1 -type d -mtime +1 -exec rm -rf {} ;


If you're lazy you can also have a wildcard expanded. Since * doesn't include the current directory by default (unless dotglob is set), you could also do:



find /var/www/html/content/processing/* -type d -mtime +1 -delete


However, this would also not include hidden folders, again due to the dotglob option.






share|improve this answer


























  • Hi, I get "find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. " and also "find: cannot delete `/folder/50d82faf0e09e': Directory not empty"

    – Elad Dotan
    May 5 '13 at 7:14











  • Add mindepth before type then. To delete the directories when not empty you can stick with the rm approach you had originally.

    – slhck
    May 5 '13 at 12:25
















22














The easiest way would be to just add -mindepth 1, which will skip the first depth hierarchy and thus leave out your parent directory.



Also, you don't need an extra -exec call to rm, you can just delete the folders directly if they're empty.



find /var/www/html/content/processing -mindepth 1 -type d -mtime +1 -delete


If they're not empty:



find /var/www/html/content/processing -mindepth 1 -type d -mtime +1 -exec rm -rf {} ;


If you're lazy you can also have a wildcard expanded. Since * doesn't include the current directory by default (unless dotglob is set), you could also do:



find /var/www/html/content/processing/* -type d -mtime +1 -delete


However, this would also not include hidden folders, again due to the dotglob option.






share|improve this answer


























  • Hi, I get "find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. " and also "find: cannot delete `/folder/50d82faf0e09e': Directory not empty"

    – Elad Dotan
    May 5 '13 at 7:14











  • Add mindepth before type then. To delete the directories when not empty you can stick with the rm approach you had originally.

    – slhck
    May 5 '13 at 12:25














22












22








22







The easiest way would be to just add -mindepth 1, which will skip the first depth hierarchy and thus leave out your parent directory.



Also, you don't need an extra -exec call to rm, you can just delete the folders directly if they're empty.



find /var/www/html/content/processing -mindepth 1 -type d -mtime +1 -delete


If they're not empty:



find /var/www/html/content/processing -mindepth 1 -type d -mtime +1 -exec rm -rf {} ;


If you're lazy you can also have a wildcard expanded. Since * doesn't include the current directory by default (unless dotglob is set), you could also do:



find /var/www/html/content/processing/* -type d -mtime +1 -delete


However, this would also not include hidden folders, again due to the dotglob option.






share|improve this answer















The easiest way would be to just add -mindepth 1, which will skip the first depth hierarchy and thus leave out your parent directory.



Also, you don't need an extra -exec call to rm, you can just delete the folders directly if they're empty.



find /var/www/html/content/processing -mindepth 1 -type d -mtime +1 -delete


If they're not empty:



find /var/www/html/content/processing -mindepth 1 -type d -mtime +1 -exec rm -rf {} ;


If you're lazy you can also have a wildcard expanded. Since * doesn't include the current directory by default (unless dotglob is set), you could also do:



find /var/www/html/content/processing/* -type d -mtime +1 -delete


However, this would also not include hidden folders, again due to the dotglob option.







share|improve this answer














share|improve this answer



share|improve this answer








edited May 5 '13 at 15:03

























answered May 2 '13 at 7:26









slhckslhck

161k47447470




161k47447470













  • Hi, I get "find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. " and also "find: cannot delete `/folder/50d82faf0e09e': Directory not empty"

    – Elad Dotan
    May 5 '13 at 7:14











  • Add mindepth before type then. To delete the directories when not empty you can stick with the rm approach you had originally.

    – slhck
    May 5 '13 at 12:25



















  • Hi, I get "find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. " and also "find: cannot delete `/folder/50d82faf0e09e': Directory not empty"

    – Elad Dotan
    May 5 '13 at 7:14











  • Add mindepth before type then. To delete the directories when not empty you can stick with the rm approach you had originally.

    – slhck
    May 5 '13 at 12:25

















Hi, I get "find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. " and also "find: cannot delete `/folder/50d82faf0e09e': Directory not empty"

– Elad Dotan
May 5 '13 at 7:14





Hi, I get "find: warning: you have specified the -mindepth option after a non-option argument -type, but options are not positional (-mindepth affects tests specified before it as well as those specified after it). Please specify options before other arguments. " and also "find: cannot delete `/folder/50d82faf0e09e': Directory not empty"

– Elad Dotan
May 5 '13 at 7:14













Add mindepth before type then. To delete the directories when not empty you can stick with the rm approach you had originally.

– slhck
May 5 '13 at 12:25





Add mindepth before type then. To delete the directories when not empty you can stick with the rm approach you had originally.

– slhck
May 5 '13 at 12:25













4














The problem is that find returns the current directory (.) along with the other directories, so it deletes the processing folder as well as the subdirectories. A quick way to get around that would be to append the option



-not -name .


which stops find from outputting the current directory, and in turn stops it from being deleted.



That would work if you were running the command within the processing directory, so to allow for the fact that you are using an absolute path:



-not -name /var/www/html/content/processing


And the whole command would be:



find /var/www/html/content/processing -type d -mtime +1 -not -name /var/www/html/content/processing -exec rm -rf {} ;





share|improve this answer
























  • The last command is wrong. -name refers to basename, it never matches anything with a slash. You probably need to use -path.

    – Kamil Maciorowski
    Jan 17 at 23:22
















4














The problem is that find returns the current directory (.) along with the other directories, so it deletes the processing folder as well as the subdirectories. A quick way to get around that would be to append the option



-not -name .


which stops find from outputting the current directory, and in turn stops it from being deleted.



That would work if you were running the command within the processing directory, so to allow for the fact that you are using an absolute path:



-not -name /var/www/html/content/processing


And the whole command would be:



find /var/www/html/content/processing -type d -mtime +1 -not -name /var/www/html/content/processing -exec rm -rf {} ;





share|improve this answer
























  • The last command is wrong. -name refers to basename, it never matches anything with a slash. You probably need to use -path.

    – Kamil Maciorowski
    Jan 17 at 23:22














4












4








4







The problem is that find returns the current directory (.) along with the other directories, so it deletes the processing folder as well as the subdirectories. A quick way to get around that would be to append the option



-not -name .


which stops find from outputting the current directory, and in turn stops it from being deleted.



That would work if you were running the command within the processing directory, so to allow for the fact that you are using an absolute path:



-not -name /var/www/html/content/processing


And the whole command would be:



find /var/www/html/content/processing -type d -mtime +1 -not -name /var/www/html/content/processing -exec rm -rf {} ;





share|improve this answer













The problem is that find returns the current directory (.) along with the other directories, so it deletes the processing folder as well as the subdirectories. A quick way to get around that would be to append the option



-not -name .


which stops find from outputting the current directory, and in turn stops it from being deleted.



That would work if you were running the command within the processing directory, so to allow for the fact that you are using an absolute path:



-not -name /var/www/html/content/processing


And the whole command would be:



find /var/www/html/content/processing -type d -mtime +1 -not -name /var/www/html/content/processing -exec rm -rf {} ;






share|improve this answer












share|improve this answer



share|improve this answer










answered May 2 '13 at 7:19









XenopathicXenopathic

5011414




5011414













  • The last command is wrong. -name refers to basename, it never matches anything with a slash. You probably need to use -path.

    – Kamil Maciorowski
    Jan 17 at 23:22



















  • The last command is wrong. -name refers to basename, it never matches anything with a slash. You probably need to use -path.

    – Kamil Maciorowski
    Jan 17 at 23:22

















The last command is wrong. -name refers to basename, it never matches anything with a slash. You probably need to use -path.

– Kamil Maciorowski
Jan 17 at 23:22





The last command is wrong. -name refers to basename, it never matches anything with a slash. You probably need to use -path.

– Kamil Maciorowski
Jan 17 at 23:22











0














Already answered, still I would like to list another approach.



find /var/www/html/content/processing -mindepth 1 -maxdepth 1 -type d



This will exclude top directory (and also sub directories), and now you can apply whatever command that we want to apply over it.



>> Options:

-mindepth 1 : To exclude root directory

-maxdepth 1 : To avoid parsing sub directories. (For particular scenario as questioned, you don't need this).

-type d : List only directory types. This option should come after mindepth maxdepth uses.






share|improve this answer


























  • Why -maxdepth 1? Why -type d twice? How is this different to the accepted answer?

    – Attie
    Oct 12 '18 at 11:17











  • @Attie: Thanks, corrected the mistake. maxdepth is not necessary, but it would be useful if you want to avoid listing sub-directories.

    – prashant
    Nov 5 '18 at 9:27
















0














Already answered, still I would like to list another approach.



find /var/www/html/content/processing -mindepth 1 -maxdepth 1 -type d



This will exclude top directory (and also sub directories), and now you can apply whatever command that we want to apply over it.



>> Options:

-mindepth 1 : To exclude root directory

-maxdepth 1 : To avoid parsing sub directories. (For particular scenario as questioned, you don't need this).

-type d : List only directory types. This option should come after mindepth maxdepth uses.






share|improve this answer


























  • Why -maxdepth 1? Why -type d twice? How is this different to the accepted answer?

    – Attie
    Oct 12 '18 at 11:17











  • @Attie: Thanks, corrected the mistake. maxdepth is not necessary, but it would be useful if you want to avoid listing sub-directories.

    – prashant
    Nov 5 '18 at 9:27














0












0








0







Already answered, still I would like to list another approach.



find /var/www/html/content/processing -mindepth 1 -maxdepth 1 -type d



This will exclude top directory (and also sub directories), and now you can apply whatever command that we want to apply over it.



>> Options:

-mindepth 1 : To exclude root directory

-maxdepth 1 : To avoid parsing sub directories. (For particular scenario as questioned, you don't need this).

-type d : List only directory types. This option should come after mindepth maxdepth uses.






share|improve this answer















Already answered, still I would like to list another approach.



find /var/www/html/content/processing -mindepth 1 -maxdepth 1 -type d



This will exclude top directory (and also sub directories), and now you can apply whatever command that we want to apply over it.



>> Options:

-mindepth 1 : To exclude root directory

-maxdepth 1 : To avoid parsing sub directories. (For particular scenario as questioned, you don't need this).

-type d : List only directory types. This option should come after mindepth maxdepth uses.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 5 '18 at 9:26

























answered Oct 12 '18 at 8:20









prashantprashant

11




11













  • Why -maxdepth 1? Why -type d twice? How is this different to the accepted answer?

    – Attie
    Oct 12 '18 at 11:17











  • @Attie: Thanks, corrected the mistake. maxdepth is not necessary, but it would be useful if you want to avoid listing sub-directories.

    – prashant
    Nov 5 '18 at 9:27



















  • Why -maxdepth 1? Why -type d twice? How is this different to the accepted answer?

    – Attie
    Oct 12 '18 at 11:17











  • @Attie: Thanks, corrected the mistake. maxdepth is not necessary, but it would be useful if you want to avoid listing sub-directories.

    – prashant
    Nov 5 '18 at 9:27

















Why -maxdepth 1? Why -type d twice? How is this different to the accepted answer?

– Attie
Oct 12 '18 at 11:17





Why -maxdepth 1? Why -type d twice? How is this different to the accepted answer?

– Attie
Oct 12 '18 at 11:17













@Attie: Thanks, corrected the mistake. maxdepth is not necessary, but it would be useful if you want to avoid listing sub-directories.

– prashant
Nov 5 '18 at 9:27





@Attie: Thanks, corrected the mistake. maxdepth is not necessary, but it would be useful if you want to avoid listing sub-directories.

– prashant
Nov 5 '18 at 9:27


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f590465%2fhow-do-i-make-find-exclude-the-folder-it-searches-in%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

Eduardo VII do Reino Unido