I want to find some files and then move them.
I can find the file with:
$ find /tmp/ -ctime -1 -name x*
I tried to move them to my ~/play
directory with:
$ find /tmp/ -ctime -1 -name x* | xargs mv ~/play/
but that didn't work. Obviously mv needs two arguments.
Not sure if (or how) to reference the xargs 'current item' in the mv command?
Best Answer
Look at Stephane's answer for the best method, take a look at my answer for reasons not to use the more obvious solutions (and reasons why they are not the most efficient).
You can use the
-I
option ofxargs
:Which works in a similar mechanism to
find
and{}
. I would also quote your-name
argument (because a file starting withx
in the present directory would be file-globed and passed as an argument to find - which will not give the expected behavior!).However, as pointed out by manatwork, as detailed in the
xargs
man page:The important thing to note is that
-L 1
means that only one line of output fromfind
will be processed at a time. This means that's syntactically the same as:(which executes a single
mv
operation for each file).Even using the GNU
-0
xargs argument and thefind -print0
argument causes exactly the same behavior of-I
- this is toclone()
a process for each filemv
: