LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-25-2014, 03:43 PM   #1
thealmightyos
Member
 
Registered: Mar 2009
Distribution: CentOS 6.5 / 7
Posts: 119

Rep: Reputation: 1
Post BASH scripting: using date to find a filename


Hello all. I thought this would be real easy. Yet here I am over 2 hours later still messing with it. Here is what I got:

I have files with the following naming convention (name_YEAR_MONTH-DAY.notes). I am attempting to find and display all files where the date within the name of the file is under 7 days old. Why don't I use the -mtime option for find? That is because these files were transferred from another source all at the same time and thus according to the filesystem they were created/modified on the same day so -mtime or any of the other time criteria would not help me in this situation.

I thought it would be real easy to just grab the date, format it, then stick it into a find function. To test the idea I created this script:
Code:
name=$(date +"%Y_%m-%d")
find *$name* -exec cat {} \;
It worked perfectly. So now all I got to do is get date to give me dates for the last 7 days. Easy right? Not so much

The command to give you a previous date is date --date='n days ago' with n being the number of days you want to go back. So I wrote this up:

Code:
for day in 1 2 3 4 5 6 7
do
name=$(date +"%Y_%m-%d" --date='$day days ago')
find *$name* -exec cat {} \;
done
Well that didn't work at all. It seems to keep getting caught in the date --date switch. It doesn't like a variable in there. I have tried creating a variable for the whole 'n days ago' string and plugging it in but it won't take that ether. Not quite sure what to do at this point. Without the output from the date function I don't think I can make this script work.

Any ideas?

Last edited by thealmightyos; 03-25-2014 at 03:47 PM.
 
Old 03-25-2014, 04:00 PM   #2
paladin.michael
Member
 
Registered: Jun 2011
Location: Danbury, CT, USA
Distribution: Kubuntu, Slackware, Debian, FreePBX
Posts: 75

Rep: Reputation: 23
Quote:
name=$(date +"%Y_%m-%d" --date='$day days ago')
use double quotes around $day days ago, single quotes prevent the variable from being de-referenced.

EDIT: ugh, my c++ is showing. dereferenced isn't the correct word but I can't remember the right one. hopefully my meaning is clear enough.

Last edited by paladin.michael; 03-25-2014 at 04:03 PM.
 
Old 03-25-2014, 04:35 PM   #3
thealmightyos
Member
 
Registered: Mar 2009
Distribution: CentOS 6.5 / 7
Posts: 119

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by paladin.michael View Post
use double quotes around $day days ago, single quotes prevent the variable from being de-referenced.

EDIT: ugh, my c++ is showing. dereferenced isn't the correct word but I can't remember the right one. hopefully my meaning is clear enough.
YES! Your meaning was understood. I have changed my script thus:
Code:
for day in {0..6}
do
name=$(date --date="$day days ago" +"%Y_%m-%d")
find *$name* 2>/dev/null -exec cat {} \;
done
Now it works great.

I added the 2>/dev/null to find in order to remove errors when we get a date that does not have a corresponding file. I also changed my $day variable to 0-6 as 1-7 would not include the current day.

Thank you for your help. I knew it had to be something minor like that.

Now, to make a script that will move files over 15 days old into folders sorted by month. Shouldn't be too much different.
 
Old 03-25-2014, 04:45 PM   #4
paladin.michael
Member
 
Registered: Jun 2011
Location: Danbury, CT, USA
Distribution: Kubuntu, Slackware, Debian, FreePBX
Posts: 75

Rep: Reputation: 23
Happy to help.
 
  


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
find before and after log files too, with date-time in filename. rudydark Linux - General 2 05-28-2013 08:14 AM
bash returns ambiguous redirect with date in filename rstoutmna Programming 5 09-13-2011 08:37 PM
Bash Scripting with file date comparison coop59 Programming 3 03-31-2011 09:02 AM
Bash scripting using date pueblonative Programming 3 01-16-2007 10:34 AM
using bash in cron to get the date in filename rstoutmna Programming 1 12-29-2004 03:39 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:32 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
Open Source Consulting | Domain Registration