testing if a file is a directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting testing if a file is a directory
# 1  
Old 06-10-2009
testing if a file is a directory

i have written this simple script called isdir.sh

Code:
#! /bin/bash 
dir=$1
_ls=`ls $dir`
for file in $_ls
do
    if [ -d "$file" ]
    then
        echo "D $file"
    fi
done

the output is not right.
for example

$ ./isdir.sh src
***no output***

but i have in ~/src some directories

drwxr-xr-x 2 and and 4096 2009-06-04 17:39 awk
drwxr-xr-x 2 and and 4096 2009-06-10 19:16 bash
drwxr-xr-x 2 and and 4096 2009-06-07 13:25 c
-rw-r--r-- 1 and and 415810 2009-05-19 21:59 prog.tar.gz
drwxr-xr-x 4 and and 4096 2009-06-09 22:20 python

what is wrong?Thank you!

Last edited by vidyadhar85; 06-10-2009 at 05:56 PM.. Reason: code tag added
# 2  
Old 06-10-2009
from where you are running this scipt??
because you are looking for dir in src but for that you should run this from src
or change dir to src in the begining of script.
Code:
#! /bin/bash 
cd ~/$1
for file in * 
do
if [ -d "$file" ]
then
echo "D $file"
fi
done

# 3  
Old 06-10-2009
Quote:
Originally Posted by and77
i have written this simple script called isdir.sh

When you post code, please wrap it in [code] tags.
Quote:
Code:
#! /bin/bash 
dir=$1
_ls=`ls $dir`
for file in $_ls


That will fail if any filenames contain spaces. Use:

Code:
for file in "$dir"/*

Quote:
Code:
do
    if [ -d "$file" ]
    then
        echo "D $file"
    fi
done

the output is not right.
for example

$ ./isdir.sh src

Put scripts in a central location, e.g., $HOME/bin, and add that to your PATH.
Quote:
***no output***

but i have in ~/src some directories

Are you in ~ when you issue that command?

If not, use the full path to the directory:

Code:
./isdir.sh ~/src

# 4  
Old 06-11-2009
[SOLVED] testing if a file is a directory

Thank you!
Now I have the problem to walk through the directory tree starting from the one passed to the script.Is it possible doing it with bash?I think to use recursive tecnique.Is it right?
# 5  
Old 06-11-2009

Bare-bones recursive walk of file hierarchy:

Code:
do_dir()
{
  cd "$1" || return 1
  for file in "$PWD"/*
  do
    if [ -d "$file" ]
    then
      printf "\nDescend into directory: %s (y/n/q)? " "$file"
      read reply
      case $reply in
        y*|Y*) do_dir "$file" ;;
        q) return ;;
      esac
    else
      printf "File: %s\n" "$file"
    fi
  done
}

do_dir "${1:-.}"

# 6  
Old 06-23-2009
Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

File and directory testing

original post -- I have a korn shell script that does some things that depend on creating and writing a file in a directory. I'm looking for a more elegant/efficient way to do the check than what I'm using now: if ] then print "Creating ${STGDIR}/${SHOW}" mkdir... (3 Replies)
Discussion started by: Dalej
3 Replies

2. Shell Programming and Scripting

Testing for one word in a file

I am trying to test the output of a file. What I have so far is this: if ]; then echo "yes";fi The problem with this is it works but I only want it to print out if the file contains the word "Compacted." The == sign means identical or equal to so it won't work. I tried ~ but that doesn't... (4 Replies)
Discussion started by: newbie2010
4 Replies

3. Shell Programming and Scripting

PERL: testing directory on windows platform

Hi Gurus, kindly analyse the following for me, please OS: Windows 7 Code location: C:\ Output: "Program Files not being recognised" "System Volume Information is a directory" "Windows not being recognised" main { my @dirlist = <*>; foreach my $fn... (0 Replies)
Discussion started by: biglau
0 Replies

4. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

5. UNIX for Advanced & Expert Users

File system testing for Data corruption

Hi, could any one tell is there any test-suite or any idea How to do data corruption validation testing, means there is no any data corruption ? Regards Manish (1 Reply)
Discussion started by: manish_tcs_hp
1 Replies

6. Shell Programming and Scripting

testing file permissions.....

script name: filetest.sh if ; then echo " You didn't enter any argument" elif ; then echo " file not exist" elif ; then echo " file not readable" elif ; then echo " file not writable" else echo " file both readable and writable" fi running like... $ ./filetest filename ... (3 Replies)
Discussion started by: ani83_pune
3 Replies

7. Shell Programming and Scripting

Testing for empty file

Hello, I need to determine if a file I have is empty or not. How can I go about doing this in shell scripting? Some sample code would be appreciated? Thanks, (6 Replies)
Discussion started by: mojoman
6 Replies

8. Shell Programming and Scripting

Syntax prob. Passing a directory to $1 and testing it.

I'm running this simple little test: #!/bin/sh if ; then echo $1 else echo "Usage:`basename $0` dir" fi echo "The end of the script." The idea is, to test if you have passed a dir to the script. The problem is, it seems to exit the if statement when $1 is null: ... (3 Replies)
Discussion started by: benjo
3 Replies

9. UNIX for Dummies Questions & Answers

Testing existence of a file /directory

hey guys How can i test existence of a file /directory in a directory in a script thanks (2 Replies)
Discussion started by: ajaya
2 Replies

10. UNIX for Dummies Questions & Answers

testing for file size in script

Has anyone got a few tips on how I can test if the file size is 0? I am moving files on a regular basis from one location to another with ftp. The files which are 0 bytes in size we want to discard. Thankyou in advance. (3 Replies)
Discussion started by: Ivo
3 Replies
Login or Register to Ask a Question