Getting started with the Linux command line

Let's dive into some of the more in-depth features of the Linux CLI

by Pete
Published: Updated: 14 minutes read

Awhile ago we started with 20 Linu commands for newbies to get you started with the Linux command line and it’s set of basic commands. Now, it’s time we dive a little deeper into the inner workings of the Linux CLI, what we can do with it and some simple hacks.

If you’re new to the Linux command line, it can be overwhelming to navigate the various commands and concepts. However, with a little bit of practice and understanding of the basics, you can unlock the powerful features of the command line and automate many tasks. In this blog post, we’ll dive into some of the more in-depth features and concepts of the Linux command line to help you become a Linux guru in no time.

Let’s get into it

Here are a few concepts and commands that are essential for an in-depth understanding of the command line. We’ll tie a few articles together but in structured way to keep you focused.

1
Accessing the Command Line

You can access the Linux command line in several ways depending on your distribution and environment:

  • Desktop Environment: If you’re using a graphical desktop environment, you can usually find a terminal emulator in the applications menu or launch bar. You can also use the keyboard shortcut Ctrl + Alt + T to open a terminal window.
  • Virtual Console: If you’re logged into your Linux machine locally, you can switch to a virtual console by pressing Ctrl + Alt + F1 through Ctrl + Alt + F6. This will take you to a text-based terminal interface where you can log in and use the command line.
  • SSH: If you’re accessing a remote Linux machine, you can use a secure shell (SSH) connection to access the command line interface. You can do this by using the ssh command in another terminal window or through an SSH client like PuTTY.

2
Navigating the file system

navigating-the-filesystem-Screenshot_20230208_165957.png

Navigating the file system is a crucial aspect of working with the Linux command line. The file system is the hierarchical structure that organizes all the files and directories on your Linux system. Understanding how to navigate this structure is essential for finding and managing the files and directories you need.

The cd (change directory) command is the primary tool for navigating the file system. The cd command allows you to change the current working directory, which is the directory that you are currently in and where all commands are executed from. For example, cd / takes you to the root directory, the top level of the file system hierarchy, cd ~ takes you to your home directory, the directory with your username, and cd .. takes you up one directory level.

Another command that’s essential for navigating the file system is ls. The ls command lists the files and directories in the current directory. By default, ls displays the files and directories in a simple list format, but it has many options you can use to customize the output. For example, ls -l displays the files and directories in a long format that includes more details such as permissions, ownership, and timestamps.

Another useful command for navigating the file system is pwd, which stands for “print working directory”. The pwd command displays the current working directory, which is useful for verifying your location in the file system.

The file command is also a useful tool, it help to determine the type of a file, whether it’s a text file, an image, or a binary file. You can use it to identify the type of a file by simply typing file filename, and it will return the type of file.

In addition to these commands, there are many other ways to navigate the file system, such as using tab completion, wildcards, and shortcuts. For example, by hitting the TAB key after typing the first few letters of a command, you can autocomplete the command name and avoid typing the full command. Using wildcards like * and ? allows you to match multiple files and directories at once. And using shortcuts like . for the current directory and .. for the parent directory can make it easier to navigate the file system.

In summary, understanding how to navigate the file system is essential for working with the Linux command line. The cd, ls, pwd, file and tree commands are key tools for navigating the file system and finding the files and directories you need. As a Linux guru, you should be familiar with these commands and their options, as well as other techniques for navigating the file system efficiently.

3
File and directory manipulation

file-and-directories-Screenshot_20230208_170342.png

The mkdir command is used to create a new directory. For example, mkdir mydir will create a new directory called “mydir”. The touch command is used to create a new empty file. For example, touch myfile.txt will create a new file called “myfile.txt”.

The cp command is used to copy files and directories. For example, cp file1.txt file2.txt will create a copy of “file1.txt” named “file2.txt”. You can also use cp to copy a directory and its contents by using the -r option, which stands for “recursive.” For example, cp -r dir1 dir2 will create a copy of the “dir1” directory and all its contents, named “dir2”.

The mv command is used to move or rename files and directories. For example, mv file1.txt file2.txt will rename “file1.txt” to “file2.txt”. You can also use mv to move a file from one directory to another by specifying both the source file and the destination directory.

The rm command is used to delete files and directories. For example, rm file1.txt will delete the file “file1.txt”. When used with the -r option, it will recursively delete a directory and all its contents. For example, rm -r dir1 will delete the directory “dir1” and all its contents.

Another useful command for file and directory manipulation is the ln command, which creates a symbolic link, or alias, to a file or directory. A symbolic link is a special file that points to another file or directory and behaves as if it were the original file or directory. For example, ln -s file1.txt link1.txt creates a symbolic link named “link1.txt” that points to the file “file1.txt”.

