1

I have a UNIX script which is supposed to scan all the folders and return the files older than speciific amount of time. I gave the logic as below, but the script is not able to read the folders with spaces. Can anyone please help on how to make this work?

dir='/a/b/test';

script is scanning all the sub directories after test, but not able to scan the directories with spaces, example 'test script'.

Karthik
  • 11
  • 1
  • 1
    Please edit your question and add the script you are using so that we can help you with that rather than shooting blindly.. – heemayl Jun 16 '15 at 20:27
  • possible duplicate of [Why does my shell script choke on whitespace or other special characters?](http://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters) – Stéphane Chazelas Jun 16 '15 at 21:01

1 Answers1

2

So you have something like this:

$ foo='my dir'
$ ls $foo
my: no file or directory found
dir: no file or directory found

Instead, try this:

$ foo='my dir'
$ ls "$foo"

Or even better:

$ ls "${foo}"

Both of these methods shield the [:space:] character from being interpreted by the shell, and ensures that it is read as a [:space:] character, not the breaking point for a command.

It's a very good habit to get into when writing shell scripts!

Klaatu von Schlacker
  • 3,028
  • 13
  • 15