Finding the Current Directory
Using the pwd
Command
The pwd
(Print Working Directory) command is the primary way to find the current working directory in the Linux file system. When executed, it will print the absolute path of the current directory.
## Print the current working directory
pwd
/home/user
Understanding the Current Directory Representation
The current directory is represented by the .
(dot) symbol. This is a special directory that refers to the directory you are currently in.
## List the contents of the current directory
ls .
file1.txt file2.txt subdirectory/
Relative Paths and the Current Directory
When using relative paths, the current directory is the starting point. For example, if you are in the /home/user
directory and you run cd documents
, you will change to the /home/user/documents
directory.
## Change to the documents subdirectory (relative path)
cd documents
Determining the Current Directory in Scripts
You can use the pwd
command within shell scripts to determine the current working directory programmatically.
#!/bin/bash
## Get the current working directory
current_dir=$(pwd)
echo "Current directory: $current_dir"
This can be useful when writing scripts that need to perform operations relative to the current location in the file system.