Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!
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!
Relevant commands, code, scripts, algorithms:
ls -lt, cut, grep,...
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
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.
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?