Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How can I copy (would rather just move) files from one directory to another and NOT have it prompt me if I want to over write. I want the answer to always be no, so skip all files that already exist and only move those that do not.
I've tried the command
mv -u files destination
However, it STILL asks me if I want to overwrite the files or not. With 1000+ files that I move, I do not want to have to wait and answer "no" to over 90% of them.
However, I can't just overwrite the existing files.
The BEST way I would like to do this would be to;
if file exists in destination, delete source
if file doesn't exist in destination, move to destination
How can I copy (would rather just move) files from one directory to another and NOT have it prompt me if I want to over write. I want the answer to always be no, so skip all files that already exist and only move those that do not.
I've tried the command
mv -u files destination
However, it STILL asks me if I want to overwrite the files or not. With 1000+ files that I move, I do not want to have to wait and answer "no" to over 90% of them.
However, I can't just overwrite the existing files.
The BEST way I would like to do this would be to;
if file exists in destination, delete source
if file doesn't exist in destination, move to destination
You can use "rsync" to do this...It will only copy the files that don't exist on the destination...something like this...
OR you can do it via a script...you have to test to see if the file exists and only move if it dosen't...something like...
Code:
#!/bin/bash
#
for file in $(ls -1 /path/to/source/)
do
if [ ! -f /path/to/destination/${file} ] ; then
mv /path/to/source/${file} /path/to/destination/
fi
done
But your results may vary depending what you want to do...
#!/bin/bash
#
for file in $(ls -1 /path/to/source/)
do
if [ ! -f /path/to/destination/${file} ] ; then
mv /path/to/source/${file} /path/to/destination/
fi
else
rm -Rf /path/to/source/${file}
done
Well what I mean is, if the file does already exist...what happens to the source?
I added that so that it would delete the file if it does exist on the destination.
Then it would look something like this...
Code:
#!/bin/bash
#
for file in $(ls -1 /path/to/source/)
do
if [ ! -f /path/to/destination/${file} ] ; then
mv /path/to/source/${file} /path/to/destination/
fi
rm -Rf /path/to/source/${file}
done
FYI, that's a number one in the ls command...NOT an l
I know it's bad, very bad. However, I have to run this script as root. Therefore when it moves the files, it gives the ownership to root instead of the user that should own it.
Reason why this is, is because there are two different users that I am moving files between and can't get the groups to work because it messes up cPanel.
So how would I tell it to then chown user:user the files it moves?
I know it's bad, very bad. However, I have to run this script as root. Therefore when it moves the files, it gives the ownership to root instead of the user that should own it.
Reason why this is, is because there are two different users that I am moving files between and can't get the groups to work because it messes up cPanel.
So how would I tell it to then chown user:user the files it moves?
That gets a little tricky...You would have to do somthing like this...
Code:
#!/bin/bash
#
ls -l /path/to/source/ | awk '{print $3, $4, $NF}' | while read user group file
do
if [ ! -f /path/to/destination/${file} ] ; then
mv /path/to/source/${file} /path/to/destination/
chown ${user}:${group} /path/to/destination/${file}
fi
rm -Rf /path/to/source/${file}
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.