Operating System - HP-UX
1828225 Members
2463 Online
109975 Solutions
New Discussion

Shell script of copy files from one directory to another

 
SOLVED
Go to solution
cybermilky
Occasional Contributor

Shell script of copy files from one directory to another

Does anyone know how to create a shell script that works like below:

It copies the files from the first argument directory to the second argument directory and deletes all the files in the first argument directory.

The script must do the right level of checking of the arguments first. For example: when the second directory does not exist, or when the second directory exists, but not the first one.

Can someone help?
5 REPLIES 5
Balaji N
Honored Contributor

Re: Shell script of copy files from one directory to another

looks like some kind of a school assignment and you r using these forums to get answers for them?
-b-
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Stefan Farrelly
Honored Contributor
Solution

Re: Shell script of copy files from one directory to another


if [ ! -d "$1" ]
then
echo "Source directory $1 does not exist"
exit 1
fi
if [ ! -d "$2" ]
then
echo "Destination dir $2 does not exist"
exit 2
fi
mv $1 $2 # mv copies first then deletes $1


Save it to a script called say copy.sh then run it as ./copy.sh
Im from Palmerston North, New Zealand, but somehow ended up in London...
Paula J Frazer-Campbell
Honored Contributor

Re: Shell script of copy files from one directory to another

Hi

If this an assignment / question then Stefan has shown you how, so look at the structute and then make it your own by useing copy instead of move with check routines to ensure that the files in the destination dirs match the files in the target dirs and perhaps also log the results.


Paula
If you can spell SysAdmin then you is one - anon
Steve Post
Trusted Contributor

Re: Shell script of copy files from one directory to another

This isn't really an answer to your question. But if your intent is really to move a directory stucture to a different spot?

To move a directory structure:

Let's move /a/b/c/dir1 to /x/y/dir1
cd /a/b/c
find dir1 -print | cpio -pdumvc /x/y

This will create "dir1" under /x/y.

Then to remove /a/b/c/dir1,
cd /a/b/c
rm -r dir1

Why would you do something like this?
The current filesystem of /a/b/c/dir1 is being reassigned to another computer. Or /a/b/c is running out of room. Or you set up a project at /a/b/c, and someone later says they want it at another spot.

Of course you have to be careful. "rm -r" is obviously powerful. Have backups. Do this when no one is on the system. Coordinate the action, etc.
John Dvorchak
Honored Contributor

Re: Shell script of copy files from one directory to another

And if it is a school assignment, you guys are providing "A" answers. The least he could do is assign some 10's for your scores.
If it has wheels or a skirt, you can't afford it.