how to recursively list files from a folder? - linux

'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

Related

How can I find a file within a specific directory name?

So I need to find all files in /home/ with a file name of "options.php".
find . -name "options.php"
When 'in home', that will find all options.php files, however, I want to only find all options.php files when they are in /public_html/.
So in other words, it should ignore all other 'options.php' files found.
eg, positive/show results:
/home/usr1/public_html/options.php
/home/usr2/public_html/options.php
eg, shouldnt show me:
/home/usr1/public_html/wp-admin/options.php
/home/usr2/public_html/wp-content/plugins/whatever/options.php
You can pass a pattern via -path option as follows:
find /home/ -path '*/public_html/options.php'
For a more flexible pattern use -regex which accepts a regular expression applied on the whole path. But in this particular case -regex has no advantage over -path:
find /home/ -regex '.*/public_html/options.php'
Filter the desired results from the found results with grep.
find . -name "options.php" | grep 'public_html/options.php'
You can limit the depth of find:
find . -maxdepth N, this way It should only find options.php in your desired folder.
The ls utility is much better suited for this task:
ls -1 /home/*/public_html/options.php
If you want to process the result list and do not want to have an error message or warning in case no such files are found, then simply redirect the error output of the command:
ls -1 /home/*/public_html/options.php 2>/dev/null
An alternative using the find utility would be:
find /home -path "*/public_html/options.php"
Or, if you want to prevent matches in folders called "public_html" further down in the hierarchy:
find /home -path "/home/*/public_html/options.php"
find /home -maxdepth 3 -path "*/public_html/options.php"

How to get a bare, recursive directory listing in Linux, excluding some directories

I need to obtain a recursive directory listing in Linux with only the directory and file name. It needs to include all files including hidden files with the exception of files name “.svn”.
I have tried multiple combinations of the “ls” command and haven’t been able to figure it out. When using “ls –R direname/ grep –v /$” I get a directory heading followed by a colon, which I cannot use.
If I have a directory name test with files and a sub-directory named test2 with files, I need the output to look like the following:
test
test/.filehidden1
test/file2
test/file3.txt
test/test2.log
test/test2/file.hidden1
test/test2/file2.boo
test/test2/file3.boo2
Notice there is no leading forward slash
find . -name .svn -prune -o -print
-prune tells it to not descend into any matching directories.
This should do:
find . ! -path \*.svn\*
This tells find to recursively list all files from . whose pathname does not contains .svn. This is not perfect since it may hide for instance file foo.svnbar.
Something like this:
find DIRNAME ! -name .svn
I need to obtain a recursive directory listing in Linux with only the directory and file name. It needs to include all files including hidden files with the exception of files name “.svn”.
Do you want to get a list of files in a Subversion repository?
This will do just that:
svn ls -R
If not, then you can use find:
find . ! -path '*/.svn/*' ! -name .svn
You mentioned "Notice there is no leading forward slash".
If that's very important, then the find command can be rephrased as:
find * .[^.]* ! -path '.svn/*' ! -name .svn

Loop through a directory with any level of depth

I want to execute a command on all files present on all levels in the directory. It may have any number of files and sub directories. Even these sub directories may contain any number of files and subdirectories. I want to do this using shell script. As I am new to this field can any one suggest me a way out.
You can use the command "find" with "xargs" after "|"(pipe).
Example: Suppose that I want to remove all files that have ".txt" extension on "Documents" directory:
find Documents -iname *.txt |xargs rm -f
Helps?
You can use a recursive command that uses wildcard characters (*) like so:
for dir in ~/dev/myproject/*; do (cd "$dir" && git status); done
If you want to apply commands on the individual files you should use the find command and execute commands on it like so:
find yourdirectory -type f -exec echo "File found: '{}'" \;
What this does:
finds all the items in the directory yourdirectory
that have the type f - so are a file
runs an exec on each file
Use find:
find -type f -exec COMMAND {} \;
-f applies the command only to files, not to directories. The command is recursive by default.

check if a file is in a folder or its subfolder using linux terminal

I want to check if the particular file is in a folder or its sub folder or not using Linux terminal.
Which should I use for this? I use find and grep command but it travels only one folder.
In order to search from your current directory, use
find . -name filename
In order to search from root directory use
find / -name filename
If you don't know the file extension try
find . -name filename.*
Also note that find command only displays the files in the path which you have permission to view. If you don't have permission for a/b/c path then it will just display a message mentioning that path can't be searched
If you want to search for by filename, use find:
find /path -name "filename"
example:
find . -name myfile.txt
If need to find all files containing a specific string, use grep:
grep -r "string" /path
example:
grep -r foobar .
By default, find will traverse all subdirectories, for example:
mkdir level1
mkdir level1/level2
touch level1/level2/file
find . -name "file"
Output:
./level1/level2/file
locate file name
This is the simple command
I also prefer using a combination of tree and grep. Something like
tree | grep filename
Try
find . -name "filename" -type f
-type f restricts to only files in the current directory (replace . with your path).

Remove files for a lot of directories - Linux

How can I remove all .txt files present in several directories
Dir1 >
Dir11/123.txt
Dir12/456.txt
Dir13/test.txt
Dir14/manifest.txt
In my example I want to run the remove command from Dir1.
I know the linux command rm, but i don't know how can I make this works to my case.
PS.: I'm using ubuntu.
To do what you want recursively, find is the most used tool in this case. Combined with the -delete switch, you can do it with a single command (no need to use -exec (and forks) in find like other answers in this thread) :
find Dir1 -type f -name "*.txt" -delete
if you use bash4, you can do too :
( shopt -s globstar; rm Dir1/**/*.txt )
We're not going to enter sub directories so no need to use find; everything is at the same level. I think this is what you're looking for: rm */*.txt
Before you run this you can try echo */*.txt to see if the correct files are going to be removed.
Using find would be useful if you want to search subfolders of subfolders, etc.
There is no Dir1 in the current folder so don't do find Dir1 .... If you run the find from the prompt above this will work:
find . -type f -name "*.txt" -delete

Resources