0

As part of a script to clean up folders on our server, I've written a find regex (on Macos 12.6.x) that will find directories that are named as "YYYY_MM_DD" and then change the name of the directory to "newName".

I will probably mount the server and then run the script from the Mac. I understand it would probably make more sense to move this to the Linux servers and run with gnu find but I'm just learning bash scripting so I have to test on the mac then later change to run on Linux.

I'm testing a find command before I incorporate it in the script. I want to make sure the result is correct.

When I run this find command I get an error "No such file or directory" even though the command worked.

I've been googling and can't figure out why I'm getting this error when the command is working.

This is the find command and result:

$ find -E ./ -type d -regex '.*([0-9]+(_[0-9]+)+)$' -execdir mv {} "newName" \;
find: .//folder1/folder2/2021_03_17: No such file or directory

The directory named "2021_03_17" has been found and renamed as "newName" as I wanted.

Why is it showing "No such file or directory"??

5
  • Why would the version of bash have anything to do with how the find command works? Do you not understand the difference between the shell and the programs you run from it?
    – Barmar
    Mar 31, 2023 at 21:58
  • 2
    Add the -depth option. The problem is that you're renaming a parent directory before descending into it to process the subdirectories.
    – Barmar
    Mar 31, 2023 at 21:59
  • Thank you @Barmar. The depth option makes sense and I can see how that can cause the error. I understand the difference between bash & find. I wasn't sure if the bash shell which is gnu and find which is bsd, would affect something when I put this together. I'll see if I can move this to Unix & Linux.
    – bunk3m
    Apr 1, 2023 at 13:59
  • In general the shell has no impact on the behavior of external programs.
    – Barmar
    Apr 1, 2023 at 15:43
  • Thanks @Barmar.
    – bunk3m
    Apr 1, 2023 at 21:12

1 Answer 1

0

The good command to run:

find -E ./foo -type d -regex '.*([0-9]+(_[0-9]+)+)$' -depth -execdir mv {} "newName" \;
vagrant@freebsd:~ % tree foo
foo
└── bar
    └── 2023_02_23

3 directories, 0 files
vagrant@freebsd:~ % find -E ./foo -type d -regex '.*([0-9]+(_[0-9]+)+)$' -depth -execdir mv {} "newName" \;
vagrant@freebsd:~ % tree foo
foo
└── bar
    └── newName

3 directories, 0 files
1
  • Thank you Gilles and Barmar. It works! Now I can add it to my script. :-)
    – bunk3m
    Apr 1, 2023 at 21:12

You must log in to answer this question.

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