tail all log files in a directory | exclude zipped files
Solution 1
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
Solution 2
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
Solution 3
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.
Solution 4
You can use the following line:
file /var/log/* | grep "ASCII text" | cut -d ":" -f 1 | xargs tail -f
Solution 5
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/*
Related videos on Youtube
JackalopeZero
Updated on September 18, 2022Comments
-
JackalopeZero almost 2 years
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.
-
Jenny D over 9 yearsDo file names before the gzip have anything else in common - file ending, number of periods in the name, or something else?
-
JackalopeZero over 9 yearsNothing 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 over 9 yearsThis 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 over 9 yearstail -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 $? -
Keith Wolters over 9 yearsA dollar sign anchors a search to the end of a string. In other words, pass through files with gz anywhere in the name
-
Johnny over 9 years@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]
-
Antonis Christofides over 9 years@JackalopeZero - I can't tell. Do the filenames have spaces or any other strange characters? Is it possible that
ls
and/orgrep
insert any color codes? (You can try to add--color=never
both tols
and togrep
.) 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. -
ZedTuX over 2 yearsThat's a clever one actually ! Thank you.