[sf-lug] bash: identifying directories
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 ..
> 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
, 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
Look at me, I am in /tmp/testing/clyde
Look at me, I am in /tmp/testing/some
Thrashing . - /tmp/testing
Thrashing ./clyde - /tmp/testing/clyde
Thrashing ./some - ./clyde.sh: line 1: cd: ./some: No such file or directory
Thrashing ./.getthisasheesh - /tmp/testing/.getthisasheesh
We are what we think. All that we are arises with our thoughts. With
our thoughts, we make the world.
More information about the sf-lug