Shell script to move several files to a different directory - shell

I have two subdomains of a website and in both accounts one folder is there with several sub folders. It's a photo folder. In dirA.xyz.com photo folder consists of several folders and sub folders and in dirB.xyz.com photo folder consists of new photos which are not available in dirA. But the sub folder name in dirA photo folder may be the same as dirB photo folder but the final name of image will be different. So I want to move all files of dirB photo folder to dirA photo folder without deleting the dirA photo folder data.
As the name and folder structure varies and data is in millions how to move them and merge so that all data remains?
I've tried using zip. I zipped the photo folder of dirB, moved it to dirA and tried to unzip it but it's not going in dirA photo folder, it's going in dirB photo folder. I've tried changing the owner of dirB photo folder to dirA owner but it also did not work. The only thing that is working is the move command so how can I achieve this with mv?
mv /home/xyz/public_html/photo/ABC/Something/something/xyz.jpg /home/ABCDE/public_html/photo/ABC/Something/something/xyz.jpg
All files are jpeg or png.

Sounds like you need rsync, with the merge option, not overwrite. If you are using any decent and recent version of linux, you should have rsync installed by default.
source: /home/xyz/public_html/photo/ABC/Something/something/xyz.jpg
target: /home/ABCDE/public_html/photo/ABC/Something/something/xyz.jpg
rsync -avibu --ignore-existing /home/xyz/public_html/photo/ /home/ABCDE/public_html/photo
On this command I selected the top level directory randomly. You may want to modify according to your needs. Also pay attention to the trailing "/" on both source and destination. Otherwise, placement of files might be a level of directory off.
-b makes rsync backup files that exist in both folders, appending ~ to the old file. You can change the ~ suffix with --suffix string_of_your_choice
-u makes rsync transfer skip files which are newer in destination than those in source

Related

How to move a file into a subdirectory with bash?

so I'm having trouble with moving my file into a subdirectory. Right now I have a jpg file in my Downloads folder that I want to move over to my images folder, which is contained inside my Desktop and odin-links-and-images folders. Just for a bit of clarification, my Downloads folder is a completely separate directory, and I want to move the jpg file inside my Downloads folder to
Desktop/odin-links-and-images/images
What I've tried so far is doing the following command inside my Downloads directory:
mv dog.jpg images/
Doing this, I ended up getting the error:
mv: cannot move 'dog.jpg' to 'images/': Not a directory
I assume this is because my dog.jpg file is in my Downloads folder, which is a completely separate directory from my Desktop/odin-links-and-images/images directory. I'm not sure if it's possible to move my jpg file, but I'd appreciate any guidance on how to do this!
Write the complete path to images:
mv dog.jpg ~/Desktop/odin-links-and-images/images
This is assuming Desktop is in home. Otherwise, use whatever is the full path to desktop.

How to move all files within subfolders into a new folder using bash?

I have a folder that I downloaded all my fonts to. When I opened the folder i noticed they were saved out into subfolders.
Is there a bash-shell script to grab all the files within the subfolders and move them to the parent folder?
You can move files with same extensions recursively using the wildcards. e.g. if you want to move all log files in nested folders under log/ directory into backup/ directory, you can use the following command:
mv log/**/*.log backup/

Move files from multiple folders to multiple folders in Linux

How can I move files from multiple folders to another location. For example if I have 3 folders with name /test/folder1, /test/folder2, /test/folder3 and I want to move the contents of these folders into another location like /temp/folder1, /temp/folder2 /temp/folder3 using a script. I do not want to move these folders, instead I want to move the files inside these folders. Please Help
Something like
cd test
for dir in folder*; do
mv "$dir/*" "/temp/$dir"
done

ApplScript to compress folder?

What is the AppleScript to compress a folder? Not let the user select a folder from the dialog but give the folder path on a string variable. The script I know compresses the folder with the full path as folder. For example: if the folder names "ABC" is in this folder location, "Applications:Data:Level" then the compressed folder will create the folder structure like this "Applications:Data:Level:ABC" and put the content. I do not want this one. The compress ApplScript must not maintain this folder structure.
?
If you use zip, cd to the parent directory of the zipped directory first:
do shell script "cd /usr/share; zip /tmp/dict.zip -r dict"
There are no AppleScript commands for creating archives, and Archive Utility is not scriptable.

sync/copy folders from source dir only if folders exist in target dir

I'm trying to write a bash script in ubuntu to do a copy of some files.. I'm working on a small Android project, where i'm translating the apps each week. I'm VERY new to bash scripting, so please bear with me ;)
I want my script to check the target directory and see if my source directory contains the same folders. If it does, it should copy (and overwrite if needed) my source folders to the target dir, preserving the structure. But also adding whatever extra files and folders i might have within those source folders.
Let's say i have folder1, folder2, folder3 in my source dir, but only folder1 and folder2 in the target dir. Then i only need folder1 and folder2 from the source dir copied to the target dir.
The content of the target dir changes often, that's why i need the check before it copies the folders/files over.
Btw, the folders in both source and target dir are named like: folder1.apk - it has an extension so it looks like a file..
Hope i provided enough info ;)
EDIT:
I ended up doing this:
for dir in `find * -maxdepth 0 -type d`; do
cp -r -f /source/$dir /destination
done
Don't know if it's the best way, but seems to do the job ;)
You would probably take a look at rsync tool, which has lot of options and easy to use (no need to use own scripts). For example, one of the options that will be useful in your case:
--existing skip creating new files on receiver
So, the following should do the job:
rsync -vur --existing ~/project/source /mnt/target/
And one of the possible benefits that you can sync files the same way through network if you will need to or even use it as a daemon to automatically sync files.

Resources