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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to count number of files in directory and write to new file with number of files and their name?
# 8  
Old 12-09-2012
You will have to redirect into a file.

Code:
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

# 9  
Old 12-09-2012
result is wrong..my dear..
# 10  
Old 12-09-2012
If you are deploying awk anyhow, why don't you do it extensively:
Code:
ls -l|awk '/^-/{Ar[++n]=$9} END {print "No. of files: ", n; for (i=1;i<=n;i++) print i".", Ar[i]}'

This User Gave Thanks to RudiC For This Post:
# 11  
Old 12-09-2012
Wrench

So, can we have the folder details??

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

Quote:
Originally Posted by RudiC
If you are deploying awk anyhow, why don't you do it extensively:
Code:
ls -l|awk '/^-/{Ar[++n]=$9} END {print "No. of files: ", n; for (i=1;i<=n;i++) print i".", Ar[i]}'

Thanks RudiC Smilie

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.Smilie
# 12  
Old 12-09-2012
Quote:
Originally Posted by PikK45
So I agree that the above code will work. What if the file name contains <space>?
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. likestat on systems where it is available.
Code:
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[i]}'


Last edited by RudiC; 12-09-2012 at 07:11 AM..
# 13  
Old 12-09-2012
This is output of stat command

Code:
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[i]}'

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

Code:
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[i]}'

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~

# 14  
Old 12-09-2012
Code:
stat -c"%F   %n" *|
           ^--- You need a <TAB> char here!

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count number of pattern matches per line for all files in directory

I have a directory of files, each with a variable (though small) number of lines. I would like to go through each line in each file, and print the: -file name -line number -number of matches to the pattern /comp/ for each line. Two example files: cat... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

2. Shell Programming and Scripting

Count the number of subset of files in a directory

hi I am trying to write a script to count the number of files, with slightly different subset name, in a directory for example, in directory /data, there are a subset of files that are name as follow /data/data_1_(1to however many).txt /data/data_2_(1 to however many).txt... (12 Replies)
Discussion started by: piynik
12 Replies

3. UNIX for Dummies Questions & Answers

Write the total number of rows in multiple files into another file

Hello Friends, I know you all are busy and inteligent too... I am stuck with one small issue if you can help me then it will be really great. My problem is I am having some files i.e. Input.txt1 Input.txt2 Input.txt3 Now my task is I need to check the total number of rows in... (4 Replies)
Discussion started by: malaya kumar
4 Replies

4. Shell Programming and Scripting

How to count the number of files starting with a pattern in a Directory

Hi! In our current directory there are around 35000 files. Out of these a few thousands(around 20000) start with, "testfiles9842323879838". I want to count the number of files that have filenames starting with the above pattern. Please help me with the command i could use. Thank... (7 Replies)
Discussion started by: atechcorp
7 Replies

5. UNIX for Dummies Questions & Answers

Count number of files in directory excluding existing files

Hi, Please let me know how to find out number of files in a directory excluding existing files..The existing file format will be unknown..each time.. Thanks (3 Replies)
Discussion started by: ammu
3 Replies

6. Shell Programming and Scripting

perl script on how to count the total number of lines of all the files under a directory

how to count the total number of lines of all the files under a directory using perl script.. I mean if I have 10 files under a directory then I want to count the total number of lines of all the 10 files contain. Please help me in writing a perl script on this. (5 Replies)
Discussion started by: adityam
5 Replies

7. UNIX for Dummies Questions & Answers

Read directory files and count number of lines

Hello, I'm trying to create a BASH file that can read all the files in my working directory and tell me how many words and lines are in that file. I wrote the following code: FILES="*" for f in "$FILES" do echo -e `wc -l -w $f` done My issue is that my file is outputting in one... (4 Replies)
Discussion started by: jl487
4 Replies

8. Shell Programming and Scripting

Count the number of files with only the partial file name

I have 1800 files in a directory. The file name is like out_cpty_XXXX. The "XXXX" vaires from file to file. I want to get the count of files with file name out_cpty_XXXX. How to get the count with just the partial file name? Any help would be appreciated? (4 Replies)
Discussion started by: Sangtha
4 Replies

9. Shell Programming and Scripting

count number of files in a directory

what's the script to do that? i want to only count the number of files in that directory, not including any sub directories at all (5 Replies)
Discussion started by: finalight
5 Replies

10. Shell Programming and Scripting

Count the number of files in a directory

Hi All, How do i find out the number of files in a directory using unix command ? (14 Replies)
Discussion started by: Raynon
14 Replies
Login or Register to Ask a Question