Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

This is what I am trying to achieve.

  • I have two folders SRC and DESTINATION. SRC contains several subdirectories and files in it.
  • A batch file should be built upon clicking it the following should be done
    1. Delete all contents from DESTINATION folder
    2. Copies all the files/subdirectories from SRC to DESTINATION

Can someone help me with this ?.

If this activity requires significant effort (I don’t know whether the above is simple to implement or not), do point me some resources for me to dig into it.

share|improve this question
1  
This is not Ubuntu specific. Additionally: how sohuld the (bash-) script know the SRC and DEST parameters without providing them manually? The two functions you are looking for are rm -rf and cp -r. Read their man pages, if you are unsure about what they do. –  onse Sep 9 '13 at 14:56
    
rsync may be something to look into –  Mateo_ Sep 11 '13 at 0:44

2 Answers 2

up vote 2 down vote accepted

Create the following file using your favorite (plain) text editor (e.g. gedit or kwrite)

#!/bin/bash

source_dir=SRC
dest_dir=DESTINATION

rm -rf "$dest_dir/*" #Remove the contents of DESTINATION
cp -r "$source_dir/* $dest_dir/" #Copy the contents of SRC into DESTINATION

The above should be saved with a meaningful name; e.g. copy_SRC_to_DESTINATION.sh and made executable with

chmod +x /path/to/copy_SRC_to_DESTINATION.sh

from a terminal

Now, in your file manager, navigate to where you saved the file, double-click on it and choose "Run".

Notes

  • As onse commented, you should edit the script file and manually enter the actual names of your directories instead of SRC and DESTINATION in the above listing.
  • Replace /path/to/ in the chmod line above with the actual path where you saved the script file.
share|improve this answer

Why don't just use cp and rm?

cp -R will copy all files and folders from selected location to other and rm -R will remove all files and folders from selected location.

cp -R <sourde> <destination>
rm -R <directory to remove>

Tou may also use rmdir for removing directories.

I think this two simple steps don't need to write a executable file for it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.