Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop for multiple extension and do something with each file

Tags:

bash

I'm trying to write a for loop in bash to get the files with extension of jpg, jpeg, png, this i my attempt, but does not work

for file in "${arg}"/*.{jpg,jpeg,png}; do echo ${arg}-something.jpg > z.txt ; done;

basically, i want to get the name of the file with those extension in the current folder, and do something with each file, then output the filename back with a new extension.

like image 819
devric Avatar asked Sep 04 '12 08:09

devric


People also ask

How do you run multiple commands in a for loop?

Chaining multiple commands in the Bash for loop You obviously aren't limited to a single command in a for loop, you can chain multiple ones inside the for-loop. You can chain multiple commands with the ; semicolon, the last command will be the done keyword to indicate you're, well, done.

How do you read multiple files in a loop in Python?

UPDATE: for all files in the same folder: first, get the folder path (on Windows, like this). Suppose it's "D:\my_data" . Then, you can get all the filepaths of the files as in the script above. Show activity on this post.


1 Answers

You are not using $file anywhere. Try

for file in "$arg"/*.{jpg,jpeg,png} ; do
    echo "$file" > z.txt
done
like image 125
choroba Avatar answered Oct 26 '22 22:10

choroba