简体   繁体   中英

Replacing a part of file path in exec

I would like to replace the part of each file path, which will be find by find linux command. My approach is attached below:

find . -type f -name "*.txt" -exec echo {} | sed "s/f/u/g" {} \;

I expect the replacement of each letter "f" with "u" in file path. Unfortunately I got this error:

find: missing argument to `-exec'
sed: can't read {}: No such file or directory
sed: can't read ;: No such file or directory

What I did wrong? Thank you for your help.

I would like to replace the part of each file path

If you want to change just the file names/paths then use:

find . -type f -name "*.txt" -exec bash -c 'echo "$1" | sed "s/f/u/g"' - {} \;

or a bit more efficient with xargs (since it avoids spawning subshell for each found file):

find . -type f -name "*.txt" -print0 |
xargs -0 bash -c 'for f; do sed "s/f/u/g" <<< "$f"; done'
find . -type f -name "*.txt" | while read files
do 
newname=$(echo "${files}" | sed s"@f@u@"g)
mv -v "${files}" "${newname}"
done

I don't completely understand what you meant by file path. If you weren't talking about the file name, please clarify further.

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