How to list folders using bash commands?
Is there any way to list just the folders in a directory using bash commands? ( as the ls command lists all the files and folders )
8 Answers 8
Since all directories end in / , this lists only the directories in the current path. The -d option ensures that only the directory names are printed, not their contents.
Stephen Martin’s response gave a warning, and listed the current folder as well, so I’d suggest
(This is on Linux; I could not find -maxdepth and -mindepth in the POSIX man page for find)
Will list just folders. And as Teddy pointed out you’ll need -maxdepth to stop it recusrsing into sub dirs
Daniel’s answer is correct. Here are some useful additions, though.
To avoid listing hidden folders (like .git ), try this:
And to replace the dreaded dot slash at the beginning of find output in some environments, use this:
You’re «not supposed to» parse the output of ls, or so is said. The reasoning behind is that the output is intended to be human-readable and that can make it unnecessarily complicated to parse, if I recall.
if you don’t want ls or find, you may want to try filtering «*» with «[ -d ]».
I did just that, for some reason ls and find weren’t working (file names with spaces and brackets I guess, or somthing else I was overlooking), then I did something along the lines of
Get a list of all files in folder and sub-folder in a file
How do I get a list of all files in a folder, including all the files within all the subfolders and put the output in a file?
7 Answers 7
You can do this on command line, using the -R switch (recursive) and then piping the output to a file thus:
this will make a file called filename1 in the current directory, containing a full directory listing of the current directory and all of the sub-directories under it.
You can list directories other than the current one by specifying the full path eg:
will list everything in and under /var and put the results in a file in the current directory called filename2. This works on directories owned by another user including root as long as you have read access for the directories.
You can also list directories you don’t have access to such as /root with the use of the sudo command. eg:
Would list everything in /root, putting the results in a file called filename3 in the current directory. Since most Ubuntu systems have nothing in this directory filename3 will not contain anything, but it would work if it did.
An alternative to recursive ls is the command line tool tree that comes with quite a lot of options to customize the format of the output diplayed. See the manpage for tree for all options.
Just use the find command with the directory name. For example to see the files and all files within folders in your home directory, use
Check the find manual manpage for the find command
Also check find GNU info page by using info find command in a terminal.
will give you the same as tree using other characters for the lines.
to display hidden files too
to not display lines
- Go to the folder you want to get a content list from.
- Select the files you want in your list ( Ctrl + A if you want the entire folder).
- Copy the content with Ctrl + C .
- Open gedit and paste the content using Ctrl + V . It will be pasted as a list and you can then save the file.
This method will not include subfolder, content though.
You could also use the GUI counterpart to Takkat’s tree suggestion which is Baobab. It is used to view folders and subfolders, often for the purpose of analysing disk usage. You may have it installed already if you are using a GNOME desktop (it is often called disk usage analyser).
You can select a folder and also view all its subfolders, while also getting the sizes of the folders and their contents as the screenshot below shows. You just click the small down arrow to view a subfolder within a folder. It is very useful for gaining a quick insight into what you’ve got in your folders and can produce viewable lists, but at the present moment it cannot export them to file. It has been requested as a feature, however, at Launchpad. You can even use it to view the root filesystem if you use gksudo baobab .
(You can also get a list of files with their sizes by using ls -shR
How to get the list of files in a directory in a shell script?
I’m trying to get the contents of a directory using shell script.
where $search_dir is a relative path. However, $search_dir contains many files with whitespaces in their names. In that case, this script does not run as expected.
I know I could use for entry in * , but that would only work for my current directory.
I know I can change to that directory, use for entry in * then change back, but my particular situation prevents me from doing that.
I have two relative paths $search_dir and $work_dir , and I have to work on both simultaneously, reading them creating/deleting files in them etc.
So what do I do now?
10 Answers 10
The other answers on here are great and answer your question, but this is the top google result for «bash get list of files in directory», (which I was looking for to save a list of files) so I thought I would post an answer to that problem:
If you want only a certain type (e.g. any .txt files):
Note that $search_path is optional; ls > filename.txt will do the current directory.
This is a way to do it where the syntax is simpler for me to understand:
./ is the current working directory but could be replaced with any path
*.txt returns anything.txt
You can check what will be listed easily by typing the ls command straight into the terminal.
Basically, you create a variable yourfilenames containing everything the list command returns as a separate element, and then you loop through it. The loop creates a temporary variable eachfile that contains a single element of the variable it’s looping through, in this case a filename. This isn’t necessarily better than the other answers, but I find it intuitive because I’m already familiar with the ls command and the for loop syntax.
Using ls to list directories and their total sizes
Is it possible to use ls in Unix to list the total size of a sub-directory and all its contents as opposed to the usual 4K that (I assume) is just the directory file itself?
After scouring the man pages I’m coming up empty.
26 Answers 26
Try something like:
Explanation:
du : Disk Usage
-s : Display a summary for each specified file. (Equivalent to -d 0 )
-h : «Human-readable» output. Use unit suffixes: Byte, Kibibyte (KiB), Mebibyte (MiB), Gibibyte (GiB), Tebibyte (TiB) and Pebibyte (PiB). (BASE2)
du -sk * | sort -n will sort the folders by size. Helpful when looking to clear space..
This will be displayed in human readable format.
To list the largest directories from the current directory in human readable format:
A better way to restrict number of rows can be
du -sh * | sort -hr | head -n10
Where you can increase the suffix of -n flag to restrict the number of rows listed
It makes it more convenient to read 🙂
To display it in ls -lh format, use:
Awk code explained:
The command you want is ‘du -sk’ du = «disk usage»
The -k flag gives you output in kilobytes, rather than the du default of disk sectors (512-byte blocks).
The -s flag will only list things in the top level directory (i.e., the current directory, by default, or the directory specified on the command line). It’s odd that du has the opposite behavior of ls in this regard. By default du will recursively give you the disk usage of each sub-directory. In contrast, ls will only give list files in the specified directory. (ls -R gives you recursive behavior.)
Put this shell function declaration in your shell initialization scripts:
I called it duls because it shows the output from both du and ls (in that order):
The paste utility creates columns from its input according to the specification that you give it. Given two input files, it puts them side by side, with a tab as separator.
We give it the output of du -hs — «$@» | cut -f1 as the first file (input stream really) and the output of ls -ld — «$@» as the second file.
In the function, «$@» will evaluate to the list of all command line arguments, each in double quotes. It will therefore understand globbing characters and path names with spaces etc.
The double minuses ( — ) signals the end of command line options to du and ls . Without these, saying duls -l would confuse du and any option for du that ls doesn’t have would confuse ls (and the options that exist in both utilities might not mean the same thing, and it would be a pretty mess).
The cut after du simply cuts out the first column of the du -hs output (the sizes).
I decided to put the du output on the left, otherwise I would have had to manage a wobbly right column (due to varying lengths of file names).
The command will not accept command line flags.
This has been tested in both bash and in ksh93 . It will not work with /bin/sh .