command line - How to delete top directory that contain all files inside modified at least n days before today? - Ask Ubuntu


consider following folder structure

dir     sandbox1         mywebsite file          ...     sandbox2         mywebsite file         ... 

i have thousands of these sandbox directory created peers.

since running out of inode, decide delete sandbox directory has content not modified 20 days.

eg.

dir     sandbox1 (modified 23 days ago)         mywebsite file (modified 22 days ago)         ... (modified 24 days ago)     sandbox2 (modified 23 days ago)         mywebsite file (modified 19 days ago)         ... 

in case sandbox1 deleted since has not been modified 20 days, , content has not been modified 20 days

sandbox2 not deleted, since has content modified 19 days ago

i know

find /dir/ -maxdepth 1 -mtime +n  

finds directory modified @ least n days, content inside each directory not reflected.

is there way find directory such directory , content have not been modified n days?

any appreciated.

if file modification times critical need @ file modification times , not modification time of parent directories. latter change when structure of directory changes (i. e. file created, moved/renamed or unlinked). changes file content not reflected in time stamp of parent directory.

therefore can find modified (less 20×24 h ago) files in directory tree with:

find /some/path -type f -mtime -20 

we can restrict output show unique directory names:

find /some/path -type f -mtime -20 -printf '%h\n' | uniq 

find directories without modified files

if need find directories without modified deep entries gets trickier since need compute inverted set, set difference of set of directories inside tree , computed set. can use -printf action partition output of find @ least list data need:

find /some/path -mindepth 1 \( -type d -printf '+%p\n' \) -o \( -type f -mtime -20 -printf '-%h\n' \) | uniq 

unfortunately, set operations aren't can done in shell script, wrote python program operates on output of previous find command:

#!/usr/bin/env python3 import sys, os.path itertools import filterfalse     def parent_dir_generator( path ):     while path:         yield path         path = os.path.dirname(path)  all_dirs = list() keep_dirs = set() keep_dir_parents = set()  line in filter(bool, map(lambda s: s.rstrip('\n'), sys.stdin)):     path = line[1:]     if path.startswith('./'):         path = path[2:]      if line.startswith('+'):         all_dirs.append(path)     elif line.startswith('-'):         keep_dirs.add(path)         keep_dir_parents.update(parent_dir_generator(path))  diff_dirs = filterfalse(     lambda path: any(map(keep_dirs.__contains__, parent_dir_generator(path))),     filterfalse(keep_dir_parents.__contains__, all_dirs))  print(*diff_dirs, sep='\n') 

assuming previous program @ ~/tree-difference.py can use this:

find /some/path -mindepth 1 -depth \( -type d -printf '+%p\n' \) -o \( -type f -mtime -20 -printf '-%h\n' \) | python3 ~/tree-difference.py 

verify result

you want verify (or i) didn't make mistake accidentally deletes modified files. fortunately can use variation of original find command inspect directories returned tree-difference.py. lists modified files in them, empty output means went according plan. can take quite while if have many files.

the following command takes input output of tree-difference.py (with pipe or intermediate file):

xargs -rd '\n' -i{} -- find {} -mindepth 1 -type f -mtime -20 

delete found directories

this 1 simple. input output of tree-difference.py.

xargs -rd '\n' -- rm -rf -- 

if rm complains non-existing directories that's because forgot -depth option in find command serving input of tree-difference.py.


Comments

Popular posts from this blog

download - Firefox cannot save files (most of the time), how to solve? - Super User

windows - "-2146893807 NTE_NOT_FOUND" when repair certificate store - Super User