How to clear all files in a folder ending with `.log`? [duplicate]
Sophia Terry
I have log files in a folder. These files after some size are creating a new file old_name.log1 and writing to it.
Now there are many files and I can't clear them one by one. I want to delete old_name.log1, old_name.log2 etc and clear old_name.log.
The file_name can be anything. But the file ends with .log and it's extended files ends with .log1, .log2, etc. How to do it?
3 Answers
To delete .log1, .log2, etc. files:
rm *.log[1-9]*rm- Delete files*.log[1-9]*- All files in the current directory that contain.logfollowed by a digit 1-9 then anything else
To test the command before running it, replace the rm with echo. It will print the matching files.
To truncate .log files:
echo -n | tee *.logecho -n- Print nothingtee *.log- Write from stdin to all .log files in the current directory
Note that OP apparently wants to truncate files. In such case, the desired command is
find /path/to/dir -regextype sed -regex ".*\.log[1-9]*" -exec truncate -s 0 {} \;Alternatively, in bash
shopt -s globstar
truncate -s 0 **/*.log[1-9]If you want to clear out any file that has .log in the name
find /path/to/dir -name "*.log*"If you target .log[digit] specifically, use
find /path/to/dir -regextype sed -regex ".*\.log[1-9]*"Once you verify either of these two commands find the files you want, append -delete to the command for actual removal
Quick and dirty... assuming that you have permissions for these log files and directory itself... and you wish to delete the .log* files...
- in a
Fileswindow, open the directory where the log files are located - search for .log files
Select All, control-click any files to exclude (like the active and open *.log)- then
Move to Trash
Note: if you wish to "clear" the .log file, close the application that makes the current .log file, then delete the current .log file with Move to Trash, and then right-click in the folder, select New Document, then Blank Document, and Rename... using the correct .log filename.