2

How to delete file from folder and subfolder currently using below command for delete logs but i want delete logs file in folder and subfolder in Linux .

currently i am doing enter every folder and delete log for specific year. is there any command for delete same file from folder and subfolder in linux

volumes/abc/http.log2019-07-09
volumes/cdf/http.log2019-07-09

i want single command for delete files all folders in Linux

Currently using below command for delete file

sudo rm http.log2019*
Addi Khan
  • 85
  • 1
  • 3
  • 12

2 Answers2

8

You can use "find" command with "delete" option. This will remove files with specified name in current directory and sub directories.

find . -name "http.log2019*" -delete
Ruchira Nawarathna
  • 1,137
  • 17
  • 30
1

You can use find command to list all the files matching a pattern and then iterate over the list to delete each of the files individually.

find <directory_path> -regex '.*http.log2019[^/]*'

This returns files within the mentioned directory and its subdirectories along with their relative paths. Now you can use rm command over the list to delete all of them. Assuming that your directory path is /Volume/, you can do as following-

for file_path in `find /Volume/ -regex '.*http.log2019[^/]*'`; do sudo rm $file_path; done

If you also want to delete the directories matching that pattern within the current directory and its subdirectories, then include option -r with rm.

yabhishek
  • 408
  • 4
  • 14