I'm not sure you can have a pipe inside -exec
. find -exec echo {} \| sed s/tiff/jpg \;
doesn't seem to work.
If think your best option is to make a script to do the conversion, e.g.
convert_tiff_to_jpg:
#!/bin/bash
echo "Processing $1"
convert "$1" "${1/%tiff/jpg}"
and call it using find -exec
like you had intended:
find ./out -regex ".*_p[bg]_.*tiff" -exec convert_tiff_to_jpg {} \;
There are a few ways to do it using find -print0 | xargs -0
, but they are all quite ugly:
find ./out -regex ".*_p[bg]_.*tiff" -print0 |
while IFS= read -d $'\0' -r filename; do
echo "Processing $filename"
convert "$filename" "$(echo "$filename" | sed 's/tiff$/jpg/')"
done
or
find ./out -regex ".*_p[bg]_.*tiff" -print0 |
while IFS= read -d $'\0' -r filename; do
echo "Processing $filename"
convert "$filename" "${filename/%tiff/jpg}"
done
or
find ./out -regex ".*_p[bg]_.*tiff" -print0 |
xargs -0 -I FILE bash -c 'F="FILE"; echo "$F" "${F/%tiff/jpg}"'
Note that I changed s/tiff/jpg
to s/tiff$/jpg
so that if tiff
appears anywhere other than the end of the file name, it is not changed.
The answer will depend more on what you intend to do with the output than on what you are looking for. If you just want to see a list for visual reference at the terminal, your first solution is actually pretty nice. If you want to process the output you should consider using another method.
One of the most robust ways to get a list to feed into another program is to use find
.
find -maxdepth 1 -type d
The reason this is good for feeds is that find
can output the data separated by nulls using -print0
or properly escape strings as arguments to another programs using -exec
. For reference on why this is better than parsing the output of ls, see ParsingLS on Greg's Wiki.
Best Answer
for the current directory as the root of the tree or in general: