Computing.Net > Forums > Unix > How to get latest updated file in a folder

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to get latest updated file in a folder

Reply to Message Icon

Name: sushilanand
Date: November 5, 2009 at 01:12:03 Pacific
OS: AIX 5.0
Product: Ibm Aix
Subcategory: Software Problems
Comment:

Hi

I need to get the name (only name) of the last updated file or directory in an existing folder and store it in a variable.

Thanks in advance.



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: November 5, 2009 at 07:21:01 Pacific
Reply:

I am assuming that the objects in your existing folder are only files and directories.

#!/bin/ksh

cd <your existing folder>
myvar=$(ls -1t|while read obj
do
  echo $obj
  break
done)

echo $myvar


0

Response Number 2
Name: tvc
Date: November 7, 2009 at 02:33:00 Pacific
Reply:

lastfile=`ls -1tr | tail -n 1`

would give the last updated file or directory in the current directory, and store it in variable


0

Response Number 3
Name: sushilanand
Date: November 8, 2009 at 20:51:41 Pacific
Reply:

Thanks for the help, appreciate if you pls let me know - I need to copy files from the existing non-empty folder to existing empty folder (created recently).

Thanks.


0

Response Number 4
Name: tvc
Date: November 9, 2009 at 09:29:19 Pacific
Reply:

Is that a new request, or does it relate to the previous question ?

Just use CP, no ?


0

Response Number 5
Name: sushilanand
Date: November 9, 2009 at 19:41:14 Pacific
Reply:

Hi

But how would I check the latest empty folder and non empty folder. I need to write a scrip, first to check the non empty folder and copy files from there to latest empty folder.

Thanks in advance.


0

Related Posts

See More



Response Number 6
Name: tvc
Date: November 10, 2009 at 03:39:31 Pacific
Reply:

That's a totally different question than the one from the initial post !

And also, somewhat more difficult. So, you need to know the name of the latest non-empty directory ?

One way would be to explicitally create one ... but that may not be acceptable as a solution. I'll have a think


0

Response Number 7
Name: sushilanand
Date: November 10, 2009 at 19:46:43 Pacific
Reply:

Hey...

I have done that... thanks a lot for your prompt support.

Regards


0

Response Number 8
Name: tvc
Date: November 11, 2009 at 03:37:45 Pacific
Reply:

OK, good ... take care that if you create a folder, it may already be there !

In that case, it may not be empty, and if it is empty, it may not be the most recent folder ...


0

Sponsored Link
Ads by Google
Reply to Message Icon





Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: How to get latest updated file in a folder

How to keep the log file a fixed size www.computing.net/answers/unix/how-to-keep-the-log-file-a-fixed-size/2436.html

how to get fixed width file in unix www.computing.net/answers/unix/how-to-get-fixed-width-file-in-unix/7719.html

how to store the output file in .xls format www.computing.net/answers/unix/how-to-store-the-output-file-in-xls-format/8402.html