DEV Community

Cover image for Linux Cheatsheet - Basic Linux Commands: Part 1 (ls, pwd, cd)
Tiffanie BOREUX
Tiffanie BOREUX

Posted on • Originally published at blog.boreux.dev

Linux Cheatsheet - Basic Linux Commands: Part 1 (ls, pwd, cd)

Cover photo by Venti Views on Unsplash.

Table of Contents


1. List Files and Directories

The ls command is one of the most frequently used commands in Linux. It is used to list the content of a directory.

1.1. Short listing

Running the ls command lists the content of the current directory without any information except the name of the sub-directories and files inside the current directory.

~ $ ls

# ↪️ Output:
# doc.odt           image.png           picture.jpg         pres.odp
# table.ods         text.pdf            web.html
Enter fullscreen mode Exit fullscreen mode

1.2. Long listing

Running the ls command with the -l option lists the content of the current directory with informations about :

  • Permissions (we will discuss permissions in a next article)
  • Size
  • Owner
  • Modification date and time
  • Name
~ $ ls -l

# ↪️ Output:
# -rw-r--r-- 24k tiffanie 22 avr 21:46 doc.odt
# -rw-r--r-- 36k tiffanie 23 avr 22:46 image.png
# -rw-r--r-- 48k tiffanie 18 avr 22:00 picture.jpg
# -rw-r--r-- 72k tiffanie 20 avr 20:45 pres.odp
# -rw-r--r-- 99k tiffanie 21 avr 20:46 table.ods
# -rw-r--r-- 34k tiffanie 19 avr 22:12 text.pdf
# -rw-r--r-- 12k tiffanie 17 avr 19:09 web.html
Enter fullscreen mode Exit fullscreen mode

1.3. Listing all the files, including the hidden ones

Running the ls command with the -a option lists the content of the current directory including hidden files. In Linux, an hidden file is a file with a name starting with a point (.).

~ $ ls -la

# ↪️ Output:
# -rw-r--r-- 24k tiffanie 22 avr 21:46 .bashrc      <--- Notice this new file
# -rw-r--r-- 24k tiffanie 22 avr 21:46 doc.odt
# -rw-r--r-- 36k tiffanie 23 avr 22:46 image.png
# -rw-r--r-- 48k tiffanie 18 avr 22:00 picture.jpg
# -rw-r--r-- 72k tiffanie 20 avr 20:45 pres.odp
# -rw-r--r-- 99k tiffanie 21 avr 20:46 table.ods
# -rw-r--r-- 34k tiffanie 19 avr 22:12 text.pdf
# -rw-r--r-- 12k tiffanie 17 avr 19:09 web.html
Enter fullscreen mode Exit fullscreen mode

1.4. Some other options

  • ls -lh: List files with Human readable format.
  • ls -F: List files with an / at the end of a directory name.
  • ls -r: List files in Reverse order (non-alphabetical order).
  • ls -R: List files Recursively, that means that the command lists the files in the directory and so on.
  • ls -ltr: List files non-chronologicaly by modification date.
  • ls -lS: List the bigger files first.

1.5. List Directory Informations

To obtain some informations on a directory and not list its files, you can use the combinaison of the -l option with the -d one.

~ $ ls -ld

# ↪️ Output:
# drwxr-xr-x - tiffanie 24 avr 00:11 /home/tiffanie/
Enter fullscreen mode Exit fullscreen mode

2. Find Out Present Working Directory

The pwd command stands for Print Working Directory. It is used to display the absolute path of the current working directory.

When you open a terminal in Linux, you are placed in a default directory called the home directory, which is represented by the tilde (~) character. You can navigate to other directories using the cd command (see the next section). When you change directories, the pwd command will display the absolute path of the current working directory.

For example, if you are in the home directory and navigate to the Documents directory using the command cd Documents, the pwd command will display the absolute path of the Documents directory.

~ $ cd Documents/
Documents/ $ pwd
# ↪️ Output:
# /home/tiffanie/Documents/
Enter fullscreen mode Exit fullscreen mode

3. Switch Between Directories

The cd command is also one of the most frequently used commands in Linux. It is used to change directory for the working space.

~ $ cd /home/tiffanie/Documents/
~/Documents $
Enter fullscreen mode Exit fullscreen mode

Note: an important path is the ~ which moves you to the user's home directory from anywhere.

~/Documents/Code/ $ cd ~
~ $
Enter fullscreen mode Exit fullscreen mode

3.1. A note about . and ..

The dot (.) represents the current directory. It is commonly used in file paths to specify the current working directory.

For example, if you are currently working in the directory /home/tiffanie/Documents, you can refer to a file called text.pdf in the current directory using the relative path ./text.pdf. The dot (.) before the forward slash (/) indicates that you want to refer to a file in the current directory.

The double dot (..) represents the parent directory of the current directory. It is used in file paths to specify the path to a file or directory relative to the parent directory.

If you want to navigate to the parent directory from the current directory, you can use the command cd .. This command will take you up one level in the directory hierarchy to the parent directory.

3.2. A note about absolute and relative path

An absolute path is a path that starts from the root directory of the file system, which is represented by the forward slash (/) character, remember? It specifies the complete path to a file or directory, starting from the root directory and traversing down through each subdirectory until the file or directory is found.

~ $ cd /home/tiffanie/Documents/
~/Documents $
Enter fullscreen mode Exit fullscreen mode

This path specifies the location of the directory Documents/ in the tiffanie/ directory, which is located in the home/ directory, which in turn is located in the root directory.

A relative path, on the other hand, specifies the path to a file or directory relative to the current working directory. It does not start with the forward slash (/) character and assumes that the file or directory is located within the current working directory or one of its subdirectories.

Here's an example of a relative path:

~/Documents $
~ $ cd ..
~ $
Enter fullscreen mode Exit fullscreen mode

This path specifies the location of the folder tiffanie in the home/ directory, which is located one directory above the current working directory.

See you soon! 🐧

Top comments (1)

Collapse
 
michaeltharrington profile image
Michael Tharrington

This is an awesome, well-organized, handy little cheatsheet! Nice post and great series. 🙌