from wikipedia:

1.

find . -name "*.foo" -print0 | xargs -0 grep bar

The above command uses GNU specific extensions to find and xargs to separate filenames using the null character;

What is the null character? What is the significance of "separate filenames using the null character;"? why should we do that? Where is the null character specified?

2.

find . -name "*.foo" -print0 | xargs -0 -t -r vi

The above command is similar to the former one, but launches the vi editor for each of the files. The -t prints the command to stderr before issuing it. The -r is a GNU extension that tells xargs not to run the command if no input was received.

VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Mar 19 2009 15:56:33)
Too many edit arguments: "vi"
More info with: "vim -h"
lin309-05:~/workspace/ruby/icm$  find . -name "*.foo" -print0 | xargs -0 -t -r vi
lin309-05:~/workspace/ruby/icm$ find . -name "*.rb" -print0 | xargs -0 -t -r vi
vi ./util.rb ./test_utf8.rb ...
Vim: Warning: Input is not from a terminal
28 files to edit

first what is the significance of -r. second, after quitting from vi by :q
the shell hangs. There is a blinking cursor, but no keyboard input is accepted. It happened twice.

3.

find . -name "*.foo" -print0 | xargs -0 -I xxx mv xxx /tmp/trash

The above command uses string xxx instead of {} as the argument list marker.

But when I tried this:

find . -name "*.foo" -print0 | xargs -0 -I xxx mv xxx /tmp/

it gives:

mv: preserving permissions for `/tmp/minf.foo': Operation not supported
mv: preserving permissions for `/tmp/state.foo': Operation not supported

changing the I switch to i (as it is ubuntu) gives this :

lin309-05:~/workspace/ruby/icm$ ls *.foo
minf.foo  state.foo
lin309-05:~/workspace/ruby/icm$ find . -name "*.foo" -print0 | xargs -0 -i xxx mv xxx /tmp/
xargs: xxx: No such file or directory

4.

find . -maxdepth 1 -type f -name "*.ogg" -print0 | xargs -0 -r cp -v -p --target-directory=/home/media

The command above does the same as:

cp -v -p *.ogg /home/media

For the first version why the cp is accepting the second argument as source argument coming from 'find' where as we know the source argument is first argument in case of cp.

Also it says You can also use -L to limit the number of arguments. If you do that, the command will be run repeatedly until it’s out of arguments. Thus, -L1 runs the command once for each argument (needed for tools like tar and such).

I didn't understand it at all. What is it talking about?

Edited 6 Years Ago by johndoe444: n/a

from wikipedia:

1.

find . -name "*.foo" -print0 | xargs -0 grep bar

The above command uses GNU specific extensions to find and xargs to separate filenames using the null character;

What is the null character? What is the significance of "separate filenames using the null character;"? why should we do that? Where is the null character specified?

Null character is a byte with a numerical value of 0 (all bits cleared).

By default, find separates the found filenames with a whitespace, which creates obvious problems if filenames have an embedded whitespace. However, it is not possible to have a filename with an embedded null character in it. Using 0 as a filename separator allows xargs to parse the find output unambiguously.

2.

find . -name "*.foo" -print0 | xargs -0 -t -r vi

first what is the significance of -r. second, after quitting from vi by :q
the shell hangs. There is a blinking cursor, but no keyboard input is accepted. It happened twice.

That was a very creative abuse of vi. Do not even know how to comment.

3.

find . -name "*.foo" -print0 | xargs -0 -I xxx mv xxx /tmp/trash

The above command uses string xxx instead of {} as the argument list marker.

But when I tried this:

find . -name "*.foo" -print0 | xargs -0 -I xxx mv xxx /tmp/

it gives:

mv: preserving permissions for `/tmp/minf.foo': Operation not supported
mv: preserving permissions for `/tmp/state.foo': Operation not supported

changing the I switch to i (as it is ubuntu) gives this :

lin309-05:~/workspace/ruby/icm$ ls *.foo
minf.foo  state.foo
lin309-05:~/workspace/ruby/icm$ find . -name "*.foo" -print0 | xargs -0 -i xxx mv xxx /tmp/
xargs: xxx: No such file or directory

-i is deprecated. Don't use it. -I worked as expected. The warnings come from some sort of misconfiguration. How your hard drive is partitioned?

4.

find . -maxdepth 1 -type f -name "*.ogg" -print0 | xargs -0 -r cp -v -p --target-directory=/home/media

The command above does the same as:

cp -v -p *.ogg /home/media

For the first version why the cp is accepting the second argument as source argument coming from 'find' where as we know the source argument is first argument in case of cp.

See highlighted. In this form a target is specified as an option, not as a last argument; every argument is treated as a "source".

Also it says You can also use -L to limit the number of arguments. If you do that, the command will be run repeatedly until it’s out of arguments. Thus, -L1 runs the command once for each argument (needed for tools like tar and such).

I didn't understand it at all. What is it talking about?

What can I say... You seem to be missing basics. find is a very tricky command, definitely not a good starting point for a beginner.

This article has been dead for over six months. Start a new discussion instead.