Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 在ssh中递归地将文件填充到3位数_Bash_Ssh - Fatal编程技术网

Bash 在ssh中递归地将文件填充到3位数

Bash 在ssh中递归地将文件填充到3位数,bash,ssh,Bash,Ssh,我有很多文件夹,其中包含两位数文件名的图像,例如 gallery/gal001/images/01.jpg gallery/gal001/images/02.jpg gallery/gal001/images/03.jpg etc... gallery/gal001/thumbnails/01.jpg gallery/gal001/thumbnails/02.jpg gallery/gal001/thumbnails/03.jpg etc... gallery/gal002/images/0

我有很多文件夹,其中包含两位数文件名的图像,例如

gallery/gal001/images/01.jpg
gallery/gal001/images/02.jpg
gallery/gal001/images/03.jpg
etc...

gallery/gal001/thumbnails/01.jpg
gallery/gal001/thumbnails/02.jpg
gallery/gal001/thumbnails/03.jpg
etc...

gallery/gal002/images/01.jpg
gallery/gal002/images/02.jpg
gallery/gal002/images/03.jpg
etc...

gallery/gal002/thumbnails/01.jpg
gallery/gal002/thumbnails/02.jpg
gallery/gal002/thumbnails/03.jpg
etc...
但是其他人会有3位数的文件名

gallery/gal100/images/001.jpg
gallery/gal100/images/002.jpg
gallery/gal100/images/003.jpg
etc...

gallery/gal100/thumbnails/001.jpg
gallery/gal100/thumbnails/002.jpg
gallery/gal100/thumbnails/003.jpg
etc...

gallery/gal500/images/001.jpg
gallery/gal500/images/002.jpg
gallery/gal500/images/003.jpg
etc...

gallery/gal500/thumbnails/001.jpg
gallery/gal500/thumbnails/002.jpg
gallery/gal500/thumbnails/003.jpg
etc...
我需要让它们都一样,所以我想把2位数的数字填充到3位数,而不要触摸已经有3位数的数字

因为它们在文件夹中,所以我必须递归地执行此操作

我已将CD放入gallery文件夹中,在搜索各种线程和谷歌搜索后,我尝试了以下方法,但这些尝试均无效:

find . -type f 'for f in [0-9]*; do mv $f `printf %03d ${f#}`; done'
如果之前有人问过这个问题,我深表歉意,但正如你所看到的,我已经事先搜索过了

非常感谢您的帮助。

仅匹配具有2位文件名的文件,并在其名称前加上零

find . -name '[0-9][0-9].jpg' -exec sh -c '
for fpath do
  echo mv "$fpath" "${fpath%/*}/0${fpath##*/}"
done' _ {} +
如果您对输出满意,请删除
echo

使用[p]重命名,当然会更容易,但在我的系统上不可用。

仅匹配具有2位文件名的文件名,并在其名称前加上零

find . -name '[0-9][0-9].jpg' -exec sh -c '
for fpath do
  echo mv "$fpath" "${fpath%/*}/0${fpath##*/}"
done' _ {} +
find . -type f -regex '.*/[0-9][0-9]\.[^/]+$' -printf "'%h/%f' '%h/0%f'\\n" | xargs -n 2 mv
如果您对输出满意,请删除
echo

使用[p]重命名,当然会更容易,但在我的系统上不可用

find . -type f -regex '.*/[0-9][0-9]\.[^/]+$' -printf "'%h/%f' '%h/0%f'\\n" | xargs -n 2 mv
您可以将
mv
更改为
echo
以在最终重命名之前进行验证


您可以将
mv
更改为
echo
以在最终重命名之前进行验证

我想发布
源代码我想发布
源代码非常感谢,这非常有效!我花了好几个小时想弄明白这一点。非常感谢,这真是一种享受!我花了好几个小时想弄明白。
find . -name '[0-9][0-9].jpg' -exec sh -c '
for fpath do
  echo mv "$fpath" "${fpath%/*}/0${fpath##*/}"
done' _ {} +
find . -type f -regex '.*/[0-9][0-9]\.[^/]+$' -printf "'%h/%f' '%h/0%f'\\n" | xargs -n 2 mv