简体   繁体   中英

How to apply regex on {} parameter in Linux in find command

I want to change the file name on a certain type of files. It should run recursively. I have almost got it and don't know how to work with the parameter {} (the absolute Path).

      find $PWD -type f -name '*.jpg' -exec echo " {} " \;

For example, I want to change the extensions using reg. expressions and not the command rename etc. I need sometimes new name to pass it to a function, therefore rename is not applicable here.It should be possible to work with the parameter like in for-case with the parameter $each:

      for each in /* do echo "${each\./\}.png" done

How can I apply regex on parameter {}, like here: "${each \\ . / \\ }.png"?

I found a workaround of the basename misbehaving when using it with find :

find $PWD -type f -name '*.jpg' -exec sh -c 'echo "$(basename "$0" .jpg).png"' {} \;

sh forces the following commands to be interpretated using your /bin/sh file. The -c option specifies arguments are passed as strings (here, your argument $0 is {} ).

If you have the following files :

/home/username/image1.jpg
/home/username/Documents/image2.jpg

This will output :

image1.png
image2.png

EDIT

If you want to keep the full path, you can use this :

find $PWD -type f -name '*.jpg' -exec sh -c 'echo "${0%%.jpg}".png' {} \;

This will output :

/home/username/image1.png
/home/username/Documents/image2.png

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM