1

Below, I am trying to find the latest version of a file that could be in multiple directories.

Example Directory:

~inventory/emails/2012/06/InventoryFeed-Activev2.csv    2012/06/05
~inventory/emails/2012/06/InventoryFeed-Activev1.csv    2012/06/03
~inventory/emails/2012/06/InventoryFeed-Activev.csv     2012/06/01

Heres the bash script:

#!/bin/bash

FILE = $(find ~/inventory/emails/ -name INVENTORYFEED-Active\*.csv | sort -n | tail -1)
#echo $FILE #For Testing

cp $FILE ~/inventory/Feed-active.csv;

The error I am getting is:

./inventory.sh: line 5: FILE: command not found

The script should copy the newest file as attempted above.

Two questions:

First, is this the best method to achive what I want? Secondly, Whats wrong above?

Tom
  • 1,234
  • 1
  • 18
  • 39
  • 2
    Please see [BashFAQ/003](http://mywiki.wooledge.org/BashFAQ/003). – Dennis Williamson Jun 24 '12 at 01:02
  • See above link please. All answers on this page still have issues, for instance, filenames containing newlines. Find doesn't sort by mtime for you, so what you have now doesn't do anything useful. – ormaaj Jun 24 '12 at 02:08

2 Answers2

6

It looks good, but you have spaces around the = sign. This won't work. Try:

#!/bin/bash

FILE=$(find ~/inventory/emails/ -name INVENTORYFEED-Active\*.csv | sort -n | tail -1)
#echo $FILE #For Testing

cp $FILE ~/inventory/Feed-active.csv;
Spencer Rathbun
  • 14,510
  • 6
  • 54
  • 73
1

... Whats wrong above?

Variable assignment. You are not supposed to put extra spaces around = sign. The following should work:

FILE=$(find ~/inventory/emails/ -name INVENTORYFEED-Active\*.csv | sort -n | tail -1)

... is this the best method to achive what I want?

Probably not. But the best way depends on many factors. Perhaps whoever writes those files, can put them in a right location in the first place. You can also check file modification time, but that could fail, too... So as long as it works for you, I'd say go for it :)