How to ls all the files in the subdirectories using wildcard? - linux

ls * can list all the files in the subdirectories.
ls *.pdb can only list all the files with extension pdb in the current directory.
So how to list all the files with extension pdb in the subdirectories?
My subdirectories are named as 1, 2, 3, .... I would like the output also include the directory information, so that I can use the output as an ensemble of input files. For example, the output should be like:
1/a.pdb 1/b.pdb 1/c.pdb 2/a.pdb 2/b.pdb 2/c.pdb 3/a.pdb 3/b.pdb 3/c.pdb

3 solutions :
Simple glob
ls */*.pdb
Recursive using bash
shopt -s globstar
ls **/*.pdb
Recursive using find
find . -type f -name '*.pdb'

Try this too:
locate */*.pdb
Its for previously made database
You can see the difference, if you code some Python and right after that use both
locate */*.py
ls */*.py

Related

How to find files without a directory path in Linux

I have a directory:
test/test1/file1.sh
test/file2.sh
What i need as a output:
file1.sh
file2.sh
I need to hide the dir.path at the output
It depends on your criteria which files should be included in the output, but if you want to print the names of all *.sh files under test/, you can do:
$ find <path_to_test_folder> '*.sh' -printf %f\\n
(If you're running it from the root folder of the search, you can drop the <...> part.)

How to list all files in all directory in linux using linux cmd

I'm trying to read all the files available in all directories along with complete path, using bash script. Have tried
ls -R cmd but it is not listing the files properly.
My requirement is to have the output which should have complete path for the file.
Maybe you want the find utility which is recursive by default.
find . -type f
The . means the current pwd/directory
You can use find command for listing all the files from a particular directory.
find /your_path/ -type f 2>/dev/null
/your_path/ : provide the path from where you will execute the command, the output will have the complete path of the files.
2>/dev/null : to suppress the STDERR
right now what you are trying is to list all of the sub-directories using ls -R, so this would not fulfill what you are trying to achieve.
See man ls:
-R, --recursive
list subdirectories recursively

How to list down the full paths to all files in multiple directories of a certain file format?

I am looking for a Bash solution to my dilemma here. I have directory project, and within that I have project/Batch1 project/Batch2 project/Batch3 and so on.
Within each Batch folder, I have a mix of files but what I am interested in are files in the .txt format, for example. I am able to recursively acquire the names of every file in my project directory using the ls -LR command. However, how should I get full paths including the file names, and also only for .txt files?
Thanks!
Use find command. Open man 1 find and check possible option you need for above task.
Here is the simple example. Open terminal and run
find pwd -type f -name "*.txt"
Or
find pwd -name "*.txt"
Or you can do same using bash script also.

Search directory and subdirectories for file matching a string and listing paths using ls and grep instead of find

I know there is a nice Linux program called find, which used as
find /home/user -name ".txt"
will print all the files with .txt extension with their relative path
How can I achieve the same using ls, grep and possibly awk?
I tried listing all files in directory and its subdirectories recursively using
ls -LR1
but have no idea how to parse it using grep.
Any advice would be greatly appraciated.
You can do this with bash4 and zsh :
shopt -s globstar # required if not enabled with bash4
printf '%s\n' **/*.txt
But don't trust anyone finding a solution with ls + awk, not the good way to achieve this with robustness

linux include all directories

how would I type a file path in ubuntu terminal to include all files in all sub-directories?
If I had a main directory called "books" but had a ton of subdirectories with all sorts of different names containing files, how would I type a path to include all files in all subdirectories?
/books/???
From within the books top directory, you can use the command:
find . -type f
Then, if you wanted to, say run each file through cat, you could use the xargs command:
find . -type f | xargs cat
For more info, use commands:
man find
man xargs
It is unclear what you actually want ... Probably you will get a better solution to your problem, if you ask directly for it, not for one other problem you've come accross trying to circumvent the original problem.
do you mean something like the following?
file */*
where the first * expands for all subdirectories and the second * for all contained files ?
I have chosen the file command arbitrarily. You can choose whatever command you want to run on the files you get shell-expanded.
Also note that directories will also be included (if not excluded by name, e.g. *.png or *.txt).
The wildcard * is not exactly the file path to include all files in all subdirectories but it expands to all files (or directories) matching the wildcard expression as a list, e.g. file1 file2 file3 file4. See also this tutorial on shell expansion.
Note that there may be easy solutions to related problems. Like to copy all files in all subdirectories (cp -a for example, see man cp).
I also like find very much. It's quite easy to generate more flexible search patterns in combination with grep. To provide a random example:
du `find . | grep some_pattern_to_occur | grep -v some_pattern_to_not_occur`
./books/*
For example, assuming i'm in the parent directory of 'books':
ls ./books/*
EDIT:
Actually, to list all the tree recursively you should use:
ls -R ./books/*

Resources