Batch rename files Batch rename files linux linux

Batch rename files


This should make it:

rename 's/^[0-9]*-//;' *

It gets from the beginning the block [0-9] (that is, numbers) many times, then the hyphen - and deletes it from the file name.


If rename is not in your machine, you can use a loop and mv:

mv "$f" "${f#[0-9]*-}"

Test

$ ls23-aa  hello aaa23-aa$ rename 's/^[0-9]*-//;' *$ lsaa  hello aaa23-aa

Or:

$ ls23-a  aa23-a  hello$ for f in *;> do>   mv "$f" "${f#[0-9]*-}"> done$ lsa  aa23-a  hello


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 echols *tmp | sed -e 'p;s/\([0-9]\)\+\.tmp/ /g' | xargs -n2 mv


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.