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
  • 72,312
JulianLai
  • 1,592

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,504
pLumo
  • 27,991