简体   繁体   中英

Renaming a set of files in linux

I have 100 files named sub1.txt, sub2.txt,..., sub100.txt . I like to rename them as all1.txt, all2.txt,..., all100.txt . How can one do this in linux (unix)?

Thanks for your help.

The script below will rename an arbitrary number of file with the pattern sub*.txt . This also does a dry-run thanks to the echo . Simply remove the echo once you are satisfied with the results.

#!/bin/bash

for file in sub*.txt; do
  echo mv "$file" "all${file#sub}"
done

Using the util-linux-ng version of rename :

rename sub all sub*.txt

Using the Perl script version of rename :

rename 's/^sub/all/' sub*.txt
for i in `seq 1 100`; do mv sub$i.txt all$i.txt; done

要么

for i in sub*.txt; do j=`echo $i|sed -e s/sub/all/`; mv $i $j; done
for F in sub*.txt ; do mv $F all${F#sub}; done

红宝石(1.9+)

ruby -e 'Dir["sub*.txt"].each {|x| File.rename(x, x.gsub(/^sub/,"all") ) }'

There is always a new way to do it:

$ ls sub*.txt | tr -d "sub" | xargs -I{} mv sub{} all{}

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM