How to count number of files in directory and write to new file with number of files and their name?

Hi!

I just want to count number of files in a directory, and write to new text file, with number of files and their name

output should look like this,,

assume that below one is a new file created by script

Number of files in directory = 25
1. a.txt
2. abc.txt
3. asd.dat
----------------
----------------
----------------
----------------
----------------
----------------
25. last file.dat 
ls | cat -n > new_file

This should do

1 a.txt
2 abc.txt
.......
......
25 last file.dat

Thank You..

Have you got the first line?? :slight_smile:

No I haven't

Can we break this into two lines?

echo "Number of files in the directory = `ls -ltr | grep -v '^d' | wc -l`
ls -ltr | grep -v '^d' | cut -c 59-100 | awk '{ print NR". "$0}'

Hi..

Your script could able to find number of files in a directory, but not able to write it into file...

You will have to redirect into a file.

echo "Number of files in the directory = `ls -ltr | grep -v '^d' | wc -l` >file
ls -ltr | grep -v '^d' | cut -c 59-100 | awk '{ print NR". "$0}' >>file

result is wrong..my dear..

If you are deploying awk anyhow, why don't you do it extensively:

ls -l|awk '/^-/{Ar[++n]=$9} END {print "No. of files: ", n; for (i=1;i<=n;i++) print i".", Ar}'
1 Like

So, can we have the folder details??

---------- Post updated at 04:04 PM ---------- Previous update was at 03:59 PM ----------

Thanks RudiC :slight_smile:

So I agree that the above code will work. What if the file name contains <space>?

UNIX systems doesn't allow that naming, but windows does. Even the O/P provided at the beginning has such a name.:confused:

You are right - what I posted was thought to be an example/hint on how to improve, not a full blown solution. You'd need to concatenate ALL trailing fields into Ar to account for that, like AR[++n]=$9" "$10... or Ar[++n]=substr($0,50) . Neither of these is really satisfying. I'd prefer to use sth. like stat on systems where it is available.

stat -c"%F   %n" *| awk  -F"\t" '/^regular/{Ar[++n]=$2} END {print "No. of files: ", n; for (i=1;i<=n;i++) print i".", Ar}'
admin@las:~/Desktop/file_make/datafilter$ stat -c"%F   %n" *| awk  -F"\t" '/^regular/{Ar[++n]=$2} END {print "No. of files: ", n; for (i=1;i<=n;i++) print i".", Ar}'

No. of files:  7
1. 
2. 
3. 
4. 
5. 
6. 
7. 

---------- Post updated at 06:29 AM ---------- Previous update was at 06:28 AM ----------

This is using awk

admin@las:~/Desktop/file_make/datafilter$ ls -l|awk '/^-/{Ar[++n]=$9} END {print "No. of files: ", n; for (i=1;i<=n;i++) print i".", Ar}'

No. of files:  7
1. a.txt
2. data_filter.sh
3. data_filter.sh~
4. data_filter.txt
5. data_filter.txt~
6. dir.sh
7. dir.sh~
stat -c"%F   %n" *|
           ^--- You need a <TAB> char here!

file names are not coming..

Pls post the output of the stat cmd alone.

Shell script approach, you have to pass absolute path as argument to this script:-

#!/bin/ksh

_DIR=$1
_SEQ=1

if [ $# -ne 1 ]
then
        echo "Usage: <script> <absolute path>"
        exit 1
fi

dir_count=$( find "${_DIR}" -path "${_DIR}/*" -prune -name "*" -type f | wc -l )

if [ $dir_count -ne 0 ]
then
        echo "Number of files in directory: $_DIR = $dir_count"
        for file in $( find "${_DIR}" -path "${_DIR}/*" -prune -name "*" -type f )
        do
                echo "${_SEQ}. $( basename $file )"; _SEQ=$( expr $_SEQ + 1 )
        done
else
        echo "No files found in directory: $_DIR"
fi

Try this, using -1 option of ls :

~/unix.com$ ls -1 | awk '{A[NR]=$0}END{print "Number of files in directory = "NR; while(i++<NR)print i"."A}'
find . -maxdepth 1 -type f -printf "%f|" | awk '{printf "Number of files: %s\n",NF;gsub(/\|/,"\n")}1' FS="|"

You can try this..Suppose vikram.txt is your file in which you want to save all the file and the number of files .

#!/bin/sh
vikram=$(ls |grep -v vikram.txt | wc -l)
vikram1=$(echo "Number of files ="$vikram"")
echo $vikram1 > vikram.txt
ls | grep -v vikram.txt |  cat -n >> vikram.txt