PDA

View Full Version : how to write a shell program for file copy



rajdey1
17th August 2008, 09:11 AM
hi
i would like to get a help from the member of the forum in writing a file copy shell script which will copy the only executable file present in present working directory to another user specified directory .........

Arpee Ong
17th August 2008, 09:21 AM
just from the top of my head, this isnt a script but might help:

assuming your executables end with .sh extension, you could simply invoke



cp *.sh /home/user2/.


to copy all *.sh files in the current directory to /home/user2 directory (where /home/user2 is the specified directory)

EDIT: What kind of exectuable files are you trying to copy over? shell? perl?

rajdey1
17th August 2008, 09:27 AM
provide code to copy exucitable file that donot end with any extension.that is assume the executible file were user made for writting script

scottro
17th August 2008, 01:36 PM
So, first you need to find if the file is executable. Let's say they're all in one directory.

I think I'd use for a loop. Assuming they're in one directory. (I don't have time to test this right now, but should work. Very simple script)


for i in $(ls directoryname/*);do
if [ -x "$i" ]; then
cp $i newdirectory
fi
done
exit 0


There's lots more you could do with it, but I'm in a bit of a rush, for example, if it's not executable, do something else.

The if [ -x "$i" ] (Note the spacing, it's necessary) is running the test command. It's testing the results of ls to see if the file is exectuable. (The -x means executable.)

Hope this helps, but you might want to wait till someone with more knowledge, or time to test their own suggestions, answers. If you're going to try mine, please test it first.

oneofmany
17th August 2008, 03:05 PM
Hope this helps, but you might want to wait till someone with more knowledge, or time to test their own suggestions, answers. If you're going to try mine, please test it first.

looks ok to me but you might want to replace the "(ls....)" option with a simple "directoryname/*"

MagicalPony
17th August 2008, 03:10 PM
You could just use an rsync command instead of a copy ... that way you only utilize CPU and I/O for files that have changed.

rsync -a /src_directory/* dst_directory

stevea
17th August 2008, 03:49 PM
Use scottro's script with oneofmany's mod. I would have typed the same - almost char for char.
or maybe:

for i in dirname/*; do [ -x '$i' ] && cp $i otherdir ; done

brevity is the soul of wit.
-S

scottro
17th August 2008, 06:44 PM
@stevea a single quote around $i?

Wouldn't that just test a file named $i?
Before I go crazy trying to figure out what I missed, I'm going to wait till you confirm it wasn't a typo.

If it wasn't a typo, just confirm it, and I'll figure it out.

owmtia
18th August 2008, 04:51 AM
or using find

find $SOURCE -maxdepth 1 -type f -perm /g=x,u=x,o=x -exec cp '{}' $DEST \;

Find all files in $SOURCE directory which are regular executable files and copy to $DEST


-maxdepth 1 (Only Current Directory no recursion)
-type f (Only Standard files)
-perm /g=x,u=x,o=x (Any with an execute bit set)
-exec cp '{}' $DEST \; (copy the file to DEST)