Rename multiple files to BASH

I would like to rename file numbering: I have files with '???' I need to put them in '????'.

myfile_100_asd_4 to myfile_0100_asd_4 

Thanks Arman.

Not so elegant SOLUTION:

 #/bin/bash snap=`ls -t *_???` c=26 for k in $snap do end=${k} echo mv $k ${k%_*}_0${k##*_}_asd_4 (( c=c-1 )) done 

This works for me because I have myfile_100 files.

+4
source share
4 answers

just use the shell

 for file in myfile* do t=${file#*_} f=${file%%_*} number=$(printf "%04d" ${t%%_*}) newfile="${f}_${number}_${t#*_}" echo mv "$file" "$newfile" done 
+5
source

Use rename , a small script that comes with perl:

 rename 's/(\d{3})/0$1/g' myfile_* 

If you pass the -n option to it before the expression, it will only print what it renamed, no action was taken. This way you can verify that it is working fine before renaming your files:

 rename -n 's/(\d{3})/0$1/g' myfile_* 
+8
source

There is a UNIX application here called ren ( manpage ), which supports renaming multiple files using search and replace patterns. You should be able to put together a template that will enter an extra value of 0 in the file name.

Edit: The project page with a link to the link can be found on Freshmeat .

+1
source

Try:

 for file in `ls my*` do a=`echo $file | cut -d_ -f1` b=`echo $file | cut -d_ -f2` c=`echo $file | cut -d_ -f3,4` new=${a}_0${b}_${c} mv $file $new done 
0
source

Source: https://habr.com/ru/post/1303652/


All Articles