diff + awk를 사용하여 폴더 동기화
실제 사용:
~/tmp/wallpapers에서 누락된 'jpg' 파일만 복사하고 싶습니다.
diff -u ~/img/backgrounds ~/tmp/wallpapers
Only in /home/sergio/tmp/wallpapers: .git
Only in /home/sergio/tmp/wallpapers: .gitignore
Only in /home/sergio/tmp/wallpapers: README.md
Only in /home/sergio/tmp/wallpapers: thumbs
Only in /home/sergio/img/backgrounds: wallpaper-1791868.jpg
Only in /home/sergio/img/backgrounds: wallpaper-2099153.jpg
Only in em /home/sergio/img/backgrounds: wallpaper-2226037.jpg
awk를 두 개의 필드 구분 기호와 함께 사용
[: ]
복사 명령을 작성하려면 세 번째 및 다섯 번째 필드만 필요합니다.diff -u ~/img/backgrounds ~/tmp/wallpapers | awk -F'[: ]' '/backgrounds/ {print $3"/"$5}'
/home/sergio/img/backgrounds/wallpaper-1791868.jpg
/home/sergio/img/backgrounds/wallpaper-2099153.jpg
/home/sergio/img/backgrounds/wallpaper-2226037.jpg
이제 awk 명령에 일부 문자열을 추가하고 그 결과를 셸에 연결하기만 하면 됩니다.
"cp " ............... copy comand with space
$3 ................ the third fild - the basename
"/" ................ add the slash so we do not mess things up
$5 ................. the missing file name
" ~/tmp/wallpapers" destination folder
| sh ................ pipe the preceding command to the shell
diff -u ~/img/backgrounds ~/tmp/wallpapers \
| awk -F'[: ]' '/backgrounds/ {print "cp " $3"/"$5" ~/tmp/wallpapers/"}' | sh
흥미로운 점은 backgrounds 폴더에는 .git 파일이 없지만 월페이퍼에는 파일이 있다는 것입니다. 그래서 월페이퍼 폴더에는 thumbs 하위 폴더가 있기 때문에 awk로 '/backgrounds/'를 필터링하고 재귀 '-r'을 사용하지 않습니다.
Reference
이 문제에 관하여(diff + awk를 사용하여 폴더 동기화), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/voyeg3r/sync-folders-using-diff-awk-55aj텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)