2

I need to rename only directories and not files, and I want to do it with rename.

Is there any way to do it?

It says nothing in the manual about differentiating between directories and files.

This command changes everything, no matter directory or files:

rename 's/ /\n/g' *

This command solved my problem:

rename 's/ /\n/g' */

I need a slash behind it.

Zanna
  • 70,465
JulianLai
  • 1,572
  • Those links are helpful to me. – JulianLai Jan 15 '19 at 10:30
  • 4
    Have you answered your own question in the question? The trailing slash requires less typing than any method that could have been provided by the developers of rename... I suggest you remove that part of your question and post it as an answer! – Zanna Jan 15 '19 at 14:50
  • but this only partly answers the question (at least the title). How to chose only files? – pLumo Jan 15 '19 at 16:07
  • 2
    by the way ... I would not recommend using newlines in file/folder names. It may cause a lot of trouble. – pLumo Jan 15 '19 at 16:09
  • Why does it cause problems? It really looks better and cleaner. – JulianLai Jan 15 '19 at 16:16
  • 1
    @JulianLai It causes problems for people that use ls | wc -l to count the number of objects, or generally when parsing the output of ls -- which is discouraged, but widely done. – PerlDuck Jan 15 '19 at 16:20
  • 2
    and many many more things. – pLumo Jan 15 '19 at 16:20

1 Answers1

3

rename does not distinguish between files and folders.


The shell is responsible for expanding the wildcard * into files and folders.

  • * will be expanded to all non-hidden files and folders.
  • */ will be expanded to all non-hidden folders.

But I don't know of any way to expand to files only. You can use find -exec instead.

only folders:

rename 's/ /\n/g' */

or

find . -maxdepth 1 -type d -exec rename 's/ /\n/g' {} +

Only files:

find . -maxdepth 1 -type f -exec rename 's/ /\n/g' {} +

Note that find will also find hidden files unlike * (unless you turn on the dotglob shell option by running shopt -s dotglob).


zsh seems to be able to match files only with wildcards.


Disclaimer: I would not recommend adding newlines to file names as it may cause trouble and break many scripts.

wjandrea
  • 14,236
  • 4
  • 48
  • 98
pLumo
  • 26,947