0

I faced some issue while i list all the files in my directory , i would like to print or fetch just the file which is before the last file was modified

server:cd user/local
server/user/local/: ls -ltrh 

-rw-r--r--  1 Pepe  staff     0B Nov  7 13:40 1.txt
-rw-r--r--  1 Pepe  staff     0B Nov  7 13:40 2.txt
-rw-r--r--  1 Pepe  staff     0B Nov  7 13:40 3.txt
-rw-r--r--  1 Pepe  staff     0B Nov  7 13:58 5.txt
-rw-r--r--  1 Pepe  staff     9B Nov  7 14:25 6.txt

I hope i can have the result 5.txt

Any idea ?

Python P
  • 25
  • 6

1 Answers1

0

This is a fairly complex-looking pipeline, but each piece is pretty straightforward:

stat -c '%Y:%n' * | sort -t: -n | cut -d: -f2- | tail -2 | head -1

check your stat man page to see if your version has the -c option.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thats Great , actually it is also working for me like : `ls -ltrh | tail -2 | head -1` – Python P Nov 07 '22 at 13:57
  • `ls` is intended for humans to read. [why you should not use it for parsing data](https://mywiki.wooledge.org/ParsingLs) -- in fact my answer is still broken for filenames containing newlines. – glenn jackman Nov 07 '22 at 14:05
  • Your script does not check for files only (it will list the second latest modified entry in current directory). – Bruno Nov 07 '22 at 17:13