1

I am trying to delete files from the Unix directory which starts with EXPORT_v1x0 and whose date is less than 2013-01-25 (Jan 25th 2013). I can delete the files one by one but it will take few days for me to delete all the files. Is there any better way to delete the files with the specific pattern?

Below are the sample files when I do ls

bash-3.00$ ls /data/bds/real
EXPORT_v1x0_20120811.dat.gz 
EXPORT_v1x0_20120811.dat.gz   

If you see the above files. Each file has a date in that. Suppose we take this file into the consideration-

EXPORT_v1x0_20120811.dat.gz

It's date is 20120811 So I need to delete all the files which starts with EXPORT_v1x0 and whose date is less than 20130125. So If I am supposed to delete all the files having date less than 20130125 then all the files above I mentioned will get deleted as there dates are less than 20130125.

NOTE:- All the files are having same pattern exactly as I mentioned above. Only date and other numbers followed by that are different.

So I just need to delete all the files which starts with EXPORT_v1x0 and whose date is less than 20130125.

I am running SunOS. I am still in the process of learning Unix better. So not sure of any high ends commands and scripts.

arsenal
  • 23,366
  • 85
  • 225
  • 331

2 Answers2

1

A first naive approach to the problem, tweek it to your needs:

find . | awk -F'_' '$3<20130125' | xargs rm

To prevent find from doing a recursive search and just stay in current folder:

find . \( ! -name . -prune \) -type f | ...

2nd update:

Add the name parameter to only list files that contains the string "EXPORT_v1x0"

find . \( ! -name . -prune \) -type f -name "EXPORT_v1x0*" | ...

Simpler way to make find non-recursive is to use the maxdepth flag

find . -maxdepth 1 -type f -name "EXPORT_v1x0*" | ...
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
  • Thanks Fredrik for the suggestion. If I need to do some dry run before deleting the files then should I just remove xargs rm right? – arsenal Feb 03 '13 at 19:53
  • exactly. and the `find` command is extremely versatile and with different parameters you can narrow down the files listed to match exactly those you're after – Fredrik Pihl Feb 03 '13 at 19:55
  • If I am trying this command `find . | awk -F'_' '{if($3 < 20121025)print}'` then it is printing all the files from other directories as well not from the current directory in which I am there. I will be deleting all the files from the current directory. Any thoughts what changes I need to make in that? – arsenal Feb 03 '13 at 19:58
  • And one more thing, there are other files as well in that same directory having dates as well. I need to delete all the files which starts with `EXPORT_v1x0` this and has the date pattern less than I mentioned in my question. I am going to edit my question as well. – arsenal Feb 03 '13 at 20:02
  • Thanks. And then I need to make any changes in the first script you have for deleting purpose If I need to delete files in the current directory for all the files starting with `EXPORT_v1x0` and whose date is less than `20130125`.? – arsenal Feb 03 '13 at 20:07
  • Make more sense now with your update.. One more thing with your 2nd update, if I need to delete the files, then I can simply put it like this in your 2nd update- `find . \( ! -name . -prune \) -type f -name "EXPORT_v1x0*" | xargs rm` right? – arsenal Feb 03 '13 at 20:10
  • Then you can just use standard shell globbing like: `rm EXPORT_V1x0*` – Fredrik Pihl Feb 03 '13 at 20:12
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/23865/discussion-between-techgeeky-and-fredrik) – arsenal Feb 03 '13 at 20:14
0

WARNING: Be very careful

You list the files that you want like this:

ls -1 | awk -F _ '$3<"20130125"'

if that gives the proper list of files, you can do

ls -1 | awk -F _ '$3<"20130125"' | xargs rm
Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
  • Thanks Vaughn for the suggestion. When I tried your first suggestion to print the files, I got this exception `bash-3.00$ ls -1 | awk -F _ '$3<"20130125" {print}' awk: syntax error near line 1 awk: bailing out near line 1 `. WHy? Because of different Unix flavor I am running? – arsenal Feb 03 '13 at 19:56
  • @TechGeeky: I'm not sure. That is very basic awk. Make sure the single quotes and double quotes are right. – Vaughn Cato Feb 03 '13 at 19:59
  • @TechGeeky: Actually, the `{print}` is unncessary. I've removed it. Although it shouldn't have caused a problem. – Vaughn Cato Feb 03 '13 at 20:01
  • @Fredrik: Agreed this isn't a good approach in general, but for this very specific case it seems like a quick and easy way to get it done. – Vaughn Cato Feb 03 '13 at 20:04