Breaking up nested folders

Joined
Aug 3, 2015
Messages
2
Reaction score
0
Points
1
Hello all,

I have a folder with a lot of nested folders in it. I need to break all of these folders down and have all my files in one large folder. How can I go about doing this without doing it all manually?

Thanks
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,770
Reaction score
2,110
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Welcome to Mac-Forums..

I like questions with command line answers..so here's my answer..:)

Code:
find . -type f -exec cp {} <output folder> \;

So if you have a folder with multiple sub-folders and want all of the files from the sub-folders in a folder called <output folder>, the command above will do that for you. You can ignore the identical errors you will get.

You want to make sure that the files in these sub-folders aren't named the same thing, if they are you're going to end up overwriting some files and lose data.

To help you understand what the command is doing, here is the breakdown..

"find ." = Find all files in the current folder
"-type f" = Only find files, ignoring directories
"-exec cp {} <output folder> \;" = Execute the copy command replacing {} with each file that is found
 
OP
C
Joined
Aug 3, 2015
Messages
2
Reaction score
0
Points
1
Thank you!

Unfortunately, some of the files in these subfolders have the same name. Is there any way to make it so command line renames the files with the same names as a copy? Like someFile.jpg ---> someFile-1.jpg… or something of the sorts.

Since I'm working with thousands of files, it would be silly to manually scan through all of them and rename them...

Thanks in anticipation!
 
Joined
Jan 20, 2012
Messages
5,068
Reaction score
431
Points
83
Location
North Carolina
Your Mac's Specs
Air M2 ('22) OS 14.3; M3 iMac ('23) OS 14.3; iPad Pro; iPhone 14
Thank you!

Unfortunately, some of the files in these subfolders have the same name. Is there any way to make it so command line renames the files with the same names as a copy? Like someFile.jpg ---> someFile-1.jpg… or something of the sorts.

Since I'm working with thousands of files, it would be silly to manually scan through all of them and rename them...

Hi Chris..... - I'm only a novice @ the command line, so I'll let Ashwin post again as to the above - but just some other questions: 1) Do you have all of these files backed up on another device, e.g. external HD, flash drive, or other? and 2) Where & what are these files, i.e. personal folders, music, system files, etc? The latter could impact on your computer and/or PATH designations. Good luck - Dave :)
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,770
Reaction score
2,110
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Thank you!

Unfortunately, some of the files in these subfolders have the same name. Is there any way to make it so command line renames the files with the same names as a copy? Like someFile.jpg ---> someFile-1.jpg… or something of the sorts.

Since I'm working with thousands of files, it would be silly to manually scan through all of them and rename them...

Thanks in anticipation!

Ahh a challenge..;)

Is it expected that you have a file with the same name in multiple folders? Or is it only up to 2 folders? That determines how complicated my rename logic has to be..

OK, here is my first iteration of the script..

Code:
#!/bin/bash

if [ -z $1 ]; then
    echo "Please provide an output folder"
    exit 0
fi

OUTPUT=$1

if [ ! -e $1 ]; then
    echo "Making output folder"
    mkdir $1
fi

function renameAndCopyFile() {
    f=`basename $1`
    COUNT=1 # Start with 1
    while [ 1 ]; do
        fname=`echo $f | cut -d '.' -f1`
        ext=`echo $f | cut -d '.' -f2`
        o="$fname-$COUNT.txt"
        if [ -e $OUTPUT/$o ]; then
            COUNT=$((COUNT+1))
        else
            echo "Rename and Copying $1 as $o"
            cp $1 $OUTPUT/$o
            break
        fi  
    done
}

# Begin of main script
for file in `find . -type f`; do  
    if [ -e $OUTPUT/`basename $file` ]; then 
        echo "File exists"
        renameAndCopyFile $file
    else 
        echo "Copying $file..."
        cp $file $OUTPUT; 
    fi; 
done

You will want to put this into a file in some folder like "~/Desktop" and then from the command line do the following "chmod 755 ~/Desktop/<name of file>".

You can run the script from within the top folder of the files you want to copy as "~/Desktop/<name of file> output" where output is going to be the folder with all the files combined. The script will automatically increment the count on files that are repeated in multiple folders.

Before you do anything, ensure that you have a good backup of these files in case my script explodes..:)
 
Last edited:

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top