Day 2 : Basics of Linux

Day2: Introduction to Linux

What is Linux?

  1. Linux is an open-source operating system, It is a freely available operating system that can be installed on a wide range of devices such as desktops, servers, smartphones and tablets, supercomputers etc.

  2. An operating system (OS) is a system of software that manages computer hardware, software resources and provide common services for computer programs.

Linux File System Hierarchy - In Linux everything is represented as a file including a hardware program the files are stored in a directory and every directory contains a file with a tree structure that is called file system hierarchy.

  1. /home - It creates a home directory for another user.

  2. /root - home directory for the root user.

  3. /boot - It contains bootable files for Linux.

  4. /etc - Contains all configuration files.

  5. /user - By default software is installed in this directory.

  6. /bin - It contains commands used by all users.

  7. /sbin - It contains commands used by the only root user.

  8. /opt - Optional application software packages.

  9. /dev - Essential device files.

  10. /mnt - Temporarily mounted file systems.

Features of Linux

  1. Open source

  2. Secure

  3. Simplified updates for all installed software.

  4. Lightweight

  5. Multiple Distribution(Fedora,Debian,RedHat)

Basic Linux Commands

  • / Is your root directory.

  • ~ Is your home directory.

  • uname : It show name of kernal (OS).

  • uname -r : It show version of kernal.

  • clear: Is used for a clear screen.

  • whoami : It shows currently login user name.

  • date : It shows time and date.

  • cal : Show this month's calendar

  • history : It show list of previously used command.

      ubuntu@ip-172-31-205-241:~$ uname
      Linux
      ubuntu@ip-172-31-205-241:~$ uname -r
      5.15.0-1028-aws
      ubuntu@ip-172-31-205-241:~$ date
      Mon Mar 27 11:59:37 UTC 2023
    
  1. ls - The most frequently used command in Linux to list directories

  2. pwd - Print working directory command in Linux

  3. cd - Linux command to navigate through directories

  4. mkdir - Command used to create directories in Linux

  5. cd .. - Change the directory to one step back.

  6. cd ../.. - Change directory to 2 lavels back.

  7. mv - Move or rename files in Linux

ubuntu@ip-172-31-205-241:~$ whoami ubuntu 
 ubuntu@ip-172-31-205-241:~$ touch my_file.txt 
 ubuntu@ip-172-31-205-241:~$ ls 
 my_file.txt 
 ubuntu@ip-172-31-205-241:~$ pwd
 /home/ubuntu 
 ubuntu@ip-172-31-205-241:~$ mkdir linux 
 ubuntu@ip-172-31-205-241:~$ ls 
 linux my_file.txt 
 ubuntu@ip-172-31-205-241:~$ cd linux 
 ubuntu@ip-172-31-205-241:~/linux$ pwd 
 /home/ubuntu/linux 
 ubuntu@ip-172-31-205-241:~/linux$ cd .. 
 ubuntu@ip-172-31-205-241:~$ ls 
 linux my_file.txt 
 ubuntu@ip-172-31-205-241:~/linux$ mv ../my_file.txt .
 ubuntu@ip-172-31-205-241:~/linux$ ls
 my_file.txt

Listing commands

ls - The most frequently used command in Linux to list directories

ls -a - you can see the whole list of files, including the hidden files.

ls -l - The ls command will only display the files.

ls -d */ - If you only want to display the sub-directories excluding all other files, you can use this command

ls -g - If you don't want to display the owner information in your list

ls -lg - f you don't want to display the group information in your list

ls ../ - This command displays the files and directories of the parent directory.

Linux Command to Create a Nested Directory :

command to create a nested directory is : mkdir -p A/B/C/D/E

-p is for no error if existing, make parent directories as needed

tree command is used to show the nested directory.

ubuntu@ip-172-31-205-241:~$ mkdir -p A/B/C/D/E 
ubuntu@ip-172-31-205-241:~$ tree
.
├── A
│   └── B
│       └── C
│           └── D
│               └── E
├── dir
│   ├── dir1
│   └── dir2
├── linux
│   ├── linux
│   └── my_file.txt
└── snap
    └── tree
        ├── 18
        ├── common
        └── current -> 18
14 directories, 2 files

Some extra commands:

1. sudo command

Short for superuser do, sudo is one of the most popular basic Linux commands that lets you perform tasks that require administrative or root permissions.

Here’s the general syntax: sudo (command)

2. cat command

Concatenate, or cat, is one of the most frequently used Linux commands. It lists, combines, and writes file content to the standard output. To run the cat command, type cat followed by the file name and its extension. For instance:

