使用 shell 脚本在 Unix 服务器中重命名具有相同文件名模式的文件

发布于 2025-01-18 20:41:52 字数 714 浏览 0 评论 0原文

我在UNIX服务器中有一些具有相同文件名模式的文件。

例子: 样本:

ABC DE FGH IJKL 04012022.csv
ABC DE FGH IJKL 04022022.csv

预期输出:

ABC_DE_FGH_IJKL_04012022.csv
ABC_DE_FGH_IJKL_04022022.csv

这是我尝试的。但是在MV命令中遇到了错误,因为它试图一次重命名两个文件。

for file in ls ABC\ DE\ FGH\ IJKL\ ????????.csv 
do 
    #Extracting date part separately 
    date_part=$(ls ABC\ DE\ FGH\ IJKL\ ????????.csv | cut -c 17-24) 
    #Appending date part to file 
    csv_file_nm="ABC_DE_FGH_IJKL_"$date_part.csv 
    #Renaming file using mv command 
    mv ABC\ DE\ FGH\ IJKL\ ????????.csv $csv_file_nm 
done

I have a few files with the same file name pattern in the Unix server.

Example:
Sample:

ABC DE FGH IJKL 04012022.csv
ABC DE FGH IJKL 04022022.csv

Expected Output:

ABC_DE_FGH_IJKL_04012022.csv
ABC_DE_FGH_IJKL_04022022.csv

This is what I tried. But got an error in the mv command as it was trying to rename both files at once.

for file in ls ABC\ DE\ FGH\ IJKL\ ????????.csv 
do 
    #Extracting date part separately 
    date_part=$(ls ABC\ DE\ FGH\ IJKL\ ????????.csv | cut -c 17-24) 
    #Appending date part to file 
    csv_file_nm="ABC_DE_FGH_IJKL_"$date_part.csv 
    #Renaming file using mv command 
    mv ABC\ DE\ FGH\ IJKL\ ????????.csv $csv_file_nm 
done

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2025-01-25 20:41:52

显示的代码被严重打破:

for file in ls ABC\ DE\ FGH\ IJKL\ ????????.csv 
do 
    #Extracting date part separately 
    date_part=$(ls ABC\ DE\ FGH\ IJKL\ ????????.csv | cut -c 17-24) 
    #Appending date part to file 
    csv_file_nm="ABC_DE_FGH_IJKL_"$date_part.csv 
    #Renaming file using mv command 
    mv ABC\ DE\ FGH\ IJKL\ ????????.csv $csv_file_nm 
done

要使用此想法(这不是一个很好的程序方式,但是…),您需要在循环中使用“ $ file”而不是重复ls操作:

for file in ABC\ DE\ FGH\ IJKL\ ????????.csv 
do 
    #Extracting date part separately 
    date_part=$(echo "$file" | cut -c 17-24) 
    #Appending date part to file 
    csv_file_nm="ABC_DE_FGH_IJKL_$date_part.csv" 
    #Renaming file using mv command 
    mv "$file" "$csv_file_nm"
done

该脚本使用球形而不是ls生成文件名 - 我从ls中删除了ls 行。您可能打算在$(ls…)中为文件编写,或者可能在`'中写下文件 - 但这不是问题。也不建议处理ls的输出。

但是,由于您想用下划线替换空间,因此一种更直接的操作方法是:

for file in ABC\ DE\ FGH\ IJKL\ ????????.csv 
do 
    csv_file_nm=$(echo "$file" | sed 's/ /_/g') 
    mv "$file" "$csv_file_nm" 
done

或者,如果您的外壳是bash(足够新的),则:

for file in ABC\ DE\ FGH\ IJKL\ ????????.csv 
do 
    mv "$file" "${file// /_}" 
done

或者,如果您具有基于Perl的Rename < /code>(有时称为prename)在系统上命令,然后:

rename 's/ /_/g' "ABC DE FGH IJKL "????????.csv

The code shown is horribly broken:

for file in ls ABC\ DE\ FGH\ IJKL\ ????????.csv 
do 
    #Extracting date part separately 
    date_part=$(ls ABC\ DE\ FGH\ IJKL\ ????????.csv | cut -c 17-24) 
    #Appending date part to file 
    csv_file_nm="ABC_DE_FGH_IJKL_"$date_part.csv 
    #Renaming file using mv command 
    mv ABC\ DE\ FGH\ IJKL\ ????????.csv $csv_file_nm 
done

To use this idea (which isn't a very good way of proceeding, but…), you'd need to use "$file" in the loop rather than repeated ls operations:

for file in ABC\ DE\ FGH\ IJKL\ ????????.csv 
do 
    #Extracting date part separately 
    date_part=$(echo "$file" | cut -c 17-24) 
    #Appending date part to file 
    csv_file_nm="ABC_DE_FGH_IJKL_$date_part.csv" 
    #Renaming file using mv command 
    mv "$file" "$csv_file_nm"
done

That script uses globbing rather than ls to generate the file names — I've removed a stray ls from the for line. You might have intended to write for file in $(ls …), or maybe for file in `ls …` — but that isn't in the question. Processing the output of ls is not recommended either.

However, since you want to replace the spaces with underscores, a more direct way of operating would be:

for file in ABC\ DE\ FGH\ IJKL\ ????????.csv 
do 
    csv_file_nm=$(echo "$file" | sed 's/ /_/g') 
    mv "$file" "$csv_file_nm" 
done

Or, if your shell is Bash (and new enough), then:

for file in ABC\ DE\ FGH\ IJKL\ ????????.csv 
do 
    mv "$file" "${file// /_}" 
done

Or, if you have the Perl-based rename (sometimes called prename) command on the system, then:

rename 's/ /_/g' "ABC DE FGH IJKL "????????.csv
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文