Paths, Directories, and File Discovery
Learn how paths relate to the current directory, navigate and inspect folders, and search for files using commands appropriate to your operating system.
Understand Paths and Navigate
Understand Paths and Navigate
A names a file or directory by describing where it is in the directory tree. The key question is whether the starts from a root or from wherever the shell currently is.
An absolute starts at the filesystem root. For example, /home/lee/notes.txt starts at / on a Unix-like system. On Windows, C:\\Users\\Lee\\notes.txt starts at the C: drive. A relative starts from the . If that directory is /home/lee, then notes.txt refers to /home/lee/notes.txt.
Two components help express location without spelling out a full : . means the current directory, and .. means its parent. Thus, if the current directory is /home/lee/projects, ../notes.txt points to /home/lee/notes.txt. On Unix-like systems, / names the filesystem root.
Use pwd on Unix-like systems to print the present working directory. For example, if pwd prints /home/lee/projects, then report.txt refers to /home/lee/projects/report.txt. This is why knowing your current location matters: the same relative can refer to a different place from a different directory.
Use cd to change the shell’s . For example, cd /home/lee/projects moves to that absolute , while cd .. moves up to the parent. In many shells, cd with no argument returns to the home directory, but that behavior depends on the shell. In Windows Command Prompt, cd with no displays the current drive and directory; pwd is not a standard Command Prompt command. To change both the directory and active drive there, use a command such as cd /d D:\\Work.
If a contains spaces, quote it when the command interpreter would otherwise treat the spaces as separators: cd "Project Files". Windows paths commonly use drive letters and backslashes, whereas Unix-like paths use / as the root.
List and Interpret Directory Contents
List and Interpret Directory Contents
On Unix-like systems, ls lists directory contents. With no , it lists the current directory; with a , it lists that location without first moving there. For example, ls /home/lee/projects lists that directory. Add -a to include hidden entries (commonly names beginning with .), or -l to request a detailed, long-format listing.
A long-format listing commonly gives entry type and permissions first, followed by details such as link count, owner, group, size, modification time, and name. For example:
-rw-r--r-- 1 lee staff 842 May 12 09:30 report.txt
drwxr-xr-x 2 lee staff 4096 May 12 09:25 drafts
In these example rows, a leading - indicates a regular file and d indicates a directory. The remaining fields provide further information about each entry. Exact fields and formatting can vary with the system and options, so treat this as a common pattern rather than a universal layout.
dir is the usual listing command in Windows Command Prompt; with no , it lists the current directory. GNU systems also provide a dir command. Options differ by environment. In Windows Command Prompt, /s includes subdirectories and /b suppresses extra listing details.
A listing shows directory entries, but does not prove that you can open them. If the output is empty or the command reports an error, check the and spelling, confirm the current directory, and consider whether access permissions may be affecting the result.
Search for Files Across a Directory Tree
Search for Files Across a Directory Tree
On Unix-like systems, find searches from one or more starting paths through their subdirectories. Its general form is find STARTING_PATH TESTS. For example:
find . -type f -name '*.txt'
Here, . makes the current directory the starting point. The test -type f selects regular files, and -name '*.txt' matches names ending in .txt. The quotes matter: they keep the shell from expanding * before find receives the pattern. To search a specific location instead, replace . with that , such as /home/lee. GNU find uses the current directory if no starting is supplied.
In Windows Command Prompt, use dir /s /b pattern for a recursive filename listing. For example, dir /s /b *.txt searches below the current directory and prints matching paths in a bare format. Do not assume that Command Prompt’s find works like Unix-like find: in Command Prompt, find searches for text within files rather than traversing directories.
A practical Unix-like sequence for locating text files under a projects directory is:
pwd
ls
find ~/projects -type f -name '*.txt'
First, pwd tells you where relative paths would be interpreted. Then ls gives a quick view of the current directory. Finally, find searches from the chosen location; its output shows the paths where matches were found. To inspect a match, either use cd to move to its containing directory or pass the matched to a suitable command.
: A command that displays entries in a directory.
: A command that searches from one or more starting paths through their subdirectories, applying specified tests.