I have a folder which in turn contain multiple sub-folders. All of these sub folders have a log folder. How to clear all the log files alone from all the log folders in these sub-directories using a single command from the parent directory?



How to clear these log files alone? Please note the sub folders sub1 and sub2 may contain other folders as well with some other files which should not be deleted. System is red hat Linux.

  • 1
    Please edit your question and use something like ls or tree to show us your directory structure. Or, at least, use the formatting tools to make it clear. I don't understand what is a subdirectory of what. Also, clarify what identified a log file. Is it any .txt file? Any file name starting with Log and a number? – terdon Apr 14 '16 at 10:37

You can use bash globbing to match multiple folders. Something like:

rm ./Folder/*/Log/*

Or maybe even better:

rm ./Folder/Sub*/Log/*

The second one will remove all the files inside the Log directory inside a directory that starts with Sub. You can add -r (recursive) or -f (force) to your rm command if needed.

For more on bash globbing, you can see another of my answers: Is it possible to specify a path in which a directory name is variable?

While that directly answers your question, you may want to consider using logrotate to manage old log files: http://www.linuxcommand.org/man_pages/logrotate8.html

  • Thaks.. Is there any command that will remove all the files except 1 file? For eg, if the log folders contain 1 type of file that is txt1.txt I want them to be untouched.. – Ra-V Apr 14 '16 at 14:22
  • @Ra-V this reeeeeeally sounds like you want to use logrotate, but you could still use globbing as per my link rm ./Folder/Sub*/Log/txt+[2-99].txt should remove files txt2.txt through txt99.txt (I didn't test this command though) – Centimane Apr 14 '16 at 14:40

Find command is very powerful, in which you are able to perform action on funded object.

 find . -type f -name "*.txt" -exec rm -f {} \;

As you can see, this command find *.txt files and perform rm on it.

To delete files just in Log folders:

find . -type d -name "Log" -exec sh -c 'find "$(realpath $1)" -type f -delete' _ {} \;

This command find all Log folders then, search for files in them.


For this case it seems like we can use find with type parameter.

    find {piyush,jain} -type f

find {piyush,jain} -type f  | xargs rm -f

find {piyush,jain} -type f
  • you can remove {} part with the directory name you have your log files. and put the same in cron job. if log file has some specific name pattern that can also be added so that it will not delete all files – Piyush Jain Apr 14 '16 at 11:01
  • 1
    If there are more files within the jain directory this will also delete those. Also, it may not be desirable to clear out both piyush/jain/ and jain/piyush/. You can also use find {piyush,jain} -type f -exec rm -f {} \; to simplify removing them. – Centimane Apr 14 '16 at 11:07
  • actually i tried to reproduce the issue with two different directories with several sub directories, that's how ended up with this solution. – Piyush Jain Apr 14 '16 at 11:08
  • But that's distinctly different from what the question is asking. What if that other directory exists but the asker doesn't want to clear out all of its files? It seems like you changed the question so that it could match an answer you already had. If you're going to answer a different question, you should mention that in your answer. – Centimane Apr 14 '16 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.