LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Delete all files in particular directory (https://www.linuxquestions.org/questions/linux-server-73/delete-all-files-in-particular-directory-4175534402/)

preetham.66666 02-18-2015 05:55 AM

Delete all files in particular directory
 
I want to find a files compile.logs that older than 90 days in /compile directory this can be achieved by using below command

find /compile -mite +90 -name "compile.logs"

and i get out like this

/compile/home/bob/work/para/build.ntb/compile.logs
/compile/home/meta/work/mac/eth/build.ntb/compile.logs
/compile/home/meta/work/core/qlo/hba/ds-x/port1/build.ntb/compile.logs


Now please advice how to delete all the files after build.ntb directory

wpeckham 02-18-2015 06:07 AM

RTFM
 
Man page for find mentions -delete option.
Use with care.

syg00 02-18-2015 06:12 AM

Might depend on how you define "all" in the following - "how to delete all the files after build.ntb directory".
And, of course, which "build.ntb directory".

pantdk 02-19-2015 02:02 AM

check it shown the exact dated files then move for deleted before delete check whether you are able to see the older logs

find /compile -type f -mtime +90 -name "file.log" -exec ls -lrt {} \;

preetham.66666 02-22-2015 09:44 PM

Hi

I wrote script to find the files which older than 80 days and file name contains compile_dut.log

output should be like below
/compile/home/bob/work/para/build.ntb/compile_dut.log
/compile/home/meta/work/mac/eth/build.ntb/compile_dut.log
/compile/home/meta/work/core/qlo/hba/ds-x/port1/build.ntb/compile_dut.log

and i want to delete all the files under (that is what are the files along with compile_dut.log)
/compile/home/bob/work/para/build.ntb/
/compile/home/meta/work/mac/eth/build.ntb/
/compile/home/meta/work/core/qlo/hba/ds-x/port1/build.ntb/

Please verify my script and let me know any changes required here

#!/bin/sh
#find the files which older than 80 days and file name contains compile_dut.log
find /dv4 -mtime +80 -name 'compile_dut.log' > /dv4/80days.txt
#Remove 15 chars in path
while read line
do
echo -e "$line" | sed 's/.\{15\}$//' >> /dv4/delete-dv4.txt
done < /dv4/80days.txt
#Go to specified directory and delete all the files
while read line
do
cd $line
rm -rf *
done < /dv4/delete-dv4.txt
rm -f /dv4/80days.txt
rm -f /dv4/delete-dv4.txt

Slax-Dude 02-27-2015 10:52 AM

Try this:
Code:

#!/bin/bash
list_of_directories=$(find /dv4 -mtime +80 -name 'compile_dut.log' | rev | cut -d '/' -f 2- | rev)
for each_directory in $list_of_directories; do
  echo "$each_directory/"
# rm -rf $each_directory/*.*
done

...if you are happy with the output, uncomment the rm line


All times are GMT -5. The time now is 12:24 PM.