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
Macos OS X-根据文件名长度批量向文件添加前缀_Macos_Shell_File Rename - Fatal编程技术网

Macos OS X-根据文件名长度批量向文件添加前缀

Macos OS X-根据文件名长度批量向文件添加前缀,macos,shell,file-rename,Macos,Shell,File Rename,我有一个包含数千个psd文件的文本列表,在文件名的第一个下划线分隔符前面都有7或8位数字 根据位数的不同,我需要在文件前面加上X0 7位或Y 8位的前缀,这样我就可以得到在第一个下划线之前长度相同的文件 例如: <br />0123456_this_is_file_1.psd <br />0654321_this_is_file_2.psd <br />30301234_this_is_file_3.psd <br />50509876_this_

我有一个包含数千个psd文件的文本列表,在文件名的第一个下划线分隔符前面都有7或8位数字

根据位数的不同,我需要在文件前面加上X0 7位或Y 8位的前缀,这样我就可以得到在第一个下划线之前长度相同的文件

例如:

<br />0123456_this_is_file_1.psd
<br />0654321_this_is_file_2.psd
<br />30301234_this_is_file_3.psd
<br />50509876_this_is_file_4.psd
添加前缀后,文件名如下:

<br />X00123456_this_is_file_1.psd
<br />X00654321_this_is_file_2.psd
<br />Y30301234_this_is_file_3.psd
<br />Y50509876_this_is_file_4.psd

如何在shell脚本OS X Sierra中实现这一点?

我不知道什么是OS X Sierra上的shell,但这对ksh和bash都有效

这将根据文件的第一个数字的长度重命名文本列表中列出的文件


如果您想了解脚本的工作原理,请告诉我。

您应该将对$line的所有引用都用双引号括起来,以防它包含空格。@MarkSetchell是的,我假设他的所有文件都是按常规命名的,但您是对的;永远不要做假设;-我已经更新了我的帖子,这样更好
while read line; do
     l=`echo "$line" | cut -d "_" -f 1`
     if [ ${#l} -eq 7 ]; then
          mv "$line" "X0$line"
     elif [ ${#l} -eq 8 ]; then
          mv "$line" "Y$line"
     else
          echo "Error with file $line"
     fi
done < $1