简体   繁体   中英

Listing directory contents

I'm trying to write a UNIX command using a pipe that will display the number of files in my home directory including hidden files that begin with a '.'

So far i have:

ls -a .* | wc -l I get a integer returned

Is my command correct?

While being in current directory:

ls -1 | wc -l

or specify full path:

ls -1 /path/to/dir | wc -l

-note that key for ls is not l , it's 1 - that will skip 'hidden' files (those who starts with . ). If you want to include them, then:

ls -1a /path/to/dir | wc -l

-but note, that . (current directory pointer) and .. (parent directory pointer) will be included, so probably you'll want to subtract 2 from result number.

Is my command correct?

No. Upon saying ls -a .* , the command would also return files inside a directory beginning with a . in addition to returning . and ..

In order to display the number of files in my home directory including hidden files that begin with a '.' , say:

find $HOME -type f | wc -l

If you want to limit it to only the HOME directory, say:

find $HOME -maxdepth 1 -type f | wc -l

You could also use find :

find ~ -type f | wc -l

or

find ~ -type f -maxdepth 1 | wc -l

if you don't want to find recursively.

YA non recursive command with more pipes:

ls -la | awk '{ print $1 }' | grep -v total | grep -v d | wc -l

Best non recursive - as colleagues above, but to avoid warnings, please put maxdepth before type option:

find ~ -maxdepth 1 -type f | wc -l

Recursive:

find ~ -type f | wc -l

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM