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
- Keyboard Shortcut: Press
.Ctrl + Alt + T
- Activities Menu: Click on “Activities” in the top-left corner, type “Terminal” in the search bar, and press Enter.
- Applications Menu: Click on “Show Applications” in the bottom-left corner, find “Terminal” under “Utilities”, and click to open.
2. KDE Plasma
- Keyboard Shortcut: Press
.Ctrl + Alt + T
- Application Launcher: Click on the KDE logo in the bottom-left corner (or press Alt + Space), type “Terminal” and press Enter.
3. XFCE
- Keyboard Shortcut: Press
.Ctrl + Alt + T
- Application Menu: Click on the “Applications Menu” in the bottom-left corner, go to “System” and click on “Terminal”.
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.ls
Usage:
: Lists files and directories in the current directory.ls
: Lists files and directories in the specified directory.ls <directory>
Useful Flags:
: Provides a detailed listing that includes permissions, owner, group, size, and modification time of each file or directory.-l
: Shows all files, including hidden files (those starting with-a
)..
: Displays file sizes in a human-readable format, such as 1K, 2M, or 3G.-h
: Sorts by modification time, showing the newest files first.-t
$ 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
cat
displays the contents of files. It’s a simple way to view the text inside a file right in your terminal window.
Usage:
: Displays the contents of the specified file.cat <file>
: Displays the contents of multiple files in the order given.cat <file1> <file2> ...
Useful Flags:
: Numbers all output lines, which can be helpful when looking at long files.-n
: Shows a-E
at the end of each line, making line breaks clear.$
3. cd
(Change Directory)
cd
cd
changes the current working directory. This command is essential for navigating through different folders in your system.
Usage:
: Changes to the specified directory.cd <directory>
: Quickly navigates to the home directory without any arguments.cd
$ cd
$ pwd
/home/meytili
4. pwd
(Print Working Directory)
pwd
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.mkdir
Usage:
: Creates a new directory with the given name in the current working directory.mkdir <directory>
Useful Flags:
: Creates parent directories as needed. For example,-p
will create the “Projects” directory and its subdirectories if they don’t already exist.mkdir -p Projects/Project1/Subfolder
: Sets the permission mode for new directories. For instance, mode-m <mode>
grants read and execute access to everyone, but write access only to the directory owner.755
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
it will remove all files and directories inside the current directory. Be careful running this command.rm -rf ./*
Usage:
: Removes the specified file.rm <file>
Useful Flags:
: Permits recursive removal of directories and their contents when the File parameter is a directory.-r
: Does not prompt before removing a write-protected file.-f
: Displays a message after each file getting removed.-e
$ 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
cp
copies files and directories. This command is handy for duplicating files or backing up important data.
Usage:
: Copies a file or directory from the source to the destination.cp <source> <destination>
: Copies multiple files into the specified directory.cp <file1> <file2> ... <directory>
Useful Flags:
: Copies directories recursively.-r
: Prompts for confirmation before overwriting an existing file.-i
: Copies only when the source file is newer than the destination file or when the destination file is missing.-u
: Displays detailed information about the files being copied.-v
$ ls
errors.txt file.txt
$ cp file.txt file2.txt
$ ls
errors.txt file2.txt file.txt
8. mv
(Move)
mv
mv
is used to move or rename files and directories.
Usage:
: Moves a file or directory from the source to the destination.mv <source> <destination>
: Renames a file or directory.mv <oldname> <newname>
Useful Flags:
: Prompts for confirmation before overwriting an existing file.-i
: Moves only when the source file is newer than the destination file or when the destination file is missing.-u
: Displays detailed information about the files being moved.-v
$ 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
(Standard Input): This is the input stream where data is fed into a command. By default, it’s your keyboard.STDIN
(Standard Output): This is the output stream where a command writes its regular output. By default, it’s your terminal screen.STDOUT
(Standard Error): This is the stream where error messages are written. By default, it’s also displayed on the terminal, but it can be redirected separately fromSTDERR
STDOUT
.
You can redirect these streams using the
and >
operators. For example, 2>
will redirect the output of command > output.txt
to command
, while output.txt
will redirect error messages to command 2> errors.txt
.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, |
“file” will list the contents of the current directory (ls | grep
) and filter the output to only show lines containing “file” (ls
).grep
You can also pipe the output of a command to another command for further processing. For example,
pipes the content of “file.txt” to the cat file.txt | wc -l
(word count) command, which counts the number of lines (wc
).-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!