Count number of files within a directory in Linux? [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
To count the number of files in a directory, I typically use
ls directory | wc -l
But is there another command that doesn't use wc ?

this is one:
ls -l . | egrep -c '^-'
Note:
ls -1 | wc -l
Which means:
ls: list files in dir
-1: (that's a ONE) only one entry per line. Change it to -1a if you want hidden files too
|: pipe output onto...
wc: "wordcount"
-l: count lines.

Related

What do terminal commands ls > wc and ls | wc show? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I know what the commands ls and wc do, but I can not find out what ls > wc and ls | wc will show. Can someone please help me flush out the meaning of this commands?
ls | wc The output from the ls command is piped into the wc command. So it will count the words which are in the output of ls. So you see simply the number of files read by ls.
ls > wc This creates a new file in your current working directory with the name wc with the output of your ls command. The program wc is not used here, simply a new file with the same name is created. You can simply look into this new file with your favorite editor or simply use cat for it.

List only numerical directory names in linux [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
How to list only numerical directory names in Linux.
Only directories, that has only numeric characters?
There are multiple solutions to do it.
1.You can List just dirs and then remove . and / from the names and then Grep just numerical ones:
ls -d ./*/ | sed 's/\.\///g' | sed 's/\///g' | grep -E '^[0-9]+$'
2.By "ls" & "grep" & then "awk". Just list with details, Grep dirs and then Print 9th column:
ls -llh | grep '^d' | awk '{print $9}'
Good luck In Arbaeen.
In bash, you can benefit from extended globbing:
shopt -s extglob
ls -d +([0-9])/
Where
+(pattern-list)
Matches one or more occurrences of the given patterns
The / at the end limits the list to directories, and -d prevents ls from listing their contents.

What is the equivalent of "grep -e pattern1 -e pattern2 <file> " in Solaris? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
What is the equivalent of grep -e pattern1 -e pattern2 "$file" in Solaris?
In Linux it works fine. but in Solaris, i got "grep: illegal option -- e
Usage: grep -hblcnsviw pattern file . . ." error.
Can anyone help please?
Instead of:
# GNU grep only
grep -e pattern1 -e pattern2 file
...you can use:
# POSIX-compliant
grep -e 'pattern1
pattern2' file

Why `ls` list multiple files per line, but `ls pipe/redirect` list just 1 file per line? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
Just curious, this is normal-expected behavior of ls:
user#host:~$ ls
Codes Documents Music Pictures Templates
Desktop Downloads Papers Public Videos
But when I use ls with pipe/redirection, it behave like ls -1:
user#host:~$ ls | cat
Codes
Desktop
Documents
Downloads
Music
Papers
Pictures
Public
Templates
Videos
Why? (and how to write such program that gives difference output between stdout and pipe like this?)
P.S. I also set alias l='ls -F', and this time pipe/redirection is no longer ls -1 style:
user#host:~$ l | cat
Codes/ Documents/ Music/ Pictures/ Templates/
Desktop/ Downloads/ Papers/ Public/ Videos/
Without using the alias, it does the command in ls -1 style, however:
$ ls -F | cat
Codes/
Desktop/
Documents/
Downloads/
Music/
Papers/
Pictures/
Public/
Templates/
Videos/
You can check this line from the source:
if (format == long_format)
format = (isatty (STDOUT_FILENO) ? many_per_line : one_per_line);
It uses the isatty function to check if stdout points to a tty, and to print many_per_line if it does or one_per_line if it does not.
Here is how GNU ls does it (ls.c):
if (isatty (STDOUT_FILENO))
{
format = many_per_line;
}
else
{
format = one_per_line;
}

Meaning of command ls -lt | wc -l [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
My friend just passed me this command to count the number of files in a directory:
$ ls -lt | wc -l
Can someone please help me flush out the meaning of this command? I know that ls is to list all the files. But what does -lt mean?
Also, I get a different count if I use ls | wc -l with no -lt option. Why is that the case?
You'll want to get familiar with the "man (manual) pages":
$ man ls
In this case you'll see:
-l (The lowercase letter ``ell''.) List in long format. (See below.) If
the output is to a terminal, a total sum for all the file sizes is
output on a line before the long listing.
-t Sort by time modified (most recently modified first) before sorting the
operands by lexicographical order.
Another way you can see the effect of the options is to run ls without piping to the wc command. Compare
$ ls
with
$ ls -l
and
$ ls -lt

Resources