sammy
sammy

Reputation: 437

how to find the last modified file and then extract it

Say I have 3 archrive file:

a.7z
b.7z
c.7z

What I want is to find the last modified archrive file and then extract it

1st: find the last modified

2nd: extract it

1st: ls -t | head -1

My question is how to approach 2nd by using "|" at the end of 1st command

Upvotes: 2

Views: 73

Answers (3)

anubhava
anubhava

Reputation: 785721

Here is a safer method of extracting last modified file in a directory:

find . -maxdepth 1 -type f -printf "%T@\0%p\0\0" |
    awk -F '\0' -v RS='\0\0' '$1 > maxt{maxt=$1; maxf=$2} END{printf "%s%s", maxf, FS}' |
    xargs -0 7z e

This required gnu find and gnu awk.

-printf option is using single NUL character or \0' as field separator and 2 NUL characters \0\0 as record separator for awk.

Upvotes: 0

Parth Pancholi
Parth Pancholi

Reputation: 85

You can use the below code for writing more than 1 command together in a single line.

ls -t | head -1 && 7z e <file_name>.tar.7z command for the extracting .7z file

Upvotes: 2

Assem
Assem

Reputation: 12107

You can do it like that:

7z e `ls -t | head -1`

Use `` to embed the first command.

Upvotes: 2

Related Questions