How to find files without a directory path in Linux - 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.)

Related

How to get a list of the filenames of a specific folder in shell script?

I am trying to get all the filenames of a specific folder in a text file and I want only the names, not the relative path. I tried:
ls -1 a/b/c>filenames.txt
and its output is
file-2021-08-18.txt
file2-2021-08-18.js
file3-2021-08-18.json
file4-2021-08-19.json
which is what I want but only a specific day's file.
But when I do this:
ls -1 a/b/c/*2021-08-18*>filenames.txt
then the output is
a/b/c/file-2021-08-18.txt
a/b/c/file2-2021-08-18.js
a/b/c/file3-2021-08-18.json
I want only the filenames not the path of the files.
So, required output:
file-2021-08-18.txt
file2-2021-08-18.js
file3-2021-08-18.json
Is there any straightforward solution for this? OR I need to trim the output.
Thanks!!
When the argument to ls is a directory, it lists the filenames in the directory.
But when you use a wildcard, the shell expands the wildcard to all the filenames. So ls doesn't receive the directory as its argument, it receives all the filenames, and it lists them as given.
You can change to the directory and then list the matching files in the current directory:
(cd /a/b/c; ls *2021-08-18*) > filenames.txt
The parentheses make this run in a subshell, so the working directory of the original shell is unaffected.
With GNU find you may use the -printf option:
find a/b/c/ -type f -name "*2021-08-18*" -printf "%f\n" > filenames.txt
The directive %f picks out the file's name with any leading directories removed. Since -printf doesn't add a newline (\n) after the filename, we add one in order to match the required output.

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 ls all the files in the subdirectories using wildcard?

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

Search recursively for files in a parent directory in Linux

I am trying to list all the files in a parent directory and its subdirectories. However, I am running this command from another location. So, at first, I need to traverse to the directory (from where I want to run this command).
Please note that I am using the find command instead of ls because I also want to list down the absolute path for each file in front of it. This is not possible with the ls command.
here is what I am doing:
cd ../../../;cd level1_dir1;find $(pwd) . -name *.* -printf "%TY-%Tm-%Td\t%p\n"
This command does not show any output.
Here is the directory structure:
level1_dir1
this has multiple subdirectories:
level2_dir1
level2_dir2
....
level2_dir10
each of these subdirectories again have subdirectories and files inside them.
however, now if I do:
cd ../../../;cd level1_dir1/level2_dir1;find $(pwd) . -name *.* -printf "%TY-%Tm-%Td\t%p\n"
it will do the recursion properly for all the subdirectories in level2_dir1 and show output like:
date level1_dir1/level2_dir1/path/to/file/filename
so, I wanted to print out for all the level2 directories, this way (by using the wild character):
cd ../../../;cd level1_dir1/*;find $(pwd) . -name *.* -printf "%TY-%Tm-%Td\t%p\n"
but it prints out the results only for the first directory in level2 (that is level2_dir1)
how can I make it list down the files for all the subdirectories?
thanks.
How about this?
find ../../../level1_dir1 -printf "%TY-%Tm-%Td\t%p\n"
If you want all the files, you don't even need -name in the find command. If you don't want to see the directories and only the files, just add "-type f" before -printf.
Hope this helps...

LINUX: How do I search for a string in a different directory?

I am working on the Linux terminal and I want to know how to search for c files in a certain directory. I have it working for the current directory but how do I do it for the directory usr/
for the current directory i did
grep -l "main" *.c
so I'd like to know how to search in the /usr directory without changing the directory first. Thanks.
You should just be able to prefix the directory name you want to the file pattern you want to search.
grep -l "main" /usr/*.c
You could also use:
find /usr/ -name "*.c"
if it is just the C files in the /usr/ directory you are after.

Resources