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
594 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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 500 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
A brief introduction to what I consider to be the best editor for PowerShell.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 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