如何循环浏览多个文件夹和子文件夹并删除文件名,以abc.txt和14天前开始 [英] How to loop through multiple folder and subfolders and remove file name start with abc.txt and 14 days old

查看:96
本文介绍了如何循环浏览多个文件夹和子文件夹并删除文件名,以abc.txt和14天前开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文件夹和子文件夹.我需要遍历每个文件夹和子文件夹,并删除或移动以abc.txt和14天为起始日期的文件名到临时文件夹.我的文件夹树结构是:

I have folder and subfolder. I need to loop through each folder and subfolder and remove or move the file names which start with abc.txt and 14 days old to temporary folder. My folder tree structure is:

该文件可能位于文件夹或子文件夹'abc.txt'

The file may be inside the folder or subfolder 'abc.txt'

我已经使用了以下代码,但无法正常工作.

I have used this below code but not working.

  1. 我使用以下命令将文件夹路径放入list.txt文件中

  1. I took the folder paths into a list.txt file using below command

find $_filepath -type d >> folderpathlist.txt

  • 我将路径列表传递给以下代码,以搜索和删除文件或将文件移动到临时文件夹

  • I pass the path list to below code to search and remove or move files to temporary folder

    find folderpathlist.txt  -name "abc*" -mtime \+14 >>temp/test/
    

  • 如何实现这种情况?

    推荐答案

    您要查找文件:-type f
    以abc.txt开头的文件:-name "abc.txt*"
    已满14天:-mtime +14
    并将它们移动到目录:-exec mv {} /tmp \;
    并查看发生了什么变化:-print

    You want to find files: -type f
    that start with abc.txt: -name "abc.txt*"
    that are 14 days old: -mtime +14
    and move them to a dir.: -exec mv {} /tmp \;
    and to see what moved: -print

    最后一条命令是:

    find . -type f -name "abc.txt*" -mtime +14 -exec mv {} /tmp \; -print

    根据需要调整目录.

    请注意,mtime是修改时间.自上次修改以来已经14天了.

    Note that mtime is the modification time. So it is 14 days old since the last modification was done to it.

    注2:-exec中的{}被找到的每个文件名替换.

    Note 2: the {} in the -exec is replaced by each filename found.

    注3:\;表示-exec

    注释4:无论如何,find都将递归到子目录中.无需列出目录并再次在其上循环.

    Note 4: find will recurse into sub-directories anyway. No need to list the directories and loop on them again.

    这篇关于如何循环浏览多个文件夹和子文件夹并删除文件名,以abc.txt和14天前开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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