Results 1 to 5 of 5
I have to browse through a directory and count the files in there with a specific date. I have to use various dates so for now I have the date ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 06-28-2012 #1Just Joined!
- Join Date
- Jun 2012
- Posts
- 11
cat doesn't like * followed by a variable in a path name
I have to browse through a directory and count the files in there with a specific date. I have to use various dates so for now I have the date in a variable saved as $myDate
I then need to run the following
the problem is, linux does not like the * and the variable together like that in the path. I tried different applying various quotes to the path to get it to recognize it all as one path, but I cannot get it to work as expected.Code:cat /path/to/files/*$myDate | blah blah
Any suggestions? I am pretty sure it works if I save the entire path as a variable, but I would rather not have to do that. I can also get it to work by taking out the variable and just adding my date command, but I don't want to do that either.
any help is appreciated. All attempts to google it has been fruitless.
thanks in advance,
Jeremy
- 06-28-2012 #2Just Joined!
- Join Date
- May 2011
- Location
- Central FL
- Posts
- 80
What is after the pipe? As a general "good rule" you should never cat into a pipe - it's a notorious bad habit that new people enter into.
bad code:
good code:Code:cat thisfile | grep foo
bad code:Code:grep foo thisfile
good code:Code:for line in `cat thisfile`; do echo $line; done
So, most likely the problem is how you are attempting to complete what you are doing - thus isn't not going to do much good unless we know in full what you are attempting to do.Code:while read line; do echo $line; done < thisfile
Not knowing at all what you're trying to accomplish - but something like the following would be more appropriate than what you currently have.
Code:find /path/to/files -type f -name "*${myDate}" -exec grep foobar {} \;
- 06-28-2012 #3Just Joined!
- Join Date
- Jun 2012
- Posts
- 11
it was
what came after the | doesn't really matter, it has to do with bash or something in the shell not liking the path with an * before the variable.Code:cat /path/to/file/*$myDate | grep contained | awk '{sum+=8} END {print sum}'
I changes the line to read
but forget the pipe, the errors I get are from the command with the path. As is, it tries to grep the string from /path/to/file/* and then tries to run the command 20120627 ($myDate) afterwards. It doesn't keep the entire path as one string.Code:grep string /path/to/file/*$myDate | awk '{sum+=8} END {print sum}'
the problem lies with something in linux not recognizing the full path when using an * and variable together within the path.
I could be mistaken, but I think I have seen the same thing happen with the cd command if I try towe need the * because the system generates a random string in front of the date on the logs files.Code:cd /path/to/*$myDir
edit:
By the way, I used your find command example.I still get the same errors. It does not like the *$myDate together. I still get the error.Code:find /path/to/file -name "*$myDate"
line 3: 20120627: command not foundLast edited by jedlickaj; 06-28-2012 at 07:59 PM.
- 06-29-2012 #4Just Joined!
- Join Date
- May 2011
- Location
- Central FL
- Posts
- 80
- 06-29-2012 #5Just Joined!
- Join Date
- Jun 2012
- Posts
- 11
I found my problem. I am such a newb. My variable to store the date was wrong.
I hadthe error I was getting was the fact I had a space between myDate= and the command. The reason why I thought it was not recognizing the full path was because the variable was storing a blank value and then running the command. I thought the error I was receiving was during the cat/grep/find command. I should have paid more attention to the line number I was receiving the error on and I could have avoided having to post this thread.Code:myDate= `date --date="1 day ago" +%Y%m%d`

All three ways work now, including your way (cat, grep, find).
thanks for the help!


Reply With Quote

