2

My directory tree is like the following -

/media/blueray/Data/Test$ tree
.
├── What????
│   └── What????
└── What????.txt

When I run the command find . -depth | rename 's/[?<>\\:*|\"]//g'

It says -

Can't rename ./What????/What???? ./What/What: No such file or directory

However, after running the command, the directory structure becomes -

/media/blueray/Data/Test$ tree
.
├── What
│   └── What????
└── What.txt

It appears to me that find is starting to search from the top of the tree, so it is loosing track of the tree.

What is the solution here?

1 Answer 1

6

The problem isn’t caused by find, it’s caused by rename: it’s processing the full path, so it tries to rename ./What????/What???? to ./What/What, which fails because the What directory doesn’t exist (yet).

Getting rename to process filenames only, with -d, will fix this:

find . -depth | rename -d 's/[?<>\\:*|\"]//g'

If your version of rename doesn’t support the -d option (introduced with version 1.10 of File::Rename, assuming your rename is the Perl version), you can get find to pass the filenames only by running rename inside each directory:

find . -depth -execdir rename 's/[?<>\\:*|\"]//g' {} +

(Thanks to Freddy for the solution.)

6
  • It says Unknown option: d Nov 23, 2019 at 8:33
  • 1
    What’s the output of rename -V? Nov 23, 2019 at 8:49
  • /usr/bin/rename using File::Rename version 0.20 Nov 23, 2019 at 8:50
  • 1
    @blueray Try find . -depth -execdir rename 's/[?<>\\:*|\"]//g' {} +
    – Freddy
    Nov 23, 2019 at 8:54
  • @Freddy Correct. Thanks. Nov 23, 2019 at 8:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .