DEV Community

Cover image for Tackling the Terminal: Introduction to the Command Line
J Edwards
J Edwards

Posted on • Edited on

Tackling the Terminal: Introduction to the Command Line

If you're new to Linux... start here.

Beginning your journey into the exciting world of Linux can be daunting (and at times a bit overwhelming). To get a good foothold on Linux and to start on your way towards being an active contributor, it is essential you understand the basics of the operating system. To become familiar with what's under the hood in your Linux OS, it is crucial you understand how to navigate its filesystem.

A computer’s filesystem organizes the data stored on a computer so that it can be easily retrieved by a user. Files are typically represented by a tree-like structure, in which any parent directory can have any number of children. The root directory is then found at the base of the tree.

The command line allows a user to navigate the Linux filesystem and run programs or custom scripts. In Linux, the command line interface is called Bash, and the shell prompt is usually $.

This article goes over the absolute basics of traversing the Linux filesystem.

Bash Background

The Bourne-Again Shell (Bash) was developed by the Free Software Foundation (FSF) under the GNU Project. Although Bash is the default user shell on most Linux installations, it is just one of several well-known UNIX shells. Still, its wide distribution with Linux makes it an important tool to learn.

The UNIX shell allows users to interact effectively with the system through the command-line interface (CLI). A shell action can invoke an executable that causes the kernel to create a new running process. The shell can send the output of one program as the input into another and facilitate interaction with the filesystem - so users can move around and make changes to files by using the command line.

Getting around the File Directory

Print Working Directory

  • pwd - The shell command pwd displays the file path from the root directory to the current working directory.
$ pwd
/Users/Downloads
Enter fullscreen mode Exit fullscreen mode

List

  • ls - The shell command ls is used to list the contents of directories. If no arguments are given, it will list the contents of the current working directory.
$ ls Desktop
assignment.pdf
photo.png
Enter fullscreen mode Exit fullscreen mode

Options can be used to modify the behavior of shell commands. Shell command options are commonly represented by a single letter preceded by a -.

List Command Options

  • -a: lists all contents, including hidden files and directories.
  • -l: lists all contents, in long format.
  • -t: lists all contents, by the time they were last modified.
  • -r: lists and the contents of the directory in reverse sorting order.
  • -R: lists the contents of all directories below the current directory recursively.
  • -S: sorts files by sizes.
  • Additionally, multiple options can be used together.
$ ls -a
$ ls -l
$ ls -t
$ ls -alt
Enter fullscreen mode Exit fullscreen mode

Make Directory

  • mkdir - The shell command mkdir can be used to make a new directory in the filesystem according to its argument. If a file path is given, the new directory will be placed at the end. Otherwise, it will create a new directory in the current working directory with the name given.
$ mkdir new-directory
$ ls 
old-directory    new-directory
Enter fullscreen mode Exit fullscreen mode

Change Directory

  • cd - The shell command cd can be used to navigate through the filesystem of a computer. You can use:

    • Full file paths.
    • Names of the children of the current directory.
    • It's important to understand the characters that have special functionality in the Linux filesystem:
    • The dot (.) represents the current directory in the filesystem.

    • The dot-dot (..) represents one level above the current directory. It is the parent of the current directory.

    • The forward-slash (/) represents the "root" of the filesystem. Every directory/file in the Linux filesystem is nested under the root/directory.

    • The tilde (~) represents the home directory of the currently logged-in user.

    • So, you can move to the filesystem’s root (/) with this command:

$ cd /
Enter fullscreen mode Exit fullscreen mode
  • To navigate back to your home directory:
$ cd ~
Enter fullscreen mode Exit fullscreen mode

Manipulating the File Directory

Remove

  • rm - The shell command rm is used to delete files and directories. The -r flag deletes a directory and all of its files and directories.
$ rm -r outdated_files
Enter fullscreen mode Exit fullscreen mode

Move

  • mv - The shell command mv is used to move a file into a directory or to rename a file. Use mv with the source file as the first argument and the destination directory as the second argument.
$ mv index.html website/
Enter fullscreen mode Exit fullscreen mode
  • You can use the mv command to rename a file or directory
mv old_name new_name
# The above command renames the file `old_name` to `new_name`.

mv ../old_name new_name
# The above command moves the file `old_name` from one directory up to the current directory and renames it `new_name`.
Enter fullscreen mode Exit fullscreen mode

Copy

  • cp - The shell command cp is used to copy files or directories.
$ cp file1 file1_copy
Enter fullscreen mode Exit fullscreen mode

Top comments (0)