Newest file changed

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Newest file changed
# 1  
Old 05-26-2010
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 Smilie!


2. Relevant commands, code, scripts, algorithms:

ls -lt, cut, grep,...

3. 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



4. 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).
# 2  
Old 05-26-2010
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.
# 3  
Old 05-26-2010
Ok, this ladog switches are good Smilie

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 Smilie

Last edited by petel1; 05-26-2010 at 04:22 PM..
# 4  
Old 05-26-2010
if your toolbox is limited to ls, cut and grep...then you might try the following:
Code:
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...
# 5  
Old 05-26-2010
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
# 6  
Old 05-26-2010
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?
# 7  
Old 05-26-2010
I can use any solution i want, as long as i can understand it Smilie and it's not to complex, do you have
something on mind?

Last edited by petel1; 05-26-2010 at 05:39 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all but newest of given file name type

I use this to make it easier to see when a backup script ran. touch /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/$( date '+%m-%d-%Y_%I:%M-%p' ) I would like a script that would delete all but the newest file ONLY of this type 08-20-2018_01:24-PM (4 Replies)
Discussion started by: drew77
4 Replies

2. Shell Programming and Scripting

Find file by filename or with newest modified date

Hi, I have a directory that has numerous files in it, and there is two which are named "filerec_ddmmyyHH24MMSS" by the time they are created so "filerec_010615012250" was created at 01:22:50 on 1st June 2015. I need to find the most recently created of those 2 files and get the contents of... (4 Replies)
Discussion started by: finn
4 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Find the newest file in two directories

Hi i have two directories and files inside them Directory 1: Directory 2: These files are the same but song.mp3 in Directory 1 is newer than song.mp3 in Directory 2, and work.txt in Directory 2 is newer than work.txt in Directory 1. Now is my question. How i can compare these files... (10 Replies)
Discussion started by: Falstaff
10 Replies

4. Shell Programming and Scripting

Script to echo "File permissions or ownership changed from required " when accidentally changed.

Hi All, I have to work in the late nights some times for server maintenance and in a hurry to complete I am accidentally changing ownership or permission of directories :( which have similar names ( /var in root and var of some other directory ).:confused: Can some one suggest me with the... (1 Reply)
Discussion started by: shiek.kaleem
1 Replies

5. Shell Programming and Scripting

Grabbing the newest file, cleaner method?

Greetings, I'm doing a process whereby I need to search for all filenames containing a given bit of text and grab the newest file from what may be 20 results. In a script I'm writing, i've got a monster line to do the sort as follows: find /opt/work/reports/input -name "*$searchtarget*" |... (4 Replies)
Discussion started by: Karunamon
4 Replies

6. Shell Programming and Scripting

Script to check for the newest file mutiple times a day and SCP it to another server.

Hi, I need a sample of a script that will check a specific directory multiple times throughout the day, and scp the newest file to another server. Example: current file is misc_file.txt_02272011 (the last part is the date), once that has been secure copied, another one may come in later the... (1 Reply)
Discussion started by: richasmi
1 Replies

7. Shell Programming and Scripting

Mail file size of newest file in directory

I have been a long time lurker, and have learned a lot from these forums, thank you to everyone. I am using Zoneminder to record a security camera feed. No motion detection, just 24 hour recording. I then have a script that checks Mysql for events dated the day before, and throws them at... (4 Replies)
Discussion started by: iamVERYhungry
4 Replies

8. Shell Programming and Scripting

Get the newest file in a directory.

I am new to shell scripting so i need some help need how to go about with this problem. I have a directory which contains files in the following format. The files are in a diretory called /incoming/external/data AA_20100806.dat AA_20100807.dat AA_20100808.dat ... (4 Replies)
Discussion started by: ziggy25
4 Replies

9. UNIX for Dummies Questions & Answers

Who changed a file?

How can I tell what user last updated a file? (1 Reply)
Discussion started by: kirkm76
1 Replies
Login or Register to Ask a Question