Kundan Kumar
Kundan Kumar

Reputation: 2002

Finding the latest file from directory with creation date as suffix

I have files in a directory having the name as /dir/file.$creation date in "yyyymmddhhmmss". Now I want to pick up the file that was created latest in that directory based on the creation date using the "yyyyddmmhhmmss" part.

please suggest how this can be acheived using shell script.

I am using K Shell.

Upvotes: 0

Views: 101

Answers (2)

Zsolt Botykai
Zsolt Botykai

Reputation: 51613

ls -t1 /IN/UR/DIR | head -1 

Might work for you if it was created last...

Upvotes: 1

dogbane
dogbane

Reputation: 274612

Use ls and sort as follows:

ls dir/file.* | sort -n -t . -k 2 | tail -1

If you have too many files, the above command will fail will a "too many arguments" error, so use find instead:

find dir -name "file.*" -maxdepth 1 | sort -n -t. -k2 | tail -1

Upvotes: 1

Related Questions