|
From: | ken |
Subject: | Re: Changing file names with "--" to "-" recursively |
Date: | Sun, 21 Feb 2021 08:41:07 -0500 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.2.1 |
On 02/21/2021 01:45 AM, Jean Louis wrote:
* wael-zwaiter@gmx.com <wael-zwaiter@gmx.com> [2021-02-21 06:07]:I have a lot of files and want to change "--" to "-". Am using rename 's/--/-/g' * on the current directory. How can I recursively go through all sub directories and do the changes for files only.The command: $ rename 's/--/-/g' * does not work on my side as it works on yours, maybe it is different software. You do as following in the top directory: for i in $(find . -type f -iname "*--*"); do mv $i $(echo $i | sed -e "s/--/-/"); done You may alias the command to: alias renamedashes='for i in $(find . -type f -name "*--*"); do mv $i $(echo $i | sed -e "s/--/-/"); done' and invoke it with $ renamedashes in future.
This is very good, especially making it into an alias. Two small improvements perhaps:
First, check to see if a file with the new name doesn't already exist... for if it does, it would be overwritten and lost. Changing "mv" to "mv -i" would also notify the user of such events.
Secondly, unfortunately some filenames may contain whitespace. In such a case the mv command will give unexpected results. So the arguments to mv should be quoted.
hth
[Prev in Thread] | Current Thread | [Next in Thread] |