Zammtronic
Zammtronic

Reputation: 13

How to read output from bzcat instead of specifying a filename

I need to use 'last' to search through a list of users who logged into a system, i.e.

last -f /var/log/wtmp <username>

Considering the number of bzipped archive files in that directory, and considering I am on a shared system, I am trying to include an inline bzcat, but nothing seems to work. I have tried the following combinations with no success:

last -f <"$(bzcat /var/log/wtmp-*)"
last -f <$(bzcat /var/log/wtmp-*)
bzcat /var/log/wtmp-* | last -f -

Driving me bonkers. Any input would be great!

Upvotes: 1

Views: 2344

Answers (2)

Jonathan Leffler
Jonathan Leffler

Reputation: 753615

You can only use < I/O redirection on one file at a time.

If anything is going to work, then the last line of your examples is it, but does last recognize - as meaning standard input? (Comments in another answer indicate "No, last does not recognize -". Now you see why it is important to follow all the conventions - it makes life difficult when you don't.) Failing that, you'll have to do it the classic way with a shell loop.

for file in /var/log/wtmp-*
do
    last -f <(bzcat "$file")
done

Well, using process substitution like that is pure Bash...the classic way would be more like:

tmp=/tmp/xx.$$   # Or use mktemp
trap "rm -f $tmp; exit 1" 0 1 2 3 13 15

for file in /var/log/wtmp-*
do
    bzcat $file > $tmp
    last -f $tmp
done

rm -f $tmp
trap 0

Upvotes: 1

Fred Foo
Fred Foo

Reputation: 363547

last (assuming the Linux version) can't read from a pipe. You'll need to temporarily bunzip2 the files to read them.

tempfile=`mktemp` || exit 1

for wtmp in /var/log/wtmp-*; do
    bzcat "$wtmp" > "$tempfile"
    last -f "$tempfile"
done

rm -f "$tempfile"

Upvotes: 1

Related Questions