Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Deleting old files from a dir

$
0
0
Hi,

I have a folder/dir which have file names as below.

file1.txt
file2.txt
file3.txt
.
.


Such files are added to the folder daily. I have to keep only latest 10 files in the folder and delete the old files.

I have tried several ways. But couldn't find a better logic.


Code:


ls -t|tail -n 10;ls|sort|uniq -u|xargs rm

or

rm `ls -t | awk 'NR>10'`


Viewing all articles
Browse latest Browse all 16232

Trending Articles