简体   繁体   English

用于删除特定文件的 Shell 脚本

[英]Shell script to delete particular files

I have a multiple layers of folder example a(b(c(d(u,v))))我有多层文件夹示例 a(b(c(d(u,v))))

Here in every level there is a folder sync , for example in every directory sync folder is present ie a/sync , a/b/sync and so on.这里在每个级别都有一个文件夹sync ,例如在每个目录中都存在sync文件夹,即a/synca/b/sync等等。

I am looking to write a shell or tcl script which will go in every folder and delete the particular file(sync).我正在寻找编写一个 shell 或 tcl 脚本,它将进入每个文件夹并删除特定的文件(同步)。

Can anyone guide me?谁能指导我?
Thanks谢谢
Good day再会

You can use rmdir ./**/.sync .您可以使用rmdir ./**/.sync
This will search recursive in the current directory for every directory called sync and delete it.这将在当前目录中递归搜索每个名为sync的目录并将其删除。

For use of the double asterisk also see this answer对于双星号的使用,另请参阅此答案

Note that this will only delete directories(folders) and if it's not empty.请注意,这只会删除目录(文件夹)并且如果它不为空。

To remove the directories with all files in it, use rm -r ./**/.sync要删除包含所有文件的目录,请使用rm -r ./**/.sync

Find and remove...查找并删除...

find . -type d -name sync -exec rm -rf {} \;

Explanations:说明:

  • . : search for current directory : 搜索当前目录
  • -type d : search for directories only -type d : 只搜索目录
  • -name sync : search for file/directory named " sync " -name sync : 搜索名为“ sync ”的文件/目录
  • -exec ... : execute this command on each file/directory found -exec ... :对找到的每个文件/目录执行此命令
    • rm -rf : remove file and directories named ... rm -rf : 删​​除文件和目录名为 ...
    • {} : is replaced by each file/directory found by find command {} :由find命令找到的每个文件/目录替换
    • \; : and of -exec option : 和-exec选项

Since all the answers so far have been shell approaches, here's a tcl one:由于到目前为止所有的答案都是 shell 方法,这里有一个 tcl 方法:

#!/usr/bin/env tclsh

proc delete_syncs {dir} {
    foreach subdir [glob -directory $dir -types d -nocomplain *] {
        delete_syncs $subdir
        set sync [file join $subdir .sync] ;# Or sync or .SYNC or whatever it's actually named
        if {[file exists $sync]} {
            file delete -force -- $sync
        }
    }
}
foreach basedir $argv {
    delete_syncs $basedir
}

Takes the base directory (Or directories) to scan as command line arguments.将基本目录(或目录)作为命令行参数进行扫描。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM