Linux is a family of free and open-source operating systems based on the Linux kernel. Operating systems based on Linux are known as Linux distributions or distros. Examples include Debian, Ubuntu, Fedora, CentOS, Gentoo, Arch Linux, and many others1.
Linux is widely used in various contexts, such as web servers, cloud computing, smartphones, embedded devices, supercomputers, and more. Linux is known for its versatility, stability, security, and customizability
The Terminal and the Shell
The terminal is an input and output environment that presents a text-only window running a shell. A shell is a program that exposes the computer’s operating system to a user or program. In Linux systems, the shell presented in a terminal is a command line interpreter
To use the terminal, you need to type commands and press Enter. The commands are usually composed of a command name followed by some options and arguments. For example, the command ls -l /home
lists the files and directories in the /home
directory in a long format.
The terminal is interactive: you specify commands to run and the terminal outputs the results of those commands. You can also use the up and down arrow keys to navigate through your command history and edit them.
There are many different shells available for Linux, such as bash, zsh, ksh, tcsh, and more. Each shell has its own features and syntax, but they all share some common commands and conventions. In this post, we will use the bash shell, which is the default shell in most Linux distributions.
Basic Commands for File and Directory Operations
One of the most common tasks that you will perform in the terminal is working with files and directories. Linux has a hierarchical filesystem, which means that everything is organized in a tree-like structure. The root of the tree is denoted by /
, and every other file or directory is a descendant of the root.
Some of the basic commands for file and directory operations are:
ls
: list the contents of a directorycd
: change the current working directorypwd
: print the current working directorymkdir
: create a new directoryrmdir
: remove an empty directorycp
: copy a file or a directorymv
: move or rename a file or a directoryrm
: remove a file or a directorytouch
: create an empty file or update the timestamp of a fileln
: create a link to a file or a directory
These commands can take various options and arguments to modify their behavior. For example, ls -a
lists all files and directories, including the hidden ones that start with a dot. cp -r
copies a directory and its contents recursively. rm -f
removes a file without prompting for confirmation.
You can use the man
command to get more information about any command. For example, man ls
shows the manual page for the ls
command, which explains its usage, options, and examples.
# List the files and directories in the current working directory
$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos
# Change the current working directory to the Downloads directory
$ cd Downloads
# Print the current working directory
$ pwd
/home/user/Downloads
# Create a new directory called test
$ mkdir test
# List the files and directories in the Downloads directory, including the hidden ones
$ ls -a
. .. test .hidden_file
# Copy the .hidden_file to the test directory
$ cp .hidden_file test
# List the contents of the test directory
$ ls test
.hidden_file
# Move the test directory to the Desktop directory
$ mv test ~/Desktop
# Remove the .hidden_file from the Downloads directory
$ rm .hidden_file
# Create an empty file called new_file
$ touch new_file
# Create a symbolic link to the new_file called link_file
$ ln -s new_file link_file
# List the files and directories in the Downloads directory, showing the file types
$ ls -F
link_file@ new_file
Input and Output Redirection and Pipes
Linux commands can take input from different sources and send output to different destinations. By default, the input source is the keyboard and the output destination is the terminal. However, you can redirect the input and output to files or other commands using some special symbols.
Some of the symbols for input and output redirection are:
>
: redirect the output of a command to a file, overwriting the file if it exists>>
: redirect the output of a command to a file, appending to the file if it exists<
: redirect the input of a command from a file|
: redirect the output of one command to the input of another command
Here are some examples of using these symbols:
# Write the current date and time to a file called date.txt
$ date > date.txt
# Append the current username to the date.txt file
$ whoami >> date.txt
# Read the contents of the date.txt file
$ cat date.txt
Thu Dec 14 09:32:17 IST 2023
user
# Count the number of words in the date.txt file
$ wc -w < date.txt
2
# List the files and directories in the current working directory and sort them alphabetically
$ ls | sort
link_file
new_file
Some Useful Commands for Text Processing and System Information
Linux has a rich set of commands for text processing and system information. These commands can be used to manipulate, filter, search, and analyze text files or the output of other commands. Some of these commands are:
cat
: concatenate and print filesecho
: display a line of textgrep
: search for a pattern in a file or inputsed
: perform text transformationsawk
: process and generate textcut
: extract fields from a line of textsort
: sort lines of textuniq
: remove duplicate lines of textps
: report information about processestop
: display dynamic information about processesfree
: display memory usagedf
: display disk space usagedu
: display disk usage by files and directoriesping
: test the network connectivity to a hostcurl
: transfer data from or to a serverssh
: connect to a remote server securelyscp
: copy files securely between hosts
These commands can also take various options and arguments to modify their behavior. You can use the man
command to get more information about any command.
Here are some examples of using these commands:
# Display the contents of the date.txt file
$ cat date.txt
Thu Dec 14 09:32:17 IST 2023
user
# Display the message "Hello, world!"
$ echo "Hello, world!"
Hello, world!
# Search for the word "user" in the date.txt file
$ grep user date.txt
user
# Replace the word "user" with "admin" in the date.txt file and save the output to a new file called new_date.txt
$ sed 's/user/admin/' date.txt > new_date.txt
# Display the contents of the new_date.txt file
$ cat new_date.txt
Thu Dec 14 09:32:17 IST 2023
admin
# Print the second field of each line in the new_date.txt file, using space as the field separator
$ awk '{print $2}' new_date.txt
Dec
2023
# Print the first three characters of each line in the new_date.txt file
$ cut -c 1-3 new_date.txt
Thu
adm
# Sort the lines of the new_date.txt file in reverse order
$ sort -r new_date.txt
admin
Thu Dec 14 09:32:17 IST 2023
# Remove the duplicate lines from the new_date.txt file
$ uniq new_date.txt
Thu Dec 14 09:32:17 IST 2023
admin
# Report information about the current processes
$ ps
PID TTY TIME CMD
123 pts/0 00:00:00 bash
456 pts/0 00:00:00 ps
Keep exploring, and soon you'll be navigating the Linux terminal like a pro!
Comments
Post a Comment