0

Please forgive me, I'm a shell noob. I need to rename a lot of directories, and would usually use a GUI utility for this, but my server access is via SMB over two VPN's, there are tens of thousands of files, and trying the GUI utility on a tiny subfolder was taking hours... So I'm using SSH, since it's many orders of magnitude faster.

I'm CD'ing to a directory with subfolders containing all the directories I need renamed, let's call it Direct1. The directories I need to rename, let's call RenameDirect

It looks like this:

/Direct1/[arbitrary directory]/SXS/[RenameDirect]/[arbitrary directories and files]

Each directory I need to rename (RenameDirect) is like this:

[arbitrary text][six-digit number][one or two spaces][4-digit year > 2000][arbitrary text]

I need to find the ones with TWO spaces in the "one or two spaces" position and change them all to ONE space there. The 2 spaces are all an error. There will be a few files with double spaces in the rest of the arbitrary text that need to preserved, so I can't just replace all double spaces.

Here's what I've come up with:

(IFS=$'\n'
find . -maxdepth 3 -type d -regextype sed -regex '.*/SXS/.*[0-9]\{6\}  20' | while read f; do
mv "$f" "$(dirname "$f")/$(basename "$f" | sed 's,\([0-9]\)  \(2\),\1 \2,')"
done)

I'm pretty sure the "while," "mv," and "sed" are all working from testing them on sample folders. It's the regex arguments to "find" that are failing.

Any help would be appreciated,

Thanks.

1 Answer 1

1

OK, I'm embarrassed to have posted given the mistake I realized. Forgot a final ".*" in the "find" regex to continue matching after the first two digits of the year, "20"

Working version was:

(IFS=$'\n'
find . -maxdepth 3 -type d -regextype sed -regex '.*/SXS/.*[0-9]\{6\}  20.*' | while read f; do
    mv "$f" "$(dirname "$f")/$(basename "$f" | sed 's,\([0-9]\)  \(2\),\1 \2,')"
done)
2
  • Although if I'm making this way too complicated, I'd still love to hear alternate solutions and learn a thing or two.
    – Tom Spoon
    Commented Nov 24, 2019 at 3:58
  • Also, I was learning RegEx for this and started with the sed expression, then learned more for the "find" expression. I see now that I should have gone back to my sed regex and made sure I was matching a six-digit number and two digits of the year to be sure I didn't accidentally hit anything I shouldn't. But I wasn't using "g" with sed, so it would only hit the first occurance. Given the dataset, it's extremely unlikely there was a single miss there.
    – Tom Spoon
    Commented Nov 24, 2019 at 4:06

You must log in to answer this question.

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