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 530697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:13:27+00:00 2026-05-13T09:13:27+00:00

The day came when I had to write a BASH script that walks arbitrary

  • 0

The day came when I had to write a BASH script that walks arbitrary directory trees and looks at arbitrary files and attempts to determine something regarding a comparison among them. I thought it would be a simple couple-of-hours tops! process – Not So!

My hangup is that sometimes some idiot -ahem!- excuse me, lovely user chooses to put spaces in directory and file names. This causes my script to fail.

The perfect solution, aside from threatening the guillotine for those who insist on using spaces in such places (not to mention the guys who put this in operating systems’ code!), might be a routine that “escapes” the file and directory names for us, kind of like how cygwin has routines to convert from unix to dos filename formats. Is there anything like this in a standard Unix / Linux distribution?

Note that the simple for file in * construct doesn’t work so well when one is trying to compare directory trees as it ONLY works on “the current directory” – and, in this case as in many others, constantly CDing to various directory locations brings with it its own problems. So, in doing my homework, I found this question Handle special characters in bash for…in loop and the proposed solution there hangs up on spaces in directory names, but can simply be overcome like this:

dir="dirname with spaces"
ls -1 "$dir" | while read x; do
   echo $x
done

PLEASE NOTE: The above code isn’t particularly wonderful because the variables used inside the while loop are INACCESSIBLE outside that while loop. This is because there’s an implied subshell created when the ls command’s output is piped. This is a key motivating factor to my query!

…OK, the code above helps for many situations but “escaping” the characters would be pretty powerful too. For example, dir above might contain:

dir\ with\ spaces

Does this already exist and I’ve just been overlooking it?

If not, does anyone have an easy proposal to write one – maybe with sed or lex? (I’m far from competent with either.)

  • 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-05-13T09:13:27+00:00Added an answer on May 13, 2026 at 9:13 am

    Make a really nasty filename for testing:

    mkdir escapetest
    cd escapetest && touch "m'i;x&e\"d u(p\nmulti)\nlines'\nand\015ca&rr\015re;t"
    

    [ Edit: Chances are that I intended that touch command to be:

    touch $'m\'i;x&e\"d u(p\nmulti)\nlines\'\nand\015ca&rr\015re;t'
    

    which puts more ugly characters in the filename. The output will look a little different. ]

    Then run this:

    find -print0 | while read -d '' -r line; do echo -en "--[${line}]--\t\t"; echo "$line"|sed -e ':t;N;s/\n/\\n/;bt' | sed 's/\([ \o47()"&;\\]\)/\\\1/g;s/\o15/\\r/g'; done
    

    The output should look like this:

    --[./m'i;x&e"d u(p
    multi)
    lines'
    re;t]--         ./m\'i\;x\&e\"d\ u\(p\\nmulti\)\\nlines\'\\nand\\015ca\&rr\\015re\;t
    

    This consists of a condensed version of Pascal Thivent’s sed monster, plus handling for carriage returns and newlines and maybe a bit more.

    The first pass through sed merges multiple lines into one delimited by “\n” for filenames that have newlines. The second pass replaces any from a list of characters with a backslash preceding itself. The last part replaces carriage returns with “\r”.

    One thing to note is that, as you know, while will handle spaces and for won’t but by sending the output of find with null termination and setting the delimiter of read to null, you can also handle newlines in filenames. The -r option causes read to accept backslashes without interpreting them.

    Edit:

    Another way to escape the special characters, this time without using sed, uses the quoting and variable-creating feature of the Bash printf builtin (this also illustrates using process substitution rather than a pipe):

    while read -d '' -r file; do echo "$file"; printf -v name "%q" "$file"; echo "$name"; done< <(find -print0)
    

    The variable $name will be available outside the loop, since using process substitution prevents the creation of a subshell around the loop.

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

Sidebar

Ask A Question

Stats

  • Questions 352k
  • Answers 352k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This should help you with the ball color; function drawBall… May 14, 2026 at 7:25 am
  • Editorial Team
    Editorial Team added an answer Look at this list from the mono project (Companies using… May 14, 2026 at 7:25 am
  • Editorial Team
    Editorial Team added an answer Because you are missing the <head> tag and doctype declaration,… May 14, 2026 at 7:25 am

Related Questions

During the work on my iPhone application the icon changed from time to time.
This was originally a question I wanted to ask, but while researching the details
Can anybody tell me why my Database selection queries asking for an orderid WHERE
When using Subversion (svn) for source control with multiple projects I've noticed that the
A friend asked me for help on building a query that would show how

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.