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
584 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 11

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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
Utilizing an array to gracefully append to a list of EmailAddresses
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now