Computing.Net > Forums > Unix > finding new files

finding new files

Reply to Message Icon

Original Message
Name: Trevor
Date: December 19, 2002 at 16:13:13 Pacific
Subject: finding new files
OS: HP Unix
CPU/Ram: don't know
Comment:

Does anybody know how to find a file that has just been created, grab that file name then assign it to a variable, in a shell script?


Report Offensive Message For Removal


Response Number 1
Name: David Perry
Date: December 20, 2002 at 04:42:59 Pacific
Subject: finding new files
Reply: (edit)

for file in `find / -type f -mtime -1 -print` ; do
echo $file
done

do a man on "find" for more options


Report Offensive Follow Up For Removal

Response Number 2
Name: James Boothe
Date: December 20, 2002 at 07:37:02 Pacific
Subject: finding new files
Reply: (edit)

The solution above would find files system-wide that have been created or modified in the last 24 hours.

If you want to target a specific directory, the following solution will return just one file name, being the regular file with the most recent modify date:

fname=`ls -lt|grep "^-"|head -1|awk '{print $NF}'`

The ls -lt will list files in order of modification date, and granularity is much more fine than the HH:MM that we can see.

This would be the file that has been most recently created OR modified. You cannot differentiate, and the inode date will not help either.

If you really need the most recently created file and not the most recently created or modified file, I would suggest an approach that saves the file names each time the script runs so that the list can be compared with the next execution.


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: finding new files

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software