How to rename with find?

HelterSkelter

New Member
Joined
Sep 23, 2025
Messages
3
Reaction score
2
Credits
25
Hi!

I have a lot of file backups line abc.txt.old or xyz.jpg.bak in several subdirs.
I can find them with (for example)

find -name "*.txt.old"
Question: How do I rename them by removing the ".old"?
Thanks for all help!
 


Thanks for your input but it doesn't work. I'm on CachyOS and it says:
Code:
fish: ${ is not a valid variable in fish.
for i in $(find . -type f);do mv $i ${i%.*}; done
                                     ^
 
Thanks for your input but it doesn't work. I'm on CachyOS and it says:
Code:
fish: ${ is not a valid variable in fish.
for i in $(find . -type f);do mv $i ${i%.*}; done
                                     ^
Aha! You have a fish shell, which was the trouble because post #2 assumed a bash shell which is the default for most distros I'm familiar with, but you didn't provide the relevant details. Never mind, the following might work for you, but I haven't tested it in fish:
Code:
$ ls
file1.txt.bak  file2.txt.bak  file3.txt.bak  file4.txt.bak

$ for i in $(ls) ;do mv $i $(echo $i|cut -d. -f1); done

$ ls
file1 file2  file3  file4

If you only wanted the .bak removed, change the field variable to the cut command from -f1 to -f1,2
Code:
$ ls
file1.txt.bak  file2.txt.bak  file3.txt.bak  file4.txt.bak

$ for i in $(ls) ;do mv $i $(echo $i|cut -d. -f1,2); done

$ ls
file1.txt  file2.txt  file3.txt  file4.txt
 
Thanks for your support but it doesn't work. This time without any error message, it just does nothing and i have to press ctrl-c to get the cursor back.

It is very important, that in the examples above only the file.txt.bak are renamed in file.txt. There are some files called "anyfile.bak" among them which must keep the ".bak"
 
Thanks for your support but it doesn't work. This time without any error message, it just does nothing and i have to press ctrl-c to get the cursor back.

It is very important, that in the examples above only the file.txt.bak are renamed in file.txt. There are some files called "anyfile.bak" among them which must keep the ".bak"
Sorry about the failure. I guess you could install bash, and the commands would work as shown. Interestingly, the install sizes in debian of the two shells are: fish at 9511K, and bash at 7273K. These things can just be a matter of personal preference of course. Certainly there would be commands in fish to achieve the aims you have described.

In relation to keeping some files in a directory with .bak and some removing the suffix, there are a few approaches to that issue. One would be to write a script which can identify the files to keep the suffix with an "if then" controlling feature. Another approach would be to move all the files that need to have the .bak removed to their own directory for the operation, and then returned to the original if required. It's just a few relatively simple commands to achieve that. There are other approaches too.
 
Last edited:
Fish shell handles loops and variable expansion differently, which is why it doesn't behave the same way.


Here’s how you can rewrite your command to work properly.

Fish Shell
Code:
for i in (find . -type f)
    mv $i (string replace -r '\.[^.]*$' '' $i)
end


for i in (find . -type f) — Fish uses parentheses () for command substitution instead of $().
string replace -r '\.[^.]*$' '' $i — This uses Fish's string command with a regular expression to strip the file extension.
 
let me give you a generic response. This is a link to an AI that will give you the commands you want for just about anything. But you do need to specify your linux flavor and what you want to do.

 


Follow Linux.org

Staff online

Members online


Top