Real life diff use:
I want to copy only the missing 'jpg' files at the ~/tmp/wallpapers
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 Using awk with two field separators [: ] I only need the third and fifth fields to compose a copy command:
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 Now I only have to add some strings to my awk command and pipe the result to the shell
"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 An interesting thing is that the backgrounds folder has no .git files on it but the wallpapers do, that's why I am filtering '/backgrounds/' with awk and not using recursion '-r', because the wallpapers folder has a thumbs subfolder
Top comments (0)