ssbb
ssbb

Reputation: 245

counting files in directory linux

Q2. Write a script that takes a directory name as command line argument and display the attributes of various files in it e.g.

  1. Regular Files
  2. Total No of files
  3. No of directories
  4. Files allowing write permissions
  5. Files allowing read permissions
  6. Files allowing execute permissions
  7. File having size 0
  8. Hidden files in directory

working in linux in shell script

what i have done is

find DIR_NAME -type f -print | wc -l

To count all files (including subdirs):

find /home/vivek -type f -print| wc -l

To count all dirs including subdirs:

find . -type d -print | wc -l

To only count files in given dir only (no subdir):

find /dest -maxdepth 1 -type f -print| wc -l

To only count dirs in given dir only (no subdir):

find /path/to/foo -maxdepth 1 -type d -print| wc -l

Upvotes: 2

Views: 9955

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74118

All your questions can be solved by looking into man find

  1. -type f
  2. no option necessary
  3. -type d
  4. -perm /u+w,g+w or some variation
  5. -perm /u+r,g+r
  6. -perm /u+x,g+x
  7. -size 0
  8. -name '.*'

Upvotes: 2

Related Questions