I'm working on a bash script that needs to (recursively) move all files and folders within a source folder into a destination folder.
In trying to make this as robust as possible, and address potential argument list too long - errors, I opted to use the find
command (to safely determine the files to move) piped into xargs
(to efficiently group the moves together). I'm also using -print0
and -0
to address potential problems with spaces.
I've written the following test script:
#!/bin/bash
# create test source file structure, and 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 from source to destination
find sourceDir -mindepth 1 -print0 | xargs -0 mv --target-directory=destDir
Which seems to work (i.e. the files are moved) but for some reason I get numerous errors as follows:
mv: cannot stat `sourceDir/subdir with space 2/file2 with space.txt': No such file or directory
mv: cannot stat `sourceDir/subdir with space 2/noExtension2': No such file or directory
mv: cannot stat `sourceDir/subdir with space 2/a2.txt': No such file or directory
mv: cannot stat `sourceDir/subdir1/file1 with 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 be a way to do this (for the source files indicated in my script) without generating errors?
Why are these errors being generated (since the files and folders are in fact being moved)?