- How to show recursive directory listing on Linux or Unix
- What is a recursive listing of files?
- How to get a recursive directory listing in Linux or Unix
- Linux recursive directory listing command
- Unix recursive directory listing command
- How to list all files recursively in a directory
- Recursively working with files
- Conclusion
- How To List Directories In Linux or Unix Recursively
- ls command examples: Get a recursive directory
- find command examples: Get a recursive directory
- du command examples: Get a recursive directory
- Linux / UNIX Recursively Search All Files For A String
- How to use grep command to recursively search All files for a String
- Following symtlinks
- Case sensitive recursive search
- Displaying files name when searching for a string/word
- Using find command to search recursively
- Finding all files containing specific text on Linux
- How to search only files that have specific extensions
- Understanding grep command options that used for searching text files
- Summing up
- How to find and delete directory recursively on Linux or Unix-like system
- Find command syntax to delete directory recursively
- Finding and deleting directory recursively using xargs
- Shell script to recursively remove backups older than 30 days
How to show recursive directory listing on Linux or Unix
I am a new Linux system user. How do I see a recursive directory listing on macOS Unix system? In Linux, how can I get a recursive directory listing?
Introduction – If you like to receive the list, all directories and files recursively try the following commands.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux and Unix-like OS |
Est. reading time | 3 minutes |
What is a recursive listing of files?
Recursive means that Linux or Unix command works with the contains of directories, and if a directory has subdirectories and files, the command works on those files too (recursively). Say you have a directory structure as follows:
tree dir1
From the above outputs, it is clear that running the tree dir1 gives a list of dir1 directory and its subdirectories and files. The base directory is dir1. Then you have all the child directroies. All all the child directories have additional files and directories (say grand directories), and so on. You can use various Linux commands going through each directory recursively until it hits the end of the directory tree. At that point Linux commands come back up to a branch in the tree a does the same thing for any sub-directories if any.
How to get a recursive directory listing in Linux or Unix
Try any one of the following command:
- ls -R : Use the ls command to get recursive directory listing on Linux
- find /dir/ -print : Run the find command to see recursive directory listing in Linux
- du -a . : Execute the du command to view recursive directory listing on Unix
Let us see some examples to get a recursive directory listing in Unix or Linux systems.
Linux recursive directory listing command
Type the following command:
ls -R
ls -R /tmp/dir1
Linux recursive directory listing using ls -R command.
Unix recursive directory listing command
Since, not all versions of Linux, macOS, *BSD, and Unix-like system have -R option for the ls command. Try to use find command:
find . -print
find /tmp/dir1 -print
find /tmp/dir1/ -print -ls
Recursive directory listing in Linux or Unix using the find command
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
How to list all files recursively in a directory
Our final example uses the du command as follows:
du -a .
du -a /tmp/dir1/
You can also use the tree command as follows:
tree .
tree /tmp/dir1/
Recursively working with files
It is possible to run command recursively on files. The syntax is:
my-command-here $(find /dir/ -name ‘pattern’ -print)
rm -i $(find /home/nixcraft/ -name ‘*.bak’ -print)
Of course, your can run command using find itself:
find /dir1/ -name ‘pattern’ -print -exec command ;
find /dir1/ -name ‘pattern’ -print -exec command <> ;
find /dir/2/foo/bar -name «*.pl» -exec rm -rivf <> \;
find /dir1/ -type f -name «*.doc» -exec rm -fiv <> \;
## find file recursively and delete them ##
find /dir1/ -name ‘pattern’ -print -delete
See “Linux / Unix: Find And Remove Files With One Command On Fly” for more info.
Conclusion
You learned how to list all files recursively in a directory under Linux, macOS, *BSD and Unix-like operating system using the ls, du, and find commands.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How To List Directories In Linux or Unix Recursively
I ‘m a new Linux and Unix-like system shell user. How can I get a recursive directory listing using command prompt on a Unix-like systems?
You can use any one of the following command to list files and directories in a recursive order on a Linux or Unix-like systems:[donotprint]
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | None |
Est. reading time | 1m |
[/donotprint]
Let use see examples of see a recursive directory listing in Unix.
ls command examples: Get a recursive directory
Type the following command list subdirectories recursively using the ls command:
Fig.01: ls command get a recursive directory listing
find command examples: Get a recursive directory
Type the following command:
Fig.02: find command get a recursive directory listing on a Unix-like system
du command examples: Get a recursive directory
The syntax is as follows to see an entry for each file in a file hierarchy:
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
For a more complete discussion of the commands used in this tutorial, see the online manual pages – du(1).
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
Linux / UNIX Recursively Search All Files For A String
H ow do I recursively search all text files for a string such as foo under UNIX / Linux / *BSD / Mac OS X shell prompt?
You can use grep command or find command as follows to search all files for a string or words recursively.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux or Unix with grep and find utilities |
Est. reading time | 2 minutes |
How to use grep command to recursively search All files for a String
The syntax is as follows for the grep command to find all files under Linux or Unix in the current directory:
cd /path/to/dir
grep -r «word» .
grep -r «string» .
The -r option read/sarch all files under each directory, recursively, following symbolic links only if they are on the command line. In other words, it will look into sub-directories too. We can also state path as follows:
grep -r ‘something’ /path/to/dir
Following symtlinks
The following syntax will read and search all files under each directory, recursively. Follow all symbolic links too by passing the -R (capital R ):
grep -R ‘word’ .
grep -R ‘string-to-search’ /path/to/dir/
Case sensitive recursive search
To ignore case distinctions, try:
grep -ri «word» .
Displaying files name when searching for a string/word
To display print only the filenames with GNU grep, enter:
grep -r -l «foo» .
You can also specify directory name:
grep -r -l «foo» /path/to/dir/*.c
Using find command to search recursively
find command is recommend because of speed and ability to deal with filenames that contain spaces.
- No ads and tracking
- In-depth guides for developers and sysadmins at Opensourceflare✨
- Join my Patreon to support independent content creators and start reading latest guides:
- How to set up Redis sentinel cluster on Ubuntu or Debian Linux
- How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
- How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
- A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
- How to protect Linux against rogue USB devices using USBGuard
Join Patreon ➔
Older UNIX version should use xargs to speed up things:
find /path/to/dir -type f | xargs grep -l «foo»
It is good idea to pass -print0 option to find command that it can deal with filenames that contain spaces or other metacharacters:
find /path/to/dir -type f -print0 | xargs -0 grep -l «foo»
OR use the following OSX/BSD/find or GNU/find example:
Sample outputs from the last command:
Fig.01: Unix and Linux: How to Grep Recursively?
Finding all files containing specific text on Linux
Say you want to find orange and mango words, then try:
grep -r -E ‘orange|mango’ .
grep -r -E ‘orange|mango’ /dir/to/search/
This is how you set up pattern
grep -r -e ‘pattern’ /dir/to/search
For extended grep (see egrep command for regular expressions):
egrep -r ‘word’ /dir/to/search/
egrep -r ‘regex’ /dir/to/search/
We can combine all options too:
grep -rnw -e ‘pattern’ /dir/to/search/
egrep -rnw ‘regex’ /path/to/search/
How to search only files that have specific extensions
Want to search files having either ‘.pl’ or ‘.php’ extensions for foo() ? Try:
grep —include=\*.
egrep —include=\*.
We can skip any command-line file with a name suffix that matches the pattern GLOB, using wildcard matching. For instance, exclude all .bin files:
grep —exclude=\*.bin -r -n -0 ‘string_to_search’ /path/
egrep —exclude=\*.bin -r -n -0 ‘regex’ /path/to/search/
When searching recursively, we can skip any subdirectory whose base name matches wildcard. For instance, skip includes and docs directory:
Understanding grep command options that used for searching text files
- -r : Rrecursive search
- -i : Ignore case distinctions in patterns and data
- -w : Match only whole words
- -n : Show line number with output lines
- -e ‘pattern’ : Use PATTERNS for matching
- -E : All search PATTERNS are extended regular expressions
- —include=GLOB : Search only files that match GLOB (a file pattern)
- —exclude=GLOB : Skip files that match GLOB
- —exclude-dir=GLOB : Skip directories that match GLOB
GLOB means to expand to wildcard patterns. For example, GLOB, *.txt means all files ending with .txt extension. A string is a wildcard pattern if it contains one of the following characters:
- ? – Matches any single character.
- * – Matches any string, including the empty string.
- [
Globbing is the operation that expands a wildcard pattern into the list of path-names matching the pattern.
Summing up
You learned how to search for text, string, or words recursively on Linux, macOS, *BSD, and Unix-like systems. See the following man pages:
man grep
man find
man 3 glob
man 7 glob
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Источник
How to find and delete directory recursively on Linux or Unix-like system
Find command syntax to delete directory recursively
Try the find command:
find /dir/to/search/ -type d -name «dirName» -exec rm -rf <> +
Another option is as follows to recursively remove folders on Linux or Unix:
find /dir/to/search/ -type d -name «dirName» -exec rm -rf \;
Warning : Be careful with the rm command when using with find. You may end up deleting unwanted data.
Find will execute given command when it finds files or dirs. For example:
find . -type d -name «foo» -exec rm -rf <> +
OR
find . -type d -name «bar» -exec rm -rf «<>» \;
Sample outputs:
You can find directories that are at least four levels deep in the working directory /backups/:
find /backups/ -type d -name «bar» -depth +4 -print0 -exec rm -rf <> +
Finding and deleting directory recursively using xargs
The syntax is as follows to find and delete directories on Linux/Unix system. For example, delete all empty directories:
find /path/to/dir/ -type d -empty -print0 | xargs -0 -I <> /bin/rm -rf «<>»
In this example, remove all foo directories including sub-diretories in /backups/ folder:
find /backups/ -type d -name «foo*» -print0 | xargs -0 -I <> /bin/rm -rf «<>»
The second command is a secure version. It is fast too, and it deals with weird directory names with white spaces and special characters in it:
Hence, I would like readers to use the following syntax:
find /path/to/search/ -name «pattern» -print0 | xargs -0 -I <> /bin/rm -rf «<>»
Where find command options are:
- -name «pattern» : Base of file name that matches shell pattern pattern. For example, foo, Foo*3 and so on.
- -print0 : Show the full file name on the standard output, followed by a null character. This allows file names that contain newlines or other types of white space to be correctly interpreted by programs that process the find output. This option corresponds to the -0 option of xargs
And xargs command options are:
- -0 : Input items given by find are terminated by a null character instead of by whitespace, and the quotes and backslash are not special (every character is taken literally). Disables the end of file string, which is treated like any other argument. Useful when input items might contain white space, quote marks, or backslashes. The GNU find -print0 option produces input suitable for this mode.
- -I <> : <> in the initial-arguments with names read from standard input. For example, dir names given by find command./li>
- /bin/rm -rf «<>« : Run rm command that remove files or directories passed by <> .
Shell script to recursively remove backups older than 30 days
Here is my sample script that removes older weekly backup of my VM tarballs stored in the following format:
Источник