bash script to copy files

hey everyone, new here

i have arch setup and i am using smbnetfs to mount some windows shares in /mnt/smbnet

what i want to do is copy files from my home dir to a dir in /mnt/smbnet but i also need it to remove files if i have deleted them from my home dir

seems that cp would be the easiest for me, but i cant get it to remove a file from the dest that has been deleted from the source

example, i can do this as root

cp -pru /home/user/Videos/* /mnt/smbnet/share/Vids

and it does copy files. and if i add a file, it copies the new one over. but if i delete a file and run this command, the deleted file does not get removed from /mnt/smbnet

didnt see anything in man cp that relates to what i am trying to do. did i miss something? is there a better way?

ok, sorry, this isnt a bash script, but i would put it in one, sorry if my title is misleading

rsync -az --delete /home/user/Videos/ /mnt/smbnet/share/Vids

Sounds like you are trying to syncronise to directories, the best tool for this sort of thing is rsync not cp.

You probably need something like:

rsync -ar --dry-run --del --progress /home/user/Videos/ /mnt/smbnet/share/Vids

Once your happy with what is going to happen remove the --dry-run to do the actual sync.

---------- Post updated at 08:57 AM ---------- Previous update was at 08:51 AM ----------

@Nevyn, Don't think compression (-z) will help here as dest filesystem is mounted via samba (compress is maily for network performance when rsync is talking to a remote rsync server). Also I included the -r flag as original cp command was doing sub-directories.

Yeah, I always put that z in there by habit. Good thinking with the --dry-run.

-a is the same as -rlptgoD so -r isn't needed.

1 Like

Thanks Nevyn, I always added -r with -a (Lucky I never needed to copy all the files in a directory without sub dirs, or I would have found my misunderstanding at the expense of syncing a heap of files I didn't want to!)

wow, thank you both for your input. i was going to look at rsync, but wasnt sure

couple of questions, explain how the -del or -delete works. is that the part that will remove a file from the dest if it has been removed from source? man rsync says -del and -delete are a bit different

if i have symlinks in certain directories that point to files that are being synced anyway do i still use the -l

---------- Post updated at 08:53 PM ---------- Previous update was at 08:32 PM ----------

think i answered my own questions

--ignore-existing will not sync the file if it exists on the dest, but will it check to see if the file has changed first before it skips it?

The difference between -del and -delete is just when the delete is done, helps with files that are linked to, it really comes into play on busy destination systems where directories are being monitored and the like.

Do you have links pointing outside the Videos tree?
You might be best in using --copy-unsafe-links anyway just incase a link outside the tree is created in the future.

--ignore-existing does what the description says - if file exists on the dest it's ignored (It will not change the dest file even if the timestamp/size/content/permissions are different)

thanks! ok, im copying mail folders from thunderbird so inbox files will be changing, rsync will simply overwrite the file? i noticed on several trial runs on the Videos dir that it copied each file on the first go around, but when i ran rsync again on the same files it just skipped them

i dont want that to happen. if a file has changed i need to copy it to the dest share and if it has not, then ignore it. kind of like an incremental backup

as for the symlinks, i got it backwards, the links are in directories that will not be copied. the hard files will

Simple, just don't use the --ignore-existing flag and rsync will compare the two files and only copy when they are different.

Stick with --copy-unsafe-links then.