简体   繁体   English

bash脚本将单词附加到文件名

[英]bash script to append word to filenames

I'm trying append to word "dicom" to the front of many filenames in a set of folders. 我正在尝试在一组文件夹中许多文件名的前面附加单词“ dicom”。 The folders all begin with "s" (referred to by "s*" in the script below), and each contain many files (specified by "*" below)--I'd like all of these files to be changed using this bash script. 文件夹都以“ s”开头(在下面的脚本中以“ s *”表示),并且每个文件夹都包含许多文件(在下面以“ *”指定)–我希望使用此文件来更改所有这些文件bash脚本。 I tried to run this: 我试图运行此:

for file in /Volumes/USB_AIB/DICOMFunCurrentBatch/MOVr1unzip/s*/*
  do
  mv $file dicom${file%%}
done

but got thousands of lines of the following error (once for each file within each folder--this is just an example of one of them): 但出现了数千行以下错误(每个文件夹中的每个文件一次-这只是其中之一的示例):

mv: rename /Volumes/USB_AIB/DICOMFunCurrentBatch/MOVr1unzip/s307_1/29217684 to dicom/Volumes/USB_AIB/DICOMFunCurrentBatch/MOVr1unzip/s307_1/29217684: No such file or directory

Any ideas on how to fix it? 关于如何解决它的任何想法?

  1. the following variable expansion ${file%%} is strange because it does nothing: 以下变量扩展${file%%}很奇怪,因为它什么都不做:

    ${parameter%%word} : remove the longest matching suffix pattern. $ {parameter %% word}:删除最长匹配的后缀模式。

  2. to move the file into a directory the path should exists, to create the path: 将文件移动到路径应该存在的目录中,以创建路径:

    mkdir -p "$(dirname "${newfilename}")" mkdir -p“ $(目录名” $ {newfilename}“)”

Maybe what you are trying to do: 也许您正在尝试做什么:

for file in /Volumes/USB_AIB/DICOMFunCurrentBatch/MOVr1unzip/s*/*
do
    mv "$file" "${file%/*}/dicom${file##*/}"
done

I don't you have a valid path as dicom/Volumes/USB_AIB/DICOMFunCurrentBatch/MOVr1unzip/s307_1/ , why do you add dicom at the beginning? 我没有有效的路径,例如dicom/Volumes/USB_AIB/DICOMFunCurrentBatch/MOVr1unzip/s307_1/ ,为什么要在开头添加dicom

maybe you want to append dicom to the end of the $file ? 也许您想将dicom附加到$file

mv "$file" "${file}_dicom"

or something like that. 或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM