[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 236
  • Last Modified:

Unix Commands

What is the command if any, to retrieve the number of files in the current directory?
0
timothy1
Asked:
timothy1
  • 3
  • 2
1 Solution
 
chris_calabreseCommented:
If you want to count all files/directories recursively from the current directory:
  find . -print | wc -l

If you want only stuff in the current directory, but not sub-directories:
  ls | wc -l

If you want only regular files in the current directory:
  ls -F | grep -v '/$' | wc -l

If you want only regular files, but recursively:
  find . -type f -print | wc -l
0
 
ahoffmannCommented:
> If you want only stuff in the current directory, but not sub-directories:
  ls -a|wc -l

> If you want only regular files in the current directory:
  ls -Fa|egrep '[/|@%=]$'|wc -l

> If you want only regular files, but recursively:
   ls -FaR|egrep '[/|@%=]$'|wc -l

;-)
0
 
ahoffmannCommented:
oops, forgot to use -B option for all above ls commands (in case of people are using crazy filenames)
0
 
chris_calabreseCommented:
Oops, you're right about the -a.

Don't know if I'd consider that last one an improvement :-)
0
 
ahoffmannCommented:
> .. an improvement
probaly not if you pipe it to wc
but you'll be supprised (just viewing), sometimes ;-)

oops, and just found an example where all the ls|wc suggestions fail (including mine):

      touch 'a\^Jb'; ls|wc
      # where ^J is ctrl-V followed by return

I think a sulution for this is find, or ls -i|awk 'sophisticated prog here'
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now