ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What you're doing it's really dangerous... have you a backup of the Pictures folder? What if instead of a bash error (that causes the command to abort) it brings to an unpredictable result? Again, keep it simple...
Given that:
1) none of the mkdirs stated by Nominal should give an -- i option
2) Nominal's commands are quoted thoroughly
I expect you're not putting all the quotes in there, they're very important, otherwise you can get bad and unpredictable results.
Otherwise Nominal's commands are safe and not dangerous at all; in fact very simple as it's all in one command that suits all.
To test out the commands you actually get, you can try to put an "echo" in front of them (so mkdir ... becomes echo mkdir ...) and see if you get results that are not so logical... Again, I expect a few missing quotes around {}.
I expect you're not putting all the quotes in there, they're very important, otherwise you can get bad and unpredictable results.
Otherwise Nominal's commands are safe and not dangerous at all; in fact very simple as it's all in one command that suits all.
Have you tested it? I tried the exact command suggested by Nominal Animal and then I applied your modification: I got the same results (errors) as the OP.
while read src
do
dst=${src/Pictures/Resized}
echo mkdir -p $(dirname $dst)
echo mv $src $dst
done < <(find /home/rjo98/Pictures -name \*a.jpg)
I'm trying to modify this, would i make it like this? I'm so confused right now. My pictures folder is /Pictures and i want them to go to /Resized if they are *A.jpg
while read src
do
dst=${src/Resized}
echo mkdir -p $(dirname $dst)
echo mv $src $dst
done < <(find /Pictures -name \*A.jpg)
I'm trying to modify this, would i make it like this? I'm so confused right now. My pictures folder is /Pictures and i want them to go to /Resized if they are *A.jpg
while read src
do
dst=${src/Resized}
echo mkdir -p $(dirname $dst)
echo mv $src $dst
done < <(find /Pictures -name \*A.jpg)
Nope. The statement
Code:
${src/Pictures/Resized}
is a parameter substitution to replace the string Pictures with the string Resized in the src variable. You have to leave it untouched. The following should work:
Code:
while read src
do
dst=${src/Pictures/Resized}
echo mkdir -p $(dirname $dst)
echo mv $src $dst
done < <(find /Pictures -name \*A.jpg)
Suppose the find command gives the only and unique result:
Indeed the substitution assumes that the two directories Pictures and Resized have the same path. Suppose you have a string like this:
Code:
/home/user/Pictures/something/some.jpg
and you want to transform it to
Code:
/home/user/Resized/something/some.jpg
if the original string is assigned to a variable (called src), you can replace the substring Pictures with the string Resized using
Code:
${src/Pictures/Resized}
that is by using the following syntax:
Code:
${variable/pattern/replacement}
Obviously if the two directories are not in the same path, you can try a slightly different approach. For example:
Code:
while read src
do
dst=/i/want/them/here"${src/*Pictures/}"
mkdir -p "$(dirname "$dst")"
mv "$src" "$dst"
done < <(find /here/are/the/Pictures -name \*a.jpg)
In this case the replacement string in the substitution is null (that is it removes the first part of the path until the Pictures directory). Then the new path is prefixed to the result. This avoids confusion between the slashes used as delimiters in the substitution syntax and the slashes contained in the destination path.
#!/bin/bash
RESIZE='Pictures/Resize'
ORIGINAL='Pictures/Original'
while read -r FILEPATH FILE
do
if [[ $FILE =~ a.jpg ]]
then
TMP="$RESIZE${FILEPATH#*Pictures}"
[[ -d $TMP ]] || mkdir -p "$TMP"
else
TMP="$ORIGINAL${FILEPATH#*Pictures}"
[[ -d $TMP ]] || mkdir -p "$TMP"
fi
cp -v "$FILEPATH/$FILE" "$TMP/$FILE"
done< <(find Pictures/ -type f -iname '*[0-9a].jpg' -printf "%h %f\n")
This is presumed to being run from the directory above your Pictures directory. If you wish to make it a little more flexible you could force the user to enter the directory path
and set it within the script, like:
Code:
if (( $# != 1 ))
then
echo "Usage: $0 <path to files>"
exit 1
else
DIR="$1"
fi
<snip>
done< <(find "$DIR" -type f -iname '*[0-9a].jpg' -printf "%h %f\n")
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.