![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2012
Posts: 2
Rep Power: 0
![]() |
Searching Directory Tree
Hello. I've written a script that searches a directory tree and performs certain tests on all files and folders of any subfolder. The script itself works, but it always creates an empty file with name "0" at the root of the directory tree. What could be the cause of that ? Since I'm fairly new to bash, I'd appreciate any help. Thank you in advance. Here is the code:
cd $TEST_DIR
for tdir in $(find -type d | sort); do
cd "$tdir"
isdir=0; isfile=0;
for cdir in $(ls -1); do
if [ -d "$cdir" ]; then ((isdir++)); fi
if [ -f "$cdir" ]; then ((isfile++)); fi
done
for i in $(seq $(echo "$tdir" | grep -o "/" | wc -l | sed s/\ //g)); do
cd ..
done
if [[ "$isdir" -gt 0 && "$isfile" -gt 0 ]]; then
printf "%s: directories and other files mixed in: %s\n" "$0" "$tdir"
fi
doneTEST_DIR is the directory tree supplied in script arguments. |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Nov 2011
Posts: 149
Rep Power: 2
![]() |
Re: Searching Directory Tree
hi,
don't use the form `for i in $(command); do', it'll loop on each word of command output. don't use `for i in $(find); do', instead use pathname expansion; and `find' has a very useful feature called `-exec'. find $test_dir -type d -exec /bin/bash -c '
for f in "$1"/*
do [ -d "$f" ] && ((isdir++))
[ -f "$f" ] && ((isfile++))
done
(( isdir && isfile )) && printf "%s: directories and other files mixed in: %s\n" "$0" "$1"
' na {} \; | sort |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2012
Posts: 2
Rep Power: 0
![]() |
Re: Searching Directory Tree
Thank you. That seems to have solved the problem and looks cleaner.
This time I got stuck on a different problem. I need to check contents of several files and verify they only contain whole decadic number with multiple digits terminated with newline character 0x0A.For example if I execute the following: printf "1234\n" >test-file The condition will be met. But it won't be in these cases. printf "1234" >test-file printf "1234\n\n" >test-file printf "\n" >test-file I tried to store the contents of something like this into variable. var=$(cat test-file | grep -E "[^0-9]^\n") Thanks again in advance. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Need help fixing Binary Search Tree java code | fistpunch | Java | 3 | Dec 6th, 2010 9:16 AM |
| Tree in C | Apprentice123 | C++ | 9 | May 9th, 2010 7:45 PM |
| Tree, Infix notation, Mathematical Expressions | S.I | C | 11 | May 7th, 2010 4:05 PM |
| display and searching through the contents of a windows directory | cwl157 | C++ | 2 | May 28th, 2007 9:59 PM |
| BinaryTree , I need help on inserting expression to the Tree | Master | C# | 3 | Oct 14th, 2005 3:42 PM |