Tuesday, July 21, 2015

Removing files 0-N days old in Unix

If you want to remove files that were created on that day from a folder use find in combination with xargs:

To list all files that are 1 day old:

find . -ctime -1 -type f | xargs ls -l

To remove them verbosely:

find . -ctime -1 -type f | xargs rm -v

removed ‘./30_S14_L001_R2_001.fastq.gz’
removed ‘./15_S7_L001_R1_001.fastq.gz’
removed ‘./32_S16_L001_R1_001.fastq.gz’
removed ‘./14_S6_L001_R2_001.fastq.gz’
removed ‘./29_S13_L001_R2_001.fastq.gz’
removed ‘./23_S11_L001_R2_001.fastq.gz’
removed ‘./32_S16_L001_R2_001.fastq.gz’
removed ‘./21_S9_L001_R1_001.fastq.gz’
removed ‘./22_S10_L001_R1_001.fastq.gz’
...



To remove files 0-4 days old 


find . -ctime -4 -type f | xargs rm -v

No comments:

Post a Comment