Listing all directories linux

ls command in Linux/Unix

ls is a Linux shell command that lists directory contents of files and directories.

ls syntax

ls command options

ls command main options:

option description
ls -a list all files including hidden file starting with ‘.’
ls —color colored list [=always/never/auto]
ls -d list directories — with ‘ */’
ls -F add one char of */=>@| to enteries
ls -i list file’s inode index number
ls -l list with long format — show permissions
ls -la list long format including hidden files
ls -lh list long format with readable file size
ls -ls list with long format with file size
ls -r list in reverse order
ls -R list recursively directory tree
ls -s list file size
ls -S sort by file size
ls -t sort by time & date
ls -X sort by extension name

ls command examples

You can press the tab button to auto complete the file or folder names.

List directory Documents/Books with relative path:

List directory /home/user/Documents/Books with absolute path.

List root directory:

List parent directory:

List user’s home directory (e.g: /home/user):

List with long format:

Show hidden files:

List with long format and show hidden files:

Sort by date/time:

Sort by file size:

List all subdirectories:

Recursive directory tree list:

List only text files with wildcard:

ls redirection to output file:

List directories only:

List files and directories with full path:

ls code generator

Select ls options and press the Generate Code button:

Источник

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,

Читайте также:  Windows 10 где касперский

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.

Источник

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/:

  1. First, finds all files and directories and set permission to read-only for security reasons.
  2. 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

By default, it lists all the contents, be it a file or a directory or a link or a named pipe.

But what if you want to list only the directories? How do you that?

Like anything in Linux, there are several ways to accomplish the same task. Listing only the directories is no different:

  • ls -d */
  • ls -l | grep ‘^d’
  • find . -maxdepth 1 -type d
  • echo */
  • tree -d -L 1

Don’t worry. I’ll explain things in detail. Here’s the content of the directory I am going to use in the examples here:

Use ls command to list directories only

It is always good to do it with the familiar ls command because this is the command you use for displaying the content of a directory.

To list only the subdirectories, use the -d option with ls command like this:

Here’s the output it shows:

Why */ ? Because without it, ls -d will only return the directory name. The -d option list directories not its contents (which includes file, directories etc).

The */ is a pattern. With * , you list all the content (including contents of the subdirectories) and the / restricts the pattern to directories.

This picture depicts the difference pretty well.

You may combine it with long listing option -l and most other options:

If you do not want the trailing slash (/) at the end of the directory names, you can use the cut command to cut it out:

List only subdirectories in a specific directory

The above command works in the current directory. What if you are not in the same directory?

In this situation, you can use */ at the end of the path of the directory with ls -d :

Here’s an example where I move out of the Documents directory and then list only the directories inside Documents directory:

Did you notice that it doesn’t list the hidden directory? That’s one shortcoming of this method. You may use ls -d .*/ to display hidden directories, but it only displays hidden directories.

Use combination of ls and grep command

If you long list the contents, you can identify the directories because start with d .

You can use grep to filter the contents that start with d:

But this gives you a lot more fields than just the directory names:

Use find command to list only directories

Here’s how to use the find command to list only the subdirectories:

I hope you are familiar with the find command. I’ll explain it nonetheless.

With type d , you ask the find command to only look for directories.

With maxdepth 1 you ask the find command to keep the search at the current level only (and not go inside the subdirectories).

As you can see in the output above, it also shows the hidden directory.

Use tree command to list only directories

If your aim is to list only the directories, you may also use the tree command.

By default, the tree command gives you the complete directory structure. You can modify it to show only directories and only at the current level.

  • d — look for directories only
  • a — look for hidden files and directories as well
  • i — remove the tree structure from the display
  • L 1 — don’t go into the subdirectories

Here’s the output:

Using echo command for listing directories

The unlikely candidate? You’ll be surprised to know that echo command in Linux can also be used for displaying the contents of a directory. Try using echo * and see for yourself.

Similar to the ls command, you can also use the */ pattern to list only the directories in the current working directory.

Here’s the output which is identical to what you got with the ls -d command:

There could be more ways for listing only the directories, not files. In fact, the methods discussed here may have some ifs and buts based on what you are looking for.

If your aim is to just display the directories, most of the commands I discussed would work. If you want something more specific like only getting the directories name with slash etc, you’ll have to do some formatting on your own.

I hope you find this Linux tip helpful. Questions and suggestions are always welcome.

Источник

List of All Folders and Sub-folders [closed]

Want to improve this question? Update the question so it’s on-topic for Stack Overflow.

Closed 8 years ago .

In Linux, I want to find out all Folder/Sub-folder name and redirect to text file

I tried ls -alR > list.txt , but it gives all files+folders

3 Answers 3

You can use find

tree , If not installed on your system.

If you are using ubuntu

If you are using mac os .

snapshot folder in it, which I want to exclude?

snapshot -prune > output.txt

Will list all directories and subdirectories under the current path. If you want to list all of the directories under a path other than the current one, change the . to that other path.

If you want to exclude certain directories, you can filter them out with a negative condition:

snapshot folder in it, which I want to exclude?

As well as find listed in other answers, better shells allow both recurvsive globs and filtering of glob matches, so in zsh for example.

. lists all directories while keeping all the «-l» details that you want, which you’d otherwise need to recreate using something like.

(not quite as easy as the other answers suggest)

The benefit of find is that it’s more independent of the shell — more portable, even for system() calls from within a C/C++ program etc..

Источник

Читайте также:  Как удалить solidworks windows 10
Оцените статью