How to remove all files from a directory?












235














The closest I've gotten is



# rm /path/to/directory/*.*


but that doesn't work for files that don't have an extension...










share|improve this question





























    235














    The closest I've gotten is



    # rm /path/to/directory/*.*


    but that doesn't work for files that don't have an extension...










    share|improve this question



























      235












      235








      235


      75





      The closest I've gotten is



      # rm /path/to/directory/*.*


      but that doesn't work for files that don't have an extension...










      share|improve this question















      The closest I've gotten is



      # rm /path/to/directory/*.*


      but that doesn't work for files that don't have an extension...







      command-line rm






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '14 at 6:48









      αғsнιη

      24.3k2295156




      24.3k2295156










      asked Sep 6 '11 at 7:58









      user784637user784637

      3,420133652




      3,420133652






















          9 Answers
          9






          active

          oldest

          votes


















          272














          Linux does not use extensions. It is up to the creator of the file to decide if he wants the name to have an extension. Linux looks at the 1st byte to figure out what kind of file it is dealing with.



          To remove everything in a directory use:



          rm /path/to/directory/*


          You can use the -r option, for example:



          rm -r /path/to/directory/*



          to also remove any sub directories (along with all their content) inside the directory you are removing the content of. Otherwise it will show an error informing you it is not removing the directory.






          share|improve this answer



















          • 12




            If you also want to delete hidden files run shopt -s dotglob before running rm (...)
            – danjjl
            Sep 6 '11 at 8:10






          • 6




            The * means all files ;) *.* means all files containing a . somewhere in the name
            – Rinzwind
            Sep 6 '11 at 8:20






          • 10




            @Rinzwind, more accurately, the asterisk means "zero or more of any character". So *a* means zero or more characters, followed by a followed by zero or more characters. It would match the filenames happy, apple, a or la.
            – DisgruntledGoat
            Sep 6 '11 at 13:43






          • 6




            @user784637 you are too easily impressed
            – barlop
            Sep 3 '14 at 16:10






          • 1




            Here's the command you want: rm -- *
            – anon58192932
            Oct 10 '14 at 21:04



















          137
















          • To remove the folder with all its contents(including all interior folders):



            rm -rf /path/to/directory



          • To remove all the contents of the folder(including all interior folders) but not the folder itself:



            rm -rf /path/to/directory/*


            or



            rm -rf /path/to/directory/{*,.*}


            if you want to make sure that hidden files/directories are also removed.




          • To remove all the "files" from inside a folder(not removing interior folders):



            rm -f /path/to/directory/{*,.*}






          • Warning: if you have spaces in your path, make sure to always use quotes.



            rm -rf /path/to the/directory/*


            is equivalent to 2 separate rm -rf calls:



            rm -rf /path/to
            rm -rf the/directory/*


            To avoid this issue, you can use 'single-quotes'(does not expand shell variables) or "double-quotes"(expands shell variables):



            rm -rf "/path/to the/directory/"*






          Where:





          • rm - stands for "remove"


          • -f - stands for "force" which is helpful when you don't want to be asked/prompted if you want to remove an archive, for example.



          • -r - stands for "recursive" which means that you want to go recursively down every folder and remove everything.






          share|improve this answer



















          • 8




            I found this to be the more comprehensive and helpful answer, over and above the answer that was marked as Accepted.
            – inspirednz
            Aug 20 '16 at 1:58






          • 2




            rm -rf /path/to/directory/* does not remove a hidden file in the folder e.g. .htaccess. Maybe rm -rf /path/to/directory/.? Haven't tried it.
            – Mark Berry
            Mar 29 '17 at 1:33










          • @MarkBerry rm -rf /path/to/directory/.*
            – Lilian A. Moraru
            Mar 29 '17 at 14:57












          • @LilianA.Moraru, I did some testing today. rm -rf /path/to/directory/.* only deletes the hidden file(s) in the specified directory. Looking at the @danjjl's comment on @Rinzwind's answer, to also delete hidden files, run shopt -s dotglob before running rm -rf /path/to/directory/*.
            – Mark Berry
            Mar 29 '17 at 22:59










          • CAUTION: rm -rf /path/to/directory/.* on my system caused deletion of items in /path/to. Fortunately, I had just backed-up my entire data to a separate disk. Needless to say, do NOT issue the command rm -rf /path/to/directory/.* unless you have backed up your whole computer to a separate / secure location!
            – lawlist
            Sep 3 '18 at 18:32



















          34














          To remove all files in directory (including hidden files and subdirectories) run:



          rm -rf /path/to/directory/{*,.*}





          share|improve this answer





















          • This should be the beautiful accepted answer. Thank you!
            – Nam G VU
            Sep 22 '16 at 15:53






          • 3




            Doesn't this glob match "." and ".." too?
            – hertzsprung
            Jan 8 '17 at 13:28






          • 3




            @hertzsprung - it does, and it will give you a warning that it cannot delete ./ and ../, but it will still delete the hidden files.
            – Ryan Wheale
            Jan 31 '17 at 21:12










          • This does not work on Mac console, hidden files are still there with that command line.
            – agapitocandemor
            Aug 21 '18 at 8:36



















          8














          If you want to delete only files in /path/to/directory you can do



          find /path/to/directory -type f -print0| xargs -0 rm 


          or



          find /path/to/directory -type f -exec rm '{}' ;


          You can do loads with find, the advantage is you can list what is found without piping it to rm so you can see what will be deleted before you start.






          share|improve this answer



















          • 2




            GNU find as a -delete predicate. If you still want to use -exec, substituting ; with + will gather rm calls together, increasing efficiency.
            – enzotib
            Sep 6 '11 at 12:19






          • 1




            large amount of files with '+' will cause problems, since list will be too large, same as rm -f *. and when removing large amounts of files from same folder (talking in millons) both of them are not good :) In the end C++ came along and removed files in order of inodes in dir-tree.
            – Osis
            Sep 6 '11 at 12:53






          • 3




            You really ought to add a -- after the rm. Without that if you have a file names -rf or similar will be interpreted as arguments to rm. e.g. xargs -0 rm -- or -exec rm -- {} ;
            – Richm
            Sep 6 '11 at 13:52












          • you can also provide the -n argument to xargs. That will cause it to split the rm commands to having a maximum number of arguments i.e. 'xargs -n 100 -0 rm --' will remove files in chunks of 100.
            – Richm
            Sep 6 '11 at 13:54










          • You can also add -maxdepth 1 to ensure that find does not return files from any subdirectories. i.e. find /path/to/directory -maxdepth 1 -type f
            – Richm
            Sep 6 '11 at 14:10



















          6














          If you also want to remove all subdirectories and the directory itself, you can use rm -rf /path/to/directory. But always double-check your line before pressing return, rm -rf can cause lots of havock as well, e.g. if you accidentally insert a space after the first slash while having superuser permissions...






          share|improve this answer





























            6














            To delete all files and directories(including the hidden ones) in a directory, you can try the following:





            • delete the folder, then recreate it



              rm -rf dir_name && mkdir dir_name



            • use find



              find dir_name -mindepth 1 -delete  



            Here we specify -mindepth 1 to exclude the directory dir_name itself.

            Take a look at the following link:
            https://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory






            share|improve this answer























            • Thanks for your advice and I've added more explanation for the links.
              – zeekvfu
              Nov 28 '13 at 5:04






            • 2




              Be careful, since deleting and re-creating the folder might result in different/wrong permissions for this folder!
              – einjohn
              Aug 29 '15 at 12:20



















            1














            You can cd into the directory and then run the command rm *.* just like in DOS if you remember.






            share|improve this answer



















            • 1




              DOS is DEL for files and RMDIR for emptied directories- and hidden and system files must have those ATTRIB attributes removed first. Not nearly as simple ;)
              – Eric
              Jun 10 '15 at 13:03



















            0














            To delete current directory, you could for example use rm -d ./*



            -d tells to delete directories as well.






            share|improve this answer





























              0














              Since this question is constantly at the top of Google when I search for this myself:



              The other answers suffer from different problems:




              1. Some of them include . and .. which is noisy, confusing, and annoying.


              2. Some of them forget hidden files (files beginning with a dot).


              3. They don't delete in a correct (deepest-first) order to allow directory deletion.


              4. They descend into other (mounted) file systems, which is often undesired.


              5. They're difficult to extend properly with extra parameters (more on that below).



              So, to RECURSIVELY delete all files AND folders in a directory, do this:



              find "${DIR}" -xdev -mindepth 1 -printf "%dt%yt%p" | sort -z -r -n | cut -z -f3- | xargs -0 -r -- rm -d --


              Note that I added an -xdev argument to prevent descending into mounts (like /proc etc.).



              Why not -depth or -delete?



              Despite people constantly downvoting me for this, those methods have a downside: it doesn't seem like they're extensible enough to allow -pruneing a subdirectory (without introducing more problems). By contrast with this method, you could insert



              -not ( -path "${DIR}/subdir" -prune )


              before the -mindepth argument to exclude subdir from having its contents deleted.






              share|improve this answer























              • And for depth-first order, there's a -depth flag in find for that.
                – Sergiy Kolodyazhnyy
                May 21 '18 at 1:36










              • Uh... why not just use -delete with find? -delete is depth-first. You're already assuming non-POSIX find with the -printf, so you might just as well use -delete or -depth -print0 | xargs -0 rm
                – muru
                May 21 '18 at 6:41












              • @muru: Because I didn't know better... I'll change it.
                – Mehrdad
                May 21 '18 at 6:47










              • @muru: Actually, it seems your method doesn't work with -not ( -path "$DIR/subdir" )... but mine does? Why?
                – Mehrdad
                May 21 '18 at 7:00












              • @Mehrdad what is that supposed to do?
                – muru
                May 21 '18 at 7:06










              protected by αғsнιη May 21 '18 at 3:04



              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?














              9 Answers
              9






              active

              oldest

              votes








              9 Answers
              9






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              272














              Linux does not use extensions. It is up to the creator of the file to decide if he wants the name to have an extension. Linux looks at the 1st byte to figure out what kind of file it is dealing with.



              To remove everything in a directory use:



              rm /path/to/directory/*


              You can use the -r option, for example:



              rm -r /path/to/directory/*



              to also remove any sub directories (along with all their content) inside the directory you are removing the content of. Otherwise it will show an error informing you it is not removing the directory.






              share|improve this answer



















              • 12




                If you also want to delete hidden files run shopt -s dotglob before running rm (...)
                – danjjl
                Sep 6 '11 at 8:10






              • 6




                The * means all files ;) *.* means all files containing a . somewhere in the name
                – Rinzwind
                Sep 6 '11 at 8:20






              • 10




                @Rinzwind, more accurately, the asterisk means "zero or more of any character". So *a* means zero or more characters, followed by a followed by zero or more characters. It would match the filenames happy, apple, a or la.
                – DisgruntledGoat
                Sep 6 '11 at 13:43






              • 6




                @user784637 you are too easily impressed
                – barlop
                Sep 3 '14 at 16:10






              • 1




                Here's the command you want: rm -- *
                – anon58192932
                Oct 10 '14 at 21:04
















              272














              Linux does not use extensions. It is up to the creator of the file to decide if he wants the name to have an extension. Linux looks at the 1st byte to figure out what kind of file it is dealing with.



              To remove everything in a directory use:



              rm /path/to/directory/*


              You can use the -r option, for example:



              rm -r /path/to/directory/*



              to also remove any sub directories (along with all their content) inside the directory you are removing the content of. Otherwise it will show an error informing you it is not removing the directory.






              share|improve this answer



















              • 12




                If you also want to delete hidden files run shopt -s dotglob before running rm (...)
                – danjjl
                Sep 6 '11 at 8:10






              • 6




                The * means all files ;) *.* means all files containing a . somewhere in the name
                – Rinzwind
                Sep 6 '11 at 8:20






              • 10




                @Rinzwind, more accurately, the asterisk means "zero or more of any character". So *a* means zero or more characters, followed by a followed by zero or more characters. It would match the filenames happy, apple, a or la.
                – DisgruntledGoat
                Sep 6 '11 at 13:43






              • 6




                @user784637 you are too easily impressed
                – barlop
                Sep 3 '14 at 16:10






              • 1




                Here's the command you want: rm -- *
                – anon58192932
                Oct 10 '14 at 21:04














              272












              272








              272






              Linux does not use extensions. It is up to the creator of the file to decide if he wants the name to have an extension. Linux looks at the 1st byte to figure out what kind of file it is dealing with.



              To remove everything in a directory use:



              rm /path/to/directory/*


              You can use the -r option, for example:



              rm -r /path/to/directory/*



              to also remove any sub directories (along with all their content) inside the directory you are removing the content of. Otherwise it will show an error informing you it is not removing the directory.






              share|improve this answer














              Linux does not use extensions. It is up to the creator of the file to decide if he wants the name to have an extension. Linux looks at the 1st byte to figure out what kind of file it is dealing with.



              To remove everything in a directory use:



              rm /path/to/directory/*


              You can use the -r option, for example:



              rm -r /path/to/directory/*



              to also remove any sub directories (along with all their content) inside the directory you are removing the content of. Otherwise it will show an error informing you it is not removing the directory.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Sep 8 '18 at 13:55









              Guy Avraham

              1136




              1136










              answered Sep 6 '11 at 8:06









              RinzwindRinzwind

              204k28389524




              204k28389524








              • 12




                If you also want to delete hidden files run shopt -s dotglob before running rm (...)
                – danjjl
                Sep 6 '11 at 8:10






              • 6




                The * means all files ;) *.* means all files containing a . somewhere in the name
                – Rinzwind
                Sep 6 '11 at 8:20






              • 10




                @Rinzwind, more accurately, the asterisk means "zero or more of any character". So *a* means zero or more characters, followed by a followed by zero or more characters. It would match the filenames happy, apple, a or la.
                – DisgruntledGoat
                Sep 6 '11 at 13:43






              • 6




                @user784637 you are too easily impressed
                – barlop
                Sep 3 '14 at 16:10






              • 1




                Here's the command you want: rm -- *
                – anon58192932
                Oct 10 '14 at 21:04














              • 12




                If you also want to delete hidden files run shopt -s dotglob before running rm (...)
                – danjjl
                Sep 6 '11 at 8:10






              • 6




                The * means all files ;) *.* means all files containing a . somewhere in the name
                – Rinzwind
                Sep 6 '11 at 8:20






              • 10




                @Rinzwind, more accurately, the asterisk means "zero or more of any character". So *a* means zero or more characters, followed by a followed by zero or more characters. It would match the filenames happy, apple, a or la.
                – DisgruntledGoat
                Sep 6 '11 at 13:43






              • 6




                @user784637 you are too easily impressed
                – barlop
                Sep 3 '14 at 16:10






              • 1




                Here's the command you want: rm -- *
                – anon58192932
                Oct 10 '14 at 21:04








              12




              12




              If you also want to delete hidden files run shopt -s dotglob before running rm (...)
              – danjjl
              Sep 6 '11 at 8:10




              If you also want to delete hidden files run shopt -s dotglob before running rm (...)
              – danjjl
              Sep 6 '11 at 8:10




              6




              6




              The * means all files ;) *.* means all files containing a . somewhere in the name
              – Rinzwind
              Sep 6 '11 at 8:20




              The * means all files ;) *.* means all files containing a . somewhere in the name
              – Rinzwind
              Sep 6 '11 at 8:20




              10




              10




              @Rinzwind, more accurately, the asterisk means "zero or more of any character". So *a* means zero or more characters, followed by a followed by zero or more characters. It would match the filenames happy, apple, a or la.
              – DisgruntledGoat
              Sep 6 '11 at 13:43




              @Rinzwind, more accurately, the asterisk means "zero or more of any character". So *a* means zero or more characters, followed by a followed by zero or more characters. It would match the filenames happy, apple, a or la.
              – DisgruntledGoat
              Sep 6 '11 at 13:43




              6




              6




              @user784637 you are too easily impressed
              – barlop
              Sep 3 '14 at 16:10




              @user784637 you are too easily impressed
              – barlop
              Sep 3 '14 at 16:10




              1




              1




              Here's the command you want: rm -- *
              – anon58192932
              Oct 10 '14 at 21:04




              Here's the command you want: rm -- *
              – anon58192932
              Oct 10 '14 at 21:04













              137
















              • To remove the folder with all its contents(including all interior folders):



                rm -rf /path/to/directory



              • To remove all the contents of the folder(including all interior folders) but not the folder itself:



                rm -rf /path/to/directory/*


                or



                rm -rf /path/to/directory/{*,.*}


                if you want to make sure that hidden files/directories are also removed.




              • To remove all the "files" from inside a folder(not removing interior folders):



                rm -f /path/to/directory/{*,.*}






              • Warning: if you have spaces in your path, make sure to always use quotes.



                rm -rf /path/to the/directory/*


                is equivalent to 2 separate rm -rf calls:



                rm -rf /path/to
                rm -rf the/directory/*


                To avoid this issue, you can use 'single-quotes'(does not expand shell variables) or "double-quotes"(expands shell variables):



                rm -rf "/path/to the/directory/"*






              Where:





              • rm - stands for "remove"


              • -f - stands for "force" which is helpful when you don't want to be asked/prompted if you want to remove an archive, for example.



              • -r - stands for "recursive" which means that you want to go recursively down every folder and remove everything.






              share|improve this answer



















              • 8




                I found this to be the more comprehensive and helpful answer, over and above the answer that was marked as Accepted.
                – inspirednz
                Aug 20 '16 at 1:58






              • 2




                rm -rf /path/to/directory/* does not remove a hidden file in the folder e.g. .htaccess. Maybe rm -rf /path/to/directory/.? Haven't tried it.
                – Mark Berry
                Mar 29 '17 at 1:33










              • @MarkBerry rm -rf /path/to/directory/.*
                – Lilian A. Moraru
                Mar 29 '17 at 14:57












              • @LilianA.Moraru, I did some testing today. rm -rf /path/to/directory/.* only deletes the hidden file(s) in the specified directory. Looking at the @danjjl's comment on @Rinzwind's answer, to also delete hidden files, run shopt -s dotglob before running rm -rf /path/to/directory/*.
                – Mark Berry
                Mar 29 '17 at 22:59










              • CAUTION: rm -rf /path/to/directory/.* on my system caused deletion of items in /path/to. Fortunately, I had just backed-up my entire data to a separate disk. Needless to say, do NOT issue the command rm -rf /path/to/directory/.* unless you have backed up your whole computer to a separate / secure location!
                – lawlist
                Sep 3 '18 at 18:32
















              137
















              • To remove the folder with all its contents(including all interior folders):



                rm -rf /path/to/directory



              • To remove all the contents of the folder(including all interior folders) but not the folder itself:



                rm -rf /path/to/directory/*


                or



                rm -rf /path/to/directory/{*,.*}


                if you want to make sure that hidden files/directories are also removed.




              • To remove all the "files" from inside a folder(not removing interior folders):



                rm -f /path/to/directory/{*,.*}






              • Warning: if you have spaces in your path, make sure to always use quotes.



                rm -rf /path/to the/directory/*


                is equivalent to 2 separate rm -rf calls:



                rm -rf /path/to
                rm -rf the/directory/*


                To avoid this issue, you can use 'single-quotes'(does not expand shell variables) or "double-quotes"(expands shell variables):



                rm -rf "/path/to the/directory/"*






              Where:





              • rm - stands for "remove"


              • -f - stands for "force" which is helpful when you don't want to be asked/prompted if you want to remove an archive, for example.



              • -r - stands for "recursive" which means that you want to go recursively down every folder and remove everything.






              share|improve this answer



















              • 8




                I found this to be the more comprehensive and helpful answer, over and above the answer that was marked as Accepted.
                – inspirednz
                Aug 20 '16 at 1:58






              • 2




                rm -rf /path/to/directory/* does not remove a hidden file in the folder e.g. .htaccess. Maybe rm -rf /path/to/directory/.? Haven't tried it.
                – Mark Berry
                Mar 29 '17 at 1:33










              • @MarkBerry rm -rf /path/to/directory/.*
                – Lilian A. Moraru
                Mar 29 '17 at 14:57












              • @LilianA.Moraru, I did some testing today. rm -rf /path/to/directory/.* only deletes the hidden file(s) in the specified directory. Looking at the @danjjl's comment on @Rinzwind's answer, to also delete hidden files, run shopt -s dotglob before running rm -rf /path/to/directory/*.
                – Mark Berry
                Mar 29 '17 at 22:59










              • CAUTION: rm -rf /path/to/directory/.* on my system caused deletion of items in /path/to. Fortunately, I had just backed-up my entire data to a separate disk. Needless to say, do NOT issue the command rm -rf /path/to/directory/.* unless you have backed up your whole computer to a separate / secure location!
                – lawlist
                Sep 3 '18 at 18:32














              137












              137








              137








              • To remove the folder with all its contents(including all interior folders):



                rm -rf /path/to/directory



              • To remove all the contents of the folder(including all interior folders) but not the folder itself:



                rm -rf /path/to/directory/*


                or



                rm -rf /path/to/directory/{*,.*}


                if you want to make sure that hidden files/directories are also removed.




              • To remove all the "files" from inside a folder(not removing interior folders):



                rm -f /path/to/directory/{*,.*}






              • Warning: if you have spaces in your path, make sure to always use quotes.



                rm -rf /path/to the/directory/*


                is equivalent to 2 separate rm -rf calls:



                rm -rf /path/to
                rm -rf the/directory/*


                To avoid this issue, you can use 'single-quotes'(does not expand shell variables) or "double-quotes"(expands shell variables):



                rm -rf "/path/to the/directory/"*






              Where:





              • rm - stands for "remove"


              • -f - stands for "force" which is helpful when you don't want to be asked/prompted if you want to remove an archive, for example.



              • -r - stands for "recursive" which means that you want to go recursively down every folder and remove everything.






              share|improve this answer
















              • To remove the folder with all its contents(including all interior folders):



                rm -rf /path/to/directory



              • To remove all the contents of the folder(including all interior folders) but not the folder itself:



                rm -rf /path/to/directory/*


                or



                rm -rf /path/to/directory/{*,.*}


                if you want to make sure that hidden files/directories are also removed.




              • To remove all the "files" from inside a folder(not removing interior folders):



                rm -f /path/to/directory/{*,.*}






              • Warning: if you have spaces in your path, make sure to always use quotes.



                rm -rf /path/to the/directory/*


                is equivalent to 2 separate rm -rf calls:



                rm -rf /path/to
                rm -rf the/directory/*


                To avoid this issue, you can use 'single-quotes'(does not expand shell variables) or "double-quotes"(expands shell variables):



                rm -rf "/path/to the/directory/"*






              Where:





              • rm - stands for "remove"


              • -f - stands for "force" which is helpful when you don't want to be asked/prompted if you want to remove an archive, for example.



              • -r - stands for "recursive" which means that you want to go recursively down every folder and remove everything.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 21 '18 at 8:05

























              answered Sep 7 '11 at 17:26









              Lilian A. MoraruLilian A. Moraru

              4,16421919




              4,16421919








              • 8




                I found this to be the more comprehensive and helpful answer, over and above the answer that was marked as Accepted.
                – inspirednz
                Aug 20 '16 at 1:58






              • 2




                rm -rf /path/to/directory/* does not remove a hidden file in the folder e.g. .htaccess. Maybe rm -rf /path/to/directory/.? Haven't tried it.
                – Mark Berry
                Mar 29 '17 at 1:33










              • @MarkBerry rm -rf /path/to/directory/.*
                – Lilian A. Moraru
                Mar 29 '17 at 14:57












              • @LilianA.Moraru, I did some testing today. rm -rf /path/to/directory/.* only deletes the hidden file(s) in the specified directory. Looking at the @danjjl's comment on @Rinzwind's answer, to also delete hidden files, run shopt -s dotglob before running rm -rf /path/to/directory/*.
                – Mark Berry
                Mar 29 '17 at 22:59










              • CAUTION: rm -rf /path/to/directory/.* on my system caused deletion of items in /path/to. Fortunately, I had just backed-up my entire data to a separate disk. Needless to say, do NOT issue the command rm -rf /path/to/directory/.* unless you have backed up your whole computer to a separate / secure location!
                – lawlist
                Sep 3 '18 at 18:32














              • 8




                I found this to be the more comprehensive and helpful answer, over and above the answer that was marked as Accepted.
                – inspirednz
                Aug 20 '16 at 1:58






              • 2




                rm -rf /path/to/directory/* does not remove a hidden file in the folder e.g. .htaccess. Maybe rm -rf /path/to/directory/.? Haven't tried it.
                – Mark Berry
                Mar 29 '17 at 1:33










              • @MarkBerry rm -rf /path/to/directory/.*
                – Lilian A. Moraru
                Mar 29 '17 at 14:57












              • @LilianA.Moraru, I did some testing today. rm -rf /path/to/directory/.* only deletes the hidden file(s) in the specified directory. Looking at the @danjjl's comment on @Rinzwind's answer, to also delete hidden files, run shopt -s dotglob before running rm -rf /path/to/directory/*.
                – Mark Berry
                Mar 29 '17 at 22:59










              • CAUTION: rm -rf /path/to/directory/.* on my system caused deletion of items in /path/to. Fortunately, I had just backed-up my entire data to a separate disk. Needless to say, do NOT issue the command rm -rf /path/to/directory/.* unless you have backed up your whole computer to a separate / secure location!
                – lawlist
                Sep 3 '18 at 18:32








              8




              8




              I found this to be the more comprehensive and helpful answer, over and above the answer that was marked as Accepted.
              – inspirednz
              Aug 20 '16 at 1:58




              I found this to be the more comprehensive and helpful answer, over and above the answer that was marked as Accepted.
              – inspirednz
              Aug 20 '16 at 1:58




              2




              2




              rm -rf /path/to/directory/* does not remove a hidden file in the folder e.g. .htaccess. Maybe rm -rf /path/to/directory/.? Haven't tried it.
              – Mark Berry
              Mar 29 '17 at 1:33




              rm -rf /path/to/directory/* does not remove a hidden file in the folder e.g. .htaccess. Maybe rm -rf /path/to/directory/.? Haven't tried it.
              – Mark Berry
              Mar 29 '17 at 1:33












              @MarkBerry rm -rf /path/to/directory/.*
              – Lilian A. Moraru
              Mar 29 '17 at 14:57






              @MarkBerry rm -rf /path/to/directory/.*
              – Lilian A. Moraru
              Mar 29 '17 at 14:57














              @LilianA.Moraru, I did some testing today. rm -rf /path/to/directory/.* only deletes the hidden file(s) in the specified directory. Looking at the @danjjl's comment on @Rinzwind's answer, to also delete hidden files, run shopt -s dotglob before running rm -rf /path/to/directory/*.
              – Mark Berry
              Mar 29 '17 at 22:59




              @LilianA.Moraru, I did some testing today. rm -rf /path/to/directory/.* only deletes the hidden file(s) in the specified directory. Looking at the @danjjl's comment on @Rinzwind's answer, to also delete hidden files, run shopt -s dotglob before running rm -rf /path/to/directory/*.
              – Mark Berry
              Mar 29 '17 at 22:59












              CAUTION: rm -rf /path/to/directory/.* on my system caused deletion of items in /path/to. Fortunately, I had just backed-up my entire data to a separate disk. Needless to say, do NOT issue the command rm -rf /path/to/directory/.* unless you have backed up your whole computer to a separate / secure location!
              – lawlist
              Sep 3 '18 at 18:32




              CAUTION: rm -rf /path/to/directory/.* on my system caused deletion of items in /path/to. Fortunately, I had just backed-up my entire data to a separate disk. Needless to say, do NOT issue the command rm -rf /path/to/directory/.* unless you have backed up your whole computer to a separate / secure location!
              – lawlist
              Sep 3 '18 at 18:32











              34














              To remove all files in directory (including hidden files and subdirectories) run:



              rm -rf /path/to/directory/{*,.*}





              share|improve this answer





















              • This should be the beautiful accepted answer. Thank you!
                – Nam G VU
                Sep 22 '16 at 15:53






              • 3




                Doesn't this glob match "." and ".." too?
                – hertzsprung
                Jan 8 '17 at 13:28






              • 3




                @hertzsprung - it does, and it will give you a warning that it cannot delete ./ and ../, but it will still delete the hidden files.
                – Ryan Wheale
                Jan 31 '17 at 21:12










              • This does not work on Mac console, hidden files are still there with that command line.
                – agapitocandemor
                Aug 21 '18 at 8:36
















              34














              To remove all files in directory (including hidden files and subdirectories) run:



              rm -rf /path/to/directory/{*,.*}





              share|improve this answer





















              • This should be the beautiful accepted answer. Thank you!
                – Nam G VU
                Sep 22 '16 at 15:53






              • 3




                Doesn't this glob match "." and ".." too?
                – hertzsprung
                Jan 8 '17 at 13:28






              • 3




                @hertzsprung - it does, and it will give you a warning that it cannot delete ./ and ../, but it will still delete the hidden files.
                – Ryan Wheale
                Jan 31 '17 at 21:12










              • This does not work on Mac console, hidden files are still there with that command line.
                – agapitocandemor
                Aug 21 '18 at 8:36














              34












              34








              34






              To remove all files in directory (including hidden files and subdirectories) run:



              rm -rf /path/to/directory/{*,.*}





              share|improve this answer












              To remove all files in directory (including hidden files and subdirectories) run:



              rm -rf /path/to/directory/{*,.*}






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 23 '14 at 9:38









              Leonid V. FedorenchikLeonid V. Fedorenchik

              586510




              586510












              • This should be the beautiful accepted answer. Thank you!
                – Nam G VU
                Sep 22 '16 at 15:53






              • 3




                Doesn't this glob match "." and ".." too?
                – hertzsprung
                Jan 8 '17 at 13:28






              • 3




                @hertzsprung - it does, and it will give you a warning that it cannot delete ./ and ../, but it will still delete the hidden files.
                – Ryan Wheale
                Jan 31 '17 at 21:12










              • This does not work on Mac console, hidden files are still there with that command line.
                – agapitocandemor
                Aug 21 '18 at 8:36


















              • This should be the beautiful accepted answer. Thank you!
                – Nam G VU
                Sep 22 '16 at 15:53






              • 3




                Doesn't this glob match "." and ".." too?
                – hertzsprung
                Jan 8 '17 at 13:28






              • 3




                @hertzsprung - it does, and it will give you a warning that it cannot delete ./ and ../, but it will still delete the hidden files.
                – Ryan Wheale
                Jan 31 '17 at 21:12










              • This does not work on Mac console, hidden files are still there with that command line.
                – agapitocandemor
                Aug 21 '18 at 8:36
















              This should be the beautiful accepted answer. Thank you!
              – Nam G VU
              Sep 22 '16 at 15:53




              This should be the beautiful accepted answer. Thank you!
              – Nam G VU
              Sep 22 '16 at 15:53




              3




              3




              Doesn't this glob match "." and ".." too?
              – hertzsprung
              Jan 8 '17 at 13:28




              Doesn't this glob match "." and ".." too?
              – hertzsprung
              Jan 8 '17 at 13:28




              3




              3




              @hertzsprung - it does, and it will give you a warning that it cannot delete ./ and ../, but it will still delete the hidden files.
              – Ryan Wheale
              Jan 31 '17 at 21:12




              @hertzsprung - it does, and it will give you a warning that it cannot delete ./ and ../, but it will still delete the hidden files.
              – Ryan Wheale
              Jan 31 '17 at 21:12












              This does not work on Mac console, hidden files are still there with that command line.
              – agapitocandemor
              Aug 21 '18 at 8:36




              This does not work on Mac console, hidden files are still there with that command line.
              – agapitocandemor
              Aug 21 '18 at 8:36











              8














              If you want to delete only files in /path/to/directory you can do



              find /path/to/directory -type f -print0| xargs -0 rm 


              or



              find /path/to/directory -type f -exec rm '{}' ;


              You can do loads with find, the advantage is you can list what is found without piping it to rm so you can see what will be deleted before you start.






              share|improve this answer



















              • 2




                GNU find as a -delete predicate. If you still want to use -exec, substituting ; with + will gather rm calls together, increasing efficiency.
                – enzotib
                Sep 6 '11 at 12:19






              • 1




                large amount of files with '+' will cause problems, since list will be too large, same as rm -f *. and when removing large amounts of files from same folder (talking in millons) both of them are not good :) In the end C++ came along and removed files in order of inodes in dir-tree.
                – Osis
                Sep 6 '11 at 12:53






              • 3




                You really ought to add a -- after the rm. Without that if you have a file names -rf or similar will be interpreted as arguments to rm. e.g. xargs -0 rm -- or -exec rm -- {} ;
                – Richm
                Sep 6 '11 at 13:52












              • you can also provide the -n argument to xargs. That will cause it to split the rm commands to having a maximum number of arguments i.e. 'xargs -n 100 -0 rm --' will remove files in chunks of 100.
                – Richm
                Sep 6 '11 at 13:54










              • You can also add -maxdepth 1 to ensure that find does not return files from any subdirectories. i.e. find /path/to/directory -maxdepth 1 -type f
                – Richm
                Sep 6 '11 at 14:10
















              8














              If you want to delete only files in /path/to/directory you can do



              find /path/to/directory -type f -print0| xargs -0 rm 


              or



              find /path/to/directory -type f -exec rm '{}' ;


              You can do loads with find, the advantage is you can list what is found without piping it to rm so you can see what will be deleted before you start.






              share|improve this answer



















              • 2




                GNU find as a -delete predicate. If you still want to use -exec, substituting ; with + will gather rm calls together, increasing efficiency.
                – enzotib
                Sep 6 '11 at 12:19






              • 1




                large amount of files with '+' will cause problems, since list will be too large, same as rm -f *. and when removing large amounts of files from same folder (talking in millons) both of them are not good :) In the end C++ came along and removed files in order of inodes in dir-tree.
                – Osis
                Sep 6 '11 at 12:53






              • 3




                You really ought to add a -- after the rm. Without that if you have a file names -rf or similar will be interpreted as arguments to rm. e.g. xargs -0 rm -- or -exec rm -- {} ;
                – Richm
                Sep 6 '11 at 13:52












              • you can also provide the -n argument to xargs. That will cause it to split the rm commands to having a maximum number of arguments i.e. 'xargs -n 100 -0 rm --' will remove files in chunks of 100.
                – Richm
                Sep 6 '11 at 13:54










              • You can also add -maxdepth 1 to ensure that find does not return files from any subdirectories. i.e. find /path/to/directory -maxdepth 1 -type f
                – Richm
                Sep 6 '11 at 14:10














              8












              8








              8






              If you want to delete only files in /path/to/directory you can do



              find /path/to/directory -type f -print0| xargs -0 rm 


              or



              find /path/to/directory -type f -exec rm '{}' ;


              You can do loads with find, the advantage is you can list what is found without piping it to rm so you can see what will be deleted before you start.






              share|improve this answer














              If you want to delete only files in /path/to/directory you can do



              find /path/to/directory -type f -print0| xargs -0 rm 


              or



              find /path/to/directory -type f -exec rm '{}' ;


              You can do loads with find, the advantage is you can list what is found without piping it to rm so you can see what will be deleted before you start.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 20 '16 at 6:41









              αғsнιη

              24.3k2295156




              24.3k2295156










              answered Sep 6 '11 at 12:13









              Richard HollowayRichard Holloway

              20.4k54252




              20.4k54252








              • 2




                GNU find as a -delete predicate. If you still want to use -exec, substituting ; with + will gather rm calls together, increasing efficiency.
                – enzotib
                Sep 6 '11 at 12:19






              • 1




                large amount of files with '+' will cause problems, since list will be too large, same as rm -f *. and when removing large amounts of files from same folder (talking in millons) both of them are not good :) In the end C++ came along and removed files in order of inodes in dir-tree.
                – Osis
                Sep 6 '11 at 12:53






              • 3




                You really ought to add a -- after the rm. Without that if you have a file names -rf or similar will be interpreted as arguments to rm. e.g. xargs -0 rm -- or -exec rm -- {} ;
                – Richm
                Sep 6 '11 at 13:52












              • you can also provide the -n argument to xargs. That will cause it to split the rm commands to having a maximum number of arguments i.e. 'xargs -n 100 -0 rm --' will remove files in chunks of 100.
                – Richm
                Sep 6 '11 at 13:54










              • You can also add -maxdepth 1 to ensure that find does not return files from any subdirectories. i.e. find /path/to/directory -maxdepth 1 -type f
                – Richm
                Sep 6 '11 at 14:10














              • 2




                GNU find as a -delete predicate. If you still want to use -exec, substituting ; with + will gather rm calls together, increasing efficiency.
                – enzotib
                Sep 6 '11 at 12:19






              • 1




                large amount of files with '+' will cause problems, since list will be too large, same as rm -f *. and when removing large amounts of files from same folder (talking in millons) both of them are not good :) In the end C++ came along and removed files in order of inodes in dir-tree.
                – Osis
                Sep 6 '11 at 12:53






              • 3




                You really ought to add a -- after the rm. Without that if you have a file names -rf or similar will be interpreted as arguments to rm. e.g. xargs -0 rm -- or -exec rm -- {} ;
                – Richm
                Sep 6 '11 at 13:52












              • you can also provide the -n argument to xargs. That will cause it to split the rm commands to having a maximum number of arguments i.e. 'xargs -n 100 -0 rm --' will remove files in chunks of 100.
                – Richm
                Sep 6 '11 at 13:54










              • You can also add -maxdepth 1 to ensure that find does not return files from any subdirectories. i.e. find /path/to/directory -maxdepth 1 -type f
                – Richm
                Sep 6 '11 at 14:10








              2




              2




              GNU find as a -delete predicate. If you still want to use -exec, substituting ; with + will gather rm calls together, increasing efficiency.
              – enzotib
              Sep 6 '11 at 12:19




              GNU find as a -delete predicate. If you still want to use -exec, substituting ; with + will gather rm calls together, increasing efficiency.
              – enzotib
              Sep 6 '11 at 12:19




              1




              1




              large amount of files with '+' will cause problems, since list will be too large, same as rm -f *. and when removing large amounts of files from same folder (talking in millons) both of them are not good :) In the end C++ came along and removed files in order of inodes in dir-tree.
              – Osis
              Sep 6 '11 at 12:53




              large amount of files with '+' will cause problems, since list will be too large, same as rm -f *. and when removing large amounts of files from same folder (talking in millons) both of them are not good :) In the end C++ came along and removed files in order of inodes in dir-tree.
              – Osis
              Sep 6 '11 at 12:53




              3




              3




              You really ought to add a -- after the rm. Without that if you have a file names -rf or similar will be interpreted as arguments to rm. e.g. xargs -0 rm -- or -exec rm -- {} ;
              – Richm
              Sep 6 '11 at 13:52






              You really ought to add a -- after the rm. Without that if you have a file names -rf or similar will be interpreted as arguments to rm. e.g. xargs -0 rm -- or -exec rm -- {} ;
              – Richm
              Sep 6 '11 at 13:52














              you can also provide the -n argument to xargs. That will cause it to split the rm commands to having a maximum number of arguments i.e. 'xargs -n 100 -0 rm --' will remove files in chunks of 100.
              – Richm
              Sep 6 '11 at 13:54




              you can also provide the -n argument to xargs. That will cause it to split the rm commands to having a maximum number of arguments i.e. 'xargs -n 100 -0 rm --' will remove files in chunks of 100.
              – Richm
              Sep 6 '11 at 13:54












              You can also add -maxdepth 1 to ensure that find does not return files from any subdirectories. i.e. find /path/to/directory -maxdepth 1 -type f
              – Richm
              Sep 6 '11 at 14:10




              You can also add -maxdepth 1 to ensure that find does not return files from any subdirectories. i.e. find /path/to/directory -maxdepth 1 -type f
              – Richm
              Sep 6 '11 at 14:10











              6














              If you also want to remove all subdirectories and the directory itself, you can use rm -rf /path/to/directory. But always double-check your line before pressing return, rm -rf can cause lots of havock as well, e.g. if you accidentally insert a space after the first slash while having superuser permissions...






              share|improve this answer


























                6














                If you also want to remove all subdirectories and the directory itself, you can use rm -rf /path/to/directory. But always double-check your line before pressing return, rm -rf can cause lots of havock as well, e.g. if you accidentally insert a space after the first slash while having superuser permissions...






                share|improve this answer
























                  6












                  6








                  6






                  If you also want to remove all subdirectories and the directory itself, you can use rm -rf /path/to/directory. But always double-check your line before pressing return, rm -rf can cause lots of havock as well, e.g. if you accidentally insert a space after the first slash while having superuser permissions...






                  share|improve this answer












                  If you also want to remove all subdirectories and the directory itself, you can use rm -rf /path/to/directory. But always double-check your line before pressing return, rm -rf can cause lots of havock as well, e.g. if you accidentally insert a space after the first slash while having superuser permissions...







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Sep 7 '11 at 8:51









                  Tobias KienzlerTobias Kienzler

                  3011521




                  3011521























                      6














                      To delete all files and directories(including the hidden ones) in a directory, you can try the following:





                      • delete the folder, then recreate it



                        rm -rf dir_name && mkdir dir_name



                      • use find



                        find dir_name -mindepth 1 -delete  



                      Here we specify -mindepth 1 to exclude the directory dir_name itself.

                      Take a look at the following link:
                      https://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory






                      share|improve this answer























                      • Thanks for your advice and I've added more explanation for the links.
                        – zeekvfu
                        Nov 28 '13 at 5:04






                      • 2




                        Be careful, since deleting and re-creating the folder might result in different/wrong permissions for this folder!
                        – einjohn
                        Aug 29 '15 at 12:20
















                      6














                      To delete all files and directories(including the hidden ones) in a directory, you can try the following:





                      • delete the folder, then recreate it



                        rm -rf dir_name && mkdir dir_name



                      • use find



                        find dir_name -mindepth 1 -delete  



                      Here we specify -mindepth 1 to exclude the directory dir_name itself.

                      Take a look at the following link:
                      https://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory






                      share|improve this answer























                      • Thanks for your advice and I've added more explanation for the links.
                        – zeekvfu
                        Nov 28 '13 at 5:04






                      • 2




                        Be careful, since deleting and re-creating the folder might result in different/wrong permissions for this folder!
                        – einjohn
                        Aug 29 '15 at 12:20














                      6












                      6








                      6






                      To delete all files and directories(including the hidden ones) in a directory, you can try the following:





                      • delete the folder, then recreate it



                        rm -rf dir_name && mkdir dir_name



                      • use find



                        find dir_name -mindepth 1 -delete  



                      Here we specify -mindepth 1 to exclude the directory dir_name itself.

                      Take a look at the following link:
                      https://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory






                      share|improve this answer














                      To delete all files and directories(including the hidden ones) in a directory, you can try the following:





                      • delete the folder, then recreate it



                        rm -rf dir_name && mkdir dir_name



                      • use find



                        find dir_name -mindepth 1 -delete  



                      Here we specify -mindepth 1 to exclude the directory dir_name itself.

                      Take a look at the following link:
                      https://unix.stackexchange.com/questions/12593/how-to-remove-all-the-files-in-a-directory







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Aug 29 '17 at 7:27









                      muru

                      1




                      1










                      answered Nov 27 '13 at 16:05









                      zeekvfuzeekvfu

                      26133




                      26133












                      • Thanks for your advice and I've added more explanation for the links.
                        – zeekvfu
                        Nov 28 '13 at 5:04






                      • 2




                        Be careful, since deleting and re-creating the folder might result in different/wrong permissions for this folder!
                        – einjohn
                        Aug 29 '15 at 12:20


















                      • Thanks for your advice and I've added more explanation for the links.
                        – zeekvfu
                        Nov 28 '13 at 5:04






                      • 2




                        Be careful, since deleting and re-creating the folder might result in different/wrong permissions for this folder!
                        – einjohn
                        Aug 29 '15 at 12:20
















                      Thanks for your advice and I've added more explanation for the links.
                      – zeekvfu
                      Nov 28 '13 at 5:04




                      Thanks for your advice and I've added more explanation for the links.
                      – zeekvfu
                      Nov 28 '13 at 5:04




                      2




                      2




                      Be careful, since deleting and re-creating the folder might result in different/wrong permissions for this folder!
                      – einjohn
                      Aug 29 '15 at 12:20




                      Be careful, since deleting and re-creating the folder might result in different/wrong permissions for this folder!
                      – einjohn
                      Aug 29 '15 at 12:20











                      1














                      You can cd into the directory and then run the command rm *.* just like in DOS if you remember.






                      share|improve this answer



















                      • 1




                        DOS is DEL for files and RMDIR for emptied directories- and hidden and system files must have those ATTRIB attributes removed first. Not nearly as simple ;)
                        – Eric
                        Jun 10 '15 at 13:03
















                      1














                      You can cd into the directory and then run the command rm *.* just like in DOS if you remember.






                      share|improve this answer



















                      • 1




                        DOS is DEL for files and RMDIR for emptied directories- and hidden and system files must have those ATTRIB attributes removed first. Not nearly as simple ;)
                        – Eric
                        Jun 10 '15 at 13:03














                      1












                      1








                      1






                      You can cd into the directory and then run the command rm *.* just like in DOS if you remember.






                      share|improve this answer














                      You can cd into the directory and then run the command rm *.* just like in DOS if you remember.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 1 '13 at 1:20









                      Eric Carvalho

                      41.4k17114145




                      41.4k17114145










                      answered May 1 '13 at 0:59









                      V K MavaniV K Mavani

                      111




                      111








                      • 1




                        DOS is DEL for files and RMDIR for emptied directories- and hidden and system files must have those ATTRIB attributes removed first. Not nearly as simple ;)
                        – Eric
                        Jun 10 '15 at 13:03














                      • 1




                        DOS is DEL for files and RMDIR for emptied directories- and hidden and system files must have those ATTRIB attributes removed first. Not nearly as simple ;)
                        – Eric
                        Jun 10 '15 at 13:03








                      1




                      1




                      DOS is DEL for files and RMDIR for emptied directories- and hidden and system files must have those ATTRIB attributes removed first. Not nearly as simple ;)
                      – Eric
                      Jun 10 '15 at 13:03




                      DOS is DEL for files and RMDIR for emptied directories- and hidden and system files must have those ATTRIB attributes removed first. Not nearly as simple ;)
                      – Eric
                      Jun 10 '15 at 13:03











                      0














                      To delete current directory, you could for example use rm -d ./*



                      -d tells to delete directories as well.






                      share|improve this answer


























                        0














                        To delete current directory, you could for example use rm -d ./*



                        -d tells to delete directories as well.






                        share|improve this answer
























                          0












                          0








                          0






                          To delete current directory, you could for example use rm -d ./*



                          -d tells to delete directories as well.






                          share|improve this answer












                          To delete current directory, you could for example use rm -d ./*



                          -d tells to delete directories as well.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 29 '17 at 7:17









                          arvimanarviman

                          1213




                          1213























                              0














                              Since this question is constantly at the top of Google when I search for this myself:



                              The other answers suffer from different problems:




                              1. Some of them include . and .. which is noisy, confusing, and annoying.


                              2. Some of them forget hidden files (files beginning with a dot).


                              3. They don't delete in a correct (deepest-first) order to allow directory deletion.


                              4. They descend into other (mounted) file systems, which is often undesired.


                              5. They're difficult to extend properly with extra parameters (more on that below).



                              So, to RECURSIVELY delete all files AND folders in a directory, do this:



                              find "${DIR}" -xdev -mindepth 1 -printf "%dt%yt%p" | sort -z -r -n | cut -z -f3- | xargs -0 -r -- rm -d --


                              Note that I added an -xdev argument to prevent descending into mounts (like /proc etc.).



                              Why not -depth or -delete?



                              Despite people constantly downvoting me for this, those methods have a downside: it doesn't seem like they're extensible enough to allow -pruneing a subdirectory (without introducing more problems). By contrast with this method, you could insert



                              -not ( -path "${DIR}/subdir" -prune )


                              before the -mindepth argument to exclude subdir from having its contents deleted.






                              share|improve this answer























                              • And for depth-first order, there's a -depth flag in find for that.
                                – Sergiy Kolodyazhnyy
                                May 21 '18 at 1:36










                              • Uh... why not just use -delete with find? -delete is depth-first. You're already assuming non-POSIX find with the -printf, so you might just as well use -delete or -depth -print0 | xargs -0 rm
                                – muru
                                May 21 '18 at 6:41












                              • @muru: Because I didn't know better... I'll change it.
                                – Mehrdad
                                May 21 '18 at 6:47










                              • @muru: Actually, it seems your method doesn't work with -not ( -path "$DIR/subdir" )... but mine does? Why?
                                – Mehrdad
                                May 21 '18 at 7:00












                              • @Mehrdad what is that supposed to do?
                                – muru
                                May 21 '18 at 7:06
















                              0














                              Since this question is constantly at the top of Google when I search for this myself:



                              The other answers suffer from different problems:




                              1. Some of them include . and .. which is noisy, confusing, and annoying.


                              2. Some of them forget hidden files (files beginning with a dot).


                              3. They don't delete in a correct (deepest-first) order to allow directory deletion.


                              4. They descend into other (mounted) file systems, which is often undesired.


                              5. They're difficult to extend properly with extra parameters (more on that below).



                              So, to RECURSIVELY delete all files AND folders in a directory, do this:



                              find "${DIR}" -xdev -mindepth 1 -printf "%dt%yt%p" | sort -z -r -n | cut -z -f3- | xargs -0 -r -- rm -d --


                              Note that I added an -xdev argument to prevent descending into mounts (like /proc etc.).



                              Why not -depth or -delete?



                              Despite people constantly downvoting me for this, those methods have a downside: it doesn't seem like they're extensible enough to allow -pruneing a subdirectory (without introducing more problems). By contrast with this method, you could insert



                              -not ( -path "${DIR}/subdir" -prune )


                              before the -mindepth argument to exclude subdir from having its contents deleted.






                              share|improve this answer























                              • And for depth-first order, there's a -depth flag in find for that.
                                – Sergiy Kolodyazhnyy
                                May 21 '18 at 1:36










                              • Uh... why not just use -delete with find? -delete is depth-first. You're already assuming non-POSIX find with the -printf, so you might just as well use -delete or -depth -print0 | xargs -0 rm
                                – muru
                                May 21 '18 at 6:41












                              • @muru: Because I didn't know better... I'll change it.
                                – Mehrdad
                                May 21 '18 at 6:47










                              • @muru: Actually, it seems your method doesn't work with -not ( -path "$DIR/subdir" )... but mine does? Why?
                                – Mehrdad
                                May 21 '18 at 7:00












                              • @Mehrdad what is that supposed to do?
                                – muru
                                May 21 '18 at 7:06














                              0












                              0








                              0






                              Since this question is constantly at the top of Google when I search for this myself:



                              The other answers suffer from different problems:




                              1. Some of them include . and .. which is noisy, confusing, and annoying.


                              2. Some of them forget hidden files (files beginning with a dot).


                              3. They don't delete in a correct (deepest-first) order to allow directory deletion.


                              4. They descend into other (mounted) file systems, which is often undesired.


                              5. They're difficult to extend properly with extra parameters (more on that below).



                              So, to RECURSIVELY delete all files AND folders in a directory, do this:



                              find "${DIR}" -xdev -mindepth 1 -printf "%dt%yt%p" | sort -z -r -n | cut -z -f3- | xargs -0 -r -- rm -d --


                              Note that I added an -xdev argument to prevent descending into mounts (like /proc etc.).



                              Why not -depth or -delete?



                              Despite people constantly downvoting me for this, those methods have a downside: it doesn't seem like they're extensible enough to allow -pruneing a subdirectory (without introducing more problems). By contrast with this method, you could insert



                              -not ( -path "${DIR}/subdir" -prune )


                              before the -mindepth argument to exclude subdir from having its contents deleted.






                              share|improve this answer














                              Since this question is constantly at the top of Google when I search for this myself:



                              The other answers suffer from different problems:




                              1. Some of them include . and .. which is noisy, confusing, and annoying.


                              2. Some of them forget hidden files (files beginning with a dot).


                              3. They don't delete in a correct (deepest-first) order to allow directory deletion.


                              4. They descend into other (mounted) file systems, which is often undesired.


                              5. They're difficult to extend properly with extra parameters (more on that below).



                              So, to RECURSIVELY delete all files AND folders in a directory, do this:



                              find "${DIR}" -xdev -mindepth 1 -printf "%dt%yt%p" | sort -z -r -n | cut -z -f3- | xargs -0 -r -- rm -d --


                              Note that I added an -xdev argument to prevent descending into mounts (like /proc etc.).



                              Why not -depth or -delete?



                              Despite people constantly downvoting me for this, those methods have a downside: it doesn't seem like they're extensible enough to allow -pruneing a subdirectory (without introducing more problems). By contrast with this method, you could insert



                              -not ( -path "${DIR}/subdir" -prune )


                              before the -mindepth argument to exclude subdir from having its contents deleted.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited May 21 '18 at 7:47

























                              answered May 21 '18 at 0:54









                              MehrdadMehrdad

                              1,65682846




                              1,65682846












                              • And for depth-first order, there's a -depth flag in find for that.
                                – Sergiy Kolodyazhnyy
                                May 21 '18 at 1:36










                              • Uh... why not just use -delete with find? -delete is depth-first. You're already assuming non-POSIX find with the -printf, so you might just as well use -delete or -depth -print0 | xargs -0 rm
                                – muru
                                May 21 '18 at 6:41












                              • @muru: Because I didn't know better... I'll change it.
                                – Mehrdad
                                May 21 '18 at 6:47










                              • @muru: Actually, it seems your method doesn't work with -not ( -path "$DIR/subdir" )... but mine does? Why?
                                – Mehrdad
                                May 21 '18 at 7:00












                              • @Mehrdad what is that supposed to do?
                                – muru
                                May 21 '18 at 7:06


















                              • And for depth-first order, there's a -depth flag in find for that.
                                – Sergiy Kolodyazhnyy
                                May 21 '18 at 1:36










                              • Uh... why not just use -delete with find? -delete is depth-first. You're already assuming non-POSIX find with the -printf, so you might just as well use -delete or -depth -print0 | xargs -0 rm
                                – muru
                                May 21 '18 at 6:41












                              • @muru: Because I didn't know better... I'll change it.
                                – Mehrdad
                                May 21 '18 at 6:47










                              • @muru: Actually, it seems your method doesn't work with -not ( -path "$DIR/subdir" )... but mine does? Why?
                                – Mehrdad
                                May 21 '18 at 7:00












                              • @Mehrdad what is that supposed to do?
                                – muru
                                May 21 '18 at 7:06
















                              And for depth-first order, there's a -depth flag in find for that.
                              – Sergiy Kolodyazhnyy
                              May 21 '18 at 1:36




                              And for depth-first order, there's a -depth flag in find for that.
                              – Sergiy Kolodyazhnyy
                              May 21 '18 at 1:36












                              Uh... why not just use -delete with find? -delete is depth-first. You're already assuming non-POSIX find with the -printf, so you might just as well use -delete or -depth -print0 | xargs -0 rm
                              – muru
                              May 21 '18 at 6:41






                              Uh... why not just use -delete with find? -delete is depth-first. You're already assuming non-POSIX find with the -printf, so you might just as well use -delete or -depth -print0 | xargs -0 rm
                              – muru
                              May 21 '18 at 6:41














                              @muru: Because I didn't know better... I'll change it.
                              – Mehrdad
                              May 21 '18 at 6:47




                              @muru: Because I didn't know better... I'll change it.
                              – Mehrdad
                              May 21 '18 at 6:47












                              @muru: Actually, it seems your method doesn't work with -not ( -path "$DIR/subdir" )... but mine does? Why?
                              – Mehrdad
                              May 21 '18 at 7:00






                              @muru: Actually, it seems your method doesn't work with -not ( -path "$DIR/subdir" )... but mine does? Why?
                              – Mehrdad
                              May 21 '18 at 7:00














                              @Mehrdad what is that supposed to do?
                              – muru
                              May 21 '18 at 7:06




                              @Mehrdad what is that supposed to do?
                              – muru
                              May 21 '18 at 7:06





                              protected by αғsнιη May 21 '18 at 3:04



                              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?



                              Popular posts from this blog

                              Ellipse (mathématiques)

                              Quarter-circle Tiles

                              Mont Emei