CodeGym /Courses /Docker SELF /Navigation through the file system: commands ls, cd, pwd

Navigation through the file system: commands ls, cd, pwd

Docker SELF
Level 1 , Lesson 5
Available

Navigation through the file system: commands ls, cd, pwd

1. Linux File System Structure

Linux is a text-based operating system, and everything in it is represented as a file. Yep, even devices like your hard drive, webcam, or network are basically a type of file. So, knowing how to move around the Linux file system? That's skill number one. You can't do much without it. It's like in real life: you wouldn't wander blindly around your house when you know where the kitchen, bedroom, or sock drawer is. Same deal here.

In this lecture, we'll break down three main commands that'll let you navigate through the Linux file system: ls, cd, pwd.

But before diving into these commands, let's figure out what the Linux file system actually looks like. It's got a tree-like structure, so all files and directories are arranged in a hierarchy. The very top of this "pyramid" is called the root directory and is represented by /.

Main Directories:

Directory Description
/ The root directory. Everything starts here.
/home User home directories. For example, /home/your_name.
/etc System configuration files. Don't mess with it unless you have to.
/var Dynamic data such as logs and temporary files.
/tmp Temporary files, automatically deleted by the system regularly.
/dev Devices connected to the system (like a hard drive or USB).
/mnt Mount points for external devices (USB drives, network drives, etc.).

Absolute and Relative Paths

  • Absolute Path starts from the root /. Example: /home/student/documents.
  • Relative Path starts from the current directory. Example: documents/file.txt.

Now that you understand what the Linux file system is, let's move on to the commands themselves.


2. The pwd Command: Where am I now?

pwd stands for "print working directory". It's the command that answers the big philosophical question: "Where am I even at?".

Usage Example

$ pwd
/home/student

The command output shows that we're currently in the home directory of the user student.

Why do I need this?

If you get lost in the directory hierarchy, the pwd command is your compass. It's especially useful when you're exploring an unfamiliar file structure or working on a large project.


3. Command ls: What's even here?

ls is a command to view the contents of a directory. It lets you see the files and folders that are in the current directory. It’s like opening a closet to check out what’s inside.

Example Usage

$ ls
Documents  Downloads  Pictures  Videos

Here we see that the current directory contains the folders Documents, Downloads, Pictures, and Videos.

Useful Options

  1. -l — displays detailed information about each file:

    $ ls -l
    drwxr-xr-x 2 student student 4096 Oct 21 10:00 Documents
    -rw-r--r-- 1 student student  234 Oct 21 09:45 notes.txt
    

    Here we see the file type, permissions, owner, group, size, modification date, and file name.

  2. -a — also displays hidden files (those starting with a dot, like .bashrc):

    $ ls -a
    .  ..  .bashrc  Documents  Downloads
    
  3. -h — outputs file sizes in a human-readable format (like KB, MB):

    $ ls -lh
    -rw-r--r-- 1 student student 4.0K Oct 21 10:00 file.txt
    

Combining Options

You can combine options. For example:

$ ls -lha

4. Command cd: Traveling the File System

The cd (change directory) command lets you move from one directory to another. Think of it as your "personal portal" to navigate through the file system.

Usage Examples

  1. Moving to another folder:

    $ cd Documents
    $ pwd
    /home/student/Documents
    
  2. Returning to the home directory:

    $ cd ~
    $ pwd
    /home/student
    
  3. Moving one level up:

    $ cd ..
    $ pwd
    /home
    
  4. Going to the root directory:

    $ cd /
    $ pwd
    /
    
  5. Using an absolute path:

    $ cd /var/log
    
  6. Using a relative path:

    $ cd ../Pictures
    
  7. Returning to the previous directory:

    $ cd -
    $ pwd
    /var/log
    

Errors When Using cd

If you try to move to a folder that doesn’t exist, Linux will definitely let you know:

$ cd nonexistent_folder
bash: cd: nonexistent_folder: No such file or directory

Make sure the folder name is spelled correctly.


5. Practice

To reinforce the material, follow these steps in your terminal.

  1. Display the current directory:

    $ pwd
    
  2. Check the contents of the current directory:

    $ ls
    
  3. Navigate to the Documents folder:

    $ cd Documents
    
  4. Return to your home directory:

    $ cd ~
    
  5. Go to the root directory and see what's there:

    $ cd /
    $ ls
    
  6. Move between folders using relative and absolute paths.


6. Useful Tips

  1. Use the Tab key for auto-completing a folder or file name. For example:

    $ cd Doc<Tab>
    

    The terminal will automatically complete it to Documents, if that folder exists.

  2. If you forget where you are, you can always check the path with the pwd command.

  3. Practice all the time! Navigating the file system using the command line becomes natural only with experience.

That's it, now you know how to move around the Linux file system using the commands pwd, ls, and cd. These commands are the foundation of terminal work, and you'll need them in almost every task you'll encounter in the future.

1
Task
Docker SELF, level 1, lesson 5
Locked
Determining the current directory
Determining the current directory
1
Task
Docker SELF, level 1, lesson 5
Locked
Contents of the current directory
Contents of the current directory
1
Task
Docker SELF, level 1, lesson 5
Locked
Navigating Between Directories
Navigating Between Directories
1
Task
Docker SELF, level 1, lesson 5
Locked
Navigation using absolute and relative paths
Navigation using absolute and relative paths
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION