1

What's a command I could use to do this on fedora 29 I need to remove [,], and -. If possible just remove the characters. Otherwise I'm good with replacing them as '_' I have 2,400+ files and directories with sub directories named similar to:

TWENTY_ONE_PILOTS_-_DISCOGRAPHY_(2009-15)_CHANNEL_NEO]

I need it to be easy to make a directory out of. For example:

TWENTY_ONE_PILOTS_DISCOGRAPHY_2009_15_CHANNEL_NEO

I've tried:

find . -depth -name '*]*' -execdir bash -c 'for i; do mv "$i" "${i

│// /_}"; done' _ {} +

And

for x in *]*; do mv -- "$x" "${x//[/}"; done

Andd

rename 's/\(|\[|\]|\)//g' *
  • Should all _-_ be converted to just _? – Inian Mar 15 at 3:06
  • If possible. Or any double __ to single _ I'm just trying to get the files to be human and machine readable. – RobertW Mar 15 at 3:10
  • In for x in *]*; do mv -- "$x" "${x//[/}"; done try escaping the replaced right bracket: for x in *]*; do mv -- "$x" "${x//\[/}"; done – RonJohn Mar 15 at 3:13
  • Better yet, use sed to generate the new names and echo them to verify that it works. Then add the mv command. – RonJohn Mar 15 at 3:14
  • I get mv: cannot move 'Rag’n’Bone_Man_Human_2017]_320' to a subdirectory of itself, 'Rag’n’Bone_Man_Human_2017]_320/Rag’n’Bone_Man_Human_2017]_320' When using the above codes. – RobertW Mar 15 at 3:16
1

With zsh:

$ autoload zmv
$ zmv -n '(**/)(*)' '$1${${${2//[^.[:alnum:]]##/_}##_#}%%_#}'
mv -- TWENTY_ONE_PILOTS_-_DISCOGRAPHY_\(2009-15\)_CHANNEL_NEO\] TWENTY_ONE_PILOTS_DISCOGRAPHY_2009_15_CHANNEL_NEO

Turns all sequences of characters other than alnums and . to _, and removes the leading and trailing _s afterwards, in all files and directories, recursively.

-n is for dry run, remove it when happy.

If you only want to rename files of type directory, change the (**/)(*) to (**/)(*)(#q/)

0

You can use : find . -exec sh -c './correct.sh {}' \; !

With correct.sh in the same directory as you launch the above command :

#!/bin/bash

mv "$1" $(echo "$1" | sed 's/_-_/_/g');

(Don't forget to chmod +x correct.sh !)

It is not a very elegant solution but it work though. Everything is self explanatory except maybe the -exec which is an option of find that will execute for each name of the files, the process mentionned (here sh -c '...') and replace {} by the file's name ! See find man pages for more informations.

If you want to rename only the file directory, insert -type d as an option of find. And of course, you can modify as much as you like the sed to replace what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.