bash 移动 30 分钟前的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37411988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:40:33  来源:igfitidea点击:

Move files that are 30 minutes old

linuxbashshellubuntucsh

提问by saurabh

I work on a server system that does not allow me to store files more than 50 gigabytes. My application takes 20 minutes to generate a file. Is there any way whereby I can move all the files that are more than 30 minutes old from source to destination? I tried rsync:

我在不允许我存储超过 50 GB 的文件的服务器系统上工作。我的应用程序需要 20 分钟来生成一个文件。有什么方法可以将超过 30 分钟的所有文件从源移动到目标吗?我试过rsync

rsync -avP source/folder/ user@destiantionIp:dest/folder

but this does not remove the files from my server and hence the storage limit fails.

但这不会从我的服务器中删除文件,因此存储限制失败。

Secondly, if I use the mvcommand, the files that are still getting generated also move to the destination folder and the program fails.

其次,如果我使用该mv命令,仍在生成的文件也会移动到目标文件夹并且程序失败。

回答by Inian

You can use findalong with -execfor this:-

您可以find-exec此一起使用:-

Replace /sourcedirectoryand /destination/directory/with the source and target paths as you need.

根据需要替换/sourcedirectory/destination/directory/源路径和目标路径。

find /sourcedirectory -maxdepth 1 -mmin -30 -type f -exec mv "{}" /destination/directory/ \;

What basically the command does is, it tries to find files in the current folder -maxdepth 1that were last modified 30 mins ago -mmin -30and move them to the target directory specified. If you want to use the time the file was last accessed use -amin -30.

该命令的基本作用是,它尝试在当前文件夹-maxdepth 1中查找30 分钟前最后修改的文件-mmin -30并将它们移动到指定的目标目录。如果要使用上次访问文件的时间,请使用-amin -30.

Or if you want to find files modified within a range you can use something like -mmin 30 -mmin -35which will get you the files modified more than 30 but less than 35 minutes ago.

或者,如果您想查找在某个范围内修改的文件,您可以使用类似的方法-mmin 30 -mmin -35,这将使您修改超过 30 分钟但不到 35 分钟前的文件。

References from the manpage:-

man页面参考:-

   -amin n
          File was last accessed n minutes ago.

   -atime n
          File was last accessed n*24 hours ago.  When find figures out how many 24-hour periods ago the file was last accessed, any fractional part is ignored, so  to  match  -atime
          +1, a file has to have been accessed at least two days ago.

   -mmin n
          File's data was last modified n minutes ago.

   -mtime n
          File's data was last modified n*24 hours ago.  See the comments for -atime to understand how rounding affects the interpretation of file modification times.