Practice some basic commands on Linux
Lab2:
Introduction to Linux and shells
• Linux is an operating system, like macOS or Windows.
• It is also the most popular Open Source and free, as in freedom, operating system.
• Android is based on (a modified version of) Linux.
• The Linux "core" (called kernel) was born in 1991 in Finland.
• It went on to be the kernel of the GNU Operating System, creating the duo
GNU/Linux.
• It is developed by volunteers, some paid by companies that rely on it, some
independently, but
there's no single commercial company that can dictate what goes into Linux, or
the project
priorities.
• Linux is also special because there's not just "one Linux", like it happens on
Windows or macOS.Instead, it has distributions.
• A "distro" is made by a company or organization and packages the Linux core
with additional programs and tools. For example, you have Debian, Red Hat, and
Ubuntu.
• You can create your own distribution, too.
• If you use a Mac you need to know that under the hood macOS is a UNIX
Operating System, and it
shares a lot of the same ideas and software that a GNU/Linux system uses,
because GNU/Linux is
a free alternative to UNIX.
• UNIX is an umbrella term that groups many operating systems used in big
corporations and institutions, starting from the ‘70s..
• A shell is a command interpreter that exposes to the
user an interface to work with the underlying operating
system.
• It allows you to execute operations using text and
commands, and it provides users with advanced
features like being able to create scripts.
• shells let you perform things more optimally than a GUI
(Graphical User Interface) could ever let you do.
Command line tools can offer many different
configuration options without being too complex to use.
• There are many different kind of shells. Many different kind of shells
were created for those systems over time, and a few of them dominate
the space: Bash, Csh, Zsh, Fish and many more!
• All shells originate from the Bourne Shell, called sh ."Bourne"
because its creator was Steve Bourne.
• Bash means Bourne-again shell. sh was proprietary and not open
source, and Bash was created in 1989 to create a free alternative for
the GNU project and the Free Software Foundation. Since projects had
to pay to use the Bourne shell, Bash became very popular.
• If you use a Mac, try opening your Mac terminal. That by default is
running ZSH. (or, pre-Catalina, Bash). You can set up your system to
run any kind of shell.
• Each single shell has its own unique features and advanced usage, but
they all share a common functionality: they can let you execute
programs, and they can be programmed
Basic Navigation Commands:
Teach essential commands for navigating the file system
Man and tldr
• This first command will help you understand all the other commands.
• Every time you don't know how to use a command, type
man <command>
You will get the manual.
tldr <command>
This is not a substitute for man , but a handy tool to avoid losing yourself in the huge
amount of
information present in a man page. Then you can use the man page to explore all the
different options andparameters you can use on a command.
mkdir(make directory)
• You create folders using the mkdir command
mkdir fruit
You can create multiple folders with one command:
mkdir dogs cars
• You can also create multiple nested folders by adding the -p option:
mkdir -p fruits/apples
Ls (list) : Display the contents of a directory
• Inside a folder you can list all the files that the folder contains using the ls command:
ls
• If you add a folder name or path, it will print that folder’s contents:
Ls /bin
• ls accepts a lot of options.
ls -al /bin
compared to the plain ls , this returns much more information.
You have, from left to right:
• the file permissions (and if your system supports ACLs, you get an ACL flag as
well)
• the number of links to that file
• the owner of the file
• the group of the file
• the file size in bytes
• the file modified datetime
• the file name
This set of data is generated by the l option. The a option instead also shows the
hidden files.
Hidden files are files that start with a dot ( . ).
Cd(change directory):
Move between directories
Once you have a folder, you can move into it using the cd command. cd means
change directory. You invoke it by specifying a folder to move into. You can specify
a folder name or an entire path.
• Example:
mkdir fruits
cd fruits
Now you are into the fruits folder.
You can use the .. special path to indicate the parent folder:
cd .. #back to the home folder
The # character indicates the start of the comment, which lasts for
the entire line after it's found.
mkdir fruits
mkdir cars
cd fruits
cd ../cars
• There is another special path indicator which is . , and indicates
the current folder.
• You can also use absolute paths, which start from the root folder /
:
cd /etc
This command works on Linux, macOS, WSL, and anywhere you
have a UNIX environment
pwd(print working directory) Show the current directory
• Whenever you feel lost in the filesystem, call the pwd command to know where you
are:
pwd
It will print the current folder path.
rmdir(remove directory)
• Just as you can create a folder using mkdir, you can delete a
folder using rmdir :
mkdir fruits
rmdir fruits
You can also delete multiple folders at once:
mkdir fruits cars
rmdir fruits cars
• The folder you delete must be empty.
To delete folders with files in them, we'll use the more generic rm command which
deletes files and folders,using the -rf options:
rm -rf fruits cars
• Be careful as this command does not ask for confirmation and it will immediately
remove anything you ask it to remove.There is no bin when removing files from the
command line, and recovering lost files can be hard.
File Operations: fundamental file operations :
touch: Create new files
• You can create an empty file using the touch command:
touch apple
• If the file already exists, it opens the file in write mode,and the timestamp of the file is
updated.
Here, the parent option of mkdir helps
us to create the parent directory (unless
it exists) without any error, while
the touch command creates a file.
Generally,the touch command doesn’t
put anything into the file.
The file which is created can be viewed
by ls command and to get more details
about the file you can use long listing
command ll or ls -l command .
Touch command to create multiple files: Touch command can be
used to create multiple numbers of files at the same time. These files would
be empty during creation.
Syntax:
touch File1_name File2_name File3_name
Multiple files with name Doc1, Doc2, Doc3 are created at the same time using
touch command here.
https://www.geeksforgeeks.org/touch-command-in-
linux-with-examples/
cp (copy): Copy files and directories
You can copy a file using the cp command:
touch test
cp apple another_apple
To copy folders you need to add the -r option to recursively copy the whole folder
contents:
mkdir fruits
cp -r fruits cars
mv (move): Move or rename files and directories
Two Distinct Functions of `mv` Command
• 1) Renaming a file or directory.
• 2) Moving a file or directory to another location
touch test
mv pear new_pear
• The pear file is now moved to new_pear. This is how you rename files and folders.If the
last parameter is a folder, the file located at the first parameter path is going to be moved
into that folder. In this case, you can specify a list of files and they will all be moved in the
folder path identified by the last parameter:
touch pear
touch apple
mkdir fruits
mv pear apple fruits #pear and apple moved to the fruits
File Viewing and Editing:
cat
can also add content to a file, and this makes it super powerful.In its simplest
usage, prints a file's content to the standard output:
cat file
You can print the content of multiple files:
cat file1 file2
and using the output redirection operator > you can concatenate the content of
multiple files into a new file:
cat file1 file2 > file3
Using >> you can append the content of multiple files into a new file, creating it if it does
not exist:
cat file1 file2 >> file3
Task
Find about following commands
less or more: View files with pagination.
head and tail: Show the beginning or end of a file.
nano or vim: Basic text editors.