- 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
- Linux / UNIX List Just Directories Or Directory Names
- Display or list all directories in Unix
- Linux list only directories using ls command
- Linux Display or list only files
- Task: Create bash shell aliases to save time
- Use find command to list either files or directories on Linux
- Putting it all together
- How to List Only Directories in Linux
- Listing directories using Wildcards
- Using -F option and grep
- Using -l option and grep
- Using echo command
- Using printf
- Using find command
- How To Find a Directory On Linux Based System
- How to find a directory on Linux
- Linux find directory command
- Finding a directory
- Dealing with “Permission denied error messages” on Linux
- How to find a directory named Documents on Linux?
- Getting a detailed list of files/dirs
- How do I list only directories?
- How do I perform a case insensitive search?
- How do I find a directory called project.images?
- A note about locate command
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
Источник
Linux / UNIX List Just Directories Or Directory Names
You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too. In this quick tutorial you will learn how to list only directories in Linux or UNIX.
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | Linux, macOS, or Unix terminal |
Est. reading time | 5 minutes |
Display or list all directories in Unix
Type the following command:
$ ls -l | grep `^d’
$ ls -l | egrep `^d’
Or better try the following ls command only to list directories for the current directory:
$ ls -d */
Sample outputs:
Fig.01: List Directories in Unix and Linux Systems
Linux list only directories using ls command
Run the following ls command:
ls -d */
Listing only directories using ls command in Linux or Unix-like systems
Linux Display or list only files
Type the following command to display list only files in Linux or Unix:
$ ls -l | egrep -v ‘^d’
$ ls -l | egrep -v ‘^d’
The grep command is used to searches input. It will filter out directories name by matching first character ‘ d ‘. To reverse effect i.e. just to display files you need to pass the -v option. It invert the sense of matching, to select non-matching lines.
Task: Create bash shell aliases to save time
You can create two aliases as follows to list only directories and files.
alias lf=»ls -l | egrep -v ‘^d'»
alias ldir=’ls -d */’
##alias ldir=»ls -l | egrep ‘^d'»
Put above two aliases in your bash shell startup file:
$ cd
$ vi .bash_profile
Append two lines:
alias lf=»ls -l | egrep -v ‘^d'»
alias ldir=’ls -d */’
#alias ldir=»ls -l | egrep ‘^d'»
Save and close the file in vim. Now just type lf – to list files. Again run ldir to list directories only:
$ cd /etc
$ ldir
Sample outputs:
List directory names only:
$ cd /etc
$ ldir
Sample outputs:
Use find command to list either files or directories on Linux
The find command can be used as follows to list all directories in /nas, enter:
Pass the -maxdepth 0 to limit listing to the starting-points i.e. the current working directory only:
find /path/to/dir -maxdepth 1 -type d
find . -maxdepth 1 -type d
find . -maxdepth 1 -type d -ls
Listing only directories using the find command in Linux
Putting it all together
Say you want to find all directories ending with .bak extension and delete it, run the following find command in the current directory:
find . -type d -iname «.bak» -delete
Verify it:
find . -type d -iname «.bak» -ls
The following shell script does two things for Apache/Nginx/Lighttpd Webroot such as /webroot/:
- First, finds all files and directories and set permission to read-only for security reasons.
- Second, it allows our web server to read files regardless of permission so that we don’t get an HTTP/403 error.
In other words, all write permissions are removed from Webroot. The server/web-app can only read files but can not alter any files or upload any files. It helps reduces attack surfaces provided that you configure the rest of the server and web application firewall correctly.
Источник
How to List Only Directories in Linux
The ls command in Linux is used to list the files and directories in a directory. But if you wish to list directories only using ls command, what are the options?
We learn how to use command chaining (using pipes) to see what directories are present in a given directory.
In this tutorial, I will show you a number of ways to list directories only in Linux.
Listing directories using Wildcards
The simplest method is using wildcards. All the directories end in forward slash.
For the long listing, just add -l option.
Using -F option and grep
The -F options appends a trailing forward slash. So we can grep the directories only by ‘grep’ ing lines ending with a forward slash (/).
or for just the directory names, without -l option,
Using -l option and grep
In the long listing of ls i.e. ls -l , we can ‘grep’ the lines starting with d .
We can extract just the file names by printing only the last columns.
Using echo command
We can use echo command to list the entries trailing with forward slash (/).
Using printf
Similarly, printf can be used to highlight strings ending with forward slash (/).
Using find command
We can always find files based on their file types using find command:
The maxdepth option in the above command specifies that the search is to be performed in specified directory only. Otherwise, find command will find the directories recursively, by traversing each directory and their subdirectories. Also, in this command, the hidden directories are also shown. In all above methods that use ls command, the same can be achieved through -a option. For example,
Thanks for reading this article. Let me know your thoughts.
Источник
How To Find a Directory On Linux Based System
I just switched from MS-Windows server admin to Debian Linux server system administration roles. I need to find a directory called project.images. I was also told that the locate command is the simplest and quickest way to find the locations of files and directories on Linux. But the locate command is not working out for me. How do I find project.images directory using command-line options only?
Tutorial details | |
---|---|
Difficulty level | Easy |
Root privileges | No |
Requirements | find command on Linux or macOS/Unix |
Est. reading time | 5m |
You need to use find command. It is used to locate files on Linux or Unix-like system. The locate command will search through a prebuilt database of files generated by updatedb.
The find command will search live file-system for files that match the search criteria.
How to find a directory on Linux
The find command syntax is:
find /where/to/look/up criteria action
find /dir/path/look/up criteria action
find /dir/path/look/up -name «dir-name-here»
find /dir/path/look/up -name «pattern»
find /dir/path/look/up -name «dir-name-here» -print
find /dir/path/look/up -name «dir-name-here»
find / -name «dir-name-here»
find / -type d -name «dir-name-here»
find / -type d -name «dir-name-here» 2>/dev/null
Linux find directory command
The following example will show all files in the current directory and all subdirectories:
Finding a directory
To find a directory called apt in / (root) file system, enter:
Alert: When searching / (root) file system, you need to run the find command as root user.
Dealing with “Permission denied error messages” on Linux
Find will show an error message for each directory/file on which you don’t have read permission
How to find a directory named Documents on Linux?
Type the following command to search for Documents directory in your $HOME dir:
$ find $HOME -type d -name Documents
Sample outputs:
Getting a detailed list of files/dirs
Pass the -ls to list current file in ls command output format:
How do I list only directories?
Just find directories and skip file names pass the -type d option as follows:
How do I perform a case insensitive search?
Replace -name option with -iname as follows:
The patterns ‘apt’ match the directory names ‘apt’, ‘APT’, ‘Apt’, ‘apT’, etc.
How do I find a directory called project.images?
Type any one of the following 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 ➔
A note about locate command
See also
- All find command examples from our /faq/ sections.
- Find command man page
🐧 Get the latest tutorials on Linux, Open Source & DevOps via
Category | List of Unix and Linux commands |
---|---|
Documentation | help • mandb • man • pinfo |
Disk space analyzers | df • duf • ncdu • pydf |
File Management | cat • cp • less • mkdir • more • tree |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Linux Desktop Apps | Skype • Spotify • VLC 3 |
Modern utilities | bat • exa |
Network Utilities | NetHogs • dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop |
Searching | ag • grep • whereis • which |
Shell builtins | compgen • echo • printf |
Text processing | cut • rev |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Comments on this entry are closed.
Why don’t you run updatedb and then locate and again you’ll have “simplest and quickest way to find the locations of files and directories on Linux”.
updatedb will update your database.
That only helps for semi-permanent files since it only checks periodically to update the updatedb database. For files that were created recently it will not be found.
Find is a great tool that i use a lot.
You could have talk about the -exec switch wich allows you to process the outpout. ie : find and delete all file in ./ that haven’t been modified since 90 day:
Anyway, great job on this website, keep it on!
I have to second that updatedb is the way to go for a novice linux user. No worries about syntax and whatnot. Its also very useful for when you need to do multiple scans since you only traverse the filesystem once.
In regards to -exec, you should be using -execdir when available due to some security implications… and the above rm -rf is somewhat dangerous since find by default traverses from the top down. Delete would be a much safer (and faster!!) operation than-exec rm.
I have to agree with your update, rm -Rf is maybe too dangerons to use for novice users.
I did not know -execdir wich seems to be very usefull.
Thank you for that update =)
Great article. so useful. since I don’t have root, I get very verbose “no permission” output that is useless and I have to find the actual location through all the muck. Is there a way to only print found paths? Thanks so much for this article!
All those “no permission” messages should be on stderr while the information you want is on stdout. Both stderr and stdout default to printing on the controlling terminal. Tacking “> stdoutfile” on the end of the command would separate them, leaving all the unwanted noise on the terminal and putting the good stuff in stdoutfile. It would make more sense to redirect stderr to /dev/null (throwing it away) and leaving the useful output on the controlling terminal, but that would require finding the instructions for redirecting stderr in the shell docs (again).
To avoid seeing stderr messages, just use something like this:
Источник