Skip to main content
edited tags
Link
terdon
  • 252.2k
  • 69
  • 480
  • 718
added 760 characters in body
Source Link
arvil
  • 690
  • 9
  • 22

I am trying to practice my unix skills, I just learned how to use for file in *.jpg; what it does is select all files with .jpg in the working directory, but I do not want to stop there and I do not want to run my bash script twice

So here is the scenario, I got these files, and I wish is select each one of them, then replace the extension name to another and I hope the process will be something like basename + ".done" so test1.jpg becomes test1.done

test1.jpg
test2.jpeg
test3.txt
test4.dummy

Now, what I want is to somehow, put these extension names .jpg,.jpeg,.txt,.dummy in an array, then use that in for loop something like for file in *{.jpg,.jpeg,.txt,.dummy}; do, but I tried it and it does not work, the shell seems to not allow arrays in for loop?

can somebody help me and give me an example how to solve this? thanks!

UPDATE

so, I learned that using for file in *.{jpg,jpeg,test} will solve my problem, but sometimes it fails, specially when there's no file with an extension declared in the array

Example:

test1.jpg
test2.jpeg
test3.test
test4.dummy

Using a bash like:

for file in *.{jpg,jpeg,test,avi}; do size=$(stat -c '%s' "$file"); echo $file filesize is: $size; done

will end up ugly:

test1.jpg filesize is: 2
test2.jpeg filesize is: 0
test3.test filesize is: 0
stat: cannot stat `*.avi': No such file or directory
*.avi filesize is:

the last 2 lines above is unnecessary, I wish there is something like a break where if for cannot find any avi then stop the do

Is this possible?

I am trying to practice my unix skills, I just learned how to use for file in *.jpg; what it does is select all files with .jpg in the working directory, but I do not want to stop there and I do not want to run my bash script twice

So here is the scenario, I got these files, and I wish is select each one of them, then replace the extension name to another and I hope the process will be something like basename + ".done" so test1.jpg becomes test1.done

test1.jpg
test2.jpeg
test3.txt
test4.dummy

Now, what I want is to somehow, put these extension names .jpg,.jpeg,.txt,.dummy in an array, then use that in for loop something like for file in *{.jpg,.jpeg,.txt,.dummy}; do, but I tried it and it does not work, the shell seems to not allow arrays in for loop?

can somebody help me and give me an example how to solve this? thanks!

I am trying to practice my unix skills, I just learned how to use for file in *.jpg; what it does is select all files with .jpg in the working directory, but I do not want to stop there and I do not want to run my bash script twice

So here is the scenario, I got these files, and I wish is select each one of them, then replace the extension name to another and I hope the process will be something like basename + ".done" so test1.jpg becomes test1.done

test1.jpg
test2.jpeg
test3.txt
test4.dummy

Now, what I want is to somehow, put these extension names .jpg,.jpeg,.txt,.dummy in an array, then use that in for loop something like for file in *{.jpg,.jpeg,.txt,.dummy}; do, but I tried it and it does not work, the shell seems to not allow arrays in for loop?

can somebody help me and give me an example how to solve this? thanks!

UPDATE

so, I learned that using for file in *.{jpg,jpeg,test} will solve my problem, but sometimes it fails, specially when there's no file with an extension declared in the array

Example:

test1.jpg
test2.jpeg
test3.test
test4.dummy

Using a bash like:

for file in *.{jpg,jpeg,test,avi}; do size=$(stat -c '%s' "$file"); echo $file filesize is: $size; done

will end up ugly:

test1.jpg filesize is: 2
test2.jpeg filesize is: 0
test3.test filesize is: 0
stat: cannot stat `*.avi': No such file or directory
*.avi filesize is:

the last 2 lines above is unnecessary, I wish there is something like a break where if for cannot find any avi then stop the do

Is this possible?

Source Link
arvil
  • 690
  • 9
  • 22

using `for file in` to select multiple extension names then get the basename of "file" then concatenate an extension name

I am trying to practice my unix skills, I just learned how to use for file in *.jpg; what it does is select all files with .jpg in the working directory, but I do not want to stop there and I do not want to run my bash script twice

So here is the scenario, I got these files, and I wish is select each one of them, then replace the extension name to another and I hope the process will be something like basename + ".done" so test1.jpg becomes test1.done

test1.jpg
test2.jpeg
test3.txt
test4.dummy

Now, what I want is to somehow, put these extension names .jpg,.jpeg,.txt,.dummy in an array, then use that in for loop something like for file in *{.jpg,.jpeg,.txt,.dummy}; do, but I tried it and it does not work, the shell seems to not allow arrays in for loop?

can somebody help me and give me an example how to solve this? thanks!