How to open a latest file in a sub directory in Linux?

87 views Asked by At

I have a logs/ directory which contains many log files. I would like to open the latest log file. My env is Ubuntu 16.

I know the name of the latest file. But the file is in logs/ dir. In such a case, how do I tell the relative pass via xargs?

$ ls -t logs/ | head -1
20180615-184233.log

$ ls -t logs/ | head -1 | xargs less
failed 
1

There are 1 answers

0
jef On

I found it by myself. I need * for printing the whole relative path.

$ ls -t logs/ | head -1
20180615-184233.log

$ ls -t logs/ | head -1 | xargs less
failed 

$ ls -t logs/* | head -1
logs/20180615-184233.log

$ ls -t logs/* | head -1 | xargs less
success