help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Changing file names with "--" to "-"


From: Robert Thorpe
Subject: Re: Changing file names with "--" to "-"
Date: Mon, 22 Feb 2021 04:42:41 +0000

wael-zwaiter@gmx.com writes:

> 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.

People have provided lots of good suggestions for doing this with the
shell.  You can also do this with Emacs.  I would tend to do it with
Emacs myself.

Firstly do "M-x find-name-dired".  You can type "M-x f-n-d" then press
tab.  Since Emacs has separate expansion for each word these days, that
will auto complete.

Then fill in the directory to start from.  After that it will prompt you
"Find name (file wildcard): ".  Just put in "*".  That will find all
files in the directory and every subdirectory.

Wait until find has finished (see it's mode line indicator).  Then
you'll have a dired buffer containing every file and sub-directory in
that directory.

Now, enable writeable dired with "C-x C-q".  Now you can treat the dired
buffer as a normal buffer.  Then use normal search-and-replace.  You can
use "M-%" to replace "--" with "-".  You can use query-replace or
replace all using "a".  Then if everything is ok press "C-c C-c" to make
the actual changes.  If things are wrong then press "C-c ESC".  Emacs
will then spend a while doing the renames.

The contents of the buffer will disappear at this point.  That's a
little disconcerting, but anyway, you're finished.  All of the files are
renamed.  To view them do "M-x find-name-dired" again.

This method will also rename *sub-directories* containing "--", which
might not be what you want.  Using query-replace and checking everything
is a way to avoid that.

BR,
Robert Thorpe



reply via email to

[Prev in Thread] Current Thread [Next in Thread]