Reputation: 629
I am in a directory /xyz . In this /xyz directory there are multiple folders of students in alphabetically stored with their names. so I have a file value.txt which i want to copy to all folders of students who have their names beginning with A, B, C, D uptil R.
For example, in /xyz i have the following directories:
/amy
/brandon
/charles
/gina
/robert
/mike
/peter
/lily
I want to copy value.txt to all folders who have names beginning from A to R that is in
/amy
/brandon
/charles
/gina
/robert
Thanks Any help would be appreciated.
Upvotes: 0
Views: 990
Reputation: 2180
Copy the following lines to a file e.g.: cpFile2Dirs.sh
#!/bin/bash
dList=`ls -d */`
for d in $dList;
do
if [[ $d == [a-rA-R]* ]];
then
cp $1 $d
fi
done
which should sit in the directory you are currently working on.
then execute
bash cpFile2Dirs.sh value.txt
this will copy the file value.txt to all directories starting with a/A up to r/R, as defined in the if statement. You can change that as you like.
You can also set execution permission to your script
chmod +x cpFile2Dirs.sh
then you don't need to run bash copyValues.sh value.txt
, but simply
./cpFile2Dirs.sh value.txt
Hope it helps.
Upvotes: 0
Reputation: 7679
On the other side, if let say the number of letters you want to exclude will make the script less complicated , then you can do the following:
[^YZ]*
This [^YZ] pattern will match any character except Y or Z - bash manual has more details . Read ths
Upvotes: 0
Reputation: 11791
You can do something like:
for s in [a-r]*; do cp file.text $s; done
Upvotes: 2