Copy One Folder/Subfolder into Multiple Folders (Finder/Mac) - macos

I have a situation where I need to copy the same folder and subfolder into multiple parent folders. For example: I have 100 folders named from Folder_001 to Folder_100; I need a Folder "A" with a sub folder "B" put into each of those 100 folders, so they all look like this:
Folder_001
>A
>B
Folder_002
>A
>B
etc. etc.
I'm working on a Mac with Finder, etc. Is there some way to do this?
Thanks!

This is most easily done in Terminal. Suppose your 100 folders are on your Desktop, you would start the Terminal application and type this
cd Desktop && for f in Folder_*; do mkdir -p $f/A/B; done
If your 100 folders are in a folder called Freddy Frog in your HOME directory, you would do
cd "Freddy Frog" && for f in Folder_*; do mkdir -p $f/A/B; done
If you don't normally use Terminal, just hit Cmd+Spacebar and start typing Terminal till it guesses correctly and then hit Enter.

Related

Copy files to their corresponding destination folders

I have many files in a folder with different extensions (.txt, .ascii, .mat). I want to move them to the destination folder which would be same as file names.
For example:
I have files like a.txt, a.ascii, b.txt, b.ascii, b.mat.
I want to make folder first in the name of a and b, then I want to move files a.txt,a.ascii to folder a and b.txt,b.ascii,b.mat to folder b.
I tried the code as follows. However I need an automatic way to make folder and move the files to it.
#!/bin/sh
mkdir a b
for file in $(<list.txt)
do
cp "$file" a
done
Seems that this Bash script can do the job
#!/usr/bin/env bash
for file in $(<list.txt); do
dirn="${file%.*}"
mkdir -p "$dirn"
cp "$file" "$dirn"
done

Renaming files after folder name

I need to rename multiple files after the folder that they are in using automator. e.g. index.html renamed to folder1.html
https://imgur.com/a/Cjgkn3V
I have about 900 folders with one file in them each all named index.html.
This is straight forward as using Terminal:
Open the Terminal app
type in "cd " (note the space) and then drag the folder containing all the folders on to the terminal window. Then press return.
Paste this in the terminal window and press enter:
for f in `ls -1`; do
if [[ -f $f/$index.html ]]; then
cp $f/index.html $f/$f.html;
fi;
done
Note that I am using the cp command here so it copies the index.html file instead of moving it. Just incase... :)
You can add this to a 'Run Shell Script' action in Automator. Just need to make sure your automator script is saved in the folder with all the sub folders.

recreate folder structure Osx

I currently have a set of folders that I need to duplicate the structure into another folder.
I currently work with photos that are dumped into a Dump folder in groups.
Eg.
Photo Dump
Group1
Group2 etc
I would like to have a script to recreate these folders without the files to the good folder so that I don't have to recreate them manually
Any ideas?
Nathan
If I understand correctly, you want to copy the parent folder and all of its subfolders, but none of the files contained therein. There might be a simpler way, but I just threw together this Terminal command (which should also work on Linux or anywhere else with Bash):
ls -R | grep :$ | sed 's/\.\/\(.*\):$/\1/' | \
while read thisFolder; do mkdir -p "destination"/"$thisFolder"; done
It will copy the folder structure of all folders in the current directory into a folder called "destination"; you can of course change this to any path you wish, e.g. ~/Desktop/"Folder Copies" or whatever.
Take care to first "cd" into whatever directory contains the folder tree you want to duplicate, because if you run it as soon as you open the terminal, you'll wind up with a replication of your entire home folder directory structure, including the many contained within Library.
I found this to be a tad clearer:
find 'Photo Dump' -type d | sed -e 's:^Photo Dump:destination:g' | sort | xargs mkdir
find 'Photo Dump' -type d -> List all folders in "Photo Dump"
sed -e 's:^Photo Dump:destination:g' - Since all folders listed in the above step will start with Photo Dump/..., we can just replace the beginning with the folder we want to copy the structure to (in this case I called it destination)
sort - Sorts results. This is required so that the parent folders are created before the children
xargs mkdir - Passes all the results from above into mkdir so it can create the folders

Bash script for taking a screenshot, renaming and moving

First bash script and I'm running into some issues. I want to take a screenshot, then change the name of the .png to a random number (so that pictures don't overwrite). After it's renamed I want to move the picture to my dropbox folder.
This is what I've got:
#!/bin/bash
#Take screenshot
import -window root $HOME/screenshot.png
#Move to dropbox folder
mv $HOME/screenshot.png $HOME/Dropbox/Max-Max/$RANDOM.png
When I run it dropbox is getting some kind of something because my taskbar icon indicates a file transfer. When I open up the folder however, nothing's there.
Thanks for the help.
Instead of $RANDOM use $(date|tr " :" _)
Much more useful
You can do that with scrot like this:
scrot -e 'mv $f ~/Dropbox/Max-Max'
But your script looks fine... Try to create an empty file first to make sure your dropbox functions fine.
echo > ~/Dropbox/Max-Max/testfile
The commands you're using are correct. The only way it could fail is if Max-Max doesn't exist. mv moves and renames files among existing directories -- mv cannot create directories.

Finding files with bash and copy to another location and reducing depth of folders

I'm trying to recover a mates hard drive, there is no structure what so ever so music and images are everywhere but in named folders sometimes >5 folders deep, I've managed to write a one-liner that finds the files and copies them to a mounted drive but it preserves the file structure completely. What I'm after is a bit of code that searches the drive and copies to another location and copies just the parent folder with the mp3/jpg files within and not the complete path. The other issue I have is the music is /folder/folder/folder/Artist/1.mp3..2.mp3..10.mp3 etc etc so I have to preserve the folder 'Artist' to give him any hope of finding his tracks again.
What I have working currently:
find /media/HP/ -name *.mp3 -fprintf /media/HP/MUSIC/Script.sh 'mkdir -p "/media/HP/MUSIC/%h" \n cp "%h/%f" "/media/HP/MUSIC/%h/"\n'
I then run the script.sh and it does all the copying.
Many Thanks
What you probably want to do will be along the lines of:
mkdir "$dest/$(basename $(dirname $source))"
OK folks - thanks for the input it did make me think deeper about this and I've come up with a result with the help of a colleague (thanks SiG):
This one-liner finds the files, and writes a script file to run separately but does copy across just the last folder as I wanted initially.
The Code:
find /some/folder/ -name *.mp3 | awk 'BEGIN{FS="/"}{print "mkdir -p \"/some/new/place/" $(NF-1) "\"\ncp -v -p \"" $0 "\" \"/some/new/place/" $(NF-1) "/" $NF "\""}' > script.sh
The output is:
mkdir -p "/media/HP/MUSIC/Satize" cp -v -p "/media/HP/Users/REBEKAH/Music/Satize/You Don't Love Me.mp3" "/media/HP/MUSIC/Satize/You Don't Love Me.mp3"
When script.sh is run it does all the work and I end up with a very reduced file structure I can copy to a new drive.
Thanks again folks much appreciated.
KjF
If you are doing the operation recursively ( entering directory by directory ), what you can do is everytime save your path as: Road_Dir=$(pwd)(let's say dir1/dir2/dir3/)
Then you detect your artist_name directory you save it is Music_Dir=$(pwd)
Finally you could extract your artist_name directory with the simple command:
Final_Dir=${Music_Dir##$Road_Dir/} (wich means take out the string $Road_Dir/ from $Music_Dir beginning from the left of $Music_Dir)
With this Final_Dir will contain artist_name, and you can copy your music file as $Final_Dir/Music.mp3 ...

Resources