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
596 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
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…

756 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