简体   繁体   English

使用bash / regex重命名特定目录中的文件

[英]Rename files in a specific directory with bash/regex

I'm trying to create some hotfolders using LaunchControl and in part of the script I would like files in a specific directory to be renamed if they match certain criteria. 我正在尝试使用LaunchControl创建一些文件夹,并且在脚本的一部分中,如果它们符合某些条件,则希望重命名特定目录中的文件。 The problem I'm having is not in the renaming part, it is in getting the script to call the directory. 我遇到的问题不在重命名部分,而是在获取脚本来调用目录。 All the scripts or articles I find online only help in renaming the files, not in correctly calling the directory path as part of the renaming script. 我在网上找到的所有脚本或文章仅有助于重命名文件,而不能作为重命名脚本的一部分正确调用目录路径。 Doesn't seem it would be that hard but I can't work it out. 似乎没有那么难,但我无法解决。

Here's what I have tried so far, which does nothing. 到目前为止,这是我尝试过的操作,但没有执行任何操作。 If I CD to the directory though and leave out the part where it is calling the directory it will rename the files. 如果我将CD复制到目录中,而忽略调用目录的部分,它将重命名文件。

find . -mindepth 3 -type f -iregex '.*/Volumes/COMMON-LIC-PHOTO/ASPERA/ASPERA_STAGING/.*' -print0 |
xargs -0 rename -v 's/([0-9]*)_LK/$1_standard/' \;

Use something like this at the beginning of your script: 在脚本开头使用以下代码:

#!/bin/bash

target_directory=/Volumes/COMMON-LIC-PHOTO/ASPERA/ASPERA_STAGING
cd "$target_directory" || {
    echo "Error cd'ing to $target_directory" >&2
    exit 1
}

...and then leave off the -iregex part of your find command. ...然后-iregex find命令的-iregex部分。 Some notes on this: 一些注意事项:

  • find . searches under the current directory , and if you haven't set that in the script, you don't know what it'll be. 在当前目录下进行搜索,如果尚未在脚本中设置该目录 ,则不知道它将是什么。 If you run that from the root directory ("/"), it'll search every directory and file you have permissions to (throwing errors for the ones you don't), and then discard the ones that aren't in the target directory; 如果您从根目录(“ /”)运行该目录,它将搜索您有权访问的每个目录和文件(如果您没有,则会抛出错误),然后丢弃目标目录中没有的目录和文件。目录; it's much better to just search the target directory. 最好只搜索目标目录。 You could also accomplish this with find "$target_dirctory" . 您也可以通过find "$target_dirctory"来完成此操作。

  • In my snippet above, I assumed that the directory is known and fixed, so it can just be included literally in the script. 在上面的代码段中,我假定目录是已知的且已修复,因此可以按字面意义将其包含在脚本中。 If that's not the case, something more complicated will be needed to set it correctly. 如果不是这种情况,则需要进行更复杂的设置才能正确设置。

  • I added an error check to the cd command -- you should always do this when using cd in a script; 我在cd命令中添加了一个错误检查-在脚本中使用cd时应始终执行此操作; otherwise if something goes wrong, the rest of the script will execute in the wrong directory, with potentially weird/destructive consequences. 否则,如果出现问题,脚本的其余部分将在错误的目录中执行,从而可能产生奇怪的/破坏性的后果。 Actually, I prefer to avoid using cd at all in scripts, and just use explicit paths -- it's generally safer and simpler. 实际上,我宁愿避免在脚本中完全使用cd ,而只使用显式路径-通常更安全,更简单。

If your version of find has the -execdir feature, then you could do something like this (I cannot test right now): 如果您的find版本具有-execdir功能,则可以执行以下操作(我现在无法测试):

find . -mindepth 3 -type f -iregex '.*/Volumes/COMMON-LIC-PHOTO/ASPERA/ASPERA_STAGING/.*' -execdir rename -v 's/([0-9]*)_LK/$1_standard/' {} \;

Notice the {} near the end, it signifies the matched path. 注意末尾的{} ,它表示匹配的路径。 The -execdir flag executed the command in the directory of the matched file. -execdir标志在匹配文件的目录中执行了命令。

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

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