count file(s) under a specific directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting count file(s) under a specific directory
# 1  
Old 03-21-2005
count file(s) under a specific directory

How do I count number of file in a directory. Here is what I want to do.

1. find total number of file in a directory in variable total

2. ftp all the file

3. ftp_return_code=`grep -c "Transfer complete" $logfile`

if [$total == ftp_return_code]

then
exit 0

else

exit -1
fi
# 2  
Old 03-21-2005
Quote:
Originally Posted by leemjesse
How do I count number of file in a directory. Here is what I want to do.

1. find total number of file in a directory in variable total
man find

Quote:
Originally Posted by leemjesse
2. ftp all the file
FAQ

Quote:
Originally Posted by leemjesse
3. ftp_return_code=`grep -c "Transfer complete" $logfile`

if [$total == ftp_return_code]

then
exit 0

else

exit -1
fi
# 3  
Old 03-21-2005
sorry for the confusion, but I want to count how many file in the directory
# 4  
Old 03-21-2005
Quote:
Originally Posted by leemjesse
sorry for the confusion, but I want to count how many file in the directory
man find

find . ! -name . -prune -type f
# 5  
Old 03-22-2005
Code:
fileCount=`find . ! -name . -prune -type f   | wc -l`

or

Code:
fileCount=`ls -ltr | grep ^"-" | wc -l `

# 6  
Old 03-22-2005
Quote:
Originally Posted by leemjesse
How do I count number of file in a directory. Here is what I want to do.

1. find total number of file in a directory in variable total
IF you want to have only regular files in the current directory without system files like .shell_profile, .sheel_history,.. .ssh then,

Code:
count=$(ls -l | awk '/^-/ { print $9 }')

If you want to have all files then,
Code:
count=$(ls -la | awk '/^-/ { print $9 }')

Quote:
Originally Posted by leemjesse
2. ftp all the file


3. ftp_return_code=`grep -c "Transfer complete" $logfile`

if [$total == ftp_return_code]

then
exit 0

else

exit -1
fi
If you go for rcp / scp then it will be very easy to check return_code and transfer files one by one.

With ftp it will get more complex to check return code that, check and trasfer will be needed as one by one.

HTH.
# 7  
Old 03-23-2005
Quote:
Originally Posted by leemjesse
sorry for the confusion, but I want to count how many file in the directory
# ls -a <dir> | wc -l

rather...

# ls -A <dir> | wc -l

Last edited by jolok; 03-23-2005 at 11:29 AM.. Reason: Doh!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Searching for file types by count in specific folder in RHEL 6

So I'm trying to search for the top 10 or 15 items under a directory by file type. I want to run a command on a directory and get something like the following: Example of expected output.. .PDF: 100, .txt: 95, .word: 80.. What would be the best way of going about this? I've searched around... (2 Replies)
Discussion started by: shackle101
2 Replies

2. Shell Programming and Scripting

Count specific character of a file in each line and delete this character in a specific position

I will appreciate if you help me here in this script in Solaris Enviroment. Scenario: i have 2 files : 1) /tmp/TRANSACTIONS_DAILY_20180730.txt: 201807300000000004 201807300000000005 201807300000000006 201807300000000007 201807300000000008 2)... (10 Replies)
Discussion started by: teokon90
10 Replies

3. UNIX for Dummies Questions & Answers

To count total of specific character in a file and save its value to a variable

Hi all, I have a file that contains characters. How do I get total of spesific character from that file and save the count to a variable for doing for calculation. data.txt 1 2 2 2 2 3 3 4 5 6 7 8 5 4 3 4 (5 Replies)
Discussion started by: weslyarfan
5 Replies

4. Shell Programming and Scripting

Shell scripting-I need a script which should watch a directory for a file with specific directory

I need a script which should watch a directory for a file with specific directory. If it finds a file in directory, it should search for few specific keyword in the file. if the keyword exists, it should trim string from specific column. The file should be moved to another directory and the a... (8 Replies)
Discussion started by: akashdeepak
8 Replies

5. UNIX for Dummies Questions & Answers

How do I count how many times a specific word appear in a file (ksh)?

Hi Please can you help how do I count the number of specific characters or words that appear in a file? (8 Replies)
Discussion started by: fretagi
8 Replies

6. Shell Programming and Scripting

how to count how many subdirectory containing more than a certain number of specific file type

hi I want to write a script which count the number of subdirectories in the current root directory that contain more than a specified number of files of a specific type. Is there an easy way to do this? Thanks Robert (2 Replies)
Discussion started by: piynik
2 Replies

7. UNIX for Advanced & Expert Users

allow user to use sudo cp on a specific directory and only a specific file

Is there a way to allow a user to use sudo cp on a specific directory and only a specific file? (6 Replies)
Discussion started by: cokedude
6 Replies

8. Shell Programming and Scripting

How to extract specific data and count number containing sets from a file?

Hello everybody! I am quit new here and hope you can help me. Using an awk script I am trying to extract data from several files. The structure of the input files is as follows: TimeStep parameter1 parameter2 parameter3 parameter4 e.g. 1 X Y Z L 1 D H Z I 1 H Y E W 2 D H G F 2 R... (2 Replies)
Discussion started by: Daniel8472
2 Replies

9. Shell Programming and Scripting

Count specific character(s) very large file

I'm trying to count the number of 2 specific characters in a very large file. I'd like to avoid using gsub because its taking too long. I was thinking something like: awk '-F' { t += NF - 1 } END {print t}' infile > outfile which isn't working Any ideas would be great. (3 Replies)
Discussion started by: dcfargo
3 Replies

10. HP-UX

count occurences of specific character in the file

For counting the occurences of specific character in the file I am issuing the command grep -o 'character' filename | wc -w It works in other shells but not in HP-UX as there is no option -o for grep. What do I do now? (9 Replies)
Discussion started by: superprogrammer
9 Replies
Login or Register to Ask a Question