shell script to remove files didn't work
I want to delete multiple images having resolution less then 228x228.
For that, I wrote this shell script:
#!/bin/bash
for i in $( ls ); do
if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
then
rm $i
fi
done
For some reasons, I got this output when I run it:
./del.sh: line 4: [640: command not found
./del.sh: line 4: [550: command not found
./del.sh: line 4: [315: command not found
...
Could you please tell me what's wrong in this script and how to fix it.
Thank you.
command-line bash scripts
add a comment |
I want to delete multiple images having resolution less then 228x228.
For that, I wrote this shell script:
#!/bin/bash
for i in $( ls ); do
if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
then
rm $i
fi
done
For some reasons, I got this output when I run it:
./del.sh: line 4: [640: command not found
./del.sh: line 4: [550: command not found
./del.sh: line 4: [315: command not found
...
Could you please tell me what's wrong in this script and how to fix it.
Thank you.
command-line bash scripts
Why not parsels
(and what do to instead)?
– dessert
3 hours ago
You need spaces after[
and before]
.
– Ralf
1 hour ago
add a comment |
I want to delete multiple images having resolution less then 228x228.
For that, I wrote this shell script:
#!/bin/bash
for i in $( ls ); do
if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
then
rm $i
fi
done
For some reasons, I got this output when I run it:
./del.sh: line 4: [640: command not found
./del.sh: line 4: [550: command not found
./del.sh: line 4: [315: command not found
...
Could you please tell me what's wrong in this script and how to fix it.
Thank you.
command-line bash scripts
I want to delete multiple images having resolution less then 228x228.
For that, I wrote this shell script:
#!/bin/bash
for i in $( ls ); do
if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
then
rm $i
fi
done
For some reasons, I got this output when I run it:
./del.sh: line 4: [640: command not found
./del.sh: line 4: [550: command not found
./del.sh: line 4: [315: command not found
...
Could you please tell me what's wrong in this script and how to fix it.
Thank you.
command-line bash scripts
command-line bash scripts
asked 5 hours ago
singriumsingrium
1,051321
1,051321
Why not parsels
(and what do to instead)?
– dessert
3 hours ago
You need spaces after[
and before]
.
– Ralf
1 hour ago
add a comment |
Why not parsels
(and what do to instead)?
– dessert
3 hours ago
You need spaces after[
and before]
.
– Ralf
1 hour ago
Why not parse
ls
(and what do to instead)?– dessert
3 hours ago
Why not parse
ls
(and what do to instead)?– dessert
3 hours ago
You need spaces after
[
and before ]
.– Ralf
1 hour ago
You need spaces after
[
and before ]
.– Ralf
1 hour ago
add a comment |
3 Answers
3
active
oldest
votes
Some issues here: Firstly, the expression in a […]
test needs spaces around it (pitfall #10), and secondly
the comparison <
doesn't work with […]
tests (pitfall #7). You either need -lt
(less than)
or use [[…]]
instead, which is a bashism. Also, the for
loop should be replaced (pitfall #1).
So:
for i in ./*; do
if [ -e "$i" ]; then
if [ $(identify -format "%w" "$i") -lt 228 ] && [ $(identify -format "%h" "$i") -lt 228 ];
then
rm -- "$i"
fi
fi
done
You may also want to avoid calling identify
twice to get the two dimensions (pitfall #58) but call
it just once instead and let it print a string ready to be used as variable assignments in shell syntax.
If we write
identify -format "width=%w height=%h" "$i"
it will print something like width=50 heigth=250
. When we eval
uate that string then we have set two variables with just one call and the condition can be written as:
eval "$(identify -format "width=%w height=%h" "$i")"
if [ $width -lt 228 ] && [ $height -lt 228 ];
then
rm -- "$i"
fi
See also: common bash pitfalls.
add a comment |
Instead of a loop, I'd use find
with -exec
and -delete
:
find . -maxdepth 1 -type f
-exec sh -c '
[ $(identify -format "%w" "$1") -lt 228 ] &&
[ $(identify -format "%h" "$1") -lt 228 ]' _ {} ;
-delete -print
This will also print the files that get deleted, you can remove -print
if you don't want that.
add a comment |
Not intended to answer but to give a useful tip that helped me much doing bash scripting.
There's a shell script linter called shellcheck
that might trap some common errors in bash scripts and also avoid some pitfalls. It can be installed like any package in ubuntu -> https://launchpad.net/ubuntu/+source/shellcheck is in universe
for current stable.
This is the output for your script
shellcheck del.sh
In del.sh line 4:
if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
^-- SC1009: The mentioned parser error was in this if expression.
^-- SC1073: Couldn't parse this test expression.
^-- SC1035: You need a space after the [ and before the ].
^-- SC1020: You need a space before the ].
^-- SC1072: Missing space before ]. Fix any mentioned problems and try again.
If you fix and apply again you'll get some other recommendations and fixes already mentioned in the accepted answer.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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%2faskubuntu.com%2fquestions%2f1108810%2fshell-script-to-remove-files-didnt-work%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Some issues here: Firstly, the expression in a […]
test needs spaces around it (pitfall #10), and secondly
the comparison <
doesn't work with […]
tests (pitfall #7). You either need -lt
(less than)
or use [[…]]
instead, which is a bashism. Also, the for
loop should be replaced (pitfall #1).
So:
for i in ./*; do
if [ -e "$i" ]; then
if [ $(identify -format "%w" "$i") -lt 228 ] && [ $(identify -format "%h" "$i") -lt 228 ];
then
rm -- "$i"
fi
fi
done
You may also want to avoid calling identify
twice to get the two dimensions (pitfall #58) but call
it just once instead and let it print a string ready to be used as variable assignments in shell syntax.
If we write
identify -format "width=%w height=%h" "$i"
it will print something like width=50 heigth=250
. When we eval
uate that string then we have set two variables with just one call and the condition can be written as:
eval "$(identify -format "width=%w height=%h" "$i")"
if [ $width -lt 228 ] && [ $height -lt 228 ];
then
rm -- "$i"
fi
See also: common bash pitfalls.
add a comment |
Some issues here: Firstly, the expression in a […]
test needs spaces around it (pitfall #10), and secondly
the comparison <
doesn't work with […]
tests (pitfall #7). You either need -lt
(less than)
or use [[…]]
instead, which is a bashism. Also, the for
loop should be replaced (pitfall #1).
So:
for i in ./*; do
if [ -e "$i" ]; then
if [ $(identify -format "%w" "$i") -lt 228 ] && [ $(identify -format "%h" "$i") -lt 228 ];
then
rm -- "$i"
fi
fi
done
You may also want to avoid calling identify
twice to get the two dimensions (pitfall #58) but call
it just once instead and let it print a string ready to be used as variable assignments in shell syntax.
If we write
identify -format "width=%w height=%h" "$i"
it will print something like width=50 heigth=250
. When we eval
uate that string then we have set two variables with just one call and the condition can be written as:
eval "$(identify -format "width=%w height=%h" "$i")"
if [ $width -lt 228 ] && [ $height -lt 228 ];
then
rm -- "$i"
fi
See also: common bash pitfalls.
add a comment |
Some issues here: Firstly, the expression in a […]
test needs spaces around it (pitfall #10), and secondly
the comparison <
doesn't work with […]
tests (pitfall #7). You either need -lt
(less than)
or use [[…]]
instead, which is a bashism. Also, the for
loop should be replaced (pitfall #1).
So:
for i in ./*; do
if [ -e "$i" ]; then
if [ $(identify -format "%w" "$i") -lt 228 ] && [ $(identify -format "%h" "$i") -lt 228 ];
then
rm -- "$i"
fi
fi
done
You may also want to avoid calling identify
twice to get the two dimensions (pitfall #58) but call
it just once instead and let it print a string ready to be used as variable assignments in shell syntax.
If we write
identify -format "width=%w height=%h" "$i"
it will print something like width=50 heigth=250
. When we eval
uate that string then we have set two variables with just one call and the condition can be written as:
eval "$(identify -format "width=%w height=%h" "$i")"
if [ $width -lt 228 ] && [ $height -lt 228 ];
then
rm -- "$i"
fi
See also: common bash pitfalls.
Some issues here: Firstly, the expression in a […]
test needs spaces around it (pitfall #10), and secondly
the comparison <
doesn't work with […]
tests (pitfall #7). You either need -lt
(less than)
or use [[…]]
instead, which is a bashism. Also, the for
loop should be replaced (pitfall #1).
So:
for i in ./*; do
if [ -e "$i" ]; then
if [ $(identify -format "%w" "$i") -lt 228 ] && [ $(identify -format "%h" "$i") -lt 228 ];
then
rm -- "$i"
fi
fi
done
You may also want to avoid calling identify
twice to get the two dimensions (pitfall #58) but call
it just once instead and let it print a string ready to be used as variable assignments in shell syntax.
If we write
identify -format "width=%w height=%h" "$i"
it will print something like width=50 heigth=250
. When we eval
uate that string then we have set two variables with just one call and the condition can be written as:
eval "$(identify -format "width=%w height=%h" "$i")"
if [ $width -lt 228 ] && [ $height -lt 228 ];
then
rm -- "$i"
fi
See also: common bash pitfalls.
edited 2 hours ago
dessert
22.3k56198
22.3k56198
answered 5 hours ago
PerlDuckPerlDuck
5,59911231
5,59911231
add a comment |
add a comment |
Instead of a loop, I'd use find
with -exec
and -delete
:
find . -maxdepth 1 -type f
-exec sh -c '
[ $(identify -format "%w" "$1") -lt 228 ] &&
[ $(identify -format "%h" "$1") -lt 228 ]' _ {} ;
-delete -print
This will also print the files that get deleted, you can remove -print
if you don't want that.
add a comment |
Instead of a loop, I'd use find
with -exec
and -delete
:
find . -maxdepth 1 -type f
-exec sh -c '
[ $(identify -format "%w" "$1") -lt 228 ] &&
[ $(identify -format "%h" "$1") -lt 228 ]' _ {} ;
-delete -print
This will also print the files that get deleted, you can remove -print
if you don't want that.
add a comment |
Instead of a loop, I'd use find
with -exec
and -delete
:
find . -maxdepth 1 -type f
-exec sh -c '
[ $(identify -format "%w" "$1") -lt 228 ] &&
[ $(identify -format "%h" "$1") -lt 228 ]' _ {} ;
-delete -print
This will also print the files that get deleted, you can remove -print
if you don't want that.
Instead of a loop, I'd use find
with -exec
and -delete
:
find . -maxdepth 1 -type f
-exec sh -c '
[ $(identify -format "%w" "$1") -lt 228 ] &&
[ $(identify -format "%h" "$1") -lt 228 ]' _ {} ;
-delete -print
This will also print the files that get deleted, you can remove -print
if you don't want that.
edited 3 hours ago
answered 3 hours ago
RoVoRoVo
6,9281741
6,9281741
add a comment |
add a comment |
Not intended to answer but to give a useful tip that helped me much doing bash scripting.
There's a shell script linter called shellcheck
that might trap some common errors in bash scripts and also avoid some pitfalls. It can be installed like any package in ubuntu -> https://launchpad.net/ubuntu/+source/shellcheck is in universe
for current stable.
This is the output for your script
shellcheck del.sh
In del.sh line 4:
if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
^-- SC1009: The mentioned parser error was in this if expression.
^-- SC1073: Couldn't parse this test expression.
^-- SC1035: You need a space after the [ and before the ].
^-- SC1020: You need a space before the ].
^-- SC1072: Missing space before ]. Fix any mentioned problems and try again.
If you fix and apply again you'll get some other recommendations and fixes already mentioned in the accepted answer.
add a comment |
Not intended to answer but to give a useful tip that helped me much doing bash scripting.
There's a shell script linter called shellcheck
that might trap some common errors in bash scripts and also avoid some pitfalls. It can be installed like any package in ubuntu -> https://launchpad.net/ubuntu/+source/shellcheck is in universe
for current stable.
This is the output for your script
shellcheck del.sh
In del.sh line 4:
if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
^-- SC1009: The mentioned parser error was in this if expression.
^-- SC1073: Couldn't parse this test expression.
^-- SC1035: You need a space after the [ and before the ].
^-- SC1020: You need a space before the ].
^-- SC1072: Missing space before ]. Fix any mentioned problems and try again.
If you fix and apply again you'll get some other recommendations and fixes already mentioned in the accepted answer.
add a comment |
Not intended to answer but to give a useful tip that helped me much doing bash scripting.
There's a shell script linter called shellcheck
that might trap some common errors in bash scripts and also avoid some pitfalls. It can be installed like any package in ubuntu -> https://launchpad.net/ubuntu/+source/shellcheck is in universe
for current stable.
This is the output for your script
shellcheck del.sh
In del.sh line 4:
if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
^-- SC1009: The mentioned parser error was in this if expression.
^-- SC1073: Couldn't parse this test expression.
^-- SC1035: You need a space after the [ and before the ].
^-- SC1020: You need a space before the ].
^-- SC1072: Missing space before ]. Fix any mentioned problems and try again.
If you fix and apply again you'll get some other recommendations and fixes already mentioned in the accepted answer.
Not intended to answer but to give a useful tip that helped me much doing bash scripting.
There's a shell script linter called shellcheck
that might trap some common errors in bash scripts and also avoid some pitfalls. It can be installed like any package in ubuntu -> https://launchpad.net/ubuntu/+source/shellcheck is in universe
for current stable.
This is the output for your script
shellcheck del.sh
In del.sh line 4:
if [$(identify -format "%w" $i) < 228] && [$(identify -format "%h" $i) < 228];
^-- SC1009: The mentioned parser error was in this if expression.
^-- SC1073: Couldn't parse this test expression.
^-- SC1035: You need a space after the [ and before the ].
^-- SC1020: You need a space before the ].
^-- SC1072: Missing space before ]. Fix any mentioned problems and try again.
If you fix and apply again you'll get some other recommendations and fixes already mentioned in the accepted answer.
answered 2 hours ago
theisttheist
67249
67249
add a comment |
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1108810%2fshell-script-to-remove-files-didnt-work%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
Why not parse
ls
(and what do to instead)?– dessert
3 hours ago
You need spaces after
[
and before]
.– Ralf
1 hour ago