Need a shell script which takes two inputs and copy the files from one directory to other


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a shell script which takes two inputs and copy the files from one directory to other
# 1  
Old 04-10-2012
Need a shell script which takes two inputs and copy the files from one directory to other

Hi,
I am using solari 10 OS which is having bash shell.
I need a shell script which takes user home directory and name of the file or directory as a input and based on that copy the files accordingly to the other directory.

example:I hava a machine1 which is having some files in a directory /spa/5.0/incude/file1 etc.

say I have user1. I want to give user1 and name of the file or directory say file1or dir1 as input to the script .based on the input given ,script should search for the filesor directory recursively in the /spa/5.0/incude directory and copy the files to the user home directory.

If files or directory not found then it should prompt a message saying directory or file does not exist.

Advance thanks for the input.
# 2  
Old 04-10-2012
Code:
#$1 space separated file/directory names

if [ $# -eq 0 ]; then
    printf "specify atleast one file or directory name to copy\n"
else
    while read fdname
    do
        if [ ! `find /spa/5.0/incude/ -xdev -name $fdname` ]; then
            printf "$fdname not found\n"
        else
        find /spa/5.0/incude/ -xdev -name $fdname | while read file
                                                    do
                                                        cp -r $file ~
                                                    done
        fi
    done < $1
fi


Last edited by 47shailesh; 04-16-2012 at 10:30 AM.. Reason: copy syntax correction
# 3  
Old 04-16-2012
Sorry for the delayed response.I have tried the following script and got the folllowing error.

Code:
-bash-3.00$ sh testscript.sh sa0_upd_estd_shipmn.shx
testscript.sh: sa0_upd_estd_shipmn.shx: cannot open

I have given full permission to the file sa0_upd_estd_shipmn.shx and the file is present in /spa/5.0/include directory.
still it is giving error.appreicate any help on it.

Moderator's Comments:
Mod Comment Please use code tags. Video tutorial on how to use them
# 4  
Old 04-16-2012
Quote:
and the file is present in /spa/5.0/include directory
In post #1 you consistently spell the directory name /spa/5.0/incude

Which is correct?
# 5  
Old 04-16-2012
Extremely sorry for the typo error.Actual directory is /spa/5.0/include .I have done this change in the script i.e replacing /spa/5.0/incude with /spa/5.0/include but there is no luck.

output:
Code:
-bash-3.00$ more testscript.sh
if [ $# -eq 0 ]; then
    printf "specify atleast one file or directory name to copy\n"
else
    while read fdname
    do
        if [ ! `find /spa/5.0/include/ -xdev -name $fdname` ]; then
            printf "$fdname not found\n"
        else
        find /spa/5.0/include/ -xdev -name $fdname | while read file
                                                    do
                                                        copy $file ~
                                                    done
        fi
    done < $1
fi

Code:
-bash-3.00$ sh testscript.sh sa0_upd_estd_shipmn.shx
testscript.sh: sa0_upd_estd_shipmn.shx: cannot open


Last edited by Franklin52; 04-16-2012 at 09:22 AM.. Reason: Please use code tags for code and data samples, thank you
# 6  
Old 04-16-2012
In the syntax you post, $1 would refer to the name of a file from which you want to read the contents into the input stream in the script. This is not correct.

In your case I think that $1 contains a filename (or directory name) which you want to check whether it exist - and if it does, copy the file (or directory) to the user's home directory.
What is not clear is what happens when the parameter is a directory? Is the script going to attempt to copy that directory and all its contents or just copy the directory file?



Quote:
Need a shell script which takes two inputs and copy the files from one directory to other
All your examples there is only one input - $1 .
# 7  
Old 04-16-2012
Hi,
thanks for the response.

In your case I think that $1 contains a filename (or directory name) which you want to check whether it exist - and if it does, copy the file (or directory) to the user's home directory--your statment is correct and if the mentioned directory or file does not exist it should throw an error message

What is not clear is what happens when the parameter is a directory? Is the script going to attempt to copy that directory and all its contents or just copy the directory file-------Requirement here is even if it is a directory it should traverse through the subdirctories and copy the subdirectories along with the files to the present directory or to the given input directory say /export/home/user1.i guess that point is not present in the script.

Let me know is this the info u r looking at?

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Moving Files one directory to another directory shell script

Hi, Could you please assist how to move the gz files which are older than the 90 days from one folder to another folder ,before that it need to check the file system named "nfs" if size is less than 90 or not. If size is above 90 then it shouldn't perform file move and exit the script throwing... (4 Replies)
Discussion started by: venkat918
4 Replies

2. Shell Programming and Scripting

Shell script to copy files from on folder to another

I am trying to copy files with specific date and name to another folder. I am very new to shell scripting so i am finding it hard to do that. see the sample code i have written below. srcdir="/media/ubuntu/CA52057F5205720D/Users/st4r8_000/Desktop/office work/26 nov"... (13 Replies)
Discussion started by: Aqeel Abbas
13 Replies

3. Shell Programming and Scripting

Bash script to copy apache log files to client directory

Our Apache log files are written to a location on the server that we as clients have no access. Don't ask. Every month, I have to e-mail the administrator to have him manually copy our Apache log files to a directory in our file space. You can probably guess how efficient it is to do things this... (3 Replies)
Discussion started by: gregraven
3 Replies

4. Shell Programming and Scripting

Script which takes two inputs based on that execute othe scripts

Hi, I am using solaris 10 bash shell.this might a small script but i am not much familiar with scripting. My requirement here is script should prompt for users two opions like this "please select either any one option makefile or make& build file". if the user selects make file option... (2 Replies)
Discussion started by: muraliinfy04
2 Replies

5. Shell Programming and Scripting

Script to Poll Directory and Copy files

Hi all, I'm looking for a script to poll a specified directory and copy new files to another location. The script should only copy new files so, I based on mtime I guess? Can anyone point me in the right direction of a script which could do this? My scripting skills aren't too bad, but... (1 Reply)
Discussion started by: JayC89
1 Replies

6. Shell Programming and Scripting

error in sh script while copy files to a backup directory

I am trying to copy files from one directory to another using shell script. Can anyone please troubleshoot the code. thanks in advance... #!C:\Shell\sh.exe files_dir="C:\Documents and Settings\scripts\files" backup_dir="C:\Documents and Settings\scripts\ztest" echo cding to... (2 Replies)
Discussion started by: sureshcisco
2 Replies

7. Shell Programming and Scripting

shell script to search and copy files

Hello Im new to this forums, I would like some help regarding a script that I need in order to copy some files. Heres the scenario: I need to search several files which have a particular code inside, lets say "test" all of them on different directories. I need to copy all of them on a new... (4 Replies)
Discussion started by: c.watson
4 Replies

8. Shell Programming and Scripting

need inputs on how i can change my script to reduce amount of time the script takes

HI , I have a list1 which consists of data that i have to search and a list2 which has the files that need to be searched .So basically i am using list1 on list2 to see if list1 data is present if found replace it .I have written the code using foreach loop for each list .This is taking the... (1 Reply)
Discussion started by: madhul2002
1 Replies

9. Shell Programming and Scripting

Simple BASH shell script to rename webcam jpg and copy into a new directory.

System: Ubuntu Intrepid Ibex I'm running webcamd as a sort of "security" program, but I need a script that will archive my webcam.jpg files. So, take the following file: /home/slag/www/webcam.jpg Rename it--preferably with a time stamp. Place it in say: /home/slag/www/history/ ... (4 Replies)
Discussion started by: robfindlay
4 Replies

10. Shell Programming and Scripting

shell script to find and copy the files creted in the year 2006 to another directory

Hi All, I am new to UNIX. I will be thankful if some one helps me. I have to write a shell script for one of the requirement. I have files created from Jan 2006 to March 2008. My requirement is to write a script in such a way that 1) To find and copy(not Moving) the files created in the... (2 Replies)
Discussion started by: manas6
2 Replies
Login or Register to Ask a Question