List directories and count files inside


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting List directories and count files inside
# 1  
Old 11-27-2013
List directories and count files inside

I'm trying to make a script that will list all directories under a selection as well as the number of files in each.
I cannot get it to work under a symbolic link.
The file structure is:
Code:
XXX_20131127_001
     dir01 (sym link)
            2404x912
                  file.0000.xxx to
                  file.0053.xxx
     dir02 (sym link)
            2404x912
                  file.0000.xxx to
                  file.0104.xxx
     dir03 (sym link)
            2404x912
                  file.0000.xxx to
                  file.0457.xxx
     dir03 (sym link)
            2404x912

I was hoping for an output something like this:
dir01 54 files
dir02 105 files
dir03 458 files
dir04 0 files

On a different note, I also need a script that will display the first and last folders in the directory. Like this:
Code:
dir01
   2404x912
        file.0000.xxx
        file.0053.xxx

Any help would be great.
Thanks in advance.
# 2  
Old 11-27-2013
tree, ls, foreach, unix

Hi
Try a recursive combination between foreach and ls to list folders contents and count thier number:

Code:
foreach $dir in (ls -d /yourDir)
$nbr=$(ls -lt) 
if ($nbr=0)
{echo the dir is empty}
elif ( $(ls -d /yourDir/$in)>0
# a new foreach in the sub dir
 foreach()
end

For the second command try the trre command whitch is available in Unix and Unix-like systems:
http://en.wikipedia.org/wiki/Tree_%28Unix%29


Sorry I haven't unix to provide an accurate answer
# 3  
Old 11-27-2013
Try this:

Code:
$ for dir in XXX_20131127_001/*
do
    echo $(basename $dir) $(find -L $dir -type f -print | wc -l) files
done
dir01 54 files
dir02 105 files
dir03 458 files
dir04 0 files

For you treeview:

Code:
$ find . -depth -type d -print | while read path
do
    path=${path#./}
    if [ ${#path} -ge ${prev:-0} ]
    then
        prev=${#path}
        indent=""
        IFS='/'
        set -- $path
        IFS=$' \t\n'
        while [ $# -gt 0 ]
        do
           echo "$indent$1"
           indent="   $indent"
           shift
        done
        echo "$indent$(ls $path | head -1)"
        echo "$indent$(ls $path | tail -1)"
    fi
done
dir01
   2404x912
      file.0000.xxx
      file.0053.xxx
dir02
   2404x912
      file.0000.xxx
      file.0104.xxx
dir03
   2404x912
      file.0000.xxx
      file.0457.xxx


Last edited by Chubler_XL; 11-27-2013 at 05:34 PM..
# 4  
Old 12-03-2013
That almost worked. I altered it slightly and now it works great.

Code:
find . -depth -type l -print | while read path
do
    path=${path#./}
    if [ ${#path} -ge ${prev:-0} ]
    then
        prev=${#path}
        indent=""
        IFS='/'
        set -- $path
        IFS=$' \t\n'
        while [ $# -gt 0 ]
        do
           echo "$indent$1"
           indent="   $indent"
           shift
        done
        echo "$indent$(ls $path | head -1)"
        echo "$indent$(ls $path | tail -1)"
    fi
done

Thank you very much!

---------- Post updated at 01:35 PM ---------- Previous update was at 01:16 PM ----------

I spoke too soon. That script works if there are no sub directories.
Without sub directories result:
Code:
DPX
   pe0880_v400002
      pe0880_v400002.1000.dpx
      pe0880_v400002.1041.dpx
DPX
   pe0880_v400003
      pe0880_v400003.1000.dpx
      pe0880_v400003.1041.dpx
DPX
   pg1100_v400019
      pg1100_v400019.1000.dpx
      pg1100_v400019.1120.dpx
QT
   pe0880_v400002.mov
      QT/pe0880_v400002.mov
      QT/pe0880_v400002.mov
QT
   pe0880_v400003.mov
      QT/pe0880_v400003.mov
      QT/pe0880_v400003.mov
QT
   pg1100_v400019.mov
      QT/pg1100_v400019.mov
      QT/pg1100_v400019.mov


With subdirectories the result:
Code:
DPX
   pe0880_v400002
      2404x912
      2404x912
DPX
   pe0880_v400003
      2404x912
      2404x912
DPX
   pg1100_v400019
      2404x912
      2404x912
QT
   pe0880_v400002.mov
      QT/pe0880_v400002.mov
      QT/pe0880_v400002.mov
QT
   pe0880_v400003.mov
      QT/pe0880_v400003.mov
      QT/pe0880_v400003.mov
QT
   pg1100_v400019.mov
      QT/pg1100_v400019.mov
      QT/pg1100_v400019.mov

Ideally, only the .dpx files would show first and last. Any other file type would simply be listed.
Like this:
Code:
DPX
   pe0880_v400002
      2404x912
         pe0880_v400002.1000.dpx
         pe0880_v400002.1041.dpx

   pe0880_v400003
      2404x912
         pe0880_v400003.1000.dpx
         pe0880_v400003.1041.dpx

   pg1100_v400019
      2404x912
         pg1100_v400019.1000.dpx
         pg1100_v400019.1120.dpx
QT
   pe0880_v400002.mov
   pe0880_v400003.mov  
   pg1100_v400019.mov

# 5  
Old 12-03-2013
This should work better but requires bash shell, I can do a ksh version if you require.

Code:
#!/bin/bash
find -L . -depth \( -type l -o -type d \) -print | while read path
do
    path=${path#./}
    IFS='/' PTH=( $path )
    if [[ ${#PTH[@]} -ge ${#prev[@]} ]]
    then
        indent=""
        diff=0
        for((i=0;i<${#PTH[@]};i++)) {
           if [[ diff -eq 1 || "${PTH[i]}" != "${prev[i]}" ]]
           then
              echo "$indent${PTH[i]}"
              diff=1
           fi
           indent="   $indent"
        }
        if ls "$path" | grep -q "\.dpx$"
        then
            echo "$indent$(ls "$path" | grep "\.dpx$" | head -1)"
            echo "$indent$(ls "$path" | grep "\.dpx$" | tail -1)"
        else
            ls "$path" | sed "s/^/$indent/"
        fi
        IFS='/' prev=( $path )
    fi
done


Last edited by Chubler_XL; 12-03-2013 at 07:12 PM..
# 6  
Old 12-03-2013
Sweet!

That script works great! Thanks.

I can usually understand what a script is doing once I see it but this one baffles me. Great work!

I was looking to see if I could add code that would list the quicktime files as well but I was lost in there. Better to leave it working I figure.

This will save me a lot of copy, paste, delete, tab, tab, select, tab, tab ...
# 7  
Old 12-03-2013
Quote:
Originally Posted by scribling
I was looking to see if I could add code that would list the quicktime files as well but I was lost in there.
Here is a version where you can specify a list of extensions you want to summarise, but it might play up a bit if you had a directory with mixed file types (eg qt and dpx files), just change EXTS to pipe list of values you would summarise:

Code:
EXTS="qt|dpx"
find -L . -depth \( -type l -o -type d \) -print | while read path
do
    path=${path#./}
    IFS='/' PTH=( $path )
    if [[ ${#PTH[@]} -ge ${#prev[@]} ]]
    then
        indent=""
        diff=0
        for((i=0;i<${#PTH[@]};i++)) {
           if [[ diff -eq 1 || "${PTH[i]}" != "${prev[i]}" ]]
           then
              echo "$indent${PTH[i]}"
              diff=1
           fi
           indent="   $indent"
        }
        if ls "$path" | grep -Eq "\.($EXTS)$"
        then
            echo "$indent$(ls "$path" | grep -E "\.($EXTS)$" | head -1)"
            echo "$indent$(ls "$path" | grep -E "\.($EXTS)$" | tail -1)"
        else
            ls "$path" | sed "s/^/$indent/"
        fi
        IFS='/' prev=( $path )
    fi
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to delete all the files and folders inside all the directories except some specific directory?

hi, i have a requirement to delete all the files from all the directories except some specific directories like archive and log. for example: there are following directories such as A B C D Archive E Log F which contains some sub directories and files. The requirement is to delete all the... (7 Replies)
Discussion started by: Little
7 Replies

2. Shell Programming and Scripting

Count files between multiple directories

Hi All, Everyday we will receive 33 files in our source directory(/prd/pk) with the current date. Once our jobs are completed all the 33 files immediately will be moved to backup folder (/prd/pk/backup). Now, I need to check between source file directory (/prd/pdk) and backup file directory... (3 Replies)
Discussion started by: suresh_target
3 Replies

3. Shell Programming and Scripting

Script to count number of files in directories

Hi All! I would like to have a script that will count the number of files at the top of the hour of soome directories and mail the results to me. I was thinking on : a=`/directory/subdirectory/ | wc -l` echo "/directory/subdirectory :$a" b=`/another_dir/subdir/ | wc -l` echo... (12 Replies)
Discussion started by: fretagi
12 Replies

4. Shell Programming and Scripting

How to list all the files, directories and sub-directories in the current path except one directory?

Can anyone come up with a unix command that lists all the files, directories and sub-directories in the current directory except a folder called log.? Thank you in advance. (7 Replies)
Discussion started by: Manjunath B
7 Replies

5. UNIX for Dummies Questions & Answers

Count of files and directories

Hi I have a jfs2 filesystem named /software in my aix 5.3 box. Please note that there are a lot of subdirectories under /software? I need to know the count of how many total files and directories are present under that mount point /software ? For example if by some commands we find that there... (3 Replies)
Discussion started by: samsungsamsung
3 Replies

6. UNIX for Dummies Questions & Answers

List directories and sub directories recursively excluding files

Hi, Please help me, how to get all the direcotries, its sub directories and its sub directories recursively, need to exclude all the files in the process. I wanted to disply using a unix command all the directories recursively excluding files. I tried 'ls -FR' but that display files as... (3 Replies)
Discussion started by: pointers
3 Replies

7. Shell Programming and Scripting

Count of files in directories

Hi, I have a requirement to find out the count of files in directories. I can do this very well by goind to each directory and then ls -lrt | wc -l. But I need to do it for hundreds of directories/sub-directories. I tried with this - for i in `ls -F | grep '/$'`; do `echo "$i"`| ls -lrt... (2 Replies)
Discussion started by: unx100
2 Replies

8. Shell Programming and Scripting

AWK Script - Count Files In Directories

Hey, I'm very new to AWK and am trying to write a script that counts the number of files in all subdirectories. So, basically, my root has many subdirectories, and each subdirectory has many files. How can I get the total count? I haven't been able to figure out how to loop through the... (1 Reply)
Discussion started by: beefeater267
1 Replies

9. Solaris

Get Count of all files in all the directories

Hello, I am looking for a way to get the TOTAL COUNT of the files present in all directories(sub directories) under the root directory..The files can be of any type viz. txt, doc, html, wav, jpeg etc. If it has an extension, it has to be counted.. I want to run the script from the root directory.... (6 Replies)
Discussion started by: oniondosa
6 Replies

10. Shell Programming and Scripting

Need script to find errored files inside directories

Hi people. I working on a script to check for files that they are suposed not to be on the directory. I mean, inside of each directory it must have some files but some could be wrong, and i want to move the files that are wrong. Ex: CSPOTGET edpst/CargadoresSPOT Historicos_Spot_MDI.zip... (4 Replies)
Discussion started by: osramos
4 Replies
Login or Register to Ask a Question