Batch rename files

2019-01-22 03:34发布

I want to batch re-name a number of files in a directory so that the preceding number and hypen are stripped from the file name.

Old file name: 2904495-XXX_01_xxxx_20130730235001_00000000.NEW
New file name:         XXX_01_xxxx_20130730235001_00000000.NEW

How can I do this with a linux command?

9条回答
看我几分像从前
2楼-- · 2019-01-22 04:12

If the first numbers are always the same length:

for F in *new ; do
    mv $F ${F:8}
done

The ${parameter:number} does a substring expansion - takes the string starting at the 8th character.

There are many other string edits available in expansions to handle other cases.

查看更多
\"骚年 ilove
3楼-- · 2019-01-22 04:15

I think this command would better if you execute the command below:

ls * | sed -e 'p;s/old-name/new-name/' | xargs -n2 mv

Here
ls * - lists files in curent folder
sed -e - executes expression
p; - prints old file name
s/old-name/new-name/ - produce new filename
xargs -n2 - handles two arguments to mv
mv - gets two parameters and do move operation

Recommendation: before executing mv verify what you do is what you want to achieve with echo.

ls * | sed -e 'p;s/old-name/new-name/' | xargs -n2 echo

Following example renames

SCCF099_FG.gz5329223404623884757.tmp to
SCCF099_FG.gz

ls *tmp | sed -e 'p;s/\([0-9]\)\+\.tmp/ /g' | xargs -n2 echo
ls *tmp | sed -e 'p;s/\([0-9]\)\+\.tmp/ /g' | xargs -n2 mv
查看更多
迷人小祖宗
4楼-- · 2019-01-22 04:16

Using renamer (Windows, Mac and Linux friendly):

$ renamer --find '/\d+-(.*)/' --replace '$1' *

This will strip all numbers and first hyphen from the start of all files in the current directory.

查看更多
登录 后发表回答