Bash script to move files into folder based on matching ID numbers?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
On ~/Desktop/a/ , I have files and folders with this pattern:
500 photodir
Photo 500.jpg
1000 photodir
Photo 1000.jpg
I would like to have the .jpgs moved into their folders:
500 photodir/Photo 500.jpg
1000 photodir/Photo 1000.jpg
This is the code so far:
!#/bin/bash/
for f in ~/Desktop/a/*.jpg
do
base=“$f%Photo*”
mv "$f" "$base/"
mv "$sub/$f"* "$base/$sub/"
done
bash
New contributor
add a comment |
up vote
0
down vote
favorite
On ~/Desktop/a/ , I have files and folders with this pattern:
500 photodir
Photo 500.jpg
1000 photodir
Photo 1000.jpg
I would like to have the .jpgs moved into their folders:
500 photodir/Photo 500.jpg
1000 photodir/Photo 1000.jpg
This is the code so far:
!#/bin/bash/
for f in ~/Desktop/a/*.jpg
do
base=“$f%Photo*”
mv "$f" "$base/"
mv "$sub/$f"* "$base/$sub/"
done
bash
New contributor
Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
– sla3k
2 hours ago
@sla3k , not at all.
– user10630009
1 hour ago
@sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
– user10630009
1 hour ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
On ~/Desktop/a/ , I have files and folders with this pattern:
500 photodir
Photo 500.jpg
1000 photodir
Photo 1000.jpg
I would like to have the .jpgs moved into their folders:
500 photodir/Photo 500.jpg
1000 photodir/Photo 1000.jpg
This is the code so far:
!#/bin/bash/
for f in ~/Desktop/a/*.jpg
do
base=“$f%Photo*”
mv "$f" "$base/"
mv "$sub/$f"* "$base/$sub/"
done
bash
New contributor
On ~/Desktop/a/ , I have files and folders with this pattern:
500 photodir
Photo 500.jpg
1000 photodir
Photo 1000.jpg
I would like to have the .jpgs moved into their folders:
500 photodir/Photo 500.jpg
1000 photodir/Photo 1000.jpg
This is the code so far:
!#/bin/bash/
for f in ~/Desktop/a/*.jpg
do
base=“$f%Photo*”
mv "$f" "$base/"
mv "$sub/$f"* "$base/$sub/"
done
bash
bash
New contributor
New contributor
edited 59 mins ago
Sparhawk
8,72663789
8,72663789
New contributor
asked 2 hours ago
user10630009
32
32
New contributor
New contributor
Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
– sla3k
2 hours ago
@sla3k , not at all.
– user10630009
1 hour ago
@sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
– user10630009
1 hour ago
add a comment |
Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
– sla3k
2 hours ago
@sla3k , not at all.
– user10630009
1 hour ago
@sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
– user10630009
1 hour ago
Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
– sla3k
2 hours ago
Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
– sla3k
2 hours ago
@sla3k , not at all.
– user10630009
1 hour ago
@sla3k , not at all.
– user10630009
1 hour ago
@sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
– user10630009
1 hour ago
@sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
– user10630009
1 hour ago
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
#!/bin/bash
cd ~/Desktop/a/
for f in *.jpg; do
target_part="$f%.jpg"
target="$target_part#Photo "
mv "$f" "$target photodir"
done
Explanation
There are two parameter substitutions occurring here. Firstly, target_part="$f%.jpg"
removes the trailing .jpg
from the filename, then target="$target_part#Photo "
removes the Photo
(+ space) at the front.
Then, you merely move the file to the number plus photodir
, i.e. mv "$f" "$target photodir"
.
Further information
There were a few things wrong with your script. Firstly, the shebang was wrong. Instead of !#
, it should be #!
.
Secondly, I'm not sure if just a typo, but the double quote should be with "
, not “
.
I'm also not sure why the *
in your parameter substitution of $f%Photo*
, nor what the variable $sub
referred to (it was never assigned).
Finally, instead of using ~/Desktop/a/*.jpg
for the loop, I preferred to cd
directly into it, to allow easier parameter substitution.
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
#!/bin/bash
cd ~/Desktop/a/
for f in *.jpg; do
target_part="$f%.jpg"
target="$target_part#Photo "
mv "$f" "$target photodir"
done
Explanation
There are two parameter substitutions occurring here. Firstly, target_part="$f%.jpg"
removes the trailing .jpg
from the filename, then target="$target_part#Photo "
removes the Photo
(+ space) at the front.
Then, you merely move the file to the number plus photodir
, i.e. mv "$f" "$target photodir"
.
Further information
There were a few things wrong with your script. Firstly, the shebang was wrong. Instead of !#
, it should be #!
.
Secondly, I'm not sure if just a typo, but the double quote should be with "
, not “
.
I'm also not sure why the *
in your parameter substitution of $f%Photo*
, nor what the variable $sub
referred to (it was never assigned).
Finally, instead of using ~/Desktop/a/*.jpg
for the loop, I preferred to cd
directly into it, to allow easier parameter substitution.
add a comment |
up vote
0
down vote
#!/bin/bash
cd ~/Desktop/a/
for f in *.jpg; do
target_part="$f%.jpg"
target="$target_part#Photo "
mv "$f" "$target photodir"
done
Explanation
There are two parameter substitutions occurring here. Firstly, target_part="$f%.jpg"
removes the trailing .jpg
from the filename, then target="$target_part#Photo "
removes the Photo
(+ space) at the front.
Then, you merely move the file to the number plus photodir
, i.e. mv "$f" "$target photodir"
.
Further information
There were a few things wrong with your script. Firstly, the shebang was wrong. Instead of !#
, it should be #!
.
Secondly, I'm not sure if just a typo, but the double quote should be with "
, not “
.
I'm also not sure why the *
in your parameter substitution of $f%Photo*
, nor what the variable $sub
referred to (it was never assigned).
Finally, instead of using ~/Desktop/a/*.jpg
for the loop, I preferred to cd
directly into it, to allow easier parameter substitution.
add a comment |
up vote
0
down vote
up vote
0
down vote
#!/bin/bash
cd ~/Desktop/a/
for f in *.jpg; do
target_part="$f%.jpg"
target="$target_part#Photo "
mv "$f" "$target photodir"
done
Explanation
There are two parameter substitutions occurring here. Firstly, target_part="$f%.jpg"
removes the trailing .jpg
from the filename, then target="$target_part#Photo "
removes the Photo
(+ space) at the front.
Then, you merely move the file to the number plus photodir
, i.e. mv "$f" "$target photodir"
.
Further information
There were a few things wrong with your script. Firstly, the shebang was wrong. Instead of !#
, it should be #!
.
Secondly, I'm not sure if just a typo, but the double quote should be with "
, not “
.
I'm also not sure why the *
in your parameter substitution of $f%Photo*
, nor what the variable $sub
referred to (it was never assigned).
Finally, instead of using ~/Desktop/a/*.jpg
for the loop, I preferred to cd
directly into it, to allow easier parameter substitution.
#!/bin/bash
cd ~/Desktop/a/
for f in *.jpg; do
target_part="$f%.jpg"
target="$target_part#Photo "
mv "$f" "$target photodir"
done
Explanation
There are two parameter substitutions occurring here. Firstly, target_part="$f%.jpg"
removes the trailing .jpg
from the filename, then target="$target_part#Photo "
removes the Photo
(+ space) at the front.
Then, you merely move the file to the number plus photodir
, i.e. mv "$f" "$target photodir"
.
Further information
There were a few things wrong with your script. Firstly, the shebang was wrong. Instead of !#
, it should be #!
.
Secondly, I'm not sure if just a typo, but the double quote should be with "
, not “
.
I'm also not sure why the *
in your parameter substitution of $f%Photo*
, nor what the variable $sub
referred to (it was never assigned).
Finally, instead of using ~/Desktop/a/*.jpg
for the loop, I preferred to cd
directly into it, to allow easier parameter substitution.
edited 43 mins ago
answered 50 mins ago
Sparhawk
8,72663789
8,72663789
add a comment |
add a comment |
user10630009 is a new contributor. Be nice, and check out our Code of Conduct.
user10630009 is a new contributor. Be nice, and check out our Code of Conduct.
user10630009 is a new contributor. Be nice, and check out our Code of Conduct.
user10630009 is a new contributor. Be nice, and check out our Code of Conduct.
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%2f480899%2fbash-script-to-move-files-into-folder-based-on-matching-id-numbers%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
Ain't this a duplicate of unix.stackexchange.com/questions/480803/… ?
– sla3k
2 hours ago
@sla3k , not at all.
– user10630009
1 hour ago
@sla3k , Differences are that I do not want to make folders in this case - the folders pre-exist, and instead of a whole chunk of the name being similar, I just want to use the numbers for processing
– user10630009
1 hour ago