如何在 bash 中重命名列表中的文件?

发布于 2024-09-02 06:27:15 字数 259 浏览 4 评论 0原文

我有一个包含如下文件名的文件:

my_cool_file.xxx
my_cool_file2.xxx
my_cool_file3.xxx

我有一个文件夹,其中包含:

some_file.xxx
some_file2.xxx
some_file3.xxx

我想要一个 bash 脚本从文件名文件中取出一行并重命名文件夹中的一个文件。

有办法做到这一点吗?

I have a file that contains filenames like this:

my_cool_file.xxx
my_cool_file2.xxx
my_cool_file3.xxx

I have a folder that has:

some_file.xxx
some_file2.xxx
some_file3.xxx

I would like to have a bash script to take one line from the filename file and rename one file in the folder.

Is there a way to do this?

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

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

发布评论

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

评论(2

不必了 2024-09-09 06:27:15

注意:这个答案一般来说不太好,但它解决了OP的问题。

所有文件都以_file.xxx或_fileN.xxx结尾吗?

基本上,您只想更改文件名的前缀吗?

假设我正确理解您的问题,并且没有数字的文件位于 file_list.txt 的最后,以下内容应该可以解决问题,


COUNTER=0
for FILE in `ls *.xxx | sort`; do
    COUNTER=$(($COUNTER+1))
    echo Moving $FILE to `sed -n ${COUNTER}p file_list.txt`
    #mv "${FILE}" `sed -n ${COUNTER}p file_list.txt`
done

如果您想要,请在带有 mv 的行中注释实际行动发生。

Note: This answer isn't that good generally, but it solved the OP's problem.

Do all files end with _file.xxx or _fileN.xxx?

Basically, is what you want to change just the prefix of the filenames?

Assuming I understand your problem correctly, and that the file without a number is last in file_list.txt the following should do the trick


COUNTER=0
for FILE in `ls *.xxx | sort`; do
    COUNTER=$(($COUNTER+1))
    echo Moving $FILE to `sed -n ${COUNTER}p file_list.txt`
    #mv "${FILE}" `sed -n ${COUNTER}p file_list.txt`
done

Comment in the line with mv if you want the actual move to happen.

木森分化 2024-09-09 06:27:15

或者,虽然不那么聪明,但仍然是另一种方法。

我的情况:

  • SRT 文件名命名正确。
  • 重命名 MP4 文件名以匹配 SRT 文件名结构。

我的命令:

ls *.srt > SRT.txt
ls *.mp4 > MP4.txt
paste -d ';' SRT.txt MP4.txt > SM.txt

while read -r line; do
  mv "$(echo $line | cut -f 2 -d ';')" \
     "$(echo $line | cut -f 1 -d ';' | sed 's/.srt/.mp4/')"
done < SM.txt

rm {MP4,SRT,SM}.txt

Or, less clever, but still another way to do it.

My case:

  • SRT filenames are named properly.
  • Renaming MP4 filenames to match SRT filename structure.

My commands:

ls *.srt > SRT.txt
ls *.mp4 > MP4.txt
paste -d ';' SRT.txt MP4.txt > SM.txt

while read -r line; do
  mv "$(echo $line | cut -f 2 -d ';')" \
     "$(echo $line | cut -f 1 -d ';' | sed 's/.srt/.mp4/')"
done < SM.txt

rm {MP4,SRT,SM}.txt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文