cat filename.txt.

3. cp command

Use the cp command to copy files or directories and their content. Take a look at the following use cases.

To copy one file from the current directory to another, enter cp followed by the file name and the destination directory. For example:

cp filename.txt /home/username/Documents

4. rmdir command

To permanently delete an empty directory, use the rmdir command. Remember that the user running this command should have sudo privileges in the parent directory.

For example, you want to remove an empty subdirectory named personal1 and its main folder mydir:

rmdir -p mydir/personal1

5. rm command

The rm command is used to delete files within a directory. Make sure that the user performing this command has write permissions.

Remember the directory’s location as this will remove the file(s) and you can’t undo it.

Here’s the general syntax:

rm filename

To remove multiple files, enter the following command:

rm filename1 filename2 filename3

6. touch command

The touch command allows you to create an empty file or generate and modify a timestamp in the Linux command line.

For example, enter the following command to create an HTML file named Web in the Documents directory:

touch /home/username/Documents/Web.html

7. locate command

The locate command can find a file in the database system.

Moreover, adding the -i argument will turn off case sensitivity, so you can search for a file even if you don’t remember its exact name.

To look for content that contains two or more words, use an asterisk (*). For example:

locate -i school*note

The command will search for files that contain the words school and note, whether they use uppercase or lowercase letters.

8. find command

Use the find command to search for files within a specific directory and perform subsequent operations. Here’s the general syntax:

find [option] [path] [expression]

For example, you want to look for a file called notes.txt within the home directory and its subfolders:

find /home -name notes.txt

9. grep command

Another basic Linux command on the list is grep or global regular expression print. It lets you find a word by searching through all the texts in a specific file.

Once the grep command finds a match, it prints all lines that contain the specific pattern. This command helps filter through large log files.

For example, you want to search for the word blue in the notepad.txt file:

grep blue notepad.txt

The command’s output will display lines that contain blue.

10. df command

Use the df command to report the system’s disk space usage, shown in percentage and kilobyte (KB). Here’s the general syntax:

df [options] [file]

For example, enter the following command if you want to see the current directory’s system disk space usage in a human-readable format:

df -h

11. du command

If you want to check how much space a file or a directory takes up, use the du command. You can run this command to identify which part of the system uses the storage excessively.

Remember, you must specify the directory path when using the du command. For example, to check /home/user/Documents enter:

du /home/user/Documents

12. head command

The head command allows you to view the first ten lines of a text. Adding an option lets you change the number of lines shown. The head command is also used to output piped data to the CLI.

Here’s the general syntax:

head [option] [file]

13. tail command

The tail command displays the last ten lines of a file. It allows users to check whether a file has new data or to read error messages.

Here’s the general format:

tail [option] [file]

14. diff command

Short for difference, the diff command compares two contents of a file line by line. After analyzing them, it will display the parts that do not match.

Programmers often use the diff command to alter a program instead of rewriting the entire source code.

Here’s the general format:

diff [option] file1 file2

15. tar command

The tar command archives multiple files into a TAR file – a common Linux format similar to ZIP, with optional compression.

Here’s the basic syntax:

tar [options] [archive_file] [file or directory to be archived]

16. chmod command

chmod is a common command that modifies a file or directory’s read, write, and execute permissions. In Linux, each file is associated with three user classes – owner, group member, and others.

Here’s the basic syntax:

chmod [option] [permission] [file_name]

17. chown command

The chown command lets you change the ownership of a file, directory, or symbolic link to a specified username.

Here’s the basic format:

chown [option] owner[:group] file(s)

For example, you want to make linuxuser2 the owner of filename.txt:

chown linuxuser2 filename.txt

18. jobs command

A job is a process that the shell starts. The jobs command will display all the running processes along with their statuses. Remember that this command is only available in csh, bash, tcsh, and ksh shells.

This is the basic syntax:

jobs [options] jobID

19. kill command

Use the kill command to terminate an unresponsive program manually. It will signal misbehaving applications and instruct them to close their processes.

To kill a program, you must know its process identification number (PID). If you don’t know the PID, run the following command:

ps ux

After knowing what signal to use and the program’s PID, enter the following syntax:

kill [signal_option] pid

20. ping command

The ping command is one of the most used basic Linux commands for checking whether a network or a server is reachable. In addition, it is used to troubleshoot various connectivity issues.

Here’s the general format:

ping [option] [hostname_or_IP_address]

For example, you want to know whether you can connect to Google and measure its response time:

ping google.com

Thanks For Regarding :)

Shubham Jangale