Register
It is currently Sat Oct 25, 2014 8:55 pm

Very simple script for copying files.


All times are UTC - 6 hours


Post new topic Reply to topic  [ 10 posts ] 
Author Message
 PostPosted: Tue Jan 16, 2007 1:54 pm   

Joined: Tue Jan 16, 2007 1:24 pm
Posts: 5
Hi guys, I do a lot of moving/copying between two different locations. I usually move folders though.
I was wondering how I can construct (or preferably, if someone could do it for me) a script to do this:

a & b are 2 different paths (ie: /home/user/storage) and each question is asked one by one.

Description [default option]: Actual Command
//////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
List files in a folder [y]? //(sudo ls -la a) #Options: [y/n]
List files in b folder [y]? //(sudo ls -la b) #Options: [y/n]
Copy or move [m]: //(sudo cp -R) or (sudo mv -f) #Options: [c/m]
(a to b) or (b -> a) [a]: //(a -> b) or (b -> a) #Options: [a/b]
Name of folder: //User enters the name of folder.
/////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

--------------------------------------------------------------------------------------
EXAMPLES:
user@server:~$ ./bash_file
List files in a folder [y]? [ENTER]
//lists contents of location 'a'

List files in b folder [y]? [ENTER]
//lists contents of location 'b'

Copy or move [m]: [ENTER]
(a to b) or (b -> a) [a]: [ENTER]
Name of folder: BLAH
//runs (sudo mv -f a/BLAH b/.)
user@server:~$

- or -

List files in a folder [y]? [n]
List files in b folder [y]? [n]
Copy or move [m]: [c]
(a to b) or (b -> a) [a]: [b]
Name of folder: BLAH

End Result would be the same as running this command:
sudo cp -R b/BLAH a/.
--------------------------------------------------------------------------------------

Thanks.


Top
 Profile  
 PostPosted: Tue Jan 16, 2007 5:01 pm   
User avatar

Joined: Mon Jul 03, 2006 8:58 pm
Posts: 52
Location: Rochester, NY
Just curious: have you looked at rsync for doing this sort of backup?


Top
 Profile WWW  
 PostPosted: Tue Jan 16, 2007 6:28 pm   

Joined: Tue Jan 16, 2007 1:24 pm
Posts: 5
Yeah, I have rsync. The reason I am using the script is because I have mainly 2 directories (on my machine) between which I do a lot of transfers. I know nothing about bash, but I looked up some references online and this is what I've come up with. My biggest problem is when I have a folder which has spaces in the name. I can't figure it out. This is what I have so far:

Code:
#!/bin/bash
echo List files in storage folder [y/n]?
read ONE
if [ $ONE = "y" ]; then
   sudo ls ../shared
fi

echo List files in FTP folder [y/n]?
read TWO
if [ $TWO = "y" ]; then
   sudo ls ../ftp
fi

echo Copy or move [m/c]:
read THREE
if [ $THREE = "m" ]; then
   THREE="sudo mv -f"
else
   THREE="sudo cp -R"
fi

echo Name of folder:
read FOUR

echo "(Storage to FTP) or (FTP -> Storage) [a/b]: "
read FIVE
if [ $FIVE = "a" ]; then
   $THREE ../shared/$FOUR ../ftp/.
else
   $THREE ../ftp/$FOUR ../shared/.
fi


I know it's crude but it works (for the most part).

When I enter a folder name with multiple words, it doesn't work (ex: "Edited Files"). I know why, and I've tried to fix it about a dozen times, but I haven't been successful. I know I can rename the folders, but I will only do that as a last resort.

Thanks again.


Top
 Profile  
 PostPosted: Tue Jan 16, 2007 6:38 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 669
Location: Des Moines, Iowa
Code:
echo Name of folder:
read FOUR

echo "(Storage to FTP) or (FTP -> Storage) [a/b]: "
read FIVE
if [ $FIVE = "a" ]; then
   $THREE ../shared/$FOUR ../ftp/.
else
   $THREE ../ftp/$FOUR ../shared/.
fi


CHANGE to

Code:
echo "Name of folder: "
read FOUR

echo "(Storage to FTP) or (FTP -> Storage) [a/b]: "
read FIVE
if [ $FIVE = "a" ]; then
   $THREE ../shared/"${FOUR}" ../ftp/.
else
   $THREE ../ftp/"${FOUR}" ../shared/.
fi


That should probably take care of that.


Top
 Profile WWW  
 PostPosted: Tue Jan 16, 2007 10:39 pm   

Joined: Tue Jan 16, 2007 1:24 pm
Posts: 5
Thanks for the advice. Unfortunately, when I tried it (I entered "Edited Files" as my folder) I received:

cp: cannot stat `../shared/Edited': No such file or directory
cp: cannot stat `Files': No such file or directory

I need to add a "\" before each space. How can I do that?

Thanks.


Top
 Profile  
 PostPosted: Wed Jan 17, 2007 2:25 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
please post your code as it is now
especially since you have made changes....
crouse's suggestion should have worked...
i'd like to see the whole thing, so i can try and figure the error out


Top
 Profile  
 PostPosted: Wed Jan 17, 2007 2:17 pm   

Joined: Tue Jan 16, 2007 1:24 pm
Posts: 5
Here it is. The only thing I changed from the original code was what he suggested.
This error is a common in linux. For example, if I wanted to move a folder called
"Edited Files" from /shared to /ftp, I'd have to enter this command into terminal:
sudo mv -f /shared/Edited\ Files /ftp/.

If I have just this:
sudo mv -f /shared/Edited Files /ftp/.

(with no backslash before the space), linux would treat is as 2 separate folders.
My problem getting my bash script to insert a backslash before any special character
(spaces, parenthesis, etc.).

Here is my current code:

Code:
#!/bin/bash
echo List files in storage folder [y/n]?
read ONE
if [ $ONE = "y" ]; then
   sudo ls ../shared
fi

echo List files in FTP folder [y/n]?
read TWO
if [ $TWO = "y" ]; then
   sudo ls ../ftp
fi

echo Copy or move [m/c]:
read THREE
if [ $THREE = "m" ]; then
   THREE="sudo mv -f"
else
   THREE="sudo cp -R"
fi

echo Name of folder:
read FOUR

echo "(Storage to FTP) or (FTP -> Storage) [a/b]: "
read FIVE
if [ $FIVE = "a" ]; then
   $THREE ../shared/${FOUR} ../ftp/.
else
   $THREE ../ftp/${FOUR} ../shared/.
fi


<edited by jbsnake>
if you notice above you don't have ${FOUR} quoted... which is what crouse was saying to do
that is why you are getting the broken arguments...
if you change the above from reading ${FOUR} to "${FOUR}" it will be fixed without the need of adding any \ anywhere.
</edit>

Thanks.


Top
 Profile  
 PostPosted: Wed Jan 17, 2007 3:53 pm   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
bump


Top
 Profile  
 PostPosted: Wed Jan 17, 2007 6:50 pm   

Joined: Tue Jan 16, 2007 1:24 pm
Posts: 5
Genius. I tend to miss little things like that.
Thanks a lot for your help guys, I really appreciate it.


Top
 Profile  
 PostPosted: Thu Jan 18, 2007 2:42 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
no problem... just glad we could help :D


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP