Newest file changed

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    The program should search all files in current directory and it's subdirectories and list out the newest file by the date(last changed). Argument is a directory!

the input is: newest /usr/etc
and the output should look like this(with the subdirectories included):
/usr/etc/httpd/httpd Oct 25 12:16

This task is giving me a headache, please help :D!

  1. Relevant commands, code, scripts, algorithms:

ls -lt, cut, grep,...

  1. The attempts at a solution (include all code and scripts):
    i'm not sure in which direction to go with this one, but i think
    i should list all files by time and then cut out the date and time and then
    grep the first one

#!/bin/bash
newest()
{
ls -lt (R?) | cut -d" " -f6,7 | grep...
}
newest /usr/etc
exit

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    University of Ljubljana, Ljubljana, Slovenia, Janez Novak, ID63709

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Assuming that the required output is the one file which is newer than any other file in the required tree.

It is not necessary or desirable to start with a formated directory listing.

One approach is:
Use "touch" to seed a temporary comparison file dated the epoch.
Use "find" with the "-type f" option to produce a list of files to a pipeline.
Use the shell "-nt" comparison operator to compare each file in the pipeline with the comparision filename and make the current filename the comparison filename if it is newer.
After processing all the files use "ls -ladog" to display the directory listing of the comparision filename - which is by now the the newest file in the list.

If the required output actually is the newest file in each directory one approach is to first use "find" with the "-type d" option to produce a list of directories to a pipeline, then manipulate a directory list by timestamp for each directory in that pipeline.

Ok, this ladog switches are good :slight_smile:

i have this so far:
ls -ladog | cut -d" " -f4,5

but how can i search the subdirectories to, i mean how can i manipulate the find -type d directories, it gives me a bunch of them :slight_smile:

if your toolbox is limited to ls, cut and grep...then you might try the following:

ls -ltR [/dir/path] |grep ^d |cut ... 

Point to note, using cut in a delimiter fashion can get hairy since you're not squeezing them down to single delimiters (ie, tr -s ' '), so ls is likely to wander as it encounters varying numbers of spaces, etc. You'd probably want to use cut in a fixed-width manner and say cut -c x-y, etc...

hm if i do this: ls -ltR /home/blabla | grep ^d | cut -c 33-48
then it doesn't show me the subdirectories and with -c switch it can cut off some data,
coz if the size of file is smaller than i get time cutted off a little.
What does grep ^d do, coz it it gives me a reverse order, it should be newest first

The "grep ^d" restricts the returnset to only directories, or files with the directory attribute in the leftmost position... It wouldn't do anything to your sorted order.

Try the ls command's -r option as well (ie, ls -ltrR).

Also, if the path length is going to vary widely, you'd need to maybe pipe it through something else to ensure proper alignment. Is the assignment restricted to only some utils, or can you leverage whatever solution you can come up with?

I can use any solution i want, as long as i can understand it :smiley: and it's not to complex, do you have
something on mind?

oh boy...now I have to know what you understand too? :wink:

How's this? I'd suggest you ask questions if you're not sure...

$ ls -ltr $(find . -type f -newer /usr/etc ) |tail -1 

Using your previous function-based approach:

$ function newest
> {
> ls -ltr $(find . -type f -newer $1 ) |tail -1
> }
$ newest /usr/etc
-rw-rw-rw-  1 curleb   sys        14232 May 26 13:39 ./uwin_log

This is understandable, so now i just have to cut things out and reorder them i think :smiley:
But it must list the files which were last changed not used!

How can i change the date display to show: Jun 12 15:55
and not 2010-06-12 15:55