Click here to Skip to main content
15,886,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to delete some files generated for a particular month to free up space in the hard disk.  
The files are populated as below:  
DRB_UCA231_MBU063_6156_20190801063645.csv.gz  
DRB_UCA231_MBU064_6156_20190801063659.csv.gz  
DRB_UCA231_MBU024_6156_20200801064402.csv.gz  
DRB_UCA231_MBU025_6156_20200801064445.csv.gz 

So  I need to remove for the month *201908*. Then I issued this command:  
rm DRB_UCA231_*201908*.csv.gz

But got an error:  
-bash: /bin/rm: Argument list too long  

Kindly help provide a working command.


What I have tried:

rm DRB_UCA231_*201908*.csv.gz

But got an error:  
-bash: /bin/rm: Argument list too long 
Posted
Updated 16-Mar-22 12:38pm

1 solution

The slow way
Bash
for file in DRB_UCA231_*201908*.csv.gz; do
  rm $file
done

The faster way
Bash
ls | grep "^DRB_UCA231_.*201908.*\.csv\.gz$" | xargs rm

The second case we're using a grep regex, so we have to escape periods, so they don't match the "any char" pattern, and we also anchor the pattern at either end. I think it might be faster with the anchors, and of course, we don't get any false hits.
In either case I think I'd do some sort of backup first, just in case the pattern/glob matches things I don't want deleted!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900