0
find . -depth -exec stat --format '%n %U %G' {} + | sort -d > acl_file_old
#some days later,do it again,and then compare
find . -depth -exec stat --format '%n %U %G' {} + | sort -d > acl_file_new
while read FILE OWNER GROUP; do
    while read FILE_NEW OWNER_NEW GROUP_NEW; do
    if [[ $FILE == $FILE_NEW ]]; then
    echo "$FILE"
    break
    done < $result_dir/acl_file_new
done < $result_dir/acl_file_old

Run above bash script,When file name has white space,such as my file here,while read FILE works not well.

I already read similar post1,post2,I know looping over find's output is bad practice.The problem is I need save find -exec result acl_file_old first.

How to save find -exec result and used to compare FILE OWNER,regardless white space file name?

kittygirl
  • 2,255
  • 5
  • 24
  • 52

2 Answers2

1

The read command assigns the words found on the line to the variables.

Note that the last variable is assigned with all the chars remaining up to the end of the line.

The first quick win would be to change the order and make sure that the filename is located at the end with ... -exec stat --format '%U %G %n' {} + ...

Then change the order of the variables to while read -r OWNER GROUP FILE ; do ...

It there are more fancy chars this would be broken anyway. Maybe for (space) it would do the trick ?

Jay jargot
  • 2,745
  • 1
  • 11
  • 14
1

You can change the separator when you write to the file:

find . -depth -exec stat --format '%n;%U;%G' {} + | sort -d > file

And read from it using IFS=";":

while IFS=";"; read FILE OWNER GROUP; do
echo $FILE
done < file

(you can use characters other than ; too)

spitfire
  • 68
  • 7