Thursday, July 16, 2020

Linux


pwd – Displays the current working directory of the terminal

/ - Root directory

echo – Command that writes its arguments to standard output

               eg echo HI

      HI

Eg x = 100

Echo $x ($ for variable name)

100       

su – Used to switch to root user (so that super user permissions can be used to execute commands

su username- Used to switch to a different user.

sudo  - Executes only that command with root/ super user privileges.

Clear (ctrl + L)- to clear cli but won’t e deleted. Scroll up to see the commands.

USE TAB KEY TO AUTOFILL.  IF MORE THAN  1 DIRECTORY, IT DISPLAYS THEM

 

DIRECTORIES

Cd – change directory.

               Cd subfoldername

               Cd .. (previous folder, one step back)

               $ means home directory

Cd ~ or cd- this commad also changes the directory to home directory

               Cd / -Changes the directory to root directory

               Cd ‘xx yy’ – we specify the folder name in inverted commas because there is a space in the folder name

 

Ls- list all content(subfolders) in the working directory.

               Ls path- byy specifying the path after ls, the content in that path will be displayed

               Ls -l – Using ‘l’ flag, listas al the contents along with its owner settings, permissions and time                                                                             stamp(long format)

               Ls -a – Using ‘a’ flag, lists all the hidden contents in the specified directory.

               Ls –author – Using ‘—author’ flag lists the contents in the specified directory along with its owner

               Ls -S – Using ‘S’ flag, sorts and lists all the contents in the specified directory by size.

               Ls *.html – Using “” flag, lists only the contents in the directory of a particular format.

               Ls -IS storelocation> file.text- Using “>” flag, copies the result of ls command into a text file (file.text). if no store location, it gets saved in home directory

We can run multiple of them at once eg ls -laS

vi filename - View file- click insert to edit

 

WORKING WITH FILE

Cat filename filename  //this command is used to display the content of text files and concatenate several files into one

Cat -b //This is used to add line numbers to non blank lines

Cat -n //This is used to add line numbers to all lines

Cat -s //This is used to squeeze blank lines into one line

Cat  -E //Shows $ at end of each line

Cat > Filename.txt //What ever you type is save in filename. Then press CTRL +D.

Cat filename.txt >> filename2.txt //what even in filename gets concatenated to filename2. Filename2 then filename1

 

Grep //to search for a particular string/word in a text file.

Grep options file.txt /Returns result for matching string “options”.

Grep -I options file.txt //Returns the results for case insensitive strings

Grep -n options file.txt // Returs the matching strings along with their line number

Grep -v options file.tx //Returns the result of lines not matching the search string

Grep – c options file.txt // Returns the no. of lines with result string.

 

Sort //sort result of search

Sort file.txt //Sorts the contents of file.txt and returns them in alphabetical order. We can do with multiple files

Sort -r file.txt //‘r’ flag returns the results in reverse order

Sort -f file.txt //‘f’ flag does case insensitive sorting.

Sort -n file.txt //‘n’ flag returns the results as per numerical order

PIPE (|)

Output the result of one command as input to another command (to perform two operations in the same command.

Grep dh File.txt File2.txt | sort //Searches for string “dh’ from both files and sorts the results

Grep dh File.txt file2.txt | sort -r //sorts the result in reverse order

 

WORKING WITH FILES AND DIRECTORIES

Cp //this command is used to copy files and directories

Cp -i //Enter interactive mode; CLI asks before overwriting files

Cp -n //does not overwrite the file.

Cp -u //Updates the destination file only when source file is different form destination file.

Cp -R //recursive copy for copying directories; Copies even hidden files

Cp -v //Verbose; Prints informative messages

 

Tip

Cp directory . (dot means the working directory)

 

Mv {options} source destination //destination full need if under the same directory =’s subfolder then no problem

Mv -I //Enters interactive mode; CLI asks before overwriting files.

Mv -u //Updates the destination file only when source file is different from destination file

Mv -v //Verbose; Prints source and destination files

 

To move or copy files of one type

Cp *.filetype  destination- * means all files with .filetype

Mv fromwhere/whattype

 

Mkdir //This command is used to create a new directory

Mkdir -p //creates both a new parent directory and a sub-directory.

Mkdir –parents //this is also used to create a parent directory and a sub-directory.

Mkdir -p file1/{multiple subdirectories separated by comma} //This is used to create multiple subdirectories inside the new parent directory.

-p to create a parent directory and then to continue executing the commands

Rmdir //this command is used to remove the specified directory(Empty)

Rmdir -p //Removes both the paret and child directory

Rmdir -pv //Removes all the parent and sub directories along with the verbose.

Rm -r //removes even non empty directories.

Rm -rp //removes non empty directories including parent and subdirectories.

 

WORKING WITH USER PERMISSIONS ‘’r’, ‘w’, ‘x’’

Block1 (1 letter)- file/DIR Type’

Block 2 – User permissions

Block 3– Group permissions

Block 4 - Other’s permissions block2,3,4 have rwx letters which, - for blank which means that permission is not granted.

 

File Types

Normal File – ‘-’

Directory – ‘d’

Character Special File – ‘c’

Binary Special File – ‘b’

 

File permissions-

Read – ‘r’

Write –‘w’

Execute – ‘x’

Block f5 – Symbolic Links

Block 6- Owner name

Block 7 – Group name

Block 8- file size in kb

Block 9- Time stamp

 

 

Denotations

Users- ‘u’

Groups- ‘g’

Others-‘o’

All – ‘a’

 

Chmod //To change the access permissions of files and directories

Chown //To change the owner of files and directories.

Chgrp //To change the group ownership of file and directories.

Chmod g+wx filename  //This gives the write and execute permission to group members

The letter g means group + means giving rights, - is removing rights, = is overwriting

Chown username filename //changes the owner of the specified file

Chown username:groupname filename //change both the owner and group ownership of the specified file

Chgrp groupname filename  //changes the group ownership of the specified file

 

UPDATE SOFTWARE

sudo yum install package-name  //for RHEL (redhat, centos)

sudo apt-get install package-name // for Debian, ubuntu

sudo dnf install package-name  //for fedora

use -y //for yes as default confirmation

 Java8 Installation

Sudo yum update

Update-alternative

Setting environment variables

Sudo gedit .bashrc //(go to cd or home directory)

Edit java location

Run source .bashrc

 

Tar files(compressed)

Gzip //to compress with .gx format

Gunzip  // to decompress .gz files

Tar //To compress and decompress files with .tar format

Tar -cvf tar-file-name filename //Compresing files to Tar format

Tar -xvf tar-file-name //Extracting/Decompressign files in tar format

 

Environment Variable – They control the behaviour of the software package installed on Linux. The path where the packages have been installed will be specified in environment variables.

Printenv //Prints the list of all environment variables

Echo $HOME //This will print the path of the home directory of the user

Echo $PATH //It is a colon-separated list of directories in which the shell looks for commands.

Echo $HOSTNAME //This will print the hostname

Echo $USERNAME //This will print the username

Echo $Lang  //This will print the language being used

Echo $BASH_VERSION //This will print the version of this instance of bash

 

Regular expressions – regular Expression(RegEx) are used to search through data

It can be piped along with ‘grep’ command to find patterns of text in the files

Symbol

Explanation

.

Replaces any character

^

Matches the start of the string

$

Matches the preceding character zero or more times

*

Matches the preceding character zero or more times

?

Matches the preceding character one or more times

()

Groups regular expressions

\

Represents special characters

 

 

Processes

An instance of a program is called a process

Any command given to Linux kernel starts a new process.

There can be multiple processes (instances) of the same program

Top //list of processes

PID|USER|PR|NI|VRT|RES|SHR|S|%CPU|%MEM|TIME+|COMMAND

Process id

Username

Priority

Niceness value (opposite of priority)

Virtual memory

Shared memory

Status

CPU time

Physical memory used

Total CPU time

Command

Ctrl+ C to exit

Type program and enter eg Firefox + enter

Ctrl + Z or W. stop process(It will run in background)

Fg programname //foreground

Bg programname //background

Ps us //Processes started by user who typed this command

Pidof process // gives the pid of the process

Kill 1010// terminates the process of pid 1111

 

CREATING USER

Sudo useradd user-name //Adding a new user

Sudo passwd username //Setting a password for that user

Sudo userdel user-name //Deleting that user

 

Sudo groupadd group-name //Adding a new group

Sudo groupdel group-name //Deleting that group

Sudo usermod -g group-name user-name //Adding a user to a primary group

 

 

SSH(Secure Shell) For Remote MachineAccess

Service iptables stop //Drops the IP table

Sudo chkconfig iptables off //Permanently disables the IP table

Sudo gedit /etc/hosts //Add the bleow IP addresses in both the master and slave’s hosts file

Master IP

Slave IP

Ip addr show //To show the Ip address

Sudo ip adder del ip-address dev eth1 //Deleting existing IP

Sudo ip addr add ip-address/mask dev eth1 // Adding IP at the master’s node or slave’s node 

Ssh master//Running this commad at the slave node wil give remote access to master

Ssh slave //Running this command at the master node will give remote access to slave

 

Ifconfig – give net information

Exit //to logout


No comments:

Post a Comment

UNIX VS LINUX UNIX ·         Founded by Ken Thompson (UTF-8) and Dennis Ritchie (C programming language) ·         Plan- to run one pr...