LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2014, 06:17 AM   #1
cosmbrth
LQ Newbie
 
Registered: Aug 2013
Posts: 5

Rep: Reputation: Disabled
Find cannot interpret date from bash variable


Hi everybody,

I'm writing a bash script to find files of a directory that their ctime is newer than a ctime of files of an other directory.

That's being, here is my script:
Code:
****************************************************
#!/bin/bash

#defining my ctime
ctimesource=`stat /mydeirectory/ | grep Change: | awk '{print $2}'`
hctimesource=`stat /mydeirectory/ | grep Change: | awk '{print $3}' | cut -d"." -f1`

#defining the parameter to pass to find

myctime="'"$ctimesource" "$hctimesource"'"
echo $myctime

find /Bdirectory/ -newerct $myctime -type f
****************************************************

The thing is, if I run the command normally ( terminal): "find /Bdirectory/ -newerct $myctime -type f" after replacing the variable, it works like a charm.



Code:
find /Bdirectory/ -newerct '2014-01-30 16:12:20' -type f
It works



But when the command is used in a bash script, it doesn't work.

Here is the output of the script:

Code:
***************************************
'2014-01-30 16:12:20'
find: I cannot figure out how to interpret `\'2014-01-30 16:12:20\'' as a date or time
***************************************

I've tried single quotes, double quotes, replace the variable by `echo $myctime`, but still "find" couldn't correctly interpret the variable.

Do you have any idea? Appearantly the variable is not well formatted after being passed to the "find" command. Can you suggest something? Do you understand, why exactly it didn't work?

Thank you in advance,

PS:
Code:
bash -version
GNU bash, version 4.1.2(1)-release
 
Old 01-31-2014, 06:56 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
myctime="$ctimesource $hctimesource"
printf 'debug: myctime="%s"\n' "$myctime"

find /Bdirectory/ -newerct "$myctime" -type f
 
1 members found this post helpful.
Old 01-31-2014, 08:23 AM   #3
cosmbrth
LQ Newbie
 
Registered: Aug 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Smile

Hi NevemTeve,

Manys thanks for the solution, it works
 
  


Reply



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
Bash scripting - find command with variable substitution and quoting bgeddy Programming 4 07-29-2022 08:28 AM
BASH to find pics and vids with no date taken exif latitudehopper Linux - Newbie 3 08-08-2011 03:35 AM
How do I get bash not to interpret the output of a backquote command? curieux Linux - General 1 10-13-2010 08:23 AM
emacs shell-mode doesn't interpret prompt variable CoonerTheRed Linux - Software 1 05-09-2005 04:23 AM
Find variable in template file replace w/date+ and save as jmanjohn61 Linux - General 14 12-13-2004 06:49 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:08 AM.

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