bash - need help utilizing find command and xargs command -
i'm trying write simple scripts can mv every file within folder folder generated current date. initiatives.
#!/bin/bash storage_folder=`date +%f` # date generated name folder mkdir "$storage_folder" #createing folder store data find "$pwd" | xargs -e mv "$storage_folder" # mv everyfile folder
xargs not needed. try:
find . -exec mv -t "$storage_folder" {} +
notes:
find's
-exec
feature eliminates needsxargs
.because
.
refers current working directoy,find "$pwd"
same simplerfind .
.the
-t target
optionmv
tellsmv
move filestarget
directory. handy here because allows fitmv
command natural formatfind -exec
command.
posix
if not have gnu tools, mv
may not have -t
option. in case:
find . -exec sh -c 'mv -- "$1" "$storage_folder"' move {} \;
the above creates 1 shell process each move. more efficient approach, suggested charles duffy in comments, passes in target directory using $0
:
find . -exec sh -c 'mv -- "$@" "$0"' "$storage_folder" {} +
safety
as gordon davisson points out in comments, safety, may want use -i
or -n
options mv
files @ destination not overwritten without explicit approval.
Comments
Post a Comment