How to know list of directories under a filesystem?
But, how will I know the list of directories under /sda2 ? For example, If I run ls / command, I got all the directories under root.
$ ls / bin cdunix dev etc lib lost+found misc mnt1 mtp net PatchInstall root selinux sys tmp usr boot cron_4058 esm home lib64 media mnt mnt2 NB_DIR opt proc sbin srv tftpboot u000 var
- But, is there any command or way through which I can also list down their filesystem too?
- Since, there is very less amount of space remaining on /dev/sda2/ . How can I vacant more space from this partition?
3 Answers 3
If you are looking for directories that are using up space, and are not on a different partition, then you want du -hx —max-depth=1 / . The -x tells it not to descend into directories that are on other filesystems ( partitions ). The —max-depth=1 asks to only print a line ( listing the total space for that directory and all subdirectories ) for each directory in / rather than every single subdirectory.
You can then start drilling down into the larger subdirectories to find what is taking up the space.
If I am reading this question correctly, there is a program called tree . This would list all directories in a tree like structure. With it installed, you can do something like:
tree -x Where -x Stay on the current file-system only. Ala find -xdev.
UPDATE: I have tried tree -P /dev/xvda and it seemed to have shown directories under that filesystem. The -P command stands for pattern. So to answer your question, you should be able to use it to list directories in filesystems.
To list the first levels in / directory, try command:
where L is level Max display depth of the directory tree.
But, how will I know the list of directories under /sda2?
There are no directories under /dev/sda2, /dev/sda2 is a partition on the disk (sda is the disk and 2 is the partitions number).
But, is there any command or way through which I can also list down their filesystem too?
Since you only have one filesystem mounted then they are all on the / fs.
A better way to see a list of mounted filesystems is to use mount:
$ sudo mount sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) udev on /dev type devtmpfs (rw,relatime,size=10240k,nr_inodes=2018106,mode=755) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000) tmpfs on /run type tmpfs (rw,nosuid,relatime,size=3232436k,mode=755) /dev/sda5 on / type ext4 (rw,noatime,errors=remount-ro,data=ordered)
To see the amount of storage used by a folder you can use the du command, eg.:
du has lots of use full options. A really good one when tracking down large folders is —max-depth=1 which will limit the depth of the output:
du -h --max-depth=1 / 7,9M /sbin 702M /lib
You will get lots of error messages if you run this on / without being root.
du can help you figure out which files to delete to make more space. If you can’t delete any files then you either need to resize the partition or move some data to a different partition.
This post and this website contains affiliate links. See my disclosure about affiliate links.
how to list folders or directories in linux
In Linux, everything is implemented as “Files“. That mostly means that they all share several “common” features/properties while having some unique properties of their own. All files and folders are implemented the same way, as Files. That is a very abstract explanation of the whole thing and there are still some differences between file systems.
If you want to really understand how files and folders are implemented in your filesystem, then you can find what specific filesystems you are using and then look into the specific implementation of the same. Folders are just “special” files with certain specific properties. Any more detailed explanation of files and folders is probably beyond the scope of this post.
The important thing for you to remember that everything including files, folders, links and devices etc. etc. are all Files at its core. That helps to better understand how the whole thing works.
So, you want to list all folders or directories inside a folder. We will mainly deal with listing only folders (and not files) although these commands can list both files and folders. There are mainly two different commands you can use to list files and folders: ls and find.
Please remember that the output might work slightly different depending on the shell you use. I use fish by default, but the following examples should work in other shells as well, such as bash and zsh.
list all folders in a folder
When using ls to list folders, the command line option of consequence is -d. The -d option will list the directories by themselves, and not its content. This, however also depends on how you specify the path to the folder. So, if you want to all directories in the current working folder…
In order to specify another directory, which is not the current folder you will need to specify the path as below…ending with */
Another option is the find command. I find that the find command is much more versatile than ls itself, but it can be slower depending on the number of files you have in the folder. So, using find command to print out folders in the current directory.
The -maxdepth command line option lets you control the depth of the folders you want to traverse. This can be an extremely powerful feature when you want to traverse the folder recursively. The -type option is used to specify what types of files you want listed. The d specifies that we want only directories. You can use f for files and l for symbolic links.
If you want to list first level folders in another directory, then substitute the path (.) in the above example with the path. Also, if you want to see the directory/file metadata such as date, permissions, user etc then tack on the -ls option at the end.
$ find newfolder/ -maxdepth 1 -type d -ls
list all folders and sub-folders
Now, let’s say you want to print out all folders and sub-folders inside a directory. In my opinion, the simplest and easiest command for this is find. You can use ls as well, but there is no straight forward way to print out only the directories without filtering the output by using either awk, grep or cut etc.
The command line option -R or –recursive will allow you to recurse into each and every folder and its sub-folders. You can use pretty much the same find command in the previous section to print the folders and sub-folders.
Now, you can easily filter the depth of the folders using the -maxdepth option. If you want to display only folders and sub-folder that are 3 levels deep, then
$ find path/to/folder -maxdepth 3 -type d
list folders by size
If you want to find the size of any particular folder, then it can easily be done with either ls or du commands. We will quickly deal with listing the folders along with the total size of the folder. We can then sort the output to find which folder is using the most space.
We use the du command to compute the size or space used by each listed folder first. We can then the sort command to sort the folders in decreasing order of size.
bash$ find ./ -maxdepth 3 -type d -exec du -s <> \; | sort -nr
You can leave out the maxdepth option if you want to list all sub-folders recursively.
The above syntax will work with bash. If you encounter an error with another shell, then refer to the shell documentation. For example, in fish you will need to either escape the “” (ie. \ ) or use quotes around it ‘<>’. The example below will work with both shells.
fish$ find ./ -maxdepth 3 -type d -exec du -s ‘<>‘ \; | sort -nr
List of All Folders and Sub-folders [closed]
In this Option , is there an Option to exclude one single folder. I have a ~snapshot folder in it, which I want to exclude?
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:
find . -type d ! -name "~snapshot" > list.txt
In this Option , is there an Option to exclude one single folder. I have a ~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..
In this Option , is there an Option to exclude one single folder. I have a ~snapshot folder in it, which I want to exclude?
@Sandeep50: in zsh, yes: setopt EXTENDED_GLOB , then ls -lad **/*~**/~snapshot(/) . Details: from man zshall / «x~y (Requires EXTENDED_GLOB to be set.) Match anything that matches the pattern x but does not match y. [. ]». (There’s also «^x (Requires EXTENDED_GLOB to be set.) Matches anything except the pattern x. [further explanation of slightly different syntax + example]»)
How can I see folders from terminal?
list directories themselves, not their contents. To explain this, consider what happens if we type ls */ . ls goes one layer down, into each subdirectory, and lists all the files in each of those sequentially
*/ is known as a «glob» in UNIX. (see Wikipedia for more details). But basically, it means «any file name ending in a forward slash.» In UNIX, directories are really just files, fundamentally. But they are specially named ending in a forward slash so the operating system knows they are directories (or folders, in everyday-person-speak). And the asterisk * is technically a wildcard standing for «any string of characters.»
What is a glob?
This paragraph will not pertain specifically to your question, but if you’ve never read about this, it’ll be good to see it. Globs are different from Regular Expressions, as (partially) explained in What is the difference between Regular Expressions and Globbing? There have been whole books written on regular expressions, but tl;dr there are a bunch of different ways to encode pattern-matching expressions.
How to show hidden folders as well? ls -d .*/ shows only hidden folders. How to view BOTH hidden and non-hidden folders? I can only think of ls -d */ .*/ Anything better?
As I am a very inexperienced user I love this website. It tells you all you want to know about bash commands, in some cases it even gives you examples. Very useful.
- ls to list the files
- ls -a to include hidden files
- ls -l for a long listing format
- .
lists one directory per line.
If you want to be able to distinguish folders from files easily, use something like ls -alhF . I usually define l as an alias for that, ie. I put the line alias l=’ls -alhF’ in my .bashrc .
include hidden files ‘-a’ grep ^d get start with ‘d’ wich means directory when name starts with dot directory is hidden
to list recursively see this
find ./ -type d | less ./ . means starts find in current folder -type . indicates the type to be searched d . means directory | . redirects the command less . enables paging using the keyboard arrows and leave with q
$ ls Desktop Downloads hadoop Pictures Templates Documents examples.desktop Music Public Videos
$ ls ~ /usr /home/hadoop1: Desktop Downloads hadoop Pictures Templates Documents examples.desktop Music Public Videos /usr: bin games include lib local locale sbin share src
$ ls -l total 48 drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Desktop drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Documents drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Downloads -rw-r--r-- 1 hadoop1 hadoop1 8980 Jul 1 2017 examples.desktop drwxr-xr-x 10 hadoop1 hadoop1 4096 Jul 1 2017 hadoop drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Music drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Pictures drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Public drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Templates drwxr-xr-x 2 hadoop1 hadoop1 4096 Jul 1 2017 Videos