How to rename files without changing extension in Linux 102221.pdf to 102221_name.pdf

前端 未结 3 1418
时光取名叫无心
时光取名叫无心 2021-01-16 09:46

How to rename files without changing extension in Linux \\

102221.pdf to 102221_name.pdf

3条回答
  •  长情又很酷
    2021-01-16 10:14

    ls * | sed -r 'p;s/\.pdf$/_name\.pdf/g' | xargs -n2 mv
    

    list all the files with ls and pipe the output to sed. sed replaces .pdf with _name.pdf and outputs both the original file name and the new file name to xargs with will call mv with the 2 parameters.

    you can also use the rename command which is simpler

     rename 's/\.pdf$/_name\.pdf/g' ./*
    

    The regex pattern remains the same though

提交回复
热议问题