You are not logged in.

#1 2008-06-01 17:47:45

chaosgeisterchen
Member
From: Kefermarkt, Upper Austria
Registered: 2006-11-20
Posts: 550

Questions concerning basic bash scripting

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

#2 2008-06-01 17:53:07

dyscoria
Member
Registered: 2008-01-10
Posts: 1,007

Re: Questions concerning basic bash scripting

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

#3 2008-06-01 18:07:31

Bebo
Member
From: Göteborg, Sweden
Registered: 2006-06-07
Posts: 207

Re: Questions concerning basic bash scripting

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

#4 2008-06-01 20:38:20

briest
Member
From: Katowice, PL
Registered: 2006-05-04
Posts: 468

Re: Questions concerning basic bash scripting

...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

#5 2008-06-01 21:36:02

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: Questions concerning basic bash scripting

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

#6 2008-06-02 03:12:27

fukawi2
Ex-Administratorino
From: .vic.au
Registered: 2007-09-28
Posts: 6,224
Website

Re: Questions concerning basic bash scripting

chaosgeisterchen wrote:
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 wink

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)

Offline

#7 2008-06-02 03:58:46

tam1138
Member
Registered: 2007-09-10
Posts: 238

Re: Questions concerning basic bash scripting

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

Board footer

Powered by FluxBB