如何删除文件夹中除最新三个文件之外的所有文件 [英] how to delete all files except the latest three in a folder

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

问题描述

我有一个文件夹,其中包含一些 subversion 修订检出(这些是在运行 capistrano 部署配方时检出的).

I have a folder which contains some subversion revision checkouts (these are checked out when running a capistrano deployment recipe).

我真正想做的是保留 capistrano 脚本检出的最新 3 个修订版并删除其他修订版,因此为此我计划使用 run 命令在终端上运行一些命令,实际上 capistrano 还没有在这里可以做任何事情,但有一个 unix 命令.

what I want to do really is that to keep the latest 3 revisions which the capistrano script checkouts and delete other ones, so for this I am planning to run some command on the terminal using a run command, actually capistrano hasn't got anything to do here, but a unix command.

我试图运行一个命令来获取除最新三个文件之外的文件列表并删除其余文件,我可以使用以下命令获取文件列表.

I was trying to run a command to get a list of files except the lastest three and delete the rest, I could get the list of files using the following command.

(ls -t /var/path/to/folder |head -n 3; ls /var/path/to/folder)|sort|uniq -u|xargs

现在,如果我在此命令的末尾添加一个 rm -Rf,它会返回找不到要删除的文件.这很明显,因为这仅返回文件夹的名称,而不是文件夹的完整路径.

now if I add a rm -Rf to the end of this command it returns me with file not found to delete. so thats obvious because this returns only the name of the folder, not the full path to the folder.

无论如何可以使用一个 unix 命令删除这些文件/文件夹吗?

is there anyway to delete these files / folders using one unix command?

推荐答案

好的,您的脚本有一些问题.

Alright, there are a few things wrong with your script.

首先,也是最有问题的是这一行:

First, and most problematically, is this line:

ls -t /var/path/to/folder |head -n 3;

ls -t 将返回一个按文件最后修改时间顺序排列的文件列表,从最近修改的开始.head -n 3 表示只列出前三行.所以这就是给我一个最近修改的三个文件的列表",我认为这不是你想要的.

ls -t will return a list of files in order of their last modification time, starting with the most recently modified. head -n 3 says to only list the first three lines. So what this is saying is "give me a list of only the three most recently modified files", which I don't think is what you want.

我不太确定你在用第二个 ls 命令做什么,但我很确定这只是将目录中的所有文件连接到你的列表中.这意味着当它被sorted 和uniq ed 时,你将只剩下该目录中所有文件的字母顺序列表.当这被传递给诸如 xargs rm 之类的东西时,您将清除该目录中的所有内容.

I'm not really sure what you're doing with the second ls command, but I'm pretty sure that's just going to concatenate all the files in the directory into your list. That means when it gets sorted and uniq'ed, you'll just be left with an alphabetical list of all the files in that directory. When this gets passed to something like xargs rm, you'll wipe out everything in that directory.

接下来,sort |uniq 不需要 uniq 部分.您可以使用 -u 开关在 sort 上删除重复项.反正你不需要这部分.

Next, sort | uniq doesn't need the uniq part. You can just use the -u switch on sort to get rid of duplicates. You don't need this part anyway.

最后,实际删除目录.在这方面,您的问题是正确的:只需使用 rm -r

Finally, the actual removal of the directory. On that part, you had it right in your question: just use rm -r

这是我能想到的最简单的方法:

Here's the easiest way I can think to do this:

ls -t1 /var/path/to/folder | tail -n +4 | xargs rm -r

这里发生了什么:

  • ls -t1 正在打印 /var/path/to/folder 中所有文件的列表,每行一个文件/目录,按 最近修改日期.
  • tail -n +4 正在打印 ls -t1 输出中的所有行,从第四行开始(即最近修改的三个文件不会列出)
  • xargs rm -r 表示从 tail 中删除任何文件输出.-r 表示递归删除文件,所以如果遇到目录,会删除该目录中的所有内容,然后删除目录本身.
  • ls -t1 is printing a list, one file/directory per line, of all files in /var/path/to/folder, ordering by the most recent modification date.
  • tail -n +4 is printing all lines in the output of ls -t1 starting with the fourth line (i.e. the three most recently modified files won't be listed)
  • xargs rm -r says to delete any file output from the tail. The -r means to recursively delete files, so if it encounters a directory, it will delete everything in that directory, then delete the directory itself.

请注意,我没有对任何内容进行排序或删除任何重复项.那是因为:

Note that I'm not sorting anything or removing any duplicates. That's because:

  • ls 只报告一个文件一次,因此没有要删除的重复项
  • 无论如何,您都会删除传递的每个文件,因此删除它们的顺序无关紧要.
  • ls only reports a file once, so there are no duplicates to remove
  • You're deleting every file passed anyway, so it doesn't matter in what order they're deleted.

所有这些都有意义吗?

由于在传递绝对目录时 ls 指定完整路径我错了,并且由于您可能无法执行 cd,也许您可​​以使用 tail 代替.

Since I was wrong about ls specifying the full path when passed an absolute directory, and since you might not be able to perform a cd, perhaps you could use tail instead.

例如:

 ls -t1 /var/path/to/folder | tail -n +4 | xargs find /var/path/to/folder -name $1 | xargs rm -r

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

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