Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read files from directory and send to a list file 1

Status
Not open for further replies.

starlite79

Technical User
Aug 15, 2008
89
US
Hi,

I would like to create a list of the 15 most recent files in a directory. So far I've used the command

ls -rt | tail -15

My question is: How do I assign this to a variable, say LIST, and then copy the list to a file, as in

cp $LIST > $INPUT_LIST ?

My goal is to use this list in a Fortran program to read those 15 files as input.

I am using Korn shell.
 
You can also save the output of ls directly to a file
Code:
ls -1 -rt | tail -15 > file
 
Yes. But I'm trying to do this inside of a script. I would like to set that ls command to a variable.
 
If you've figured it out, you might like to share for the benefit of others searching for a similar solution. Presumably, you based it on fpmurphy's contribution?

I want to be good, is that not enough?
 
try


LIST=/somedirectory/tmpfile.tmp
ls -al /TheDirectoryYouWantToMonitor> 2>/dev/null | tail -5 > $LIST
cat $LIST > /somedirectory/output.txt
rm /somedirectory/tmpfile.tmp

Please let me know if the information that was provided is helpfull.
Edwin Plat
A.K.A. Europe
 
Good point, Ken. Yes, I did follow fpmurphy's suggestion.

Since I wanted to use the latest 15 files of a particular extension, I did the following:

LIST=$(ls -rt *.ext | tail -15 > OUTPUT.LIST )

where .ext is any extension you want. Europe's suggestion looks good too, though I have not tested it. The OUTPUT.LIST will get overwritten each time a newer file comes along, but that is what I want it to do.

Hopefully others may find the above useful.

 
Hi

Huh ?
Code:
                     [red]redirecting output[/red]
                               [red]v[/red]
LIST=$(ls -rt *.ext | tail -15 > OUTPUT.LIST )
      [blue]^[/blue]                                      [blue]^[/blue]
      [blue]`----------- capturing output ---------'[/blue]
After the output is redirected nothing remains to be captured. So $LIST will be always empty. You will need only one of those : either the redirection or the capturing.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top