Bash script to move all png files in folder and its subfolders to another directory?
In ~/Desktop/a/ , I have .png files, and there are also subfolders within this that also have .png files.
I'd like to move all of those .png files to another folder.
This is my code so far. It runs, but nothing is placed into the target folder. What is the problem?
#!/bin/bash
cd ~/Desktop/a/
for f in $(find . -type f -name "*.png")
do
mv $f ~/Desktop/new/
done
bash
add a comment |
In ~/Desktop/a/ , I have .png files, and there are also subfolders within this that also have .png files.
I'd like to move all of those .png files to another folder.
This is my code so far. It runs, but nothing is placed into the target folder. What is the problem?
#!/bin/bash
cd ~/Desktop/a/
for f in $(find . -type f -name "*.png")
do
mv $f ~/Desktop/new/
done
bash
2
This might help: How to debug a bash script?
– Cyrus
Nov 25 '18 at 8:25
See: Copy every file of entire directory structure into base path of another
– Cyrus
Nov 25 '18 at 8:28
I do not see why this script should fail silently. But in any case I would enclose$f
in double quotes. You may want to tryecho
instead ofmv
.
– tif
Nov 25 '18 at 8:30
for f in $(find ...)
is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.
– melpomene
Nov 25 '18 at 8:32
add a comment |
In ~/Desktop/a/ , I have .png files, and there are also subfolders within this that also have .png files.
I'd like to move all of those .png files to another folder.
This is my code so far. It runs, but nothing is placed into the target folder. What is the problem?
#!/bin/bash
cd ~/Desktop/a/
for f in $(find . -type f -name "*.png")
do
mv $f ~/Desktop/new/
done
bash
In ~/Desktop/a/ , I have .png files, and there are also subfolders within this that also have .png files.
I'd like to move all of those .png files to another folder.
This is my code so far. It runs, but nothing is placed into the target folder. What is the problem?
#!/bin/bash
cd ~/Desktop/a/
for f in $(find . -type f -name "*.png")
do
mv $f ~/Desktop/new/
done
bash
bash
asked Nov 25 '18 at 8:22
user10701455user10701455
152
152
2
This might help: How to debug a bash script?
– Cyrus
Nov 25 '18 at 8:25
See: Copy every file of entire directory structure into base path of another
– Cyrus
Nov 25 '18 at 8:28
I do not see why this script should fail silently. But in any case I would enclose$f
in double quotes. You may want to tryecho
instead ofmv
.
– tif
Nov 25 '18 at 8:30
for f in $(find ...)
is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.
– melpomene
Nov 25 '18 at 8:32
add a comment |
2
This might help: How to debug a bash script?
– Cyrus
Nov 25 '18 at 8:25
See: Copy every file of entire directory structure into base path of another
– Cyrus
Nov 25 '18 at 8:28
I do not see why this script should fail silently. But in any case I would enclose$f
in double quotes. You may want to tryecho
instead ofmv
.
– tif
Nov 25 '18 at 8:30
for f in $(find ...)
is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.
– melpomene
Nov 25 '18 at 8:32
2
2
This might help: How to debug a bash script?
– Cyrus
Nov 25 '18 at 8:25
This might help: How to debug a bash script?
– Cyrus
Nov 25 '18 at 8:25
See: Copy every file of entire directory structure into base path of another
– Cyrus
Nov 25 '18 at 8:28
See: Copy every file of entire directory structure into base path of another
– Cyrus
Nov 25 '18 at 8:28
I do not see why this script should fail silently. But in any case I would enclose
$f
in double quotes. You may want to try echo
instead of mv
.– tif
Nov 25 '18 at 8:30
I do not see why this script should fail silently. But in any case I would enclose
$f
in double quotes. You may want to try echo
instead of mv
.– tif
Nov 25 '18 at 8:30
for f in $(find ...)
is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.– melpomene
Nov 25 '18 at 8:32
for f in $(find ...)
is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.– melpomene
Nov 25 '18 at 8:32
add a comment |
2 Answers
2
active
oldest
votes
I guess that these image filenames maybe include spaces or other special characters.
find ~/Desktop/a/ -type f -name "*.png" -exec mv "{}" ~/Desktop/new/ ;
or
find ~/Desktop/a/ -type f -name "*.png" -print0 | xargs -0 -I{} mv "{}" ~/Desktop/new/
add a comment |
If your bash is new enough, you can also use globstar
:
cd ~/Desktop/a || exit 1
shopt -s globstar
mv -- **/*.png ~/Desktop/new
Or (if there are too many files to fit in a single command line):
shopt -s globstar
for f in ~/Desktop/a/**/*.png; do
mv -- "$f" ~/Desktop/new
done
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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%2fstackoverflow.com%2fquestions%2f53465806%2fbash-script-to-move-all-png-files-in-folder-and-its-subfolders-to-another-direct%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I guess that these image filenames maybe include spaces or other special characters.
find ~/Desktop/a/ -type f -name "*.png" -exec mv "{}" ~/Desktop/new/ ;
or
find ~/Desktop/a/ -type f -name "*.png" -print0 | xargs -0 -I{} mv "{}" ~/Desktop/new/
add a comment |
I guess that these image filenames maybe include spaces or other special characters.
find ~/Desktop/a/ -type f -name "*.png" -exec mv "{}" ~/Desktop/new/ ;
or
find ~/Desktop/a/ -type f -name "*.png" -print0 | xargs -0 -I{} mv "{}" ~/Desktop/new/
add a comment |
I guess that these image filenames maybe include spaces or other special characters.
find ~/Desktop/a/ -type f -name "*.png" -exec mv "{}" ~/Desktop/new/ ;
or
find ~/Desktop/a/ -type f -name "*.png" -print0 | xargs -0 -I{} mv "{}" ~/Desktop/new/
I guess that these image filenames maybe include spaces or other special characters.
find ~/Desktop/a/ -type f -name "*.png" -exec mv "{}" ~/Desktop/new/ ;
or
find ~/Desktop/a/ -type f -name "*.png" -print0 | xargs -0 -I{} mv "{}" ~/Desktop/new/
answered Nov 25 '18 at 8:33
FengFeng
1,0881811
1,0881811
add a comment |
add a comment |
If your bash is new enough, you can also use globstar
:
cd ~/Desktop/a || exit 1
shopt -s globstar
mv -- **/*.png ~/Desktop/new
Or (if there are too many files to fit in a single command line):
shopt -s globstar
for f in ~/Desktop/a/**/*.png; do
mv -- "$f" ~/Desktop/new
done
add a comment |
If your bash is new enough, you can also use globstar
:
cd ~/Desktop/a || exit 1
shopt -s globstar
mv -- **/*.png ~/Desktop/new
Or (if there are too many files to fit in a single command line):
shopt -s globstar
for f in ~/Desktop/a/**/*.png; do
mv -- "$f" ~/Desktop/new
done
add a comment |
If your bash is new enough, you can also use globstar
:
cd ~/Desktop/a || exit 1
shopt -s globstar
mv -- **/*.png ~/Desktop/new
Or (if there are too many files to fit in a single command line):
shopt -s globstar
for f in ~/Desktop/a/**/*.png; do
mv -- "$f" ~/Desktop/new
done
If your bash is new enough, you can also use globstar
:
cd ~/Desktop/a || exit 1
shopt -s globstar
mv -- **/*.png ~/Desktop/new
Or (if there are too many files to fit in a single command line):
shopt -s globstar
for f in ~/Desktop/a/**/*.png; do
mv -- "$f" ~/Desktop/new
done
answered Nov 25 '18 at 8:39
melpomenemelpomene
61.6k54994
61.6k54994
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- 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%2fstackoverflow.com%2fquestions%2f53465806%2fbash-script-to-move-all-png-files-in-folder-and-its-subfolders-to-another-direct%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
2
This might help: How to debug a bash script?
– Cyrus
Nov 25 '18 at 8:25
See: Copy every file of entire directory structure into base path of another
– Cyrus
Nov 25 '18 at 8:28
I do not see why this script should fail silently. But in any case I would enclose
$f
in double quotes. You may want to tryecho
instead ofmv
.– tif
Nov 25 '18 at 8:30
for f in $(find ...)
is basically a bug. See mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29.– melpomene
Nov 25 '18 at 8:32