command line - Writing out only files existing in one directory or other or having different size or time of last modify - Ask Ubuntu


i writing script need compare 2 directories (recursively) , write out files if have different size or time of modify(yy-mm-dd hh:mm) or if file exists in 1 directory.

output in format:

<dir1>:<local-path> <size> <last-modify> <dir2>:<local-path> <size> <last-modify> 

if file exists in 1 directory:

<dir1>:<local-path> <size> <last-modify> 

or

<dir2>:<local-path> <size> <last-modify> 

so far managed data in specified format using:

find dir1 -type f -exec stat -c '%n %s %y' {} \; | sed 's,^[^/]*/,,' | sed 's/\:[^:]*$//' | sort # > dir1.txt find dir2 -type f -exec stat -c '%n %s %y' {} \; | sed 's,^[^/]*/,,' | sed 's/\:[^:]*$//' | sort # > dir2.txt 

which gives me 2 ordered lists of files in given directories , subdirectories , size , last modified timestamp.

now need somehow compare them , them specified format above. tried using diff -y compares line line need same name same name. tried comm dont know how transform output format.

any ideas?

i think i'd try put based around using rsync in dry-run mode (--dry-run or -n).

to illustrate, given:

$ tree -ds adir/ bdir/ adir/ ├── [       4096 nov 19  9:36]  sub1 │   ├── [         35 nov 19  9:35]  common │   └── [         23 nov 19  9:36]  onlya ├── [       4096 nov 19  9:41]  sub2 │   ├── [         35 nov 19  9:35]  common │   ├── [         44 nov 19  9:44]  newera │   ├── [         44 nov 19  9:37]  oldera │   └── [          6 nov 19 10:36]  size └── [       4096 nov 19  9:35]  sub3     └── [         35 nov 19  9:35]  common bdir/ ├── [       4096 nov 19  9:46]  sub1 │   └── [         35 nov 19  9:35]  common ├── [       4096 nov 19 10:36]  sub2 │   ├── [         35 nov 19  9:35]  common │   ├── [         44 nov 19  9:38]  newera │   ├── [         44 nov 19  9:44]  oldera │   └── [         24 nov 19 10:36]  size └── [       4096 nov 19  9:40]  sub3     ├── [         35 nov 19  9:35]  common     └── [         23 nov 19  9:40]  onlyb  6 directories, 14 files 

then can list files have different sizes or modification times follows:

$ rsync -aon --delete --itemize-changes adir/ bdir/ *deleting   sub3/onlyb >f+++++++++ sub1/onlya >f..t...... sub2/newera >f..t...... sub2/oldera >f.s....... sub2/size 

[the change string doesn't matter our purposes, instance *deleting indicates sub3/onlyb not present in source directory; s indicates size difference; t indicates difference in modification time.]

unfortunately doesn't seem possible actual timestamps directly rsync output, can read file list , stat the corresponding files in each directory:

#!/bin/bash  dira="$1" dirb="$2"  rsync -aon --itemize-changes --delete "$dira"/ "$dirb"/ | while read -r c f ;   printf '%s:%s  ' "$dira" "$(cd "$dira" && stat -c '%n %s %y' "$f" 2>/dev/null || printf '(none) - - - -')"   printf '%s:%s\n' "$dirb" "$(cd "$dirb" && stat -c '%n %s %y' "$f" 2>/dev/null || printf '(none) - - - -')" done 

which can use follows

$ ./rstat.sh adir bdir | column -t adir:(none)       -   -           -                   -      bdir:sub3/onlyb   23  2016-11-19  09:40:12.253318393  -0500 adir:sub1/onlya   23  2016-11-19  09:36:52.220421434  -0500  bdir:(none)       -   -           -                   - adir:sub2/newera  44  2016-11-19  09:44:45.953236221  -0500  bdir:sub2/newera  44  2016-11-19  09:38:33.270838033  -0500 adir:sub2/oldera  44  2016-11-19  09:37:41.675642039  -0500  bdir:sub2/oldera  44  2016-11-19  09:44:45.953236221  -0500 adir:sub2/size    6   2016-11-19  10:36:31.460487036  -0500  bdir:sub2/size    24  2016-11-19  10:36:31.460487036  -0500 

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

sql server - "Configuration file does not exist", Event ID 274 - Super User