2

This question already has an answer here:

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.

marked as duplicate by Kulfy, karel, Elder Geek, waltinator, Thomas Jan 16 at 18:22

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Those links are helpful to me. – JulianLai Jan 15 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 at 14:50
  • 2
    by the way ... I would not recommend using newlines in file/folder names. It may cause a lot of trouble. – pLumo Jan 15 at 16:09
  • 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 at 16:20
  • 2
    and many many more things. – pLumo Jan 15 at 16:20
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.

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