0

I have a LARGE amount of files in the following structure all files in /base/, then 4 folders with 2 "random" letters, and then a series of files related to each other.

Example:

/base/ab/12/13/37/file1.txt
/base/ab/12/13/37/file2.txt
/base/ab/12/13/37/file3.txt
/base/cd/b8/e2/a1/other1.txt
.... 
/base/cd/b8/e2/a1/other52.txt
/base/af/f3/45/9e/third1.txt
/base/af/f3/45/9e/third2.txt

etc

I want to keep most of the structure, but add one ADDITIONAL (extra_folder) folder at the end, in which my files belong. Such that the above is changed to:

/base/ab/12/13/37/extra_folder/file1.txt
/base/ab/12/13/37/extra_folder/file2.txt
/base/ab/12/13/37/extra_folder/file3.txt
/base/cd/b8/e2/a1/extra_folder/other1.txt

I expect that I will need a shell script and the move command.

Thank you very much.

  • 1
    Yes, you'll need a script. You could try to do something, probably based on for, sed and/or find,and get back to us should you have any problem with it? – Shlublu Oct 11 '17 at 6:23
0

This is what we start with:

$ tree base/
base/
|-- ab
|   `-- 12
|       `-- 13
|           `-- 37
|               |-- file1.txt
|               |-- file2.txt
|               `-- file3.txt
|-- af
|   `-- f3
|       `-- 45
|           `-- 9e
|               |-- third1.txt
|               `-- third2.txt
`-- cd
    `-- b8
        `-- e2
            `-- a1
                |-- other1.txt
                `-- other52.txt

12 directories, 7 files

First we add the new directories:

$ find base -type d -mindepth 4 -maxdepth 4 -exec mkdir {}/extra_folder ';'

We use both -mindepth 4 and -maxdepth 4 to create new directories on level four only. Without the -mindepth 4 we would get new directories on higher levels, and without -maxdepth 4 the new directories would themselves be filled with new directories until the pathnames became so long that find no longer was able to create more.

The extra_folder directory is created with mkdir called from -exec. Now we have

$ tree base/
base/
|-- ab
|   `-- 12
|       `-- 13
|           `-- 37
|               |-- extra_folder
|               |-- file1.txt
|               |-- file2.txt
|               `-- file3.txt
|-- af
|   `-- f3
|       `-- 45
|           `-- 9e
|               |-- extra_folder
|               |-- third1.txt
|               `-- third2.txt
`-- cd
    `-- b8
        `-- e2
            `-- a1
                |-- extra_folder
                |-- other1.txt
                `-- other52.txt

15 directories, 7 files

Then we'll move the files down:

$ find base -maxdepth 5 -type f -execdir mv {} extra_folder ';'

This looks for any regular file in or under the base directory (I'm assuming there are files only on the lowest level) that are on level five. It then uses -execdir to run the mv command inside the directory where the found file is located ({} will be the basename of the found file).

We end up with

$ tree base/
base/
|-- ab
|   `-- 12
|       `-- 13
|           `-- 37
|               `-- extra_folder
|                   |-- file1.txt
|                   |-- file2.txt
|                   `-- file3.txt
|-- af
|   `-- f3
|       `-- 45
|           `-- 9e
|               `-- extra_folder
|                   |-- third1.txt
|                   `-- third2.txt
`-- cd
    `-- b8
        `-- e2
            `-- a1
                `-- extra_folder
                    |-- other1.txt
                    `-- other52.txt

15 directories, 7 files

In one go:

$ find base -type f \
    -execdir sh -c '[ ! -d "$1" ] && mkdir "$1"; mv "$2" "$1"' sh 'extra_folder' {} ';'

This finds all regular files and moves them into a directory called extra_folder regardless of where they are to start with. Running this command multiple time will move them further and further down.

The mini-script that is called by -execdir:

[ ! -d "$1" ] && mkdir "$1"
mv "$2" "$1"

This will be called with the folder name as $1 and with the filename as $2 and will create the folder if it doesn't exist and then move the file into it.

  • I tested the first commands. Has to be done in two steps, but they appear to work :) Thank you. – Nixxon Oct 14 '17 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.