Navigating Linux with ‘pwd’, ‘cd’, and ‘ls’: A Beginner’s Guide

code
linux
Author

Steven P. Sanderson II, MPH

Published

September 6, 2024

Keywords

Programming, Linux, cd, ls, pwd, Linux Navigation, Linux commands, pwd command, cd command, ls command, Linux filesystem, Linux directory structure, Basic Linux commands, Linux terminal, Command line interface, CLI

Introduction

I have mentioned in my previous linux post that I am on my own personal journey to learn it. I have been using it for sometime but not really understanding the commands. So I have started this blog post series on Linux for Friday’s. This is the second post in the series. So thanks for joining!

Understanding the pwd Command

The pwd (print working directory) command is used to display the full path of the current directory you are in. This is particularly useful when you are navigating deep into the directory structure and need to confirm your location.

$ pwd
/home/user_name

Using the cd Command

The cd (change directory) command allows you to move between directories. By default, using cd without any arguments will return you to your home directory. You can navigate to a specific directory by providing its path:

  • Absolute Path: Starts from the root directory.

    $ cd /usr/bin
  • Relative Path: Based on your current directory.

    $ cd ../Documents

You can also use shortcuts like ~ to quickly navigate to your home directory or .. to move up one directory level.

Shortcuts

Here are some helpful shortcuts courtesy of “The Linux Command Line” page 11 by William Shotts:

cd Shortcuts
Shortcut Result
cd Changes the working directory to your home directory
cd - Changes the working directory to the previous working directory
cd ~user_name Changes the working directory to the home directory of user_name.

Exploring with the ls Command

The ls (list) command is used to display the contents of a directory. By default, it lists the files and directories in the current directory.

$ ls
Documents  Downloads  Music  Pictures

Now with a little more information:

terminal@terminal-temple ~ $ ls -lh
total 5
drwxr-xr-x  5 terminal  staff  160 Mar 19 01:54 PM Documents
drwxr-xr-x  3 terminal  staff   96 Mar 19 01:54 PM Downloads
drwxr-xr-x  2 terminal  staff   64 Mar 19 01:54 PM Music
drwxr-xr-x  3 terminal  staff   96 Aug 23 07:16 AM my_new_directory
drwxr-xr-x  2 terminal  staff   64 Mar 19 01:54 PM Pictures

You can use various options with ls to modify its output, such as -a to include hidden files or -l for a detailed list.

Practical Examples

Here are some practical examples to illustrate the use of these commands:

  • Navigate to a Directory and List its Contents:

    $ cd /var/log
    $ pwd
    /var/log
    $ ls
    syslog  kern.log  auth.log
  • Return to Home Directory:

    $ cd ~
    $ pwd
    /home/user_name
  • Move Up One Directory Level:

    $ cd ..
    $ pwd
    /home

Important Facts on Filenames

  • Filenames that begin with a period (.) are hidden files. This means ls will not display them unless you use the -a option.
  • Filenames are case-sensitive. For example, file.txt and File.txt are considered two different files.
  • Filenames can contain spaces and punctuation, but it is generally recommended to avoid spaces in filenames to prevent issues with scripts and commands.
  • Linux has no concept of a file extension like Windows. The file type is determined by the content of the file, not the extension. However, it is common practice to use extensions for certain file types (e.g., .txt for text files) as many applications do use the extension to determine the file type.

Conclusion

Mastering the pwd, cd, and ls commands is essential for efficient navigation and management of the Linux filesystem. These commands provide the foundation for more advanced file system operations.


Happy Navigating!

pwd cd and ls