How to rename files with different extensions

nalzok

Say I have these files:

essay.aux                   essay.out
essay.dvi                   essay.pdf
essay.fdb_latexmk           essay.tex
essay.fls                   essay.toc
essay.log                   ......

How do I rename them to:

new_name.aux                new_name.out
new_name.dvi                new_name.pdf
new_name.fdb_latexmk        new_name.tex
new_name.fls                new_name.toc
new_name.log                ......

The problem is that they have different extensions rather than different names, so I cannot use answers from this question. Also, I'm on macOS which doesn't have a rename command.

jesse_b

Here is a solution I was able to get working:

#!/bin/bash
shopt -s nullglob

my_files='/root/temp/files'
old_name='essay'
new_name='new_name'

for file in "${my_files}/${old_name}"*; do
    my_extension="${file##*.}"
    mv "$file" "${my_files}/${new_name}.${my_extension}"
done
  • shopt -s nullglob

This will prevent an error if the directory it's parsing is empty

  • for file in "${my_files}/${old_name}"*; do

We are going to loop over every file in /root/temp/files/ so long as it begins with essay

  • my_extension="${file##*.}"

This will greedily trim anything up to the last . found in the filename (hopefully leaving you with only the extension)

  • mv "$file" "${my_files}/${new_name}.${my_extension}"

This moves the old file to the new filename while reserving the extension. (rename)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

zip multiple files with the same name but different extensions

분류에서Dev

Batch file to rename files by adding different suffix to each file

분류에서Dev

How to Rename Multiple Files With Their First 10 Characters?

분류에서Dev

how to batch rename files while coping?

분류에서Dev

How to rename multiple files by adding a common string at beginning of the files?

분류에서Dev

How to rename all files in a directory adding prefix of current unix date

분류에서Dev

How to rename batches of files in sequences of 12 for multiple years?

분류에서Dev

(Automator/AppleScript) Rename files to a folder name, save to different folder & add prefix

분류에서Dev

Recursive rename files and folders

분류에서Dev

Rename/renumerate files in directory

분류에서Dev

Delete and rename files using unlink() and rename()

분류에서Dev

How to change the color of different files in ls

분류에서Dev

QDir::rename() for 2 different partitions

분류에서Dev

Bulk rename files ascending in number

분류에서Dev

cannot rename files with `[ ]` on its name?

분류에서Dev

How to batch rename JPEG files to image date, adding a count that resets for each day?

분류에서Dev

List all files that do not have extensions

분류에서Dev

List all files that do not have extensions

분류에서Dev

How to rename folders in terminal?

분류에서Dev

What's the difference between the different "rename" commands?

분류에서Dev

Rename files in subfolders with python and os.walk()

분류에서Dev

Python - Rename files and give them sequential number

분류에서Dev

Building a DOS batch script to rename files

분류에서Dev

Rename non alphabetical & numeric characters in files with nothing

분류에서Dev

Batch File to rename multiple files and move to folder

분류에서Dev

Excel/VBA: Rename files based on contents

분류에서Dev

Bash - rename 'Image (x).png' files

분류에서Dev

Rename multiple files remove all characters

분류에서Dev

Find, rename and copy files to a new directory

Related 관련 기사

  1. 1

    zip multiple files with the same name but different extensions

  2. 2

    Batch file to rename files by adding different suffix to each file

  3. 3

    How to Rename Multiple Files With Their First 10 Characters?

  4. 4

    how to batch rename files while coping?

  5. 5

    How to rename multiple files by adding a common string at beginning of the files?

  6. 6

    How to rename all files in a directory adding prefix of current unix date

  7. 7

    How to rename batches of files in sequences of 12 for multiple years?

  8. 8

    (Automator/AppleScript) Rename files to a folder name, save to different folder & add prefix

  9. 9

    Recursive rename files and folders

  10. 10

    Rename/renumerate files in directory

  11. 11

    Delete and rename files using unlink() and rename()

  12. 12

    How to change the color of different files in ls

  13. 13

    QDir::rename() for 2 different partitions

  14. 14

    Bulk rename files ascending in number

  15. 15

    cannot rename files with `[ ]` on its name?

  16. 16

    How to batch rename JPEG files to image date, adding a count that resets for each day?

  17. 17

    List all files that do not have extensions

  18. 18

    List all files that do not have extensions

  19. 19

    How to rename folders in terminal?

  20. 20

    What's the difference between the different "rename" commands?

  21. 21

    Rename files in subfolders with python and os.walk()

  22. 22

    Python - Rename files and give them sequential number

  23. 23

    Building a DOS batch script to rename files

  24. 24

    Rename non alphabetical & numeric characters in files with nothing

  25. 25

    Batch File to rename multiple files and move to folder

  26. 26

    Excel/VBA: Rename files based on contents

  27. 27

    Bash - rename 'Image (x).png' files

  28. 28

    Rename multiple files remove all characters

  29. 29

    Find, rename and copy files to a new directory

뜨겁다태그

보관