Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm writing script is ksh. Need to find all directory names directly under the current directory which contain only files, not subdirectories.

I know that I could use ls -alR and recursively parse output for the first letter in the first field (d for a directory). I think awk is the best way to parse and find but I'm a little confused with it. Please help!!

For example, a simple ls -al output in the current directory:

   drwxr-xr-x  22 af      staff    748 18 Mar 22:21 .
   drwxr-xr-x   5 root    admin    170 17 Mar 18:03 ..
   -rw-------   1 af      staff      3 17 Mar 16:37 .CFUserTextEncoding
   drwxr-xr-x   5 af      staff    170 17 Mar 17:12 Public
   drwxr-xr-x   9 af      staff    306 18 Mar 17:40 Sites
   -rw-------   1 af      staff      3 17 Mar 16:37 textd
   …

There are 2 directories in this output: Public and Sites. The directory Public doesn't contain subdirectories, but Sites does. There are 3 subdirectories in Sites. So I need to echo only the directories which don't contain directories in them. In my case, this is only Sites.

share|improve this question
add comment

3 Answers 3

up vote 1 down vote accepted

You don't need to use awk at all. Use the built-in tests that ksh provides, something like this:

#!/bin/ksh

for NAME in *
do
    FOUND=no
    if [[ -d $NAME && $NAME != '.' && $NAME != '..' ]]
    then
        for SUBNAME in $NAME/*
        do
            if [[ -d $SUBNAME ]]
            then
                FOUND=yes
                break
            fi
        done
        if [[ $FOUND == no ]]
        then
            echo Found only files in $NAME
        fi
    fi
done

That little script looks in all the directories in the current directory, and tells you if they only contain files, no sub-directories.

share|improve this answer
    
Thanks! Best way! –  Nat Kup Mar 20 '13 at 17:11
add comment

if I am not misunderstanding you, you only want to find files in the directory not the subdirectories. If this is your intention, here is the solution

find . -type f

if you want to find other than regular files (like block device files, character device files etc) then see the man page for find command and look for the type keyword on that page. You will see different filetypes, including regular files and directories etc.

Hope this is what you are looking for

share|improve this answer
    
No, that's not the question at all. It's reasonably clear once you s/catalogue/directory/g. Nat Kup is looking for directories that don't contain subdirectories. –  Gilles Mar 20 '13 at 23:12
add comment

*/ matches the subdirectories of the current directory. This includes symbolic links to directories, which you may or may not desire.

In ksh93, adding ~(N) at the beginning of the pattern makes it expand to the empty list if there is no match. Without this, the pattern remains unchanged if there is no match.

The following ksh93 function lists the subdirectories of the current directories that do not contain any subdirectory or link to a directory.

list_leaf_directories () {
  local FIGNORE='.?(.)'        # don't ignore dot files
  local d
  for d in */; do
    [[ -L $d ]] || continue;   # skip symbolic links
    set -- ~(N)"$d"/*/
    if ((!#)); then echo "$d"; fi
  done
done
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.