diff to output only the file names

前端 未结 6 559
我在风中等你
我在风中等你 2021-01-29 18:28

I\'m looking to run a Linux command that will recursively compare two directories and output only the file names of what is different. This includes anything th

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-29 19:05

    If you want to get a list of files that are only in one directory and not their sub directories and only their file names:

    diff -q /dir1 /dir2 | grep /dir1 | grep -E "^Only in*" | sed -n 's/[^:]*: //p'
    

    If you want to recursively list all the files and directories that are different with their full paths:

    diff -rq /dir1 /dir2 | grep -E "^Only in /dir1*" | sed -n 's/://p' | awk '{print $3"/"$4}'
    

    This way you can apply different commands to all the files.

    For example I could remove all the files and directories that are in dir1 but not dir2:

    diff -rq /dir1 /dir2 | grep -E "^Only in /dir1*" | sed -n 's/://p' | awk '{print $3"/"$4}' xargs -I {} rm -r {}
    

提交回复
热议问题