Handling spaces in a directory in bash sh -
i have line cp $(find "$directory" -type f | grep -ie '\.(c|cc|cpp|cxx)$') ~/src searches given directory (in case, $directory /home) , copies file extensions of .c, .cc, .cpp , .cxx src folder, error of cp:cannot stat directory: no such file or directory.
i thought putting directory in quotes prevent that. doing wrong?
the error command cp, quoting $directory, while idea, won't solve error.
your construct fail file/directory names contain spaces, cases grep turns out 0 matches, , other cases can't think of right now.
some better solutions:
use
find's name matching instead ofgrep, , use-execit:find "$directory" -type f \( -name '*.c' -o -name '*.cc' -o -name '*.cpp' -o -name '*.cxx' \) -exec cp '{}' ~/src ';' find "$directory" -type f -regextype posix-egrep -iregex '.*\.(c|cc|cpp|cxx)$' -exec cp '{}' ~/src ';'use
xargs\0separators instead of\n:find "$directory" -type f -print0 | grep -z -ie '\.(c|cc|cpp|cxx)$' | xargs -0 -i{} cp "{}" ~/srcif file structure flat (no subdirectories), use
cp:cd "$directory"; cp *.c *.cc *.cpp *.cxx ~/src
Comments
Post a Comment