diff + awk를 사용하여 폴더 동기화

2379 단어 adminlinuxawkdiff

실제 사용:



~/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'을 사용하지 않습니다.

좋은 웹페이지 즐겨찾기