[sf-lug] bash: identifying directories

Clyde Jones slash5toaster at gmail.com
Sat Jun 7 14:47:54 PDT 2008


On Sat, Jun 7, 2008 at 11:37 AM, Asheesh Laroia <asheesh at asheesh.org> wrote:
> On Tue, 3 Jun 2008, Matt Price wrote:
>
>> another little scripting question:
>>
>> i'd like this tiny script i'm writing to scan the current directory,
>> identify the subdirectories, and then do something in each of them.  so
>> in pseudocode:
>>
>> [find dirs]
>>
>> for DIR in $dirs ; do
>>       cd $DIR
>>       [do some stuff]
>>       cd ..
>> done
>
> As MP suggested, you use "find -type d" if you really want just
> directories.  But if you do that, you're going to end up accidentally sad
> if there are spaces in their filenames, or intentionally careful to avoid
> that problem.
>

True - that is the purpose of setting the $IFS at the top of my script
[1],  that makes bash use the carriage return as a field delimiter
instead of spaces plus carriage returns (you could set it to anything
you want; tabs, commas, colons etc).

However Michael correctly points out that bash will allow you to have
a carriage return as *part* of the name (like the fscking "Icon<cr>"
name on Macs), my script will specifically miss those directories.

Your script will miss any .directories (which may or maynot be bad thing)

So far Michael is the winner in the bash scripting contest.

[testing]$ ls -ab
.  ..  asheesh.sh  clyde  clyde.sh  .getthisasheesh  michael.sh  some\n

[testing]$ ./asheesh.sh
/tmp/testing/clyde /tmp/testing
Look at me, I am in /tmp/testing/clyde
/tmp/testing
/tmp/testing/some
 /tmp/testing
Look at me, I am in /tmp/testing/some

/tmp/testing

[testing]$ ./clyde.sh
Thrashing . - /tmp/testing
Thrashing ./clyde - /tmp/testing/clyde
Thrashing ./some - ./clyde.sh: line 1: cd: ./some: No such file or directory
/tmp/testing
Thrashing ./.getthisasheesh - /tmp/testing/.getthisasheesh

[testing]$ ./michael.sh
cd /tmp/testing/clyde
cd /tmp/testing/some
cd /tmp/testing/.getthisasheesh
[testing]$

Clyde


[1] http://www.faqs.org/docs/bashman/bashref_33.html
-- 
We are what we think. All that we are arises with our thoughts. With
our thoughts, we make the world.
-Buddha




More information about the sf-lug mailing list