一旦目录中有8个文件,我试图将其删除到目录中最旧的文件 [英] I am trying to delete to the oldest files in a directory once there are 8 files in the directory

查看:49
本文介绍了一旦目录中有8个文件,我试图将其删除到目录中最旧的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦目录中有8个文件,我试图将其删除到目录中最旧的文件.

I am trying to delete to the oldest files in a directory once there are 8 files in the directory.

ls -Ct /tmp/test/ | awk '{$1=$2=$3=$4=""; print $0}' | xargs rm

我希望它删除以下内容的输出:

I would like it delete the output of:

ls -Ct /tmp/test/ | awk '{$1=$2=$3=$4=""; print $0}' 

但是我不断收到错误消息,显示那些文件不存在.我知道这是因为xargs在我当前所在的目录中查找,但是我需要它来查找/tmp/test/.有什么办法可以做到吗?

but I keep getting an error showing those files don't exist. I know it is because xargs is looking in the directory I'm currently in, but i need it to look off /tmp/test/ instead. Is there any way this can be done?

推荐答案

我将假设您保留8个最新文件,并删除其余文件.

I'm going to assume that you leave 8 newest files and delete the rest.

$ mkdir test
$ cd test
$ touch "0 0"
$ for i in $(seq 1 10) ; do touch $i ;sleep 1 ; done
$ touch "a a"
$ ls -t

ls -t1 | sed -n '9,$p' | while read f; do rm "$f" ; done

$ ls -t1
a a
10
9
8
7
6
5
4

这篇关于一旦目录中有8个文件,我试图将其删除到目录中最旧的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