5

Im trying to find the right command to tail a bunch of log files whilst excluding the zipped files in a set directory. The log files are being zipped as they become oversized.

At the moment Im using:

tail -f /var/logs/myLog*

Which works fine, but it will also tail the .gz files which are a garbled mess. I need to tail only files without this extension.

  • 1
    Do file names before the gzip have anything else in common - file ending, number of periods in the name, or something else? – Jenny D Oct 20 '14 at 10:37
  • Nothing that they dont share with the zipped versions. The logs are simply nameOfLog with no extension and the zipped files are nameOfLog.gz.... unfortunately – JackalopeZero Oct 20 '14 at 14:20
3

If the filenames have anything else in common - e.g. length of name, number of periods in name, name ending... you can simply adjust your glob.

If not, there are some other ways:

tail -f `ls -l /var/logs/myLog* |grep -v .gz$`

or, using xargs:

ls /var/logs/myLog* | grep -v .gz$ | xargs tail -f
  • tail -f ls -l /var/logs/myLog* |grep -v .gz$ works straight away! Awesome. Could you expand on the $ at the end of the grep? I understand -v to invert the selection but why the $? – JackalopeZero Oct 20 '14 at 14:18
  • 3
    A dollar sign anchors a search to the end of a string. In other words, pass through files with gz anywhere in the name – Keith Wolters Oct 20 '14 at 14:47
6

Usually tail -f /var/logs/myLog*log will work. However, if the end of the filenames is unpredictable, and really the only way is to exclude files ending in .gz, it becomes more complicated. One possibility is this:

ls /var/logs/myLog* | grep -v .gz$ | xargs tail -f
  • This is a great answer. Although when I run it I get a list of the correct files (so it is finding them properly!) but with the errors "Cannot open [file] for reading: No such file or directory". Obviously the file exists, its finding it itself! I must be doing something wrong. – JackalopeZero Oct 20 '14 at 14:15
  • @JackalopeZero - If the log files don't end in the letter z, in bash you can ignore files ending in z (like .gz) with: tail *[^z] – Johnny Oct 20 '14 at 15:00
  • @JackalopeZero - I can't tell. Do the filenames have spaces or any other strange characters? Is it possible that ls and/or grep insert any color codes? (You can try to add --color=never both to ls and to grep.) These are wild guesses though - I can't tell you if I don't see it. Maybe if you copy and paste the exact messages we could get a clue. – Antonis Christofides Oct 20 '14 at 20:57
2

In bash, if the extendedglob option is set (it is by default), you can negate a glob pattern by wrapping it in parentheses and prepending a bang (!). For example, !(*.gz) matches all items whose names don't end with .gz. See the Pathname Expansion subsection in the EXPANSION section in the bash manual page for more information.

In zsh, if the extglob option is set (it is not, by default), you can negate a glob pattern by prepending a caret (^). For example, ^*.gz matches all items whose names don't end with .gz. See the FILENAME GENERATION section in the zshexpn manual page for more information.

Note that in general, if you want to use ls with a glob pattern, you should specify -d. This is because the shell expands the glob pattern into a list of matching names, passing each one to ls as a separate argument. If you don't use -d, ls will list the contents of any directories whose names it's given.

1

You can use the following line:

file /var/log/* | grep "ASCII text" | cut -d ":" -f 1 | xargs tail -f
0

You can also use -n option to specify that you don't want the "old" ones:

tail -f -n 0 /var/log/*

or

tail -fn0 /var/log/*

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.