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
605 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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 You Headed to Black Hat USA 2017?

Getting ready for Black Hat next week? Kick things off with the WatchGuard Badge Challenge and test your puzzle and cipher skills. Do you have what it takes to earn our limited edition Firebox Badge? Get started today - https://crimsonthorn.net

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
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…

623 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