A Beginner's Guide to Linux Command Line

Published on

A Beginner's Guide to Linux Command Line
A Beginner's Guide to Linux Command Line

Hello there! If you’re new to Linux and want to learn how to use the command line, you’ve come to the right place. In this guide, you’ll learn the basics of the Linux command line, also known as the Terminal. By the end, you’ll have a solid understanding of how to navigate your Linux system and perform some basic tasks. Let’s dive in!

What is the Command Line

The command line is a text-based interface that lets you interact with your operating system by typing commands. It might seem tough at first, but it’s the best way to have full control over your Linux system compared to a graphical user interface (GUI). There are plenty of ways to open the terminal, depending on your desktop environment (DE).

How to Open the Terminal

Here are some examples of how to open the terminal in popular DEs:

1. Gnome

2. KDE Plasma

3. XFCE

Basic Commands and Some Useful Flags

Here are some essential commands to get you started:

1. ls (List)

ls lists the contents of a directory. This command is useful for seeing what files and directories exist in your current location.

Usage:

Useful Flags:

$ ls -lah
total 20K
drwxr-xr-x  5 meytili meytili 4.0K Jun 22 23:36 .
drwxr-xr-x 11 meytili meytili 4.0K Jun 22 23:36 ..
drwxr-xr-x  2 meytili meytili 4.0K Jun 22 23:36 project
drwxr-xr-x  2 meytili meytili 4.0K Jun 22 23:36 project2
drwxr-xr-x  2 meytili meytili 4.0K Jun 22 23:36 project3

2. cat (Concatenate)

cat displays the contents of files. It’s a simple way to view the text inside a file right in your terminal window.

Usage:

Useful Flags:

3. cd (Change Directory)

cd changes the current working directory. This command is essential for navigating through different folders in your system.

Usage:

$ cd
$ pwd
/home/meytili

4. pwd (Print Working Directory)

pwd prints the path of the current directory. This command is useful when you need to know exactly where you are in the filesystem.

$ pwd
/home/meytili/w/directory

5. mkdir (Make Directory)

mkdir creates new directories. This command helps you organize files by placing them into separate folders.

Usage:

Useful Flags:

6. rm (Remove)

rm removes files and directories. Be cautious with this command, especially when using flags that force deletion. like when you execute rm -rf ./* it will remove all files and directories inside the current directory. Be careful running this command.

Usage:

Useful Flags:

$ mkdir project
$ mkdir project2
$ ls -lah
total 16K
drwxr-xr-x  4 meytili meytili 4.0K Jun 22 23:39 .
drwxr-xr-x 11 meytili mevtili 4.0K Jun 22 23:36 ..
drwxr-xr-x  2 meytili mevtili 4.0K Jun 22 23:39 project
drwxr-xr-x  2 meytili mevtili 4.0K Jun 22 23:39 project2
$ rm -r project
$ ls -lah
total 12K
drwxr-xr-x  3 meytili meytili 4.0K Jun 22 23:39 .
drwxr-xr-x 11 meytili meytili 4.0K Jun 22 23:36 ..
drwxr-xr-x  2 meytili meytili 4.0K Jun 22 23:39 project2

7. cp (Copy)

cp copies files and directories. This command is handy for duplicating files or backing up important data.

Usage:

Useful Flags:

$ ls
errors.txt  file.txt
$ cp file.txt file2.txt
$ ls
errors.txt  file2.txt  file.txt

8. mv (Move)

mv is used to move or rename files and directories.

Usage:

Useful Flags:

$ ls
file.txt
$ mv file.txt file_renamed.txt
$ ls
file_renamed.txt

These commands and their flags provide a solid foundation for navigating and managing files and directories in the command line. Feel free to experiment and explore their usage!

Standard Streams, STDIN, STDOUT, and STDERR

You can redirect these streams using the > and 2> operators. For example, command > output.txt will redirect the output of command to output.txt, while command 2> errors.txt will redirect error messages to errors.txt.

$ curl htps://google.com 2> errors.txt
$ cat errors.txt
curl: (1) Protocol "htps" not supported

Piping: Combining Commands with Pipes

Pipes (|) are a powerful feature that allows you to chain commands together. They take the output of one command and use it as input for the next command. For instance, ls | grep “file” will list the contents of the current directory (ls) and filter the output to only show lines containing “file” (grep).

You can also pipe the output of a command to another command for further processing. For example, cat file.txt | wc -l pipes the content of “file.txt” to the wc (word count) command, which counts the number of lines (-l).

$ cat file.txt | wc -l
58

Conclusion

Congratulations! You’ve taken your first steps into the world of the Linux command line. With practice and exploration, you’ll become proficient at navigating and managing your system using the command line. Remember to refer to online resources and continue experimenting with commands to enhance your skills. Happy Linux’ing!