# DBT JRF 2022 — Question 133

Exam: DBT JRF 2022 · Section: B · Topic: Bioinformatics › Unix/Linux Commands · Difficulty: Medium

## Question

Which command is suitable to list out all the directories in the working directory, in linux/unix environment?

## Options

- **A.** ls -1|grep ^d
- **B.** grep ^d | ls -1
- **C.** ls -l | grep ^d
- **D.** ls | grep ^dir

## Answer

Option C

## Explanation

Linux command to list only directories: The correct Unix/Linux command pipeline: $\texttt{ls -l | grep }\hat{}\texttt{d}$ Step-by-step explanation: 1. $\texttt{ls -l}$ : long listing format; each line starts with a character indicating file type: - $\texttt{d}$ : directory - $\texttt{-}$ : regular file - $\texttt{l}$ : symbolic link - $\texttt{b}$ : block device 2. $\texttt{| grep }\hat{}\texttt{d}$ : pipe output to grep; $\hat{}\texttt{d}$ matches lines STARTING WITH 'd' (i.e., directories only). Why the other options are wrong: • $\texttt{ls -1|grep }\hat{}\texttt{d}$ : $\texttt{ls -1}$ (one column) doesn't show file type prefix → won't work. • $\texttt{grep }\hat{}\texttt{d | ls -1}$ : wrong pipe direction (grep before ls makes no sense). • $\texttt{ls | grep }\hat{}\texttt{dir}$ : this would only match files/dirs literally named 'dir...' Answer: $\texttt{ls -l | grep }\hat{}\texttt{d}$ correctly filters long-listing output to show only directories.

Source: https://mypyqbuddy.com/questions/which-command-is-suitable-to-list-out-all-6a104cd5920734408bc0c794
