Reputation: 55
I have two folders, for arguments sake /Volumes/A and /Volumes/B. They are mounted network shares from a Windows server containing a load of .bkf files from Backup Exec.
I am looking for a script which will look into both folders and find the most recently modified .bkf file and copy it to another location. There are other files in the folders which must be ignored.
Thanks in advance!!
Shaun
Edit:
I knocked this together:
cp ls -alt /Volumes/E /Volumes/F| grep bkf | head -n 1 | awk '{print $8}' /Volumes/$xservedisk/Windows/
Can anyone think of any reasons why I shouldnt use it?
Thanks again Shaun
Upvotes: 4
Views: 8809
Reputation: 2486
I prefer this for finding the most recently modified file:
find . -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort
Upvotes: 5
Reputation: 67859
Finding files is done with: find /Volumes/[AB] -name '*.bkf'
Sorting files by modification time is done with: ls -t
if load of files is not that much, you can simply use:
ls -lrt `find /Volumes/[AB] -name '*.bkf'`
The last displayed file is the most recently modified.
edit
A more robust solution (thanks ephemient) is:
find /Volumes/[AB] -type f -name '*.bkf' -print0 | xargs -0 ls -lrt
Upvotes: 2
Reputation: 204876
Goes through some twists just to make sure to handle filenames with odd characters well, which mouviciel's doesn't:
NEWEST=$(find /Volumes/A /Volumes/B -name '*.bkf' -printf '%T@ %p\0' | \
sort -rnz | xargs -0n1 2>/dev/null | head -n1 | cut -d' ' -f2-)
[[ -n "$NEWEST" ]] && cp -v "$NEWEST" /other-location
Actually, since these files are coming from Windows and are thus pretty much guaranteed not to have odd characters in their names (like embedded newlines),
NEWEST=$(find /Volumes/A /Volumes/B -name '*.bkf' -printf '%T@ %p\n' | \
sort -rn | head -n1 | cut -d' ' -f2-)
[[ -n "$NEWEST" ]] && cp -v "$NEWEST" /other-location
Upvotes: 2
Reputation: 10637
cp `find /Volumes/[AB] -name '*bkf' -type f -printf "%A@\t%p\n" |sort -nr |head -1 |cut -f2` dst_directory/
Upvotes: 1
Reputation: 53320
NEWEST=
for f in /Volumes/A/*.bkf /Volumes/B/*.bkf
do
if [ -z "$NEWEST" ]
then
NEWEST=$f
elif [ "$f" -nt "$NEWEST" ]
then
NEWEST=$f
fi
done
Upvotes: 4