#Devops #TrainwithShubham #90DaysOfDevOpsTABLE OF CONTENTS
How does Linux work?
Open source
different pieces
Why use Linux?
What is Linux?
Linux is an open source operating system (OS) based family on the Linux kernel. An operating system is the software that directly manages a system’s hardware and resources, like CPU, memory, and storage. The OS sits between applications and hardware and makes the connections between all of your software and the physical resources that do the work.
How does Linux work?
Think about an OS like a car engine. An engine can run on its own, but it becomes a functional car when it’s connected with a transmission, axles, and wheels. Without the engine running properly, the rest of the car won’t work.
Linux was designed to be similar to UNIX, but has evolved to run on a wide variety of hardware from phones to supercomputers. Every Linux-based OS involves the Linux kernel—which manages hardware resources—and a set of software packages that make up the rest of the operating system. Organizations can also choose to run their Linux OS on a Linux server.
The OS includes some common core components, like the GNU tools, among others. These tools give the user a way to manage the resources provided by the kernel, install additional software, configure performance and security settings, and more. All of these tools bundled together make up the functional operating system. Because Linux is an open source OS, combinations of software can vary between Linux distributions.
Open source
Linux is also distributed under an open source license. Open source follows these key tenets:
The freedom to run the program, for any purpose.
The freedom to study how the program works, and change it to make it do what you wish.
The freedom to redistribute copies so you can help your neighbor.
The freedom to distribute copies of your modified versions to others.
These points are crucial to understanding the community that works together to create the Linux platform. Without a doubt, Linux is an operating system that is “by the people, for the people”. These tenets are also a main factor in why many people choose Linux. It’s about freedom and freedom of use and freedom of choice.
Linux operating system comprises several different pieces:
Bootloader – The software that manages the boot process of your computer. For most users, this will simply be a splash screen that pops up and eventually goes away to boot into the operating system.
Kernel – This is the one piece of the whole that is actually called ‘Linux’. The kernel is the core of the system and manages the CPU, memory, and peripheral devices. The kernel is the lowest level of the OS.
Init system – This is a sub-system that bootstraps the user space and is charged with controlling daemons. One of the most widely used init systems is systemd, which also happens to be one of the most controversial. It is the init system that manages the boot process, once the initial booting is handed over from the bootloader (i.e., GRUB or GRand Unified Bootloader).
Daemons – These are background services (printing, sound, scheduling, etc.) that either start up during boot or after you log into the desktop.
Graphical server – This is the sub-system that displays the graphics on your monitor. It is commonly referred to as the X server or just X.
Desktop environment – This is the piece that the users actually interact with. There are many desktop environments to choose from (GNOME, Cinnamon, Mate, Pantheon, Enlightenment, KDE, Xfce, etc.). Each desktop environment includes built-in applications (such as file managers, configuration tools, web browsers, and games).
Applications – Desktop environments do not offer the full array of apps. Just like Windows and macOS, Linux offers thousands upon thousands of high-quality software titles that can be easily found and installed. Most modern Linux distributions (more on this below) include App Store-like tools that centralize and simplify application installation. For example, Ubuntu Linux has the Ubuntu Software Center (a rebrand of GNOME Software) which allows you to quickly search among the thousands of apps and install them from one centralized location.
Why use Linux?
This is the one question that most people ask. Why bother learning a completely different computing environment, when the operating system that ships with most desktops, laptops, and servers works just fine?
To answer that question, I would pose another question. Does that operating system you’re currently using really work “just fine”? Or, do you find yourself battling obstacles like viruses, malware, slow downs, crashes, costly repairs, and licensing fees?
If you struggle with the above, Linux might be the perfect platform for you. Linux has evolved into one of the most reliable computer ecosystems on the planet. Combine that reliability with zero cost of entry and you have the perfect solution for a desktop platform.That’s right, zero cost of entry… as in free. You can install Linux on as many computers as you like without paying a cent for software or server licensing.
Basic Commands–
1. PWD command
The pwd
Linux command prints the Present Working Directory path, starting from the root (/
). Use the pwd
command to find your way in the Linux file system structure maze or to pass the working directory in a Bash script. It's a command-line utility tool that returns the path to the directory you're in at that moment. The output contains the full system path of the current working directory. By default,pwd
ignores the symbolic links but with a proper option, you can look at the full physical path of the current working directory.
pwd Syntax
The pwd
command takes the following syntax:
pwd [-options]
The available options are discussed in the following section.
Exit Status
The pwd
command has two exit statuses:
0. Success.
Non-zero. Failure.
$ cd /ubuntu/saswata/Text
$ pwd
/ubuntu/saswata/Text
Use theP
switch to find the full physical path if you have traversed inside a directory which is symbolically linked.$ pwd -P /ubuntu/saswata/Text/test_dir
[cta_inline]
2. CD command
Linux treats the Home directory as the default working directory. Using the cd
command in Linux allows you to change the current working directory.Thecd
command stands for Change Directory, and it allows you to navigate from one directoryto another. To navigate to a particular folder withcd
command, pass the folder path as the parameter.
Linux CD Command Syntax
The cd
command in Linux uses the following syntax:
cd [options] [directory]
In the command above:
cd
: Invokes thecd
command.[options]
: Adding options changes the way the command executes.[directory]
: Path to the directory you want to move into.
For instance, to move to the saswata directory, run:
cd /home/ubuntu/Desktop
$ cd /ubuntu/saswata/Doc
$ pwd
/ubuntu/saswata/Doc
With no options, thecd
command changes the working directory to the user’s home directory.
$ cd
$ pwd
/ubuntu/saswata
Another way of doing the same i.e to navigate to the home directory quickly is to use the~
switch.
$ cd ~
$ pwd
/ubuntu/saswata
You may want to navigate to the previous working directory without typing the entire folder path again.cd
-
does exactly that.
$ cd /ubuntu/saswata/Doc
$ pwd
/ubuntu/saswata/Doc
$ cd -
$ pwd
/ubuntu/saswata
3. MV Command
mv stands for move and is essentially used for moving files or directories from one location to another.
The syntax is similar to the cp command in Linux however there is one fundamental difference between these two commands.
You can think of the cp command as a copy-paste operation. Whereas the mv command can be equated with the cut-paste operation.
Which means that when you use the mv command on a file or directory, the file or directory is moved to a new place and the source file/directory doesn’t exist anymore. That’s what a cut-paste operation, isn’t it?
cp command = copy and paste
mv command = cut and paste
mv command can also be used for renaming a file. Using mv command is fairly simple and if you learn a few options, it will become even better.
1. How to move a file to different directory
The first and the simplest example is to move a file. To do that, you just have to specify the source file and the destination directory or file.
mv source_file target_directory
This command will move the source_file and put it in the target_directory.
2. How to move multiple files
If you want to move multiple files at once, just provide all the files to the move command followed by the destination directory.
mv file1.txt file.2.txt file3.txt target_directory
You can also use glob to move multiple files matching a pattern.
For example in the above example, instead of providing all the files individually, you can also use the glob that matches all the files with the extension .txt and moves them to the target directory.
mv *.txt target_directory
3. How to rename a file
One essential use of mv command is in renaming of files. If you use mv command and specify a file name in the destination, the source file will be renamed to the target_file.
mv source_file target_directory/target_file
In the above example, if the target_fille doesn’t exist in the target_directory, it will create the target_file.
However, if the target_file already exists, it will overwrite it without asking. Which means the content of the existing target file will be changed with the content of the source file.
I’ll show you how to deal with overwriting of files with mv command later in this tutorial.
You are not obliged to provide a target directory. If you don’t specify the target directory, the file will be renamed and kept in the same directory.
Keep in mind*: By default, mv command overwrites if the target file already exists. This behavior can be changed with -n or -i option, explained later.*
4. How to move a directory in Linux with mv command
You can use mv command to move directories as well. The command is the same as what we saw in moving files.
mv source_directory target_directory
In the above example, if the target_directory exists, the entire source_directory will be moved inside the target_directory. Which means that the source_directory will become a sub-directory of the target_directory.
5. How to rename a directory
Renaming a directory is the same as moving a directory. The only difference is that the target directory must not already exist. Otherwise, the entire directory will be moved inside it as we saw in the previous directory.
mv source_directory path_to_non_existing_directory
6. How to deal with overwriting a file while moving
If you are moving a file and there is already a file with the same name, the contents of the existing file will be overwritten immediately.
This may not be ideal in all the situations. You have a few options to deal with the overwrite scenario.
To prevent overwriting existing files, you can use the -n option. This way, mv won’t overwrite existing files.
mv -n source_file target_directory
But maybe you want to overwrite some files. You can use the interactive option -i and it will ask you if you want to overwrite existing file(s).
mv -i source_file target_directory
mv: overwrite 'target_directory/source_file'?
You can enter y for overwriting the existing file or n for not overwriting it.
There is also an option for making automatic backups. If you use -b option with the mv command, it will overwrite the existing files but before that, it will create a backup of the overwritten files.
mv -b file.txt target_dir/file.txt
ls target_dir
file.txt file.txt~
By default, the backup of the file ends with ~. You can change it by using the -S option and specifying the suffix:
mv -S .back -b file.txt target_dir/file.txt
ls target_dir
file.txt file.txt.back
You can also use the update option -u when dealing with overwriting. With the -u option, source files will only be moved to the new location if the source file is newer than the existing file or if it doesn’t exist in the target directory.
To summarize:
-i : Confirm before overwriting
-n : No overwriting
-b : Overwriting with backup
-u : Overwrite if the target file is old or doesn’t exist
7. How to forcefully move the files
If the target file is write protected, you’ll be asked to confirm before overwriting the target file.
mv file1.txt target
mv: replace 'target/file1.txt', overriding mode 0444 (r--r--r--)? y
To avoid this prompt and overwrite the file straightaway, you can use the force option -f.
mv -f file1.txt target
If you do not know what’s write protection, please read about file permissions in Linux.
You can further learn about mv command by browsing its man page. However, you are more likely to use only these mv commands examples I showed here. FYI, you may also use rename command for renaming multiple files at once.
I hope you like this article. If you have questions or suggestions, please feel free to ask in the comment section below.
Themv
command is a utility command thatmoves files and folders from one location to another. Themv
command can move a single file, multiple files, and directories. To move a single file usingmv
, pass the name of the file that needs to be moved as a first parameter and the new file name as a second parameter. In this casemv
commandsrenamesthe filename.
$ mv Text_1.txt Text_2.txt
// renames the file Text_1.txt to Text_2.txt
$ mv some_directory new_directory
// renames the folder some_directory to new_directory
To move a group of files to a folder, pass the name of the files followed by the destination folder name withcd
command.
$ mv Text_1.txt Text_2.txt c.txt some_directory
OR
$ mv *.txt some_directory
By default themv
command overwrites the destination file. To prompt before overwriting the destination file, use the-i
option.
-i Text_1.txt Text_2.txt
mv: overwrite 'Text_2.txt' ?
4. RM Command
Use the rm command to remove files you no longer need.
The rm command removes the entries for a specified file, group of files, or certain select files from a list within a directory. User confirmation, read permission, and write permission are not required before a file is removed when you use the rm command. However, you must have write permission for the directory containing the file.
The following are examples of how to use the rm command:
To delete the file named myfile, type the following:
rm myfile
To delete all the files in the mydir directory, one by one, type the following:
rm -i mydir/*
After each file name displays, type y and press Enter to delete the file. Or to keep the file, just press Enter.
See the rm command in the Commands Reference, Volume 4 for the complete syntax.
Therm
command is stands for remove. It's used to delete files and directories.Be cautious when you use therm
command because once a file or directory is deleted, you cannot recover it later.To delete a single file, just pass the name of the file along with therm
command.
$ rm file.txt
It is also possible to delete multiple files at one go.
$ rm file1.txt file2.txt image.png
To delete a directory, use the-r
switch, which means to delete all files and folders recursively.
rm -r some_directory
To perform deletion safely and interactively, use the-i
switch, which prompts before each delete action is performed.
$ rm -i file.txt
rm: remove regular file ‘file.txt’? y
5. MKDIR command
Use the mkdir command to create one or more directories specified by the Directory parameter.
Each new directory contains the standard entries dot (.) and dot dot (..). You can specify the permissions for the new directories with the -m Mode flag.
When you create a directory, it is created within the current, or working, directory unless you specify an absolute path name to another location in the file system.
The following are examples of how to use the mkdir command:
To create a new directory called Test in the current working directory with default permissions, type the following:
mkdir Test
To create a directory called Test with
rwxr-xr-x
permissions in a previously created /home/demo/sub1 directory, type the following:mkdir -m 755 /home/demo/sub1/Test
To create a directory called Test with default permissions in the /home/demo/sub2 directory, type the following:
mkdir -p /home/demo/sub2/Test
The -p flag creates the /home, /home/demo, and /home/demo/sub2 directories if they do not already exist.
See the mkdir command in the Commands Reference, Volume 3 for the complete syntax.
6. LS Command
If you have worked in Linux, you may be familiar with the ls
command. Ls is short for “list”. This command lists information about directories and any type of files in the working directory.ls
is thelistcommand in Linux, and it shows full list of files or contents of a directory. Just typels
and press theEnter
key. The entire contents of the directory will be shown.
ls Command Syntax
Let’s start by explaining the syntax of the ls
command.
ls [options]
$ ls
Use the-l
switch to show the list of files of the current directory in a long list format.
$ ls -l
In Linux, hidden files start with a.
(dot) symbol and are invisible to the regular directory. The-a
switch will list entire contents of current directory including the hidden files.
$ ls -la
Sometimes you may want to get the details of a directory rather than its content. To get the details of a directory, use-d
option. For example, if you usels -l /home
, it will display all the files under/home
directory. But if you want to display the information about the/home
directory then use-ld
option as shown below.
$ ls -ld.
ls Command Additional Options
The additional options detailed below give users more flexibility when using the ls
command:
ls -F
ls -m
ls -Q
ls -i
Type ls -F
in a terminal window to add “/
” at the end of each directory.
Sorting Options
There are three different sorting options that you can use:
ls -r
ls -t
ls -X
View Hidden Files
When using the basic ls
command, you can’t see hidden files and files starting with “.
”. Type the ls -a
command to display them.
Directory Trees With the ls Command
If you want to access long listing directory trees, type ls -R
.
Advanced ls Commands
So far, you have learned simple ls
commands that display a limited amount of information about directories and files. You should use advanced ls
commands to access extensive information about your files and directories. Advanced ls
commands are:
ls -l
ls -n
ls -lh
ls -ltr
ls -lS
ls -l /tmp
ls -ld /tmp/
ls --help
View Long Listing Format
Run the ls -l
command to print out a long listing format of files and directories. The output displays information, such as file or folder name, owner of the file and its permission, size and modified date and time.