What is the DOS-counterpart of the bash command "ls -lad" on Unix?
Sebastian Wright
What is the DOS-counterpart of the bash command ls -lad on Unix?
The "dir" command on Command Prompt (cmd.exe) on Windows is usually considered to be equivalent to the "ls" command of bash on Unix/Linux. However, "dir" lacks the feature of the "-d" option of "ls". The "-d" option treats directories like plain files, and prevents their child files and subdirectories from being displayed.
ls -lad ITEM- Basically, what I need is the existence and type of the given ITEM.
- As for type, I need to know whether it is a directory, a regular file or a link.
- If it is a directory, I do not want its child files nor subdirectories to be displayed.
- If it is a link, I need to know the target.
Is there any DOS command (or even PowerShell command) with these features (1) through (4)?
dir ITEM /a
attrib ITEMThe "attrib" command on DOS also tells the existence of the given ITEM; and, if it is a directory, "attrib" refrains from displaying its child files and subdirectories. However, "attrib" fails to tell whether it is a directory, a regular file or a link.
1 Answer
What is the DOS-counterpart of the bash command ls -lad?
There is no direct equivalent. You can, of course, install Cygwin or the Windows Subsystem for Linux and run ls -lad directly.
You can also create a batch file that will do most of what you want. Below are some hints.
To check for the existence of a file:
if exist filename ( echo filename exists )To check if a file is a directory:
if exists filename\nul ( echo directory
) else ( echo file
)To check if a file is a link:
dir /a:l filename | find "<SYMLINK>" >nul && echo file symbolic linkTo check if a directory is a link:
dir /a:l filename | find "<SYMLINKD>" >nul && echo directory symbolic linkTo get the target of a file link:
for /f "usebackq delims=[] tokens=2" %i in (`dir /a:l filename ^| find "<SYMLINK">`) do @echo %iTo get the target of a directory link:
for /f "usebackq delims=[] tokens=2" %i in (`dir /a:l filename* ^| find "<SYMLINKD>"`) do @echo %iFurther Reading
- An A-Z Index of the Windows CMD command line - An excellent reference for all things Windows cmd line related.
- dir - Display a list of files and subfolders.
- find - Search for a text string in a file & display all the lines where it is found.
- for /f - Loop command: against a set of files - conditionally perform a command against each item.
- if - Conditionally perform a command.