Sunday, 15 April 2012

bash - "mv: cannot stat" error when using find to move files via xargs -



bash - "mv: cannot stat" error when using find to move files via xargs -

i'm working on bash script needs (recursively) move files , folders within source folder destination folder.

in trying create robust possible, , address potential argument list long - errors, opted utilize find command (to safely determine files move) piped xargs (to efficiently grouping moves together). i'm using -print0 , -0 address potential problems spaces.

i've written next test script:

#!/bin/bash # create test source file structure, , destination folder mkdir -p sourcedir/{subdir1,subdir\ with\ space\ 2} mkdir -p destdir touch sourcedir/subdir1/{a1.txt,noextension1,file1\ with\ space.txt} touch sourcedir/subdir\ with\ space\ 2/{a2.txt,noextension2,file2\ with\ space.txt} #move files/folders source destination find sourcedir -mindepth 1 -print0 | xargs -0 mv --target-directory=destdir

which seems work (i.e. files moved) but reason numerous errors follows:

mv: cannot stat `sourcedir/subdir space 2/file2 space.txt': no such file or directory mv: cannot stat `sourcedir/subdir space 2/noextension2': no such file or directory mv: cannot stat `sourcedir/subdir space 2/a2.txt': no such file or directory mv: cannot stat `sourcedir/subdir1/file1 space.txt': no such file or directory mv: cannot stat `sourcedir/subdir1/noextension1': no such file or directory mv: cannot stat `sourcedir/subdir1/a1.txt': no such file or directory

should there not way (for source files indicated in script) without generating errors?

why these errors beingness generated (since files , folders in fact beingness moved)?

i've sussed it.

short answer: missing -maxdepth 1 tag. next command works

find sourcedir -mindepth 1 -maxdepth 1 -print0 | xargs -0 mv --target-directory=destdir # find sourcedir -mindepth 1 -maxdepth 1 -exec mv --target-directory=destdir '{}' +

longer answer:

my original find command listing paths each , every file, , trying move each , every 1 of them. 1 time top level folder had been moved, there no need move files/folders underneath - script still trying to!

the script below demonstrates (without performing moves):

#!/bin/bash mkdir -p sourcedir/{subdir1/subsubdir1,subdir\ with\ space\ 2} touch sourcedir/subdir1/{subsubdir1/deeperfile.log,a1.txt,noextension1,file1\ with\ space.txt} touch sourcedir/subdir\ with\ space\ 2/{a2.txt,noextension2,file2\ with\ space.txt} touch sourcedir/rootfile.txt echo -e "\n--- output of 'ls -1: sourcedir':" echo "$(ls -1 sourcedir)" echo -e "\n--- original wrong effort trying move each of following:" find sourcedir -mindepth 1 echo -e "\n--- working effort needs move each of top level files/folders, underneath folders moved automatically" find sourcedir -mindepth 1 -maxdepth 1

giving output:

--- output of 'ls -1: sourcedir': rootfile.txt subdir1 subdir space 2 --- original wrong effort trying move each of following: sourcedir/rootfile.txt sourcedir/subdir space 2 sourcedir/subdir space 2/file2 space.txt sourcedir/subdir space 2/noextension2 sourcedir/subdir space 2/a2.txt sourcedir/subdir1 sourcedir/subdir1/file1 space.txt sourcedir/subdir1/noextension1 sourcedir/subdir1/a1.txt sourcedir/subdir1/subsubdir1 sourcedir/subdir1/subsubdir1/deeperfile.log --- working effort needs move each of top level files/folders, underneath folders moved automatically sourcedir/rootfile.txt sourcedir/subdir space 2 sourcedir/subdir1

bash find xargs stat mv

No comments:

Post a Comment