Server - System - Manager - CentOS, Operation System, VBB, HACKING AND SECURITY

Go Back   Server - System - Manager - CentOS, Operation System, VBB, HACKING AND SECURITY > CentOS Việt Nam - Viet Nam Linux CentOS Community > Hỏi đáp về CentOS

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-08-2008
sieuhosting's Avatar
Moderator
 
Join Date: Jul 2008
Posts: 75
Thanks: 7
Thanked 3 Times in 3 Posts
Default Làm sao để đếm số file trong 1 thư mục sử dụng shell command?

How to count files in directory? .
Em phải đếm quá nhiều file, nên muốn đếm các file tự động mà không được
em đă thử một số lệnh
for i in `ls -la`; do echo $i; done;
nhưng nó list ra các file không phải đếm ạ
Bác nào làm ơn giúp em với
__________________
www.sieuhosting.com
Reply With Quote
  #2 (permalink)  
Old 05-08-2008
Lovelinux's Avatar
Super Moderator
 
Join Date: Jun 2008
Posts: 221
Thanks: 22
Thanked 46 Times in 24 Posts
Default sử dụng wc

bạn có thể sử dụng wordcount (wc) trong Linux command.
ví dụ một cách đơn giản nhất:
Cách 1:
HTML Code:
ls -1 | wc -l
Cách 2:
HTML Code:
find . -type d |wc -l
HTML Code:
find . -type f |wc -l
Cách 3:
HTML Code:
ls -1p | grep -vc "/$"
Cách 4: Sử dụng perl hoặc python
HTML Code:
#!/usr/bin/env python

import os, sys

def numOfFiles(dir):
  count = 0
  for tup in os.walk(dir): count += len(tup[2])
  return count

for arg in sys.argv[1:]:
  print arg, '->', numOfFiles(arg)

.....

Last edited by Lovelinux; 05-08-2008 at 11:29 AM.
Reply With Quote
Reply

Tags
count, directory, files, wordcount

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:27 AM.


© Diễn đàn HackingArt (HA) được xây dựng và phát triển bởi các thành viên.
+ Diễn đàn HackingArt là nơi trao đổi của các webmaster chuyên nghiệp.