0

I would like to list last 3 modified files in /user directory , and then cat the result for every file , after the cat would like to awk and print 2 column and pass all the out put to csv file .

for i in $(ls); do cat "$i"; done;

Here will list and cat all the files, but i do not know how to cat just last 3 files

Any idea

Desired result : csv-file

file-name , 1st clom , 3 colm

for i in $(ls); do cat "$i"; done;

Here just cat all the files :(

MenZ
  • 1
  • 3
    [Why you shouldn't parse the output of `ls`](https://mywiki.wooledge.org/ParsingLs) – Charles Duffy Nov 07 '22 at 21:03
  • 2
    [BashFAQ #3](https://mywiki.wooledge.org/BashFAQ/003): *How can I sort or compare files based on some metadata attribute (newest / oldest modification time, size, etc)?* – Charles Duffy Nov 07 '22 at 21:04
  • please update the question with 3 sample input files, the `awk` code you've attempted, the (wrong) output generated by your code and the (correct) expected contents of the csv file – markp-fuso Nov 07 '22 at 21:10
  • 1
    This might help: [How to recursively find the latest modified file in a directory?](https://stackoverflow.com/q/4561895/3776858) – Cyrus Nov 07 '22 at 21:23

1 Answers1

0

3 most recent files:

stat -c '%Y:%F:%n' * | grep ':regular file:' | sort -t: -n | cut -d: -f3- | tail -3

And then cat them:

... | xargs cat
glenn jackman
  • 238,783
  • 38
  • 220
  • 352