In addition to these commands, there are many other options and techniques for file and directory manipulation, such as using wildcards, batch processing, and redirection. For example, using wildcards like * and ? allows you to manipulate multiple files and directories at once. Batch processing with commands like find and xargs can automate complex file and directory manipulation tasks. And redirection with commands like > and >> allows you to capture the output of a command and write it to a file.

4
Permissions

permissions-Screenshot_20230208_170919.png

Every file and directory in Linux has a set of permissions that specify what actions users are allowed to perform on the file or directory. These permissions are divided into three categories: read, write, and execute. The permissions can be set for the owner of the file or directory, the group that the file or directory belongs to, and everyone else.

The ls command can be used to view the permissions of files and directories. The output of the ls -l command shows the permissions in the first column, with the first character indicating whether the entry is a file or a directory. The next three characters indicate the read, write, and execute permissions for the owner of the file or directory, respectively. The next three characters indicate the read, write, and execute permissions for the group, and the last three characters indicate the read, write, and execute permissions for everyone else.

The chmod command is used to modify the permissions of files and directories. The chmod command uses a numeric mode to specify the permissions, with each category of permissions represented by a number: read = 4, write = 2, execute = 1. The numeric mode for each category is added together to determine the complete numeric mode for the file or directory. For example, to grant read and write permissions to the owner of a file, the numeric mode would be 6 (4 + 2).

In addition to using numeric modes, the chmod command can also use symbolic modes to modify permissions. A symbolic mode consists of three characters, representing the permissions for the owner, group, and everyone else, respectively. The first character is + to add a permission, – to remove a permission, or = to set a permission. The second character is either r for read, w for write, or x for execute. The third character is either u for owner, g for group, or o for everyone else. For example, chmod u+rw file1.txt would add read and write permissions to the owner of the file “file1.txt”.

The chown command is used to change the owner and/or group of a file or directory. For example, chown new_owner file1.txt changes the owner of the file “file1.txt” to “new_owner”. The chgrp command is used to change the group of a file or directory. For example, chgrp new_group file1.txt changes the group of the file “file1.txt” to “new_group”.

5
Pipes and redirects

pipes-and-redirects-Screenshot_20230208_170621.png

Pipes and redirects are two of the most powerful and versatile tools in the Linux command line. They allow you to connect commands together, making it possible to manipulate and combine the output of multiple commands in new and interesting ways. Understanding pipes and redirects is an essential part of becoming a proficient Linux user.

A pipe, represented by the | symbol, is used to connect the standard output of one command to the standard input of another command. The standard output of the first command is passed as the standard input to the second command, allowing the two commands to work together in a pipeline. For example, the command ls -l | grep txt lists the contents of the current directory in long format and pipes the output to grep, which searches for files containing the string “txt”. The result is a list of all files in the current directory with “txt” in their name.

Redirects are used to control the input and output of commands, allowing you to specify where the standard input or output should come from or go to. The most commonly used redirects are > (output redirection) and < (input redirection). The > symbol is used to redirect the standard output of a command to a file, overwriting the contents of the file if it already exists. For example, the command ls -l > file_list.txt lists the contents of the current directory in long format and redirects the output to a file called “file_list.txt”. The >> symbol can be used to append the output to an existing file, rather than overwriting its contents.

The < symbol is used to redirect the standard input of a command from a file, rather than from the keyboard. For example, the command sort < file_list.txt sorts the contents of the file “file_list.txt” and displays the sorted output on the screen.

In addition to input and output redirects, the 2> and 2>> symbols are used to redirect standard error output to a file. The 2> symbol overwrites the contents of the file, while 2>> appends the output to the file. For example, the command ls non_existent_dir 2> error_output.txt attempts to list the contents of a non-existent directory and redirects the error output to a file called “error_output.txt”.

Pipes and redirects can be combined to create more complex pipelines and automate repetitive tasks. For example, the command ls -l | grep txt | sort > sorted_txt_files.txt lists the contents of the current directory in long format, pipes the output to grep, which searches for files containing the string “txt”, and then pipes the result to sort, which sorts the output and redirects it to a file called “sorted_txt_files.txt”.

In Summary

When becoming a Linux command line expert, it’s important to have a deep understanding of pipes and redirects, two of the most powerful tools in the Linux ecosystem. These features allow you to manipulate and combine the output of multiple commands in new and innovative ways, making it possible to automate complex and repetitive tasks with ease.

A pipe, represented by the | symbol, connects the standard output of one command to the standard input of another command, allowing two commands to work together in a pipeline. This is useful for chaining multiple commands and using their combined output in new ways.

Redirects, represented by the >, <, >>, and 2> symbols, give you control over the input and output of commands, allowing you to specify where the standard input or output should come from or go to. This includes the ability to redirect output to a file, read input from a file, and redirect error output to a file.

By combining pipes and redirects, you can create complex pipelines and automate repetitive tasks, all while leveraging the full power of the Linux command line. Whether you’re just starting out with Linux or you’re a seasoned expert, mastering pipes and redirects is key to unlocking the full potential of this powerful operating system.