LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   command find, wildcard and bash script howto (https://www.linuxquestions.org/questions/linux-newbie-8/command-find-wildcard-and-bash-script-howto-942796/)

ratotopi 05-01-2012 04:53 PM

command find, wildcard and bash script howto
 
I am trying to run the command
find /var/log/httpd -iname access_log-201204* > /tmp/accesslist
from the bashscript but I cannot get it to work. How do I provide the * wildcard in bash script for the following script. I am trying to make a list of all the previous months access_log files

#!/bin/bash
YESTERDAY=`date -d " -1 day" +%Y%m%`
NOW=`date +%d`

if [ $NOW== "1" ]; then
find /var/log/httpd -iname access_log-$YESTERDAY > /tmp/filename
else
exit 0
fi


thank you for your help.

suicidaleggroll 05-01-2012 05:15 PM

Put it in quotes

Code:

find /var/log/httpd -iname "access_log-201204*" > /tmp/accesslist
Without quotes your shell is expanding the access_log-201204* to all matches and then passing them into find

ratotopi 05-01-2012 05:58 PM

@suicidaleggroll
see my script I cannot execute the following command with * at the end of $YESTERDAY can I ??

find /var/log/httpd -iname access_log-$YESTERDAY* > /tmp/filename

suicidaleggroll 05-01-2012 06:03 PM

Sure, but you need to put it in quotes

Code:

$ ls
access_log-20120405  access_log-20120407  access_log-20120506
access_log-20120406  access_log-20120505  access_log-20120507
$ YESTERDAY=`date -d "-1 day" +%Y%m`
$ echo $YESTERDAY
201204
$ find . -iname "access_log-$YESTERDAY*"
./access_log-20120407
./access_log-20120406
./access_log-20120405


ratotopi 05-01-2012 06:46 PM

@suicidaleggroll
thanks!! I was amazed you could run that script and I couldn't, so I check the script again and found % at the end of YESTERDAY=`date -d " -1 day" +%Y%m%` that was creating the problem. Removed the % and it worked , my silly mistake.


All times are GMT -5. The time now is 10:39 PM.