Saturday, May 18, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 486  / 2 Years ago, mon, february 28, 2022, 10:38:27

If I try to run a regex operation on files like so (example):



$ mv *.ext *.new.ext


That does not work.



How do I ensure that when I batch rename or perform an operation on multiple files that they keep their current name (not the extension, just the name)?



Note: I am not looking for something like the rename command because this has to work for everything, not just renaming files.



Note 2: Okay, these answers are good and explains things to me that I didn't properly understand before. Thanks!



The command in question I am trying to use is called firmtool, and is used in the 3DS homebrew scene.



It packs .bin files into .firm files and I would like it to run on all .bin files in a directory, packing them while preserving the original name.



Here is the syntax I need for the firmtool command:



firmtool build test.firm -n 0x23F00000 -e 0 -D arm9loaderhax.bin -A 0x23F00000 -C NDMA


where test.firm is the output and arm9loaderhax.bin is the input.



I want to run this command that if I have a directory with test1.bin and test2.bin that it produces test1.firm and test2.firm. Please let me know if I can use regular expression or a loop to complete this command. Thank you for any help!


More From » command-line

 Answers
0

Hmm. You can't1, not universally.



* is a wildcard. The shell expands it. When you type



*.ext


The shell expands it to a list of all the matching files, like:



1.ext 2.ext whatever.ext


That is what is passed to the command, in your case mv. If mv receives more than two arguments, the last must be a directory, so



mv *.ext *.new


fails with *.new is not a directory, because mv is receiving a long list of arguments.



The shell itself does not support regex, but Linux has many utilities that do, like perl rename...



rename -n 's/(.*).ext/$1.new/' *


And remove -n after testing to really rename...



But you can capture the varying names in a variable and use it to loop over the files...



for f in *.ext; do echo mv -v -- "$f" "${f/.ext/.new}"; done


and remove echo after testing to complete the renaming... You can use almost any command with for, not only mv. Here's a slightly different example based on your question:



for f in *.bin; do
firmtool build "${f%.bin}.firm" -n 0x23F00000 -e 0 -D "$f" -A 0x23F00000 -C NDMA
done


In this case ${f%.bin} will expand to the value of the variable f with the suffix .bin stripped off (if it has such a suffix; otherwise it expands to the value of f).



See the manual on Shell Parameter Expansion for other expansion types.



1waits to be proved wrong


[#11205] Monday, February 28, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
stantildlike

Total Points: 363
Total Questions: 135
Total Answers: 120

Location: Pitcairn Islands
Member since Fri, Dec 17, 2021
2 Years ago
;