简体   繁体   中英

extract output from command and pipe it on the command

So basically I have log files in /var/log/xxx.log. These log files are changed daily so I have to use: ls -lt /var/log/ in order to figure out the latest logs. I used:

ls -lt /var/log/ | head -2 | tail -1 | awk '{print $9}'

to extract the name, however how do I combine it with the directory path without "cd"? Like:

ls -lt /var/log/ | head -2 | tail -1 | awk '{print $9}' | tail -100 /var/log/[from pipe]

You can simply replace awk '{print $9}' | tail -100 /var/log/[from pipe] awk '{print $9}' | tail -100 /var/log/[from pipe] by tail -100 /var/log/$(awk '{print $9}')

A better way to find the files inside your directory which have been changed or created less than one day ago is this:

find /var/log/ -type f -maxdepth 1 -mtime -1

Explanation:

-type f     : only look for files
-maxdepth 1 : only look inside this directory
-mtime -1   : only look for the once, being changed at most one day ago

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM