We ❤️ Open Source
A community education resource
How to navigate the command line
Use these two commands to navigate Linux or FreeDOS on the command line.
The command line can be a very powerful environment to run commands. But because it’s all text, the command line can also be a bit intimidating to new users. If you aren’t familiar with the command line, you really only need to know two commands to get around.
Navigating Linux with cd and ls
When you start a terminal session and see that friendly $ prompt, the first thing you can do is list the files in the current directory using the ls command. The term “directory” is really the same thing as “folder,” but it’s the older way to describe it because of some technical history.
Here’s what I see when I type ls from my home directory:
$ ls
bin Documents games Music Public Templates virtualmachines
Desktop Downloads lib Pictures src Videos work
Most Linux distributions will set up your home directory with folders like Desktop for files that live on the desktop, Documents to store your work, and so on for other folders. I’ve added new folders on my system to do other things, which is why you’re seeing a bin directory for programs I wrote for myself and games for games that I like to play.
Let’s navigate into a directory with the cd command. I use the src directory for my programming projects, so let’s see what I keep in there:
$ cd src
$ ls
assembly cpy fill lights odt readfile translation
assoc curses fortran macros options rh type
catalogs def-demo getline mad progress runoff unhtml
c-example demo1 ifdef map pvpgem1 shuffle untab
conio drop lanpar numlines read smore
There’s a lot of stuff here. If you were looking at this list on a terminal, you’d likely see colors to help you see what’s what. For example, the ls command usually displays directories in blue, executable programs in green, images in a light purple, and so on. Without color, it can be difficult to see if these directory entries might be files or folders, so I’ll add the -F option so ls will add a slash after folders:
$ ls -F
assembly/ cpy/ fill/ lights/ odt/ readfile/ translation/
assoc/ curses/ fortran/ macros/ options/ rh/ type/
catalogs/ def-demo/ getline/ mad/ progress/ runoff/ unhtml/
c-example/ demo1/ ifdef/ map/ pvpgem1/ shuffle/ untab/
conio/ drop/ lanpar/ numlines/ read/ smore/
So we can see that all of these are just other folders; the technical term you might see in some documentation is subdirectories. Let’s go into one of these subdirectories to see what’s there. I’ll pick the unhtml directory, where I have a small program that removes HTML code from web pages:
$ cd unhtml
$ ls
unhtmlify unhtmlify.c
The first ls command shows two files: unhtmlify.c that contains the source code for the program, and unhtmlify which is the version that I can run.
To go back to the previous directory, just use .. which means “the parent directory” or the directory that’s “above” this one. If you feel lost, type pwd to print the working directory (that’s what pwd stands for).
$ pwd
/home/jhall/src/unhtml
$ cd ..
$ pwd
/home/jhall/src
$ cd ..
$ pwd
/home/jhall
Navigating FreeDOS with cd and dir
Getting around on FreeDOS is much the same as navigating directories on Linux. You still use the cd command to change to a new directory, and .. also means “the parent directory.” But FreeDOS uses dir to display the contents of a directory instead of the ls command on Linux.
FreeDOS also makes it easy to see where you are by printing the working directory as part of the prompt. For example, when you see C:\>, that means you are on the C: drive (the first hard drive) in the “root” (\) directory. The > is the prompt where you type a command.
C:\>dir
Volume in drive C is FREEDOS2024
Volume Serial Number is 313E-17F3
Directory of C:\
APPS <DIR> 05-12-2024 4:24p
DEVEL <DIR> 05-12-2024 4:17p
FREEDOS <DIR> 05-12-2024 4:11p
GAMES <DIR> 05-30-2024 1:48p
GW <DIR> 05-14-2024 2:15p
TEMP <DIR> 05-27-2024 5:05p
COMMAND COM 85,480 07-10-2021 7:28p
FDAUTO BAT 2,478 05-12-2024 4:43p
FDCONFIG SYS 947 05-12-2024 4:12p
KERNEL SYS 46,256 05-01-2024 8:58a
4 file(s) 135,161 bytes
6 dir(s) 298,115,072 bytes free
The dir command also prints a lot of extra information like the free space on the drive and the size of each file. If you don’t want to see that, or just want to make the output look more like ls on Linux, add the /w (“wide”) and /b (“bare”) options:
C:\>dir /w /b
[APPS] [DEVEL] [FREEDOS] [GAMES] [GW]
[TEMP] COMMAND.COM FDAUTO.BAT FDCONFIG.SYS KERNEL.SYS
FreeDOS, like any DOS, is actually case insensitive. That means you don’t need to type everything in uppercase. But here, the output is in all-uppercase. To display this in lowercase, add the /l (“lowercase”) option:
C:\>dir /w /b /l
[apps] [devel] [freedos] [games] [gw]
[temp] command.com fdauto.bat fdconfig.sys kernel.sys
If you don’t want to type those options all the time, you can set an environment variable called DIRCMD to keep any dir options you want to use. I’ve already set my DIRCMD to /O:GNE (“order the listing by: group directories first, then sort by name and extension”) and /Y (“4-digit years”). But to make this look more like Linux ls output, let’s update my DIRCMD with the extra options:
C:\>set DIRCMD=/o:gne /y /w /b /l
C:\>dir
[apps] [devel] [freedos] [games] [gw]
[temp] command.com fdauto.bat fdconfig.sys kernel.sys
Note that the command line options to dir are also case insensitive; you can use either uppercase or lowercase for the options.
Let’s go into the games directory and see what’s there. Just like on Linux, we can use the cd command to change the directory. Once in the new directory, we can use dir to list its contents.
C:\>cd games
C:\GAMES>dir
[.] [..] [senet]
This shows that there’s just one subdirectory in games: the Simple Senet game is in the senet folder:
C:\GAMES>cd senet
C:\GAMES\SENET>dir
[.] [..] senet.exe
And having navigated there, we can “back up” to the previous directories, making our way back to the “root” directory, using .. to mean the parent directory:
C:\GAMES\SENET>cd ..
C:\GAMES>cd ..
C:\>
Navigating made easy
Armed with just these two basic commands (cd and ls on Linux, cd and dir on FreeDOS) you can navigate your way around to find files and directories. Try it on your system to locate your files using the command line.
The opinions expressed on this website are those of each author, not of the author's employer or All Things Open/We Love Open Source.