Bash

剝離部分文件名

  • February 24, 2020

範例文件:

1_this is_file one-xhdjsnsk.mp4
2_this_is file two-hdksbdg.mp4
3_this is_file three-hsislnsm.mp4
4_this is file four-gwywkkd.mp4

如何將所有內容從'-'to剝離'.',結果將如下所示:

1_this is_file one.mp4
2_this_is file two.mp4
3_this is_file three.mp4
4_this is file four.mp4

Perl 版本的rename實用程序可以做到這一點(有另一個程序稱為包rename的一部分util-linux)。例子:

$ touch '1_this is_file one-xhdjsnsk.mp4' '2_this_is file two-hdksbdg.mp4' '3_this is_file three-hsislnsm.mp4' '4_this is file four-gwywkkd.mp4'
$ ls -l
total 0
-rw-r--r-- 1 ja users 0 Feb 24 12:43 1_this is_file one-xhdjsnsk.mp4
-rw-r--r-- 1 ja users 0 Feb 24 12:43 2_this_is file two-hdksbdg.mp4
-rw-r--r-- 1 ja users 0 Feb 24 12:43 3_this is_file three-hsislnsm.mp4
-rw-r--r-- 1 ja users 0 Feb 24 12:43 4_this is file four-gwywkkd.mp4
$ perl-rename 's,\-.+\.,.,' *                                                     
$ ls -l
total 0
-rw-r--r-- 1 ja users 0 Feb 24 12:43 1_this is_file one.mp4
-rw-r--r-- 1 ja users 0 Feb 24 12:43 2_this_is file two.mp4
-rw-r--r-- 1 ja users 0 Feb 24 12:43 3_this is_file three.mp4
-rw-r--r-- 1 ja users 0 Feb 24 12:43 4_this is file four.mp4

rename程序在您的系統上可能有不同的名稱,例如在 Ubuntu 上它是file-rename.

引用自:https://unix.stackexchange.com/questions/569377