how do i find all files with the same name in all subdirectories - linux

I want to find all the different .gitignore files I have to combine them into one.
I tried something like find */**/.gitignore but that came up with nothing.
All the files are in sub directories of my current dir, as well as the current directory.
I am using Bash on linux

find -name .gitignore
That should do
Since you are using bash:
shopt -s globstar
echo **/.gitignore
From man bash:
globstar
If set, the pattern ** used in a pathname expansion context will match a
files and zero or more directories and subdirectories. If the pattern is
followed by a /, only directories and subdirectories match.

Try this
find /home/user1 -name 'result.out' 2>/dev/null
In your case
find /<your DIR> -name '*.gitignore' 2>/dev/null
This results in
/home/user1/result.out
/home/user1/dir1/result.out
/home/user1/dir2/result.out

Related

GNU findutils doesn't find file

When searching for .txt files that are in 2 different home directories only one shows up depending on the present working directory. Why is this?
/home/bob/1.txt
/home/alice/dir1/2.txt
pwd /tmp
[root#host tmp]#find /home -name *.txt
/home/bob/1.txt
/home/alice/dir1/2.txt
pwd /home
[root#host bob]#find /home -name *.txt
/home/bob/1.txt
Why does searching from within the bob directory only return the one file?
Why does searching from within the bob directory only return the one file?
Because when the working directory is /home/bob, the *.txt in the find command is expanded by the shell (to 1.txt) and that is what is passed to find. That is, find /home -name 1.txt. That will find the file in /home/bob, but not the differently named one in /home/alice. It would find /home/alice/1.txt if such a file existed.
On the other hand, when the pattern does not match any file (relative to the working directory) it is passed on as a literal. At least by default -- you should be careful about this, because the pattern would instead be expanded to nothing if the nullglob shell option were in effect and the find command were executed from a location where the pattern didn't match any files.
If you want to ensure that shell pathname expansion is not applied to the pattern then quote it:
find /home -name '*.txt'
or
find /home -name \*.txt
or ....

Replace underscore with minus recursively but only for certain extensions?

I want to replace all underscores with a minus for files in a folder and its subfolders and I want to include only jpg, JPG and png. For a start I tried rename '/_/-/g' ./* but that does not include subfolders. How would I do that?
Thanks
Globstar allows you to match files anywhere in a folder or under it. ${f//_/-} is a bash "Pattern substitution": it replaces the value of $f, the double slashes before the pattern mean replace all matches.
shopt -s globstar
for f in **/*.jpg **/*.JPG **/*.png
do
mv "$f" "${f//_/-}"
done
My system does not support this syntax of rename, so I can't give you good advice in its use, but you might also be able to use globstar to generate filenames for it.
On Linux, you can traverse the folders with "find":
find . -name "*.pdf" -type f -exec #rename command here, current filename is "{}"# \;
Edit: Forgot the \;

Using "grep" to search for specific type of files in all subdirectories

I am trying to find a specific line in files that contains "Mutual_Values_23.0" in a directory that contains a lot of subdirectories. I know this line number is stored in a file which starts with "gnuout_mutual_....txt" (the ellipses part of the file name is the time stamp so that varies).
I wanted to know if there is a way to specify "grep" command to look into the subdirectories only for the files starting with "gnuout_mutual_....txt"
I have tried
grep -r "Mutual_Values_23.0" *
but that's taking a long time
You can use the following option of grep:
--include=GLOB
Search only files whose base name matches GLOB (using wildcard matching as described under --exclude).
And for the line number you should use the -n option.
From within the root of the folders you want to look into, you can use this final command:
grep -nr "Mutual_Values_23.0" --include="gnuout_mutual_*txt"
Use find to search all sub-directories for the "gnuout...txt` file with the search string "Mutual_Values_23.0"
find . -mindepth 1 -name gnuout_mutual_\*.txt -type f -exec grep "Mutual_Values_23.0" {} +
If you make use of bash, you can use the globstar option:
globstar
If set, the pattern ** used in a pathname expansion context will
match all files and zero or more directories and subdirectories.
If the pattern is followed by a /, only directories and
subdirectories match.
So you can use it like:
$ shopt -s globstar
$ grep "search_string" **/glob-pattern
or in the case of the OP:
$ shopt -s globstar
$ grep Mutual_Values_23.0 **/gnuout_mutual_*.txt
GNU grep has the --include GLOB option where GLOB can be used to specify the file name pattern that you need to match.
grep -rn --include 'gnuout_mutual_*txt' 'Mutual_Values_23.0' .
You could use find to search for files and pass results to grep.
find /directory_where_to_search/ -iname 'gnuout_mutual_*.txt' | xargs grep 'Mutual_Values_23.0' -sl
Use this command:
$ find . -name "*Mutual_Values_23.0*"
Note: Run this command in the directory where you want to search your set of files.
Hope it helps, cheers!

Finding subdirectories of depth 1 that do _not_ include a file

I am working on an open-source project. In most, but not all, of the sub-directories of depth 1, a file called "test.c" can be found. How can I find out those directories that do not include "test.c"?
For example, I have subdirectories dir1, dir2, dir3. Dir2 and dir3 have "test.c". I have to manually check them with "ls" to determine "dir1" does not have "test.c". Probably there is a simpler way (such as a bash command) to do so? I am under Ubuntu 16. So a bash command would be preferred.
You may use this find command from base directory of all the sub-directories:
find . -type d -exec bash -c 'for d; do [[ -f "$d"/test.c ]] || echo "$d"; done' - {} +
This command finds all sub directories from current directory and checks for presence of file test.c in each directory in the bash command. If file is not present then directory name is printed.

how to recursively list files from a folder?

'ls dir1/*/.ext' just lists all the files with just one level of nesting. What is the command to recursively list all the files with any level of nesting in linux?
ls -R dir1
Or:
find dir1 -name "*.ext"
The find command is one way to do this:
find dir1 -name .ext
The -name operator can take a wildcard to match with, but it's important to quote the wildcard expression so that it won't be expanded by your shell before calling into find:
find dir1 -name "*.ext"
The find command has many operators that can do various different tests on the files in the directory, of which -name is just one example. Consult the find manual page for more information.
To list folder recursively:
ls -R
You could use find:
find .
That command would list everything under the current folder

Resources