You are not logged in.

#1 2021-03-22 23:39:18

shoelesshunter
Member
From: USA
Registered: 2014-05-18
Posts: 289

[solved] need help with find / rename and a regex

I am trying to remove all double spaces from directory names  in a particular path.

I have tried this:

find . -type d -exec rename "s/\s\+/ /g" {} \;

But it is not doing the trick. Any suggestions?

Last edited by shoelesshunter (2021-03-23 11:03:08)

Offline

#2 2021-03-23 01:18:35

jonno2002
Member
Registered: 2016-11-21
Posts: 893

Re: [solved] need help with find / rename and a regex

this seems to work

find . -type d -name "*  *" -exec rename -v "  " " " {} +

remove the "-v" if you dont want verbose output

Offline

#3 2021-03-23 01:55:43

ponyrider
Member
Registered: 2014-11-18
Posts: 112

Re: [solved] need help with find / rename and a regex

rename will rename the specified files by replacing the first occurrence of expression in their name by replacement.

this will change all double spaces to single spaces

find . -type d -name "*  *" | xargs -i sh -c 'v="{}"; printf -v singlespace "$(echo $v | sed "s:  : :g")"; mv "$v" "$singlespace"'

EDIT
Uses bash global substitution

find . -type d -name "*  *" | xargs -i sh -c 'v="{}"; mv "$v" "${v//  / }"'

Last edited by ponyrider (2021-03-23 02:37:37)

Offline

#4 2021-03-23 02:29:30

eschwartz
Fellow
Registered: 2014-08-08
Posts: 4,097

Re: [solved] need help with find / rename and a regex

shoelesshunter wrote:

I am trying to remove all double spaces from directory names  in a particular path.

I have tried this:

find . -type d -exec rename "s/\s\+/ /g" {} \;

But it is not doing the trick. Any suggestions?

I notice you're trying to pass a regular expression as an argument to /usr/bin/rename, the crippled coreutils binary that only supports string literal replacements. Perhaps you meant to use /usr/bin/perl-rename, the superior program which lets you use perl-compatible regular expressions when renaming things?


Managing AUR repos The Right Way -- aurpublish (now a standalone tool)

Offline

#5 2021-03-23 03:14:55

GaKu999
Member
From: US/Eastern
Registered: 2020-06-21
Posts: 696

Re: [solved] need help with find / rename and a regex

An alternative if you like (neo)vi(m), vidir from moreutils. smile

Tho methinks it should work with any text editor.

Last edited by GaKu999 (2021-03-23 03:17:34)


My reposSome snippets

Heisenberg might have been here.

Offline

#6 2021-03-23 11:02:40

shoelesshunter
Member
From: USA
Registered: 2014-05-18
Posts: 289

Re: [solved] need help with find / rename and a regex

thanks vidir did the trick.

Offline

Board footer

Powered by FluxBB