I have a directory named as assets, which in further has a set of directories.
user_images
|-content_images
|-original
|-cropped
|-resize
|-gallery_images
|-slider_images
|-logo
These folders can have folders like original
, cropped
, resize
. And these folders further will have images. These images are named something like this – 14562345+Image.jpeg
. I need to replace all the images/files that have +
to _
.
for f in ls -a;
do
if [[ $f == + ]]
then
cp "$f" "${f//+/_}"
fi
done
I was able to do this in the current directory. But I need to iterate this to other many other directories. How can I do that?
答案 0 :(得分:2)
You can use this loop using find
in a process substitution:
cd user_images
while IFS= read -r -d '' f; do
echo "$f"
mv "$f" "${f//+/_}"
done < <(find . -name '*+*' -type f -print0)
答案 1 :(得分:0)
With find -exec
:
find user_images -type f \
-exec bash -c '[[ $0 == *+* ]] && mv "$0" "${0//+/_}"' {} \;
Notice that this uses mv
and not cp
as the question states "rename", but simply replace by cp
if you want to keep the original files.
The bash -c
is required to be able to manipulate the file names, otherwise we could use {}
directly in the -exec
action.
答案 2 :(得分:0)
以下将以递归方式运行,并将使用_:
重命名所有替换+的文件find . -name '*+*' -type f -execdir bash -c 'for f; do mv "$f" "${f//+/_}"' _ {} +
请注意使用-execdir
:
与-exec类似,但指定的 命令是从包含匹配文件 的子目录运行的,该子目录通常不是您开始查找的目录 - 引自
man find
。
如果目录名与您不想重命名的模式*+*
匹配,这将保护我们。