如何删除除N个最新文件以外的所有文件? [英] How to delete all files except the N newest files?

查看:38
本文介绍了如何删除除N个最新文件以外的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命令允许我登录服务器,从我的电脑到特定目录 ssh -t xxx.xxx.xxx.xxx"cd/directory_wanted; bash"

this command allows me to login to a server, to a specific directory from my pc ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted ; bash"

然后如何在该目录中执行此操作.我希望能够基本上删除除N个最新文件以外的所有文件. find ./tmp/-maxdepth 1 -type f -iname * .tgz |排序-n |头-n -10 |xargs rm -f

How can I then do this operation in that directory. I want to be able to basically delete all files except the N most newest. find ./tmp/ -maxdepth 1 -type f -iname *.tgz | sort -n | head -n -10 | xargs rm -f

推荐答案

您可以使用此命令,

ssh -t xxx.xxx.xxx.xxx "cd /directory_wanted; ls -t *.tgz  | tail -n
+11 | xargs rm -f; bash"

在单引号中,我们可以添加要在远程计算机中执行的所有操作.但是每个命令都应以分号(;)终止

In side quotes, we can add what ever the operations to be performed in remote machine. But every command should be terminated with semicolon (;)

注意:包含由silentMonk建议的相同命令.它很简单,正在运行.但是在执行该操作之前,请对其进行一次验证.

Note: Included the same command suggested by silentMonk. It is simple and it is working. But verify it once before performing the operation.

这篇关于如何删除除N个最新文件以外的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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