Count files, except the hidden files [duplicate] - linux

This question already has answers here:
Why does find . -not -name ".*" not exclude hidden files?
(2 answers)
How to count all files inside a folder, its subfolder and all . The count should not include folder count [closed]
(2 answers)
Closed 4 years ago.
I'm trying to count all the files from the current folder and each of it's sub-folders.
find . -type f \(! -iname ".*" \) -print

You can use this find command:
find -type f ! -regex ".*/\.[^/]+$" | wc -l
It will find all files in the current directory with filename not starting with a ., aka hidden files.

find . -type f -not -path "./.*" | wc -l
explanation:
find in the current directory(.) all things from type file(f) which does not have a path that starts with ./.(./ is prepend on every output item of find which means current directory) and then give the output to wc which is a program to count and the -l parameter tells wc to count lines.

Just pip wc -l to it like so:
find . -type f -not -path "*/\.*" | wc -l

Related

Omit a directory when searching with find [duplicate]

This question already has answers here:
How do I exclude a directory when using `find`?
(46 answers)
Closed 1 year ago.
Searching whole system using:
find / -type f -size +100M -exec ls -lh {} \; | awk '{ print $9 "|| Size : " $5 }'
How can I omit a directory? I see -prune -o, but I am not sure how to format the option.
find / -path /path/to/ommit -prune -o -type f -size +100M -exec ...
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From man:
To ignore a whole directory tree, use -prune rather
than checking every file in the tree. For example, to skip the
directory src/emacs and all files and directories under it,
and print the names of the other files found, do something like
this:
find . -path ./src/emacs -prune -o -print
Note that the pattern match test applies to the whole file name,
starting from one of the start points named on the command line.

How to get the number of files with a specfic extension in a directory and it's sub directories on Linux terminal?

The question is itself self-explanatory.
I tried the following command I found somewhere on the internet but it shows the number just in the immediate directory and not its subdirectories.
ls -lR ./*.jpg | wc -l
I am searching for all the files with the extension ".jpg" in the current folder and its subdirectories.
find . -type f -name '*.jpg' | wc -l
Find all the files (type f) that have a name that matches '*.jpg' then count them with wc
It's a job for find:
find -name "*.jpg" | wc -l

How do I find the number of all .txt files in a directory and all sub directories using specifically the find command and the wc command?

So far I have this:
find -name ".txt"
I'm not quite sure how to use wc to find out the exact number of files. When using the command above, all the .txt files show up, but I need the exact number of files with the .txt extension. Please don't suggest using other commands as I'd like to specifically use find and wc. Thanks
Try:
find . -name '*.txt' | wc -l
The -l option to wc tells it to return just the number of lines.
Improvement (requires GNU find)
The above will give the wrong number if any .txt file name contains a newline character. This will work correctly with any file names:
find . -iname '*.txt' -printf '1\n' | wc -l
-printf '1\n tells find to print just the line 1 for each file name found. This avoids problems with file names having difficult characters.
Example
Let's create two .txt files, one with a newline in its name:
$ touch dir1/dir2/a.txt $'dir1/dir2/b\nc.txt'
Now, let's find the find command:
$ find . -name '*.txt'
./dir1/dir2/b?c.txt
./dir1/dir2/a.txt
To count the files:
$ find . -name '*.txt' | wc -l
3
As you can see, the answer is off by one. The improved version, however, works correctly:
$ find . -iname '*.txt' -printf '1\n' | wc -l
2
find -type f -name "*.h" -mtime +10 -print | wc -l
This worked out.

Count only visible files in directory

I'm having problem with hidden file in my directory. If I use $(find . -type f | wc -l) it shows 8 files, which counts hidden file too, there should be only 7 files.
Is there anything that could count only visible files?
Ignore the names that start with . by saying:
find . ! -name '.*' -type f | wc -l
From the man page:
! expression
-not expression
This is the unary NOT operator. It evaluates to true if the
expression is false.
If you have filenames with newlines, then you can do using gnu find (as suggested by gniourf gniourf in comments):
find . ! -name '.*' -type f -maxdepth 1 -printf 'x' | wc -c
find . -type f -not -path '*/\.*' | wc -l
-not -path allows you to ignore files with name starting with . (hidden files)
Exclude all files starting with ( . )
find ./ ! -name '\.*' -type f | wc -l
! simply negates the search
If that doesnt work then try this dirty looking solution:
ls -lR | egrep '^(-|l|c|b|p|P|D|C|M|n|s)' | wc -l
Listed all types of files there excluding directories.
You can find the type of files in linux here
without -R of you want to look only in same dir.

Want to find any reference in any file to a certain string in linux [duplicate]

This question already has answers here:
how to find files containing a string using egrep
(7 answers)
Closed 8 years ago.
I am trying to search All .PHP files or ALL .SH files for any reference that contains:
'into tbl_free_minutes_mar'
I have command line access to the server but the files may be scattered in different directories.
For all directories everywhere,
find / -type f \( -name '*.php' -o -name '*.sh' \) \
-exec fgrep 'into tbl_free_minutes_mar' {} \+
For fewer directories elsewhere, just give a list of paths instead of /. To just list the matching files, try fgrep -l. If your file names might not always match the wildcards in the -name conditions, maybe scan all files.
find / -type f \( -name \*.php -o -name \*.sh \) -exec grep 'into tbl_free_minutes_mar' {} /dev/null \;
Change find / ... to to something less all-encompassing if you know the general area that you want to look in, e.g. find /home ...
Provided /base/path is the path where you want to start looking this will get you a list of files:
find /base/path -type f -iregex '.*\.\(php\|sh\)$' -exec grep -l 'into tbl_free_minutes_mar' '{}' \;

Resources