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.
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] 삭제
몇 마디 만하겠습니다