Bourne returning files based on creation date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bourne returning files based on creation date
# 1  
Old 08-17-2016
Lightbulb Bourne returning files based on creation date

I'm wanting to write a bourne shell script that takes in two command line arguments - a directory and a file. With this I want to return a list of files within the directory that are older (based on creation date) than the given file, and print the number of files that have not been listed (they are newer than the given file).
I've never used bourne before so I'm hoping someone could help me out with a few tips on how I'd go about this. I've been thinking it will need an if statement that will separate those older files from the newer ones and update a count of the newer ones? But I'm unsure about how to work with the dates and such.

I've written a little mock up, that I know isn't correct but would something like this come anywhere close to working?

Code:
#!/bin/sh

directory = $1
file = $2
count = 0

for f in $directory
do
    if [ "$f" –ot "$file"]; then
	echo "$f"
	count='expr $count + 1'
    fi
done

echo "There are $x newer files"

Any help would be greatly appreciated!
Thanks!

Last edited by britty4; 08-17-2016 at 01:11 PM..
# 2  
Old 08-17-2016
here's something to start with for the 'older' files:
Code:
find myDirectory ! -cnewer myReferenceFile -print

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 08-17-2016
-ot is defined for the [[ ]] compound in ksh and bash:
Code:
#!/bin/ksh

directory=$1
file=$2
count=0

for f in "$directory"/*
do
  if [[ "$f" -ot "$file" ]]; then
    echo "$f"
    count=$((count + 1))
  fi
done

echo "There are $x newer files"

As you see, ksh and bash also have the built-in $(( )) arithmetic.

As already suggested, this might be easier with find, and can be done also in an old Bourne shell:
Code:
#!/bin/sh

directory=$1
file=$2

find "$directory" -newer "$file" -o -print

Both -newer and -ot take the file's mtime (not ctime as -cnewer does).
But the find will descend into sub-directories and sub-sub-directories if present.

Last edited by MadeInGermany; 08-17-2016 at 02:39 PM.. Reason: added missing space
This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 08-17-2016
Thanks heaps for clearing that up! Do you know if there's a way to do the same as find, but instead count the files instead of printing them all out?
# 5  
Old 08-17-2016
Quote:
Originally Posted by britty4
Thanks heaps for clearing that up! Do you know if there's a way to do the same as find, but instead count the files instead of printing them all out?
Code:
find .... | wc -l

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Copy files in order of creation date

Hi everyone :-) I ran into a small issue. I would like to copy some files in the precise order they were created. So the oldest files should be copied first and the newest ones last. I tried cp -r $(ls -1t) ./destination but the files are still not sorted properly. I was thinking, that... (11 Replies)
Discussion started by: officiallyme
11 Replies

2. UNIX for Dummies Questions & Answers

Unable to find files, those can be present anywhere in the directory tree,based on its creation date

Hi I am unable to find files, those are present anywhere in the same directory tree, based on the creation date. I need to find the files with their path, as I need to create them in another location and move them. I need some help with a script that may do the job. Please help (2 Replies)
Discussion started by: sam192837465
2 Replies

3. UNIX for Dummies Questions & Answers

Select all files in a folder based on creation date (ls command)

Hi All, <Re-posting in Correct group> I'm trying to select all the files in a folder that starts with a particular name format and are created in a gven date range using 'ls' command...but i'm not successful.... Example : I'm trying to see all the text files in a folder who names start... (6 Replies)
Discussion started by: Satya C1
6 Replies

4. Shell Programming and Scripting

Moving files based on file creation

Hi, I have a directory having so many number of files. Now I want to move the files which are older than one month (lets say) from this directory to another directory (say BKP dir). Simply, if file is olderthan one month move it from source1 dir to BKP1 dir. My file names doesn't have... (7 Replies)
Discussion started by: karumudi7
7 Replies

5. Shell Programming and Scripting

Move files from one directory to another based on creation/modification date

Hi All, Really stuck up with a requirement where I need to move a file (Lets say date_Employee.txt--the date will have different date values like 20120612/20120613 etc) from one directory to another based on creation/modification dates. While visiting couple of posts, i could see we can... (3 Replies)
Discussion started by: dsfreddie
3 Replies

6. Shell Programming and Scripting

copy files based on creation timestamp

Dear friends.. I have the below listing of files under a directory in unix -rw-r--r-- 1 abc abc 263349631 Jun 1 11:18 CDLD_20110603032055.xml -rw-r--r-- 1 abc abc 267918241 Jun 1 11:21 CDLD_20110603032104.xml -rw-r--r-- 1 abc abc 257672513 Jun 3 10:41... (5 Replies)
Discussion started by: sureshg_sampat
5 Replies

7. Shell Programming and Scripting

mp3 tag/rename based on creation (last modified date)

Arg, I'm trying to figure out how to create a album tag based on the last modified date stamp for files which don't have a corresponding .talk file. IE. 2009 12 10 - Talk Radio.mp3 is how I want them structured, they should all have a corresponding .talk file so my mp3 player can speak the name ie... (0 Replies)
Discussion started by: mrplow
0 Replies

8. Shell Programming and Scripting

Move files based on year of creation

Hi All, I have a directory which has crores of files since from 2003 till now. I want to move only the 2003 files to another directory. Please help. Thanks (2 Replies)
Discussion started by: IHK
2 Replies

9. UNIX for Dummies Questions & Answers

Moving files based on creation date

Howdy, I'm trying to figure out how to move multiple files based on their creation date. If anyone can enlighten me it would be most appreciated!! Thanks! :D (1 Reply)
Discussion started by: dgoyea
1 Replies
Login or Register to Ask a Question