tail all log files in a directory | exclude zipped files
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.
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
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
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.
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/*
You can use the following line:
file /var/log/* | grep "ASCII text" | cut -d ":" -f 1 | xargs tail -f