0

I have a multiple layers of folder example a(b(c(d(u,v))))

Here in every level there is a folder sync, for example in every directory sync folder is present i.e a/sync, a/b/sync and so on.

I am looking to write a shell or tcl script which will go in every folder and delete the particular file(sync).

Can anyone guide me?
Thanks
Good day

Vishal Vasani
  • 647
  • 8
  • 16
  • 1
    Is `sync` a regular file or a directory? If the former, you probably want one of:`find a -name sync -delete` or `find a -name sync -exec rm {} \;` or `find a -name sync -exec rm -rf {} +` The last option (`rm -rf`) can also be used if `sync` is a directory. – William Pursell Jul 11 '22 at 12:28
  • its a .SYNC its hidden – Yash Motagi Jul 11 '22 at 12:48
  • 1
    So use `find a -name .sync ...`. It is only "hidden" in the sense that some tools by default do not display names that begin with `.`. That doesn't really change anything significant. – William Pursell Jul 11 '22 at 14:08

3 Answers3

0

You can use rmdir ./**/.sync.
This will search recursive in the current directory for every directory called sync and delete it.

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

Mime
  • 1,142
  • 1
  • 9
  • 20
  • its a .SYNC so hiddne , but it has one txt file , so i guess it woww be also removed. – Yash Motagi Jul 11 '22 at 12:49
  • Updated the answer for usage with `.sync`-directories, but no, `rmdir` will not delete the directory if there are files inside – Mime Jul 11 '22 at 12:53
0

Find and remove...

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

Explanations:

  • .: search for current directory
  • -type d: search for directories only
  • -name sync: search for file/directory named "sync"
  • -exec ...: execute this command on each file/directory found
    • rm -rf: remove file and directories named ...
    • {}: is replaced by each file/directory found by find command
    • \;: and of -exec option
Arnaud Valmary
  • 2,039
  • 9
  • 14
  • If you're calling this from cron, make sure you `cd` to where you need to go; or replace `.` in the find command with an absolute path. I once made this kind of mistake on the job and it took 2 days to restore the machine from backup. – glenn jackman Jul 11 '22 at 13:40
  • yes. in cron table replace `.` path by an absolute path - (not a `cd` command) – Arnaud Valmary Jul 12 '22 at 08:55
0

Since all the answers so far have been shell approaches, here's a tcl one:

#!/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.

Shawn
  • 47,241
  • 3
  • 26
  • 60