-r0
is two flags. It is -r
and it is -0
.
From the xargs(1)
manpage:
-r
If the standard input does not contain any nonblanks, do not run the command.
Normally, the command is run once even if there is no input. This option is a
GNU extension.
-0
Input items are terminated by a null character instead of by whitespace, and
the quotes and backslash are not special (every character is taken
literally). Disables the end of file string, which is treated like any other
argument. Useful when input items might contain white space, quote marks, or
backslashes. The GNU find -print0 option produces input suitable for this
mode.
If I understand correctly, you want to fire up one instance flac … | lame …
for each input line, and interpolate the input into the arguments to both commands.
Since you need xargs
to start a pipeline, you need to make it start a program that's capable of creating pipelines, i.e. a shell.
inotifywait -m -r -q -e moved_to --format "%w%f" ~/test |
xargs -l sh -c 'flac -cd "$0" - | lame -b 320 - "/media/1tb/$0.mp3"'
Alternatively, have the calling shell read lines one by one and run the pipeline.
inotifywait -m -r -q -e moved_to --format "%w%f" ~/test |
while IFS= read -r file; do
flac -cd "$file" - | lame -b 320 - "/media/1tb/$file.mp3"
done
Note that the format %w%f
produces an absolute path, to which you're prepending /media/1tb
and appending .mp3
. If you want to strip off the directory part of the file in the lame
command, change $file
to ${file##*/}
. If you want to strip off the extension, change $file
to ${file%.*}
. If you want to do both, you'll have to do it in two steps. If you want to reproduce the directory hierarchy under /media/1tb
, you can use mkdir -p
.
cd ~/test
inotifywait -m -r -q -e moved_to --format "%w%f" . |
while IFS= read -r file; do
[ -f "$file" ] || continue; # skip directories and other special files
dir=${file%/*}; file=${file##*/}
mkdir -p "/media/1tb/$dir"
flac -cd "$dir/$file" - | lame -b 320 - "/media/1tb/$dir/${file#.*}.mp3"
done
Best Answer
xargs
transforms input to arguments. The-I
option specifies the string to be used as a placeholder for the argument. Therefore, if the pipeline outputs something likethen the
xargs
line transforms that toThe
head -1
only returns one line, though, so I can see no real benefit of usingxargs
instead of, sayMoreover, since the target file is always the same, it will be overwriten by the last file in
${files[@]}
.