简体   繁体   中英

How to rename all files in a directory in linux with string substitution?

i want to rename multiple files in my linux system directory....

my file names are as:

Lec 1 - xxx.webm
Lec 2 - xxx.webm
Lec 3 - xxx.webm
Lec 4 - xxx.webm

and the list goes on...

here xxx could be any list of characters(not consistent)....

i would like to rename every file in here like:

mv Lec 1 - xxx.webm Lec 1.webm
mv Lec 2 - xxx.webm Lec 2.webm
mv Lec 3 - xxx.webm Lec 3.webm

and so on....

for in loop could do but how to do the substitution?

*strip all characters after the number should be my renamed file

This for loop should do the job:

for f in *.webm; do
   mv "$f" "${f/ -*/}.webm"
done

${string%substring} : deletes shortest match of $substring from back of $string .

for i in *.webm; do mv $i ${i%xxx}; done

Or check out:

${string%%substring} : deletes longest match of $substring from back of $string .

If you have util-linux-ng installed:

find . -name "Lec*.webm" | xargs rename s/ -*//

or:

for file in $(find . -name "Lec*.webm")
do 
  echo mv "$file" "`echo $file | sed s/ -*$//`"
done

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