Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Check for the NEWEST file in a linux directory & cp the NEWEST file to from /path1/file.txt to /path2/file2/txt

Posted on 2011-03-01
9
Medium Priority
?
620 Views
Last Modified: 2012-05-11
       I need to pull the lastest file from a directory so the script needs to check by when the file was most recently added. Is this possible? If not, an alternative would be to look at a file with the highest number after a certain part of the file name and copy that file to path 1. So if the files are name sequentially, the higher number after the name portion of the file would need to be pulled. Example /path1/stuff55555.txt and if that was the highest numbered file it would look at the number portion only of all files beginning with stuff and copy that file to /path2/file2/txt
0
Comment
Question by:libertyforall2
  • 4
  • 4
9 Comments
 
LVL 8

Expert Comment

by:pwust
ID: 35012778
ls -rt --group-directories-first|tail -n1

Open in new window

This will give you the latest non-directory file in the current dir. You can reuse that snippet surrounded by $(( and )) in a cp command.
0
 

Author Comment

by:libertyforall2
ID: 35013085
cp $((ls -rt --/path -first|tail -n1)) /path2

I'm getting illegal variable name.
0
 
LVL 12

Expert Comment

by:tel2
ID: 35013656
Hi lfa2,

Your error is probably the result of you partially replacing "--group-directories-first" with a path name.  Don't do that, because it is a switch for some versions of the "ls" command.  To see if your "ls" command allows that switch, just type this from the command line:
    ls --group-directories-first
and report back here.
It looks as if pwust's command is meant to be run from the source folder (i.e. cd there first).

Questions:
1. Do you need to cater for the scenario where the newest file could be not yet completely written, when this proposed script tries to take a copy of it?
2. If so, how long should we allow for the file to be completely written, at the longest?
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 8

Expert Comment

by:pwust
ID: 35016383
Please rety with using back ticks:
cp `ls -rt --group-directories-first|tail -n1` /path2

Open in new window


Piping within a $(( )) environment usually causes problems.
0
 
LVL 8

Expert Comment

by:pwust
ID: 35016395
Another suggestion, now with the source path:

cp `ls -rt --group-directories-first /path1|tail -n1` /path2

Open in new window


Hope this helps.
0
 

Author Comment

by:libertyforall2
ID: 35022039
I tried this

cp `ls -rt --group-directories-first /path1|tail -n1` /path2 

Open in new window


I get this error message

-------
ls: unrecognized option '--group-directories-first'
try 'ls --help' for more information.
cp:missing destination file operand after /path2
Try 'cp --help'  for more information.
0
 

Author Comment

by:libertyforall2
ID: 35022065
I tried to cd in path directory then using
cp `ls -rt --group-directories-first|tail -n1` /path2 

Open in new window

and got the same message. Also, this doesn't appear to differentiate between files within the directory. It should only look at files beginning with "stuff" and ignore eveything else.
0
 
LVL 8

Accepted Solution

by:
pwust earned 2000 total points
ID: 35022280
you can retry to leave out this option --group-directories-first. Its existence depends on the actual shell version.

However, you should not have any subdirectories in your source dir that could have a newer modification time than your newest regular file.
0
 

Author Closing Comment

by:libertyforall2
ID: 35184436
Still doesn't seem to work.
0

Featured Post

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question