How to move many different sub folders one level down?

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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.
linux shell-script directory move
add a comment |Â
up vote
0
down vote
favorite
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.
linux shell-script directory move
1
Yes, you'll need a script. You could try to do something, probably based onfor,sedand/orfind,and get back to us should you have any problem with it?
â Shlublu
Oct 11 '17 at 6:23
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
linux shell-script directory move
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.
linux shell-script directory move
linux shell-script directory move
asked Oct 11 '17 at 6:05
Nixxon
1051
1051
1
Yes, you'll need a script. You could try to do something, probably based onfor,sedand/orfind,and get back to us should you have any problem with it?
â Shlublu
Oct 11 '17 at 6:23
add a comment |Â
1
Yes, you'll need a script. You could try to do something, probably based onfor,sedand/orfind,and get back to us should you have any problem with it?
â Shlublu
Oct 11 '17 at 6:23
1
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
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
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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
add a comment |Â
up vote
0
down vote
accepted
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
add a comment |Â
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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.
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.
edited Oct 11 '17 at 7:10
answered Oct 11 '17 at 6:56
Kusalananda
105k14209326
105k14209326
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
add a comment |Â
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
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
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f397349%2fhow-to-move-many-different-sub-folders-one-level-down%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Yes, you'll need a script. You could try to do something, probably based on
for,sedand/orfind,and get back to us should you have any problem with it?â Shlublu
Oct 11 '17 at 6:23