Knowledgebase

Bash Scripting question

Posted by mindbend, 04-04-2009, 01:38 PM
I'm writing an inode and directory size counter, but hit a snag with directories that contain a space. Simple code, finds all directories within a folder, sets the current directory: for i in `find . -type d`; do ls $i; done Looks correct? It works great, until you hit directories with spaces. So I try the following methods: for i in `find . -type d|sed 's/ /\\ /g'`; do ls $i; done for i in `find . -type d|sed 's/ /\\ /g'`; do ls "$i"; done What is happening is, at the ( for i in ), it treats each item at the first break, if its \n or a ' '. Is there a flag I can set to make it only use \n? When I pipe the data, it sends each chunk of the directory through. [root@home /home/mindbend/dev_html]# for i in `find . -type d|sed 's/ /\\ /g'`; do ls $i; done ls: ./test: No such file or directory ls: ing: No such file or directory ls: 12: No such file or directory ls: 3: No such file or directory ls: ./test: No such file or directory ls: ing: No such file or directory ls: 12: No such file or directory ls: 3/test: No such file or directory ls: 2: No such file or directory # ls -d test\ ing\ 12\ 3/ test ing 12 3/ #ls -d test\ ing\ 12\ 3/test\ 2/ test ing 12 3/test 2/ Any ideas? GNU bash, version 3.2.39(1)-release (i386-portbld-freebsd7.1) Copyright (C) 2007 Free Software Foundation, Inc. Issue exists on linux and freebsd, same code.

Posted by mindbend, 04-04-2009, 02:28 PM
Found a fix for it, in case anyone wants to know -=-- # IFS=$'\012' # for i in `find . -type d`; do set -- $i; ls -d $i; done . ./images ./images/nav ./test ing 12 3 ./test ing 12 3/test 2 -=-- I'll post a link later to the entire inode counter script when I have completed it. Thx for the reads!

Posted by nwilkens, 04-04-2009, 07:13 PM
Another potential solution: find ./ -type d|while read i do ls -ld "$i" done

Posted by mindbend, 04-06-2009, 02:00 AM
Thx for the tip, I have tried it both ways and the processing speed appears to be close enough for a fast seek that both methods are feasible for this. As promised in case anyone is interested, here is a quick script I whipped out ( freebsd ). The purpose of this script is to audit a directory tree to find inode/disk usage. -=-- IFS=$'\012'; for d in `find . -type d`; do s=$(ls -AlF $d | grep -v / | awk '{ $5 = $5/1024; sum += $5; } END { print sum}') c=$(ls -AF $d|wc -l) ; printf "$c\t\t $s Kb\t\t\t\t - $d\n" >>usage; done ;
Output:
      17                 37.0977 Kb                              - ./dev_html
      13                 24.3496 Kb                              - ./dev_html/images
       2                 0.289062 Kb                             - ./dev_html/images/nav
      27                 238.59 Kb                               - ./dev_html/images-old
      54                 263.379 Kb                              - ./dev_html/images-old/cards
       3                 1.18945 Kb                              - ./dev_html/radmin



Was this answer helpful?

Add to Favourites Add to Favourites

Print this Article Print this Article

Also Read
RAID over IP (Views: 606)
Fully :: Partilly (Views: 622)
burst.net down? (Views: 676)


Language:

Client Login

Email

Password

Remember Me

Search