Move files two directories up & delete directories afterwards, using wildcards
I have a folder structure like the below.
1/data/f4aa9d39291bc37a7cacedfab656a941/x1.jpg
The first slot and the random string in the third slot vary depending on the folder. (Let's say for this example, I have 30 folders at that top level, in an incrementing fashion.)
I need to move the JPGs up two levels, so it's using this folder structure and deletes that data folder in each directory as it goes along:
1/x1.jpg
I searched for any examples that could help, but I think because of the wildcard aspect I'm running into an issue. I had tried the below (note in the code I went one level up), but it didn't work.
find 1/*/data/*/. -name '*.jpg' -exec cp {} 1/* ;
Any help would be greatly appreciated. Combining both elements would be ideal, but I'm alright with help just for the non-deletion portion as I probably can write up a separate deletion script if I needed to.
(I'm using macOS's Terminal, if that affects anything code-wise.)
linux bash terminal terminal.app
add a comment |
I have a folder structure like the below.
1/data/f4aa9d39291bc37a7cacedfab656a941/x1.jpg
The first slot and the random string in the third slot vary depending on the folder. (Let's say for this example, I have 30 folders at that top level, in an incrementing fashion.)
I need to move the JPGs up two levels, so it's using this folder structure and deletes that data folder in each directory as it goes along:
1/x1.jpg
I searched for any examples that could help, but I think because of the wildcard aspect I'm running into an issue. I had tried the below (note in the code I went one level up), but it didn't work.
find 1/*/data/*/. -name '*.jpg' -exec cp {} 1/* ;
Any help would be greatly appreciated. Combining both elements would be ideal, but I'm alright with help just for the non-deletion portion as I probably can write up a separate deletion script if I needed to.
(I'm using macOS's Terminal, if that affects anything code-wise.)
linux bash terminal terminal.app
add a comment |
I have a folder structure like the below.
1/data/f4aa9d39291bc37a7cacedfab656a941/x1.jpg
The first slot and the random string in the third slot vary depending on the folder. (Let's say for this example, I have 30 folders at that top level, in an incrementing fashion.)
I need to move the JPGs up two levels, so it's using this folder structure and deletes that data folder in each directory as it goes along:
1/x1.jpg
I searched for any examples that could help, but I think because of the wildcard aspect I'm running into an issue. I had tried the below (note in the code I went one level up), but it didn't work.
find 1/*/data/*/. -name '*.jpg' -exec cp {} 1/* ;
Any help would be greatly appreciated. Combining both elements would be ideal, but I'm alright with help just for the non-deletion portion as I probably can write up a separate deletion script if I needed to.
(I'm using macOS's Terminal, if that affects anything code-wise.)
linux bash terminal terminal.app
I have a folder structure like the below.
1/data/f4aa9d39291bc37a7cacedfab656a941/x1.jpg
The first slot and the random string in the third slot vary depending on the folder. (Let's say for this example, I have 30 folders at that top level, in an incrementing fashion.)
I need to move the JPGs up two levels, so it's using this folder structure and deletes that data folder in each directory as it goes along:
1/x1.jpg
I searched for any examples that could help, but I think because of the wildcard aspect I'm running into an issue. I had tried the below (note in the code I went one level up), but it didn't work.
find 1/*/data/*/. -name '*.jpg' -exec cp {} 1/* ;
Any help would be greatly appreciated. Combining both elements would be ideal, but I'm alright with help just for the non-deletion portion as I probably can write up a separate deletion script if I needed to.
(I'm using macOS's Terminal, if that affects anything code-wise.)
linux bash terminal terminal.app
linux bash terminal terminal.app
asked Dec 29 '18 at 9:23
ScherisScheris
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
If your directory structure is
1/data/f4aa9d39291bc37a7cacedfab656a941/x1.jpg
and only data
is a fixed string, then wildcards in the find command would be
find */data/* -name '*.jpg'
Run that command and see if the output is what you expect.
Running a batch script or command to delete files or directories is always dangerous.
This command will only move the files to the parent directory two levels above.
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};echo "mv -v $path/$name ${path%%/*}"' ;
Run that and see if the output appears ok to you. It will only print the command line without actually execute it.
To really run the command, erase the echo
command and the double quotes
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};mv -v $path/$name ${path%%/*}' ;
To delete empty directories, the command would be like this, a little more safe since rmdir
only deletes empy directories.
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};echo "mv -v $path/$name ${path%%/*} && rmdir -v $path && rmdir -v ${path%/*}"' ;
That command worked perfectly, thank you so much! (At first it didn't run and I was confused until I saw the .txt --- just had to change it to .jpg and it worked without a hitch.)
– Scheris
Dec 30 '18 at 4:02
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1388695%2fmove-files-two-directories-up-delete-directories-afterwards-using-wildcards%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If your directory structure is
1/data/f4aa9d39291bc37a7cacedfab656a941/x1.jpg
and only data
is a fixed string, then wildcards in the find command would be
find */data/* -name '*.jpg'
Run that command and see if the output is what you expect.
Running a batch script or command to delete files or directories is always dangerous.
This command will only move the files to the parent directory two levels above.
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};echo "mv -v $path/$name ${path%%/*}"' ;
Run that and see if the output appears ok to you. It will only print the command line without actually execute it.
To really run the command, erase the echo
command and the double quotes
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};mv -v $path/$name ${path%%/*}' ;
To delete empty directories, the command would be like this, a little more safe since rmdir
only deletes empy directories.
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};echo "mv -v $path/$name ${path%%/*} && rmdir -v $path && rmdir -v ${path%/*}"' ;
That command worked perfectly, thank you so much! (At first it didn't run and I was confused until I saw the .txt --- just had to change it to .jpg and it worked without a hitch.)
– Scheris
Dec 30 '18 at 4:02
add a comment |
If your directory structure is
1/data/f4aa9d39291bc37a7cacedfab656a941/x1.jpg
and only data
is a fixed string, then wildcards in the find command would be
find */data/* -name '*.jpg'
Run that command and see if the output is what you expect.
Running a batch script or command to delete files or directories is always dangerous.
This command will only move the files to the parent directory two levels above.
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};echo "mv -v $path/$name ${path%%/*}"' ;
Run that and see if the output appears ok to you. It will only print the command line without actually execute it.
To really run the command, erase the echo
command and the double quotes
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};mv -v $path/$name ${path%%/*}' ;
To delete empty directories, the command would be like this, a little more safe since rmdir
only deletes empy directories.
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};echo "mv -v $path/$name ${path%%/*} && rmdir -v $path && rmdir -v ${path%/*}"' ;
That command worked perfectly, thank you so much! (At first it didn't run and I was confused until I saw the .txt --- just had to change it to .jpg and it worked without a hitch.)
– Scheris
Dec 30 '18 at 4:02
add a comment |
If your directory structure is
1/data/f4aa9d39291bc37a7cacedfab656a941/x1.jpg
and only data
is a fixed string, then wildcards in the find command would be
find */data/* -name '*.jpg'
Run that command and see if the output is what you expect.
Running a batch script or command to delete files or directories is always dangerous.
This command will only move the files to the parent directory two levels above.
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};echo "mv -v $path/$name ${path%%/*}"' ;
Run that and see if the output appears ok to you. It will only print the command line without actually execute it.
To really run the command, erase the echo
command and the double quotes
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};mv -v $path/$name ${path%%/*}' ;
To delete empty directories, the command would be like this, a little more safe since rmdir
only deletes empy directories.
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};echo "mv -v $path/$name ${path%%/*} && rmdir -v $path && rmdir -v ${path%/*}"' ;
If your directory structure is
1/data/f4aa9d39291bc37a7cacedfab656a941/x1.jpg
and only data
is a fixed string, then wildcards in the find command would be
find */data/* -name '*.jpg'
Run that command and see if the output is what you expect.
Running a batch script or command to delete files or directories is always dangerous.
This command will only move the files to the parent directory two levels above.
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};echo "mv -v $path/$name ${path%%/*}"' ;
Run that and see if the output appears ok to you. It will only print the command line without actually execute it.
To really run the command, erase the echo
command and the double quotes
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};mv -v $path/$name ${path%%/*}' ;
To delete empty directories, the command would be like this, a little more safe since rmdir
only deletes empy directories.
find */data/* -name '*.txt' -exec bash -c 'file={};path=${file%/*};name=${file##*/};echo "mv -v $path/$name ${path%%/*} && rmdir -v $path && rmdir -v ${path%/*}"' ;
answered Dec 29 '18 at 22:31
PauloPaulo
57628
57628
That command worked perfectly, thank you so much! (At first it didn't run and I was confused until I saw the .txt --- just had to change it to .jpg and it worked without a hitch.)
– Scheris
Dec 30 '18 at 4:02
add a comment |
That command worked perfectly, thank you so much! (At first it didn't run and I was confused until I saw the .txt --- just had to change it to .jpg and it worked without a hitch.)
– Scheris
Dec 30 '18 at 4:02
That command worked perfectly, thank you so much! (At first it didn't run and I was confused until I saw the .txt --- just had to change it to .jpg and it worked without a hitch.)
– Scheris
Dec 30 '18 at 4:02
That command worked perfectly, thank you so much! (At first it didn't run and I was confused until I saw the .txt --- just had to change it to .jpg and it worked without a hitch.)
– Scheris
Dec 30 '18 at 4:02
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1388695%2fmove-files-two-directories-up-delete-directories-afterwards-using-wildcards%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown