- ls command in Linux/Unix
- ls syntax
- ls command options
- ls command examples
- ls code generator
- 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
- 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
- Use ls command to list directories only
- List only subdirectories in a specific directory
- Use combination of ls and grep command
- Use find command to list only directories
- Use tree command to list only directories
- Using echo command for listing directories
- How to list only files and not directories of a directory Bash?
- 10 Answers 10
- Installation
- From the npm registry
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,
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/:
- 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
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.
Источник
How to list only files and not directories of a directory Bash?
How can I list all the files of one folder but not their folders or subfiles. In other words: How can I list only the files?
10 Answers 10
Using the -maxdepth 1 option ensures that you only look in the current directory (or, if you replace the . with some path, that directory). If you want a full recursive listing of all files in that and subdirectories, just remove that option.
ls -p lets you show / after the folder name, which acts as a tag for you to remove.
carlpett’s find -based answer ( find . -maxdepth 1 -type f ) works in principle, but is not quite the same as using ls : you get a potentially unsorted list of filenames all prefixed with ./ , and you lose the ability to apply ls ‘s many options;
also find invariably finds hidden items too, whereas ls ‘ behavior depends on the presence or absence of the -a or -A options.
An improvement, suggested by Alex Hall in a comment on the question is to combine shell globbing with find :
- However, while this addresses the prefix problem and gives you alphabetically sorted output, you still have neither (inline) control over inclusion of hidden items nor access to ls ‘s many other sorting / output-format options.
Hans Roggeman’s ls + grep answer is pragmatic, but locks you into using long ( -l ) output format.
- a utility that provides the output flexibility of ls while also providing type-filtering capability,
- simply by placing type-filtering characters such as f for files, d for directories, and l for symlinks before a list of ls arguments (run fls —help or fls —man to learn more).
Examples:
Installation
Supported platforms
- When installing from the npm registry: Linux and macOS
- When installing manually: any Unix-like platform with Bash
From the npm registry
Note: Even if you don’t use Node.js, its package manager, npm , works across platforms and is easy to install; try
curl -L https://git.io/n-install | bash
With Node.js installed, install as follows:
Note:
Whether you need sudo depends on how you installed Node.js / io.js and whether you’ve changed permissions later; if you get an EACCES error, try again with sudo .
The -g ensures global installation and is needed to put fls in your system’s $PATH .
Источник