Move files from multiple folders to multiple folders in Linux - bash

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

Related

Using Ansible to move subdirectories and files in a given path to a parent

I need to move files from /var/www/html/internal/packages.confluent.io/rpm/5.5 to /var/www/html/internal
I am using the below script to perform the move. I also trtied it with command in stead of shell
The below script moves the files from under the 5.5 directory to /var/www/html/internal/5.5. I need them to be under /var/www/html/internal
- name: Move confluent packages to /var/www/html/internal
command: mv /var/www/html/internal/packages.confluent.io/rpm/5.5 /var/www/html/internal/
when: internal_stat.stat.exists
How should I formulate the command to make sure the files and subdirectories are moved under /var/www/html/internal and not /var/www/html/internal/5.5 ?
Thanks
I was doing
mv /var/www/html/internal/packages.confluent.io/rpm/5.5/*.* /var/www/html/internal/
that was not copying the subdirectories only files. It seems I have to do
mv /var/www/html/internal/packages.confluent.io/rpm/5.5/* /var/www/html/internal/.
Initially I had thought that wouldn't copy the files, only directories, but this seems to work for both files and directories. This does not move the . files, I will research that later

Moving files of a particular File Extension from multiple folders into one specific folder

I'm only started scripting a few weeks ago and have been testing a few scripts I've put together with help from some posted on this site.
Currently I'm struggling and require some help or assistance.
I want to be able to move all files with any particular/given file extension, from within any directory, and any of their sub-directories, if they exist. I just want the files, not the folders copied.
The script works, I'm hoping for anyway, as such:
You enter the directory you want to move the files from e.g. D:\TestMove
You enter the directory you want to move the files to e.g. D:\Test
You enter the file extension of all of the files that you want to be moved from the given directory, and then sub-directories eg. .txt
If the directory you're moving them to doesn't exist, it should be created.
So in my case, I've got the directory D:\Test, with multiple sub-directories, (TestMove\Test2, etc.), and within each of those directories multiple .txt files, (for example). I want to be able to move all of the .txt files from the D:\Test directory to wherever I want.
At the moment, it's just moving the files from the directory I choose, but not from within the sub-directories.
I'm happy to get this script ripped to bits so that I can learn where I'm going wrong.
set /P DMF=Please enter Directory moving from:
set /P DMT=Please enter Directory moving too:
set /P FEX=Please enter File Extension:
if not exist %DMT% mkdir %DMT%
for %%a in (%FEX%) do (
echo Moving all %%a files to "%DMT%" ...
move "%DMF%\*%%a" "%DMT%"
)

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/

CMD move files without knowing folder name

I'm using Windows CMD via an ANT Build file to move files from a sub-folder into a parent folder that is three levels up. The structure looks like this:
containingFolder
-tempFolder
-unkownNamedFolder
-contents.xyz
Using linux I can do this with the command mv */*.* .. with the current working directory being the temp folder. I don't know what the unknownNamedFolder will be called, but I do know that it will always be the only folder within the temp folder and that whatever content is within it needs to be extracted out to the containingFolder so that temp can be deleted and only the files will remain.
I've tried a command such as /c move *\*.* .. 2>NUL but this doesn't work.

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