Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8004139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:50:17+00:00 2026-06-04T16:50:17+00:00

I’m trying to rename files with spaces and other such script breaking characters in

  • 0

I’m trying to rename files with spaces and other such script breaking characters in them.

The platform is Ubuntu server 12 (with Bash 4) and any instance of the ‘rename’ command refers to Prename aka Perl Rename.

I’m either having a problem writing a suitable regex, or I’m having a problem creating a valid variable from the find command.

Here’s my maturely named test data (all files, no directories):

/test/-poop
/test/poopfile.txt
/test/po_________!!!!!!!! $$^^^   op.sh

(EDIT) Expected output: I want to change the filenames to

/test/poop
/test/poopfile.txt
/test/po_op.sh

(/EDIT)

If I run this command in /test:

find * -type f -exec rename -n 's/[^a-zA-Z0-9-_.]/_/g; s/_+/_/g' {} \;

I of course get:

find: unknown predicate `-poop'

So I changed things: (find * -> find . so that the results of the find command are prefixed with ./)

find . -type f -exec rename -n 's/[^a-zA-Z0-9-_.]/_/g; s/_+/_/g' {} \;

Which returns

./-poop renamed as ._-poop
./poopfile.txt as ._poopfile.txt
./po_________!!!!!!!! $$^^^   op.sh renamed as ._po_op.sh

That’s no good. All my files are now prefixed _ because my regex did exactly what I told it to. If I didn’t have to cope with files that had a leading “-” then I’d be fine, but that’s exactly the kind of thing I’m trying to treat with this script.

This is where I’m stuck – I can’t figure out how to get my regex to not match either the first two characters in a line, or the first instance of “./”

^.{2} ## matches the first two characters in the string
^./   ## matches the ./ at the start of the line (assuming I change my delimiter from /)

But how do I say “Anything that is NOT the first two characters in the string and is NOT a character in [a-zA-Z0-9.-_]?”

OR

Am I doing this entirely wrong? Is there a way that I can pass the results of the find command to the rename script so that a leading “-” does not cause find to barf?

Actually thinking on those lines – I wrote this:

for f in $( find . -type f ); do
    p=$( printf "%s\n" "$f" | sed 's/^..//' );
    rename -n 's/[^a-zA-Z0-9-_.]/_/g; s/_+/_/g' "$p";
done

Result:

Unknown option: p
Unknown option: o
Unknown option: o
Unknown option: p
Usage: rename [-v] [-n] [-f] perlexpr [filenames]

So even if I can get find to hand “-poop” to rename as “-poop” it’s still barfing on the leading hyphen.

Pretty sure the answer lies in the regex.

I’m very grateful for any guidance that can be offered! This is my first question here – please let me know if I’ve made mistakes or etiquette errors. I’m very aware that my knowledge of regex is poor, but I think I’ve reached the point on this where not asking a question would be the silly course of action.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:50:19+00:00Added an answer on June 4, 2026 at 4:50 pm

    Use rename -n -- $regex $filenames.

    The key here is --, which is a widely used method of saying “options end here, filenames start here”. find itself doesn’t support it, but rename does.

    Some more details here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.