You are not logged in.
Good evening,
at the moment we do bash scripting at university, but I seen to encounter some problems in understanding the functionality of bash. I would be very grateful if you guys could help me out with one or another question.
First one:
Task to accomplish: Scripts with three arguments. $1 -> source directory, $2 -> file suffix, $3 -> target directory. Search source directory for all files with suffix $2 and copy them to $3.
I tried to do it, using pipe and xargs, but I am wondering how to pass the filenames found over the pipe.
find $source -name "*.$suffix" | xargs cp [MISSING ARGUMENT] $target
How do I access the data I need to fill in as first argument of cp, or isn't it possible to accomplish the task this way?
Regards,
cg
celestary
Intel Core2Duo E6300 @ 1.86 GHz
kernel26
KDEmod current repository
Offline
A for loop should do the trick:
sources=$(find $source -name "*.$suffix")
for file in $sources
do
cp $file $target
done
Actually, no need for the for loop:
sources=$(find $source -name "*.$suffix");cp $sources $target
#
Well actually, this is probably best to avoid problems with files/folders with spaces:
sources=$(find $source -name "*.$suffix")
for file in $sources
do
cp "$file" "$target"
done
Last edited by dyscoria (2008-06-01 17:58:16)
flack 2.0.6: menu-driven BASH script to easily tag FLAC files (AUR)
knock-once 1.2: BASH script to easily create/send one-time sequences for knockd (forum/AUR)
Offline
xargs does have the functionality you want. Check the man page for the -I option.
Last edited by Bebo (2008-06-01 18:08:23)
Offline
...or check cp man page for --target-directory option.
In addition, when it comes to find ... | xargs, options -print0 (find) and -0 (xargs) are really useful.
Offline
Not to mention find can perform operations too. Check man find on -exec, and use something like find -name 'foo*bar' -exec cp '{}' $destination \;
Offline
find $source -name "*.$suffix" | xargs cp [MISSING ARGUMENT] $target
Almost, you just need the -t flag to cp which basically reverses the order of SOURCE and TARGET in this instance
find $source -name "*.$suffix" | xargs cp -t $target
Also, I suggest you escape the * in your find to prevent bash expanding it. Bash shouldn't expand, find should.
You might like to also use the -iname (as opposed to -name) predicate to find if you don't care about the case of the source files.
Last edited by fukawi2 (2008-06-02 03:13:37)
Are you familiar with our Forum Rules, and How To Ask Questions The Smart Way?
BlueHackers // fscanary // resticctl
Offline
Does it need to search all subdirectories of $source? If not, then why not
$ cp $source/*.$suffix $target
That could be a problem if there are too many files in $source to put on a single command-line, in which case I would use
$ find "$source" -name "*.$suffix" -print0 | xargs -0 cp -t "$target"
I would definitely not use a for-loop, as it will almost certainly be spectacularly inefficient compared to find/xargs.
Last edited by tam1138 (2008-06-02 03:59:44)
Offline