A folder flattening script for your files. Takes files and folders in a location & makes the folder tree less deep (if files have same name it will rename a file so that you dont lose any files). can make it even have no subfolders (SS=1). This can be useful to reorganize your media folders (pictures, videos, documents, etc). Dont ever run this on a folder where applications exist as applications rely on their folder structures.

First lets see an example:

So if you flatten the following to keep 1 subfolder deep (SS=2). The subfolder name that is created will be the folder/path right before (to the left) of the filename.

 Here are the details:

First get into the SRC Directory, then Read the comments, and Set the Variables (I recommend running DRY first to see what happens by setting DRY=1 and then when satisfied run with DRY=0 so that the copies happen)

Then once the script below looks good copy paste it into the bash.

Script:

CopyPasteable Script. Source is Current Working Directory. Destination is DSTF variable. SS is the level of folders (1 is no subfolders, 2 is onelayer of subfolders). So cd into the source directory where all of the files & folders that you want flattened & then set the DSTF variable & SS variable (by default its 1 so that everything just goes to your DSTF folder, and if thats what you want keep SS the same).

The end.

Note you can make your own edits to make this, executable as shell script, instead of a copy paste (I just use copy pastes alot in my day 2 day linuxing)

Note (requirements) this is tested with Bash4, but im sure it works with Bash3. also you need commands like basename (to get the filename), dirname (to get the dirname) and awk (which processes the new file name).

 

Leave a Reply

Your email address will not be published. Required fields are marked *