Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.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
Macos 使用正则表达式重命名文件_Macos_Sed_Grep_Xargs - Fatal编程技术网

Macos 使用正则表达式重命名文件

Macos 使用正则表达式重命名文件,macos,sed,grep,xargs,Macos,Sed,Grep,Xargs,我的文件夹里有一堆文件。其中一些文件的格式如下: IMG_YYYYMMDD_junk.ext 我想将这些文件重命名为 YYYY-MM-DD垃圾.ext 示例:IMG\u 20170214\u 3939233.jpg变为2017-02-14 3939233.jpg 到目前为止,我成功地筛选了我需要的文件: 查找*.jpg*.jpeg*.png | egrep'^IMG\[0-9]{1,8}' 我知道我需要使用sed,但我没有在regex中指定和引用匹配组以进行进一步的文件名转换。我知道以后可能需要

我的文件夹里有一堆文件。其中一些文件的格式如下:

IMG_YYYYMMDD_junk.ext

我想将这些文件重命名为

YYYY-MM-DD垃圾.ext

示例:IMG\u 20170214\u 3939233.jpg变为2017-02-14 3939233.jpg

到目前为止,我成功地筛选了我需要的文件:

查找*.jpg*.jpeg*.png | egrep'^IMG\[0-9]{1,8}'

我知道我需要使用sed,但我没有在regex中指定和引用匹配组以进行进一步的文件名转换。我知道以后可能需要在管道中使用xarg,但到目前为止,我没有成功地将每个文件名转换为打印出来


也许,sed不是这里的最佳选择

使用Perl的独立重命名命令和bash选项:


如果一切正常,请使用Perl的独立重命名命令和bash的选项删除选项
-n


如果一切看起来都很好,请删除
-n
选项
bash
中的逻辑,无需外部工具

您可以从包含这些图像的文件夹中运行以下脚本

#!/bin/bash

for file in *.{jpg,jpeg,png}; do      
    IFS="_" read -ra fileNameList <<<"$file"

    year="${fileNameList[1]:0:4}"
    month="${fileNameList[1]:4:2}"
    day="${fileNameList[1]:6:2}"

    targetFileName="${year}-${month}-${day} ${fileNameList[2]}"

    # Remove this line and uncomment the line with 'mv' if things look OK
    echo "$file" "$targetFileName"
    #mv -v "$file" "$targetFileName"
done
#/bin/bash
用于*.jpg,jpeg,png}格式的文件;做

IFS=“\u”read-ra fileNameList是
bash
中的一个逻辑,没有外部工具

您可以从包含这些图像的文件夹中运行以下脚本

#!/bin/bash

for file in *.{jpg,jpeg,png}; do      
    IFS="_" read -ra fileNameList <<<"$file"

    year="${fileNameList[1]:0:4}"
    month="${fileNameList[1]:4:2}"
    day="${fileNameList[1]:6:2}"

    targetFileName="${year}-${month}-${day} ${fileNameList[2]}"

    # Remove this line and uncomment the line with 'mv' if things look OK
    echo "$file" "$targetFileName"
    #mv -v "$file" "$targetFileName"
done
#/bin/bash
用于*.jpg,jpeg,png}格式的文件;做

IFS=“u”read-ra fileNameList我假设您对find命令并不感兴趣,而是对确定
sed
regex:

find ~ -type f -maxdepth 1 -name IMG*.jpg | sed -e 's/\(IMG_\)\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)_/\2-\3-\4 /g'
你的例子是:

echo "IMG_20170214_3939233.jpg" | sed -e 's/\(IMG_\)\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)_/\2-\3-\4 /g'
输出:

2017-02-14 3939233.jpg

我假设您对find命令不是很感兴趣,而是对确定
sed
regex:

find ~ -type f -maxdepth 1 -name IMG*.jpg | sed -e 's/\(IMG_\)\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)_/\2-\3-\4 /g'
你的例子是:

echo "IMG_20170214_3939233.jpg" | sed -e 's/\(IMG_\)\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\)_/\2-\3-\4 /g'
输出:

2017-02-14 3939233.jpg

使用GNU Parallel时,它看起来如下所示:

find *.jpg *.jpeg *.png |
  parallel mv {} '{= s/IMG_(....)(..)(..)_/$1-$2-$3 / =}'
或:


使用GNU Parallel时,它看起来如下所示:

find *.jpg *.jpeg *.png |
  parallel mv {} '{= s/IMG_(....)(..)(..)_/$1-$2-$3 / =}'
或:


一种主要使用正则表达式匹配运算符
=~
的-
bash
解决方案,它支持通过内置
“${bash\u REMATCH[@]}”数组变量访问的捕获组:

用于*中的文件;做
[[$file=~^IMG([0-9]{4})([0-9]{2})([0-9]{2})\+\.(jpg | jpeg | png))$]}继续
mv“$file”“${BASH_REMATCH[1]}-${BASH_REMATCH[2]}-${BASH_REMATCH[3]}${BASH_REMATCH[4]}”
完成

一个主要使用regex匹配运算符
=~
的bash
解决方案,它支持通过内置
“${bash\u REMATCH[@]}”数组变量访问的捕获组:

用于*中的文件;做
[[$file=~^IMG([0-9]{4})([0-9]{2})([0-9]{2})\+\.(jpg | jpeg | png))$]}继续
mv“$file”“${BASH_REMATCH[1]}-${BASH_REMATCH[2]}-${BASH_REMATCH[3]}${BASH_REMATCH[4]}”
完成


你想在文件名中有空格吗?@ini我想有空格你想在文件名中有空格吗?@ini我想有空格看起来你需要安装Perl的
重命名
。看起来你需要安装Perl的
重命名
@AstroSharp:在中替换
你的文件夹
该命令包含您拥有的文件夹,即基本上来自包含图像文件的基本文件夹。此外,还有其他文件与指定的文件结构不匹配。这些应该放在一边。文件夹名中有一堆uu。它破坏了代码。@AstroSharp:你能试着把
cd
放到文件夹中运行吗!检查我的更新!非常感谢。已尝试,但注意到文件夹中的其他文件已损坏it@AstroSharp:将命令中的
yourFolder
替换为您拥有的文件夹,即基本上来自包含图像文件的基本文件夹。此外,还有其他文件与指定的文件结构不匹配。这些应该放在一边。文件夹名中有一堆uu。它破坏了代码。@AstroSharp:你能试着把
cd
放到文件夹中运行吗!检查我的更新!非常感谢。尝试过,但注意到文件夹中还有其他文件在第一个命令中中断了itran。有四个文件类似于IMG_*(除其他文件外):
find:IMG_20160714_165051.jpg:unknown primary or operator
在find命令中似乎缺少连字符?在连接到sed命令之前,确保您的查找工作正常。您使用此命令将波浪号(~)更改为开始搜索的目录?
find~-type f-maxdepth 1-name IMG*.jpg
。你认为是哪一个连字符?@AstroSharp我使用的是unix find,如果你没有,那么就使用你原来问题中的命令:“find*.jpg*.jpeg*.png”并将其传输到我的sed regex。运行第一个命令。有四个文件类似于IMG_*(除其他文件外):
find:IMG_20160714_165051.jpg:unknown primary or operator
在find命令中似乎缺少连字符?在连接到sed命令之前,确保您的查找工作正常。您使用此命令将波浪号(~)更改为开始搜索的目录?
find~-type f-maxdepth 1-name IMG*.jpg
。你认为是哪一个连字符?@AstroSharp我使用的是unix find,如果你没有,那么就使用你在原始问题中使用的命令:“find*.jpg*.jpeg*.png”并将其传输到我的sed regex。