Bash by example

xargs

  • -I replace-str

    -i default is {} as replace-str

    1
    2
    3
    4
    
    $ echo argument1 argument2 argument3 | xargs -I % sh -c 'echo %'
    $ echo argument1 argument2 argument3 | xargs -I # sh -c 'echo #'
    $ echo argument1 argument2 argument3 | xargs -i sh -c 'echo {}'
    argument1 argument2 argument3
    
  • -n max-args

    1
    2
    3
    4
    
    $ echo argument1 argument2 argument3 | xargs -n 1
    argument1
    argument2
    argument3
    

for loop

  • in

    1
    2
    
    $ export destination=/mnt/storage/
    $ for folder in ./*;do 7zz a -mx0 $destination/$folder.7z $folder;done;
    

gawk

  • using regex group, add prefix and suffix

    1
    
    grep "file not in Local file system at /mnt/storage/courses" nohup.out | gawk 'match($0, /:\s(.*7z)/, ary) {print "\"webdav:/189cloud/courses/"ary[1]"\""}' | xargs -n 1 rclone delete