LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-26-2011, 04:14 PM   #1
Chupakabra
LQ Newbie
 
Registered: Jul 2011
Posts: 12

Rep: Reputation: Disabled
how do u write a script that searches files by date?


I'm a newb to linux. I'm trying to write a script that searches my files and lists them by date. Can someone point me in the right direction? I've been looking through the books that i have but i'm just not finding the right commands to search dates. Thank you for any help you might give me.
 
Old 07-26-2011, 04:16 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Code:
ls -lt
should work well for this if you need to include subdirectories, then perhaps you'll have to pipe output to "sort"
 
Old 07-26-2011, 04:23 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Or if searching multiple layers you can have a look at find.
 
Old 07-26-2011, 05:28 PM   #4
Chupakabra
LQ Newbie
 
Registered: Jul 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
still having problems,, I need to creat an if statement that will list whether a file is new or older than the name inputted. I'm so stuck on this and getting fustraited.
 
Old 07-26-2011, 06:05 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Have a look at the find man page as there are options for that as well
 
Old 07-26-2011, 06:44 PM   #6
Chupakabra
LQ Newbie
 
Registered: Jul 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
in vi what is the if [ $# -lt 2 ] supposed to look like if i want it to echo the newest first. the example i used does not work properly, i need it to look at the date and list the newest one first so far my program looks like this

#!/bin/bash

if [ $1 -lt $2 ]
then
echo " must have two inputs "
exit 66 # Use a none zero exit code
else
echo " the newest file was $1"
echo " the last file was $2"
exit 66 # Use a none zero exit code
fi # end of if statement

this will list the first variable first no matter what i put down and i need it to look at the date and list the newest file first no matter what i put for variable one and variable two
 
Old 07-26-2011, 06:55 PM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
I think this would be of interest, especially the '-nt' comparison...
 
Old 07-26-2011, 06:56 PM   #8
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by Chupakabra View Post
#!/bin/bash

if [ $1 -lt $2 ]
then
echo " must have two inputs "
exit 66 # Use a none zero exit code
else
echo " the newest file was $1"
echo " the last file was $2"
exit 66 # Use a none zero exit code
fi # end of if statement
I'm not sure what you want to do here. You're treating the input filenames as numbers, and if the first filename is less, then is complains that there are not enough arguments, and otherwise it says that the first file is newest. If I correctly understood what you want to do, this should work:

Code:
if (( $# != 2 ))
then
    echo "Wrong number of arguments (2 expected)" 1>&2
    exit 1
fi

if [[ $1 -nt $2 ]]
then
    echo "File 1 is newer"
elif [[ $1 -ot $2 ]]
then
    echo "File 2 is newer"
fi
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to remove old files based on date WindozBytes Linux - General 12 06-04-2012 02:21 AM
Please help me write shell script to check the modified date of a file. thinhnguyenduy79 Linux - Newbie 1 06-17-2011 05:14 AM
need help - script to display modified date&time of files Pawang Linux - General 3 06-01-2010 03:40 AM
Script to grep log files based on system date cyclegar Programming 2 05-05-2009 07:08 PM
Script for deleting files based on date MaverickApollo Linux - General 3 02-03-2004 08:54 PM


All times are GMT -5. The time now is 08:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration