- what is Linux ?
- Advantages of using Linux
- Disadvantages of using Linux
- Is linux for me?
- Who uses Linux and where
- what is virtualbox
- install virtualbox in Windows
- install virtualbox in MacOS
- install ubuntu in virtualbox
- install ubuntu on a computer
Command line interface
Master the Terminal
In the world of computing, where graphical user interfaces (GUIs) have become the norm, the command line interface (CLI) continues to hold its ground as a powerful tool for performing tasks efficiently and effectively. By leveraging the command line, also known as the terminal or shell, users can tap into a realm of unparalleled control and flexibility. While GUIs provide visual representations of operations, the command line goes beyond superficial appearances, allowing users to harness the true potential of their machines.
The command line offers a direct and text-based interaction with the underlying operating system, empowering users to execute commands, manipulate files and directories, manage processes, and perform a wide range of tasks with remarkable speed and ease. By mastering the command line, individuals can unlock a multitude of possibilities for automating tasks, navigating complex systems, and working with large volumes of data more efficiently than ever before.
Unlike GUIs, which often require multiple clicks and navigation through menus, the command line offers a concise and straightforward syntax that can be executed by simply typing commands and pressing Enter. This streamlined approach not only saves time but also enables users to perform complex operations with a few simple keystrokes. Whether you are a system administrator, a developer, a data scientist, or an enthusiastic computer user, understanding the fundamentals of the command line is essential for maximizing your productivity and unleashing the full potential of your computing environment.
In this guide, we will delve into the realm of command line, equipping you with the knowledge and skills needed to harness the immense power and efficiency of the terminal. We will explore essential commands for navigating the file system, manipulating files and directories, managing processes, and configuring system settings. Along the way, we will uncover valuable tips, tricks, and shortcuts to enhance your command line prowess.
what is a shell
A shell is a command line interpreter or program that provides a user interface for interacting with an operating system. It acts as a bridge between the user and the operating system, allowing users to execute commands, run programs, and perform various tasks.
Shells provide a text-based interface where users can type commands and receive output or perform actions based on those commands. They interpret and execute the commands entered by the user, facilitating tasks such as file manipulation, process management, and system configuration.
There are different shells used in Linux, like bash, zsh, fish, ksh, and more. But don't worry, you can choose the shell you prefer for your Linux distribution. Many shells have similar syntax because they are based on bash.
what is the terminal
The terminal is a text-based interface that takes input from the user (commands) and
provides them to the shell to execute the commands.
The terminal can be different and the prompt also, so don’t be surprised if you see another prompt like only the “$” sign or terminal theme.
Which shell is the default in my linux distro ?
to know which shell is the default in your linux distro, open a terminal and run this command
basename $SHELL |
Here is a list of known linux shell:
In my linux I use zsh as my default shell since it has a good syntax highlight and auto-suggestions.
Which shell should I use ?
The shell you should use depends on your taste, choose one from the list or try them all to know the best shell for you.
Use terminal to do tasks
when you open the terminal it opens in the home directory by default which is indicated with the sign “~”.
TEXT MANIPULATIONS
Print text to terminal
To print text to terminal we use “echo”
If you want to create a new line use \n:
Clear the terminal
you can remove all the previous command output by running
clear |
FILES AND DIRECTORIES MANIPULATIONS
List files and directories
If you want to see files and directories existing in this directory you can run the command
ls |
result:
But there are hidden files and directories which start with a point, to list them in addition to normal files use the command
ls -a |
result:
as you can see there are other files which start with a point in their
name
If you want to see more information like file permissions use the command ls with the argument -l.
ls -l |
result:
As you can see there is other information about the files.
What if we want to list hidden files and their information, we can combine two argument -a and -l like this:
ls -a -l |
result:
By default the ls command list files of the working directory you can use another directory with:
ls directory_name |
Print the content of a file
Use the command cat to print the content of the file to terminal like this:
Change the working directory
Use cd to jump to another directory
the working directory changed to Desktop
Show the current working directory path
Use pwd
Create and Remove a directory
Create a directory with mkdir and remove it with
rmdir
Move, copy and rename a file
Use the command mv to move files and cp to copy them from a location to another one
The difference between cp and mv is that mv remove the file from its
old location while cp keep the file in the two locations, you can copy or move multiple file by adding their
name before the destination like this:
cp/mv file1 file2 directory3 destination |
you can also copy or move the directory.
The mv command is used also to change a filename with
mv oldfilename newfilename |
Create a file
The touch command is used to create an empty file
Find a file
find command is used to search for a file in the specified directory in active mode which mean that it open and read directory to find files, to search for a file with the exact name use
find /path -name "exactname" |
if you are not sure about the name of the file you can use the * sign, the asterisk symbol (*) is known as a wildcard or a globbing character. It is used for pattern matching and represents any number of characters (including zero characters) in a filename or a string.
find /path -name "*ctnam*" |
or
find /path -name "*ctnam*.txt" |
this will output only files that end with .txt and have the string “ctnam” in their names.
Locate is a command used to search for files in passive mode, it has a database to search in. You can use the locate command like this:
locate expression |
you can also use the asterisk symbol like in find command.
locate is faster than find since it uses a database of existing files but if the file is newly created and not indexed by locate use find to search all the system.
Dealing with large files
It’s time consuming to open and manipulate large files, but you can use the tail command to make it faster
tail -f file.txt |
tail -n 10 file |
tail -n +10 file |
Sorting a file
sorting a file mean to arrange his lines alphabetically or numerically
To sort in ascending use
sort filename.txt |
To sort in descending use
sort -r filename.txt |
Search for expression in a file
Sometimes, you have a long file and you want to search for one line or more that contain an expression, you can use the grep command to search in a file like this:
grep expression file |
example:
regex or regular expressions are like codes and patterns that can find a specific string in a text following rules. For example you can find all words that start with “a” and end with “b”.
grep -E "\bz\w*w\b" file.txt |
The \bz indicates that “z” is the first character in the word.
The \w indicate any single character, when adding * mean any number of characters.
The w\b indicates that “w is the last character in the word.
Note: when using regular expressions with grep use the -E argument.
Print a file without duplicate lines
The uniq command can print a file without printing duplicates lines multiple times you can use it like this:
uniq file.txt |
Find the difference between two files
let’s say you have two files and you can’t find the difference between them, you can use diff
diff file1.txt file2.txt |
Change the owner of a file
as you learned in chapter “user and group management” files have owner and you can change the owner of a file with chown
chown new_owner file |
if the permission is denied use sudo, sudo run a command as the root user
sudo command |
so use
sudo chown new_user file |
to change the owner of all files inside a directory use
sudo chown -R new_user directory |
Change the group of a file
as you learned in chapter “user and group management” files have group and you can change the group of a file with chgrp
sudo chgrp group file |
or with chown
sudo chown :group file |
to change the group of all files inside a directory use
sudo chgrp -R group directory |
or
sudo chown -R :group directory |
you can change the owner and group in the same command with chown:
sudo chown owner:group file |
Change the mode
as you learned in chapter file permissions, files have permissions for owner, group and others. To edit these permissions we use the command chmod permissions file
permissions are set like this u=owner_permission,g=group_permission,o=others_permission
you can set the permissions in two presentations, octal and symbolic. Symbolic provide more human readable way, “w” present write permission, “r” present read permission, and “x” present execute permission, so to set permission that give read, write, execute access to owner and read, execute to group, and read to others in symbolic presentation use
chmod u=rwx,g=rx,o=r file |
in octal presentation we use number:
so the permission read, write, execute is 4+2+1 and it’s 7
and read, execute is 4+1 which is 5
and read is 4, so you can use
chmod 754 file |
the first number is for owner, the second is for group and the third is for others
Calculate the size of a directory or more
To see the size of a directory use du directory
But it print the size in non human readable format, to print it in human readable format use the -h argument
SYSTEM
Processes in Linux are running instances of programs or tasks that are executed by the operating system. Each process has its own unique process identifier (PID) and consumes system resources, such as CPU time, memory, and input/output.
Stop running process
To stop a running process using its PID you can use
kill PID |
you can also kill all process with a specific name for example if you have multiple process with the name i3wm you can use
killall i3wm |
you must use the complete name else it won’t kill the process
Pass a command to background
When you pass a command to the background you won’t see any output and you can execute more commands in the terminal session, but the command is still running in the background.
To pass command to the background you can append “&” in the end of the
command
Show command running in background
To see all commands running in the background use the command jobs
Resume suspended job in the background
When a command is running you can suspend it by pressing Ctrl+z and it will be displayed when
running jobs
you can resume the command by using bg
[JOB_SPEC] to resume it in the background. Where JOB_SPEC can be one of the
following:
Resume a job in the foreground (terminal)
What if you want to resume the job in the foreground and not in the background, you can use fg [JOB_SPEC] instead of bg [JOB_SPEC]
Create an alias
Sometimes, you run a command with a set of arguments multiple times and you are tired of repeating this long command, so you can create an alias.
An alias is one command that runs another command with a set of argument, it’s like a shortcut for a long command.
To create an alias you can use the command alias name="cmd –arg1 –arg2". For example to create an alias for the command ls with the argument -l and -a to show more informations you can run the command:
alias lss="ls -a -l" |
This command create an alias named lss that run the command ls -a -l
To remove an alias you can use the command
unalias aliasName |
example
unalias lss |
Locate the executable file associated with a command
When you run a command you actually run an executable file associated with it except shell built-in command that may doesn’t have an executable file, sometimes they are included in the shell executable, to find the file you can use the command which command_name
To show all paths to the executable file of a command use the -a argument.
Make a command run even if you close the session
Sometimes, you need to keep a command running even if you close the session. For example when closing the terminal you close the session, or when you connected to another computer remotely and then get disconnected, the session will be closed and there are commands that you don’t want them to stop running so you can use the nohup command and you can run it also in the background with nohup command & .The output of the command will be written in the file nohup.out
Print username of the user currently logged in to terminal
Just use whoami
Display users logged in to the system
just use who
Switch to another user in terminal
Use su another_username
Change the password of a user
To change the password of a user you can use the passwd command, but you must be the root or use sudo or have the user's old password.
When running from the root or using sudo, you don’t need the user’s
password:
When running from normal users without sudo, you can only change the password of
current user and you need the old password:
Show commands history
You can use the history command to achieve this
purpose. This show commands with numbers
You can use the syntax !<command number> to repeat the command
HARDWARE AND SYSTEM
Uname
Run the uname command to print kernel name, add:
Neofetch
Neofetch is a command-line tool for displaying system and hardware informations, it’s not a part of the shell but it’s included in almost all linux distro, or you can install it from https://github.com/dylanaraps/neofetch/wiki/Installation
NOTE: almost all commands have a help option and a manual, you can access the help by adding -h or –help that depend on command developer, to access the manual on how to use the command use man command
Sometimes, you want to use the output of a command as the input for other command, and this is called piping
For example if i have a file named animal.txt and i want to print its content but it’s a large file, you can pip the output of cat to the command less like this, so it print few lines and when you enter it add some few other lines too:
cat animals.txt | less
Another example, if you are in a directory that has a lot of files and you want to search for file containing the string “dog”, you just run:
ls | grep "dog"
Sometimes, you need to save the output of a command to a file, this is called output redirection
echo dog > animals.txt #create or rewrite the file dir.txt to the string "dog"
echo dog >> animals.txt #create or append to the file dir.txt the string "dog"
You can run multiple commands separated by “;” in one time like
this:
you can run whatever number of commands you want.
Terminal shortcuts can be customized but most terminals come with default shortcuts.
Navigation Shortcuts:
Text Manipulation Shortcuts:
Command History Shortcuts:
Terminal Control Shortcuts: