This command lists directories in the current path: ls -d */
What exactly does the pattern */
do?
And how can we give the absolute path in the above command (e.g. ls -d /home/alice/Documents
) for listing only directories in that path?
This command lists directories in the current path: ls -d */
What exactly does the pattern */
do?
And how can we give the absolute path in the above command (e.g. ls -d /home/alice/Documents
) for listing only directories in that path?
O/P (On my machine) --
Above output can be refined more by using cut .
A plain listing of the current directory, it'd be:
Do you want it sorted and clean?
One-liner to list directories only from "here".
With file count.
Four ways to get this done, each with a different output format
1. Using
echo
Example:
echo */
,echo */*/
Here is what I got:
2. Using
ls
onlyExample:
ls -d */
Here is exactly what I got:
Or as list (with detail info):
ls -dl */
3. Using
ls
andgrep
Example:
ls -l | grep "^d"
Here is what I got:4. Bash Script (Not recommended for filename contain space.)
Example:
for i in $(ls -d */); do echo ${i%%/}; done
Here is what I got:
If you like to have '/' as ending character, the command will be:
for i in $(ls -d */); do echo ${i}; done
I just add this to my
.bashrc
file (you could also just type it on the command line if you only need/want it for one session)then lsd will produce the desired result.
The
tree
command is also pretty useful here. By default it will show all files and directories to a complete depth, with some ascii characters showing the directory tree.But if we wanted to get just the directories, without the ascii tree, and with the full path from the current directory, you could do:
The arguments being:
And if you then want the absolute path, you could start by specifying the full path to the current directory:
And to limit the number of subdirectories, you can set the max level of subdirectories with
-L level
, eg:More arguments can be seen with man tree