Find the answer to your Linux question:
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.
  1. #1
    Just 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

    Code:
    cat /path/to/files/*$myDate | blah blah
    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.

    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

  2. #2
    Just Joined!
    Join Date
    May 2011
    Location
    Central FL
    Posts
    91
    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:
    Code:
    cat thisfile | grep foo
    good code:
    Code:
    grep foo thisfile
    bad code:
    Code:
    for line in `cat thisfile`; do echo $line; done
    good code:
    Code:
    while read line; do echo $line; done < thisfile
    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.

    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 {} \;

  3. #3
    Just Joined!
    Join Date
    Jun 2012
    Posts
    11
    it was

    Code:
    cat /path/to/file/*$myDate | grep contained | awk '{sum+=8} END {print sum}'
    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.

    I changes the line to read

    Code:
    grep string /path/to/file/*$myDate | awk '{sum+=8} END {print sum}'
    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.

    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 to
    Code:
    cd /path/to/*$myDir
    we need the * because the system generates a random string in front of the date on the logs files.



    edit:

    By the way, I used your find command example.
    Code:
     find /path/to/file -name "*$myDate"
    I still get the same errors. It does not like the *$myDate together. I still get the error.
    line 3: 20120627: command not found
    Last edited by jedlickaj; 06-28-2012 at 07:59 PM.

  4. #4
    Just Joined!
    Join Date
    May 2011
    Location
    Central FL
    Posts
    91
    Quote Originally Posted by jedlickaj View Post
    By the way, I used your find command example.
    Code:
     find /path/to/file -name "*$myDate"
    I still get the same errors. It does not like the *$myDate together. I still get the error.
    You'll notice that's not how my example is - my example is:
    Code:
    find /path/to/files -type f -name "*${myDate}" -exec grep foobar {} \;
    The ${myDate} vs $myDate is a huge difference. You should try again what I originally suggested.

  5. #5
    Just 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 had
    Code:
    myDate= `date --date="1 day ago" +%Y%m%d`
    the 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.

    All three ways work now, including your way (cat, grep, find).

    thanks for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •