Windows command prompt MS-DOS provides the dir command in order to list files. The dir command provides different features while listing the files. Actually, the dir command does not only list files it also lists directories too. In this tutorial, we will learn how to list files in command prompt in different ways like specific extension, recursive list, etc. The dir command is a very old command provided by MS-DOS. The dir command is used to list files and folders. Without providing any option to the dir command prints all files and folders in the current directory. Only files can be listed by using the /b and /a-d options with dir command. The /b command use bare format where only file names are listed. Also /a-d option is used to not list directories where only files are listed. dir /b /a-dList Files with dir CommandAlternatively the bare formattin can be disabled without providing the /b option like below. dir /a-dList Files with dir CommandList Files Located in Different PathFiles located different than current working directory or different path can be listed. The path is provided to the dir command as parameter. In the following example we list files located in the C:\Users\ismail\Desktop . dir C:\Users\ismail\DesktopList Files in Different PathAlternatively only files can be listed like below. dir /b /a-d C:\Users\ismail\DesktopList Files in Different PathList File TypesThere are a lot of different file types. The dir command can be used to list files according to their types or extension. The extension is used to specify file type. Generally the file extension is specified by using glob operator like *.txt . In the following example we list only zip files using the *.zip . dir *.zipAlternatively we can list zip files located other directories by providing the directory path. dir C:\Users\ismail\Desktop\*.zipList All Files RecursivelyBy default, the dir lists files located in the current working directory or provided path. It does not list the child directories and child files located in these directories. The /S option can be used to list all files located in the child directories. We can specify the path to list another than the current working directory recursively. dir /S C:\Users\ismail\Desktop\We can list specific file types recursively. In the following exmaple we list all *.sys files located under the Desktop and its child directories. dir /S C:\Users\ismail\Desktop\*.sysThe DIR command is a powerful Windows Command Prompt function that lists all files and subdirectories contained in a specific directory. The DIR command also offers some switches that unlock some powerful functionality. Let’s take a look. DIR Command SwitchesYou can use the DIR command by itself (just type “dir” at the Command Prompt) to list the files and folders in the current directory. To extend that functionality, you need to use the various switches, or options, associated with the command. Display Based on File AttributesYou can add “/A” followed by a letter code after the DIR command to display files with a specific attribute. These letter codes include:
So, for example, to display just the directories in the current path, you’d type the following command and then hit Enter: dir /adYou can combine those codes, too. For example, if you wanted to show only system files that are also hidden, you could use the following command: dir /ashYou also can add a “-” (minus) in front of any of those letter codes to specify that the DIR command does not show that kind of file. So, for example, if you don’t want to see any directories in the results, you could use this command: dir /a-dOne more tip: Instead of cramming the main switch and the letter code together the way we did in our examples, you can use a colon to separate the switch from its optional codes. Like this: dir /a:dIt can make things a little easier to parse, but it’s entirely optional. Display Stripped ResultsUsing the /b switch with the DIR command strips away all excess information, displaying only the name of the folders and files in the current directory and not attributes like file size and time stamps. Type the following command to make it work: dir /bDisplay Using Thousands SeparatorIn modern versions of Windows, the Command Prompt shows large numbers separated by commas (so: 25,000 instead of 25000). This wasn’t always the case. In older versions, you had to use the /c switch to show those commas. Why bother including it here if it’s already the default? Because if for whatever reason you don’t want to show those commas, you can use this switch along with the “-” minus sign: dir /-cDisplay Results in ColumnsYou can use the /D switch to display results in two columns instead of one. When you display results this way, the Command Prompt does not show extra file information (file size and so on)—just the names of the files and directories. dir /DDisplay Results in LowercaseThe /L switch displays all names of files and folders as lowercase. dir /LDisplay Filename Results on the Far RightBy default, the Command Prompt displays the names of files to the far right. The /N switch used to be used to achieve this effect. Now, you can use it along with a “-” (minus) to have filenames displayed on the far left instead. dir /-NDisplay Results in Sorted OrderYou can use the /O switch followed by a letter code to display directory results sorted in various ways. Those letter codes include:
So, for example, you could use the following command to sort results by time and date, with older entries appearing first: dir /ODYou can also add “-” (minus) before any of the above options to reverse the order. So, for example, if you want to sort files by time and date with newer entries appearing first, you could use this command: dir /O-DDisplay Results One Page at a TimeSome directories have hundreds or thousands of files. You can use the /P switch to have the Command Prompt pause the results after it displays each screen. You have to press a key to continue viewing the next page of results. Display MetadataUsing the /Q switch on the DIR command displays metadata tied to files and directories, along with ownership details. dir /QDisplay Alternate Data Streams (ADS)The /R switch displays any alternate data streams (ADS) that files might contain. ADS are a feature of the NTFS file system that let files contain additional metadata for locating files by author and title. dir /RDisplay All Files and Folders and Everything InsideYou can use the /S switch to recursively show all files and folders inside the current directory. This means all files and folders in every subdirectory, all files and folders in those subdirectories, and so on. Be prepared for a lot of results. dir /SDisplay Results Sorted by TimeUsing the /T switch along with a letter code lets you sort results by the different time stamps associated with files and folders. These letter codes include:
So, for example, to sort results by the time items were created, you could use the following command: dir /TCDisplay Results in Wide FormatThe /W switch is similar to /D (which shows columns), but instead, it sorts the results in wide format horizontally. dir /WDisplay Short Name FilenamesThe /X switch shows a file’s short name when the long name doesn’t comply with 8.3 naming rules. dir /XDisplay Help Pages For DIRUsing the /? switch displays helpful information regarding the DIR command, including a brief description of all the switches we’ve talked about. DIR Command ExamplesAll right, now you know about the switches and options associated with the DIR command. Let’s take a look at a few real-world examples to gain a better understanding as to how you can start putting them to use. A simple dir command returns a list of all files and folders in the current directory you’re in. Running the following command shows all system files inside your current path by utilizing the “s” attribute: But what if you want to view all files of a certain type within all subsequent folders of your current path. That’s easy, just run this extremely fast and useful command: dir \*.mp3 /sYou can replace the “.mp3” part with whatever file format you’re looking for. The asterisk acts as a wildcard, saying “find anything with .mp3 file format at the end” while the “/s” recursively looks through all folders within your current path. RELATED: This Command Prompt Trick Searches Way Faster Than Windows Explorer Now, you may have noticed that returned a LOT of results. Almost too many to be able to read before they scrolled off the screen. This is where we can use the pause switch to give you a chance to read them. To do that, modify the command like this: dir \*.mp3 /s /pAnother trick the Command Prompt offers is called piping. You can use the “>” character to send the results of one command to another place or service. A good example of this is sending all your results to a text file. You can then scroll through them later or import them into other types of documents. To do that, you could use the command: dir \*.mp3 /s /b > filename.txtRELATED: How to Print or Save a Directory Listing to a File in Windows We added the /b switch in there to only output the filenames themselves, without any of the other details. The greater than symbol reroutes everything normally displayed in your results directly to the file. There are many more combinations and uses for the DIR command, but this should be a good starting point to help you understand the basics. READ NEXT
Which of the following Windows command prompt commands can be used to list all directory files and subdirectories contained in the current directory?Which of the following Windows Command Prompt commands can be used to list all directory's files and subdirectories contained in the current directory? dir.
What is the command to list all files and subdirectories in a directory?The dir command displays a list of files and subdirectories in a directory. With the /S option, it recurses subdirectories and lists their contents as well.
How do I list all directories in a directory in command prompt?Type dir /A:D. /B > FolderList. txt and press Enter to generate a top-level folder list. When the list is complete, a new, blank prompt with a flashing cursor will appear.
Which of the following command line commands are used in Windows to create a directory or subdirectory?To create a directory in MS-DOS or the Windows command line (cmd), use the md or mkdir MS-DOS command.
|