Velvet Star Monitor

Standout celebrity highlights with iconic style.

general

What is the DOS-counterpart of the bash command "ls -lad" on Unix?

Writer 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
  1. Basically, what I need is the existence and type of the given ITEM.
  2. As for type, I need to know whether it is a directory, a regular file or a link.
  3. If it is a directory, I do not want its child files nor subdirectories to be displayed.
  4. 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 ITEM

The "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 link

To check if a directory is a link:

dir /a:l filename | find "<SYMLINKD>" >nul && echo directory symbolic link

To get the target of a file link:

for /f "usebackq delims=[] tokens=2" %i in (`dir /a:l filename ^| find "<SYMLINK">`) do @echo %i

To get the target of a directory link:

for /f "usebackq delims=[] tokens=2" %i in (`dir /a:l filename* ^| find "<SYMLINKD>"`) do @echo %i

Further 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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy