Types of available:
*date: shows the current data & time
$ date
Mon Dec 25 22:16:37 IST 2023
sed 's/old_text/new_text/' filename (if the complete word match then only it will replace) it won't update file
What is the booting Process of Linux?
What is OS: OS is a set of programs or modules that control or co-ordinates the hardware resourses for diff application programme. OS is the mediator b/w user/application & hardware OS MOdules: 1.File System Management 2.Process Management 3.Memory Management 4.Device Management 5.Network Management #######TO Get the Kernal version: raj [ ~ ]$ uname -r 5.10.102.2-microsoft-standard ##Boot loader is the special process invoked by you BIOS to load the complete image of your os into Memory User SPace: Kernal Space 1.User login to the centos/rhel as root 1.Kernal will be running 2.Executing any commands or running any application 2. Kernal always will be in protected mode 3.Process are running on u space have low priority 3.Process are ruuning in kernal space as high priority compare to kernal process **to find the path location of package raj [ ~ ]$ whereis git && which git (both commands are same) git: /usr/bin/git raj [ ~ ]$ whereis python python: /usr/bin/python /opt/ansible/bin/python /opt/blobxfer/bin/python **Ways to communicate from user to kernal 1.Linux commands 2.System Calls 3.Proc File System 4.IOCTl 5 sys File System 6.Interupts & signals ############Linux users############### In Linux we have mainly devided 3 types of users 1.root --- > Super user 2.Normal User : Rajesh & Mahesh 3.System user -> ftp, mail, cups Hardlink: Create a files called file1 ls file1 rajharden (Create a link b/w the files) now if u save some data in file1 it will auto saved saved in rajharden file as well raj [ ~/test ]$ cat >> file1 singam hjsbdkf ^C raj [ ~/test ]$ cat file1 singam hjsbdkf raj [ ~/test ]$ cat rajharden singam hjsbdkf ls -li (to files hardlinks by using ls command at 106498 ) aj [ ~/test ]$ ls -li 106498 -rw-r--r-- 2 raj raj 15 May 24 06:30 file1 106498 -rw-r--r-- 2 raj raj 15 May 24 06:30 rajharden ** hardlink only for files ** in hard link if you delete the parent file called file1 still you can access u r data from rajharden raj [ ~/test ]$ rm -rf file1 raj [ ~/test ]$ ls rajharden raj [ ~/test ]$ cat rajharden singam hjsbdkf **************Soft link also called symbolic :- it will indicate like softlinktest -> file2 raj [ ~/test ]$ ln -s file2 softlinktest raj [ ~/test ]$ ls -li total 0 106500 -rw-r--r-- 1 raj raj 0 May 24 06:33 file2 106501 lrwxrwxrwx 1 raj raj 5 May 24 06:33 softlinktest -> file2 In softlink if you remove the parent the softlink file also will be lost raj [ ~/test ]$ rm -rf file2 raj [ ~/test ]$ ls softlinktest raj [ ~/test ]$ cat softlinktest cat: softlinktest: No such file or directory It will supports directories ln -s directoryname filename ``` | Soft Links | Hard Links | |-------------------------------|-------------------------------| | Also known as symbolic | Point directly to the | | links supports both file & directpry | physical file in the file | | | system | |-------------------------------|-------------------------------| | They are pointers to | They are directory entries | | another file | that point to the same | | | inode as the original file | |-------------------------------|-------------------------------| | If the original file is | If the original file is | | deleted or moved, the soft | deleted, the hard link | | link becomes broken | still points to the | | | original file until all | | | hard links are removed | |-------------------------------|-------------------------------| | Soft links can point to | Hard links cannot cross | | directories | file systems | |-------------------------------|-------------------------------| | They can be created across | Hard links are restricted | | file systems | to the same file system | |-------------------------------|-------------------------------| | They use filenames to | They use inodes to link | | reference files | files | |-------------------------------|-------------------------------| | Soft links can have | Hard links cannot have | | different file permissions | different file permissions | | from the original file | from the original file | ``` 3. Hard Link - ln 1. Hardlink - is always created for the files not for the directory 2. whenever you create hard link,new inode is not allocated, the same inode is used 3. Hardlink cannot be created across partition 4.Soft Link - ln -s 1. Softlink = can be created both for the normal file as well directory 2. Whenever you create a soft link ,a new inode is always allocated 3. Softlink can be created across partition To find the which linux we have in os raj [ ~ ]$ echo $SHELL /bin/bash Shell is the Interface b/w user application & kernal raj [ ~ ]$ ps PID TTY TIME CMD 1159 pts/1 00:00:00 bash 2199 pts/1 00:00:00 ps raj [ ~ ]$ echo $$ ----------------> it will give the shell process id echo $$ 1159 ############PROC################ All process infomation will be there in the folder of Proc if you want to see the child process that are running under parent process Example: raj [ / ]$ cd proc/ raj [ /proc ]$ ls 1 1173 37 cgroups devices filesystems irq kmsg locks mtrr softirqs thread-self vmallocinfo 1119 17 60 cmdline diskstats fs kallsyms kpagecgroup meminfo net stat timer_list vmstat 1154 23 acpi consoles dma interrupts kcore kpagecount misc pagetypeinfo swaps tty zoneinfo 1156 2335 buddyinfo cpuinfo driver iomem keys kpageflags modules partitions sys uptime 1159 30 bus crypto execdomains ioports key-users loadavg mounts self sysvipc version raj [ /proc ]$ cd 1154/ raj [ /proc/1154 ]$ ls arch_status clear_refs cpuset fd map_files mounts oom_adj personality smaps statm timers attr cmdline cwd fdinfo maps mountstats oom_score projid_map smaps_rollup status timerslack_ns auxv comm environ gid_map mem net oom_score_adj root stack syscall uid_map cgroup coredump_filter exe limits mountinfo ns pagemap setgroups stat task wchan file /foldername --> it wll tell you the type of file like exec or directory ------------------Linux supports 7 types of files--------------------------- 1.Normal & white files --> touch, cat , echo , vi , head , less , > , nano 2.Directory --> mkdir 3.Hardlink --> ln 4.softlink(symbolic link) --> ls -s 5.Pipe file /Fifo file --> mkfifo 6.character device file or block device file --> mknod 7.socket file --> mksoc ****to get the directory using ls raj [ ~ ]$ ls -la | grep ^d drwxr-xr-x 2 raj raj 4096 Apr 17 17:52 ansible drwx------ 5 raj raj 4096 Apr 17 16:20 .azure *Stat will give the complete info about file or folder raj [ ~/test ]$ stat file2 File: file2 Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 700h/1792d Inode: 106498 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 9527/ raj) Gid: ( 9527/ raj) Access: 2024-05-24 06:37:49.716379430 +0000 Modify: 2024-05-24 06:37:42.881414949 +0000 Change: 2024-05-24 06:37:42.881414949 +0000 Birth: 2024-05-24 06:37:42.881414949 +0000 ##########################5.Pipe file /Fifo file --> mkfifo###################### special type of files will be there under /dev those will be used for communicating the devices under dev all buffered files will be there #############################Most widely important commands############################### ls,date,who,whoami,calendar, tar,gzip,(gzip2, Bzip2), tr, cp , my ,rm , mkdir ,rmdir, rm -f sed,awk , cut,paste, find ,grep , test , chmod,chown,chgrp , ping , ssh , rsync, man ,info, aprpos, Network: path: /etc/network/interfaces or /etc/sysconfig/network-scripts/ifcfg-* netstat, nmap,traceroute, arp, head ,tail ,df ,fdisk , mount,umount, fsck , mkfs.ext3, mkfs.ntfs kill, strace, ptrace , lsblk ####################File System######################### File system is a software which is part of OS that will take care of any operations into file Create a file create the directory Permission & directory reading a file & deleting a file TYpes: EXT3,EXT4,EXT5, Reiser FS windows VFAT, NTFS 65 types of linux can support alll file system windows doesn't understarbd the ext5 file sysstem Linux OS --> VFS (Virtual File System) Name | Description --------------------|------------------------------------------------------------ / | Root directory - the top of the file system hierarchy /bin | Essential binary executables (common commands like ls, cp) most of the commands files will be in bin directory /boot | Files needed to boot the system (kernel, boot loader files)uname -r (get the kernal version) /dev | Device files representing hardware and peripherals /etc | System configuration files & dependency configuration files all the configuration file will be in ETC /home | User home directories /lib | Essential shared libraries and kernel modules it will contain all system librariesgvx /media | Mount points for removable media (USB drives, CDs) /mnt | Temporary mount points for mounting file systems /opt | Optional application software packages database related or any other application related /proc | Virtual file system providing process and kernel information & stores the info about proocess id's /root | Home directory of the root user /run | Run-time variable data (system information since last boot) /sbin | System binaries (essential commands for system administration) /srv | Data for services provided by the system (web server data) /sys | Virtual file system providing device and kernel information /tmp | Temporary files /usr | Secondary hierarchy for read-only user data (applications, libraries, documentation) librarries of yum packages /usr/bin | Non-essential binary executables for user commands /usr/lib | Non-essential shared libraries /usr/local | User-provided applications and data /var | Variable data files (logs, databases, websites) /home | For the users who logged in it acts as workspace. ** Kernal path cd /usr/lib/modules 0-999 all allocated to system users after all users aligned to normal users raj [ ~ ]$ id uid=9527(raj) gid=9527(raj) groups=9527(raj) cat /etc/passwd --> list of all users to view the users password cat /etc/shadow --> password in deprecated format **To view the types of shells present in linux raj [ ~ ]$ cat /etc/shells /bin/sh /bin/bash /usr/bin/sh /usr/bin/bash /usr/bin/pwsh /usr/bin/zsh /bin/zsh We can see every user by default which shell they can use raj [ ~ ]$ cat /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/dev/null:/bin/false whenever the shell execute any command successfully,the return value is "0" 1,2,3......255 ---- Error value char,int,flat,double,array ...types of varaible #!/bin/bash echo "Enter the value" read var echo $var 3 method to execute the script a. sh first_script.sh b. ./first_script.sh ---> throw an error , chmod +x first_script.sh c. source first_script.sh or . first_script.sh alias k="kubectl" if u want make permanenet change vi .bashrc add alias ** to load again source .bashrc ?------> it will give the exit status of last executed command raj [ ~ ]$ echo $? ---> if the output comes 0 its always success. 0 apart from 1 to 255 all or errors system level configuration files /etc/profile /etc/inputrc /etc/bashrc /etc/DIR_COLORS /etc/profile.d/---- ---- ----------------------- User-level configuration files .bash_profile .bashrc .bash_logout Shell Variables Variables are nothing but ,it is label that assign some value 2 types of variable 1.local variable 2. Environment variable Local variable are those variables which are defined in the local shell itself you cannot make use of the same variable in some other shell,if u still want to use the variable in some other shell then you need to export that variable by using export command bash also has its inbuilt shell variable which we call it as Environment Variables a=10 export a vi first_script.sh #!/bin/bash echo "Enter the value" read var echo $var 3 method to execute the script a. sh first_script.sh b. ./first_script.sh ---> throw an error , chmod +x first_script.sh c. source first_script.sh or . first_script.sh ls -R folderorpathname it will complete info about folder and subfolder or path inside of the data ***ps -ef State of the Process: 1. Running----------------------------------------> R 2. Sleeping or Interruptable sleep---------> S 3. Stopped-----------------------------------> T ---> Ctrl+z to stop the process or u can send SIGSTOP signal kill -19 <pid> 4. Orphan --------------------------------> O 5. Zombie--- Defunc state -------------------------------> Z 6. Daemon - -----------------------------------------> D #######################Package management##############Redhat package management##############Redhat As RPM not fullfiled everything they came up with the yum --> yellow dog modifier yum dnf rpm yum is the primary package management for rhel Yum is front end everything wll be backend as rpm raj [ ~ ]$ rpm -qa filesystem-1.1-20.cm2.x86_64 zlib-1.2.13-2.cm2.x86_64 from rpm to install any package wget gethe package name rpm -ivh example.rpm ########################################SEd################# SED : Stream editor used for file manipulation raj [ ~ ]$ cat /etc/passwd | sed 's/root/raj/' raj:x:0:0:root:/root:/bin/bash raj [ ~ ]$ cat /etc/passwd | sed -e 's/root/raj/ig' --> g means globally i is case sensitive by default all will be small -e is expression # sed syntax: sed [options] 'command' file 1. Print lines matching a pattern -------------------------------- sed -n '/pattern/p' filename # Example: Print lines containing "hello" sed -n '/hello/p' file.txt 2. Replace a string (first occurrence in each line) --------------------------------------------------- sed 's/old_string/new_string/' filename # Example: Replace "apple" with "orange" sed 's/apple/orange/' file.txt 3. Replace a string globally (all occurrences in each line) ----------------------------------------------------------- sed 's/old_string/new_string/g' filename # Example: Replace all occurrences of "apple" with "orange" sed 's/apple/orange/g' file.txt 4. Replace a string and save changes to the file ------------------------------------------------ sed -i 's/old_string/new_string/' filename # Example: Replace "apple" with "orange" and save sed -i 's/apple/orange/' file.txt 5. Delete lines matching a pattern ---------------------------------- sed '/pattern/d' filename # Example: Delete lines containing "hello" sed '/hello/d' file.txt 6. Delete a specific line number -------------------------------- sed 'linenumberd' filename # Example: Delete line 2 sed '2d' file.txt 7. Delete a range of lines -------------------------- sed 'startline,endline d' filename # Example: Delete lines 2 to 4 sed '2,4d' file.txt 8. Insert text before a specific line ------------------------------------- sed 'linenumber i\text' filename # Example: Insert "hello world" before line 3 sed '3i\hello world' file.txt 9. Append text after a specific line ------------------------------------ sed 'linenumber a\text' filename # Example: Append "hello world" after line 3 sed '3a\hello world' file.txt 10. Substitute only on a specific line -------------------------------------- sed 'linenumber s/old_string/new_string/' filename # Example: Replace "apple" with "orange" on line 2 sed '2s/apple/orange/' file.txt 11. Print a specific line number -------------------------------- sed -n 'linenumberp' filename # Example: Print line 3 sed -n '3p' file.txt 12. Print a range of lines -------------------------- sed -n 'startline,endlinep' filename # Example: Print lines 2 to 4 sed -n '2,4p' file.txt 13. Substitute with a captured group ------------------------------------- sed 's/\(old\)_string/new_\1/' filename # Example: Replace "apple_fruit" with "orange_fruit" sed 's/\(apple\)_fruit/orange_\1/' file.txt 14. Multiple commands with -e option ------------------------------------ sed -e 'command1' -e 'command2' filename # Example: Delete line 2 and replace "apple" with "orange" sed -e '2d' -e 's/apple/orange/' file.txt 15. Using sed with a script file -------------------------------- sed -f script.sed filename # script.sed contains sed commands # Example: script.sed # 2d # s/apple/orange/ # Command to run: sed -f script.sed file.txt #####################################################################AWK###################################################### # awk syntax: awk 'pattern {action}' filename 1. Print all lines ------------------ awk '{print}' filename # Example: Print all lines in file.txt awk '{print}' file.txt 2. Print specific fields ------------------------ awk '{print $1, $3}' filename # Example: Print the 1st and 3rd fields awk '{print $1, $3}' file.txt 3. Print lines matching a pattern --------------------------------- awk '/pattern/' filename # Example: Print lines containing "hello" awk '/hello/' file.txt 4. Print line number and content -------------------------------- awk '{print NR, $0}' filename # Example: Print line number and line content awk '{print NR, $0}' file.txt 5. Sum values in a column ------------------------- awk '{sum += $1} END {print sum}' filename # Example: Sum values in the 1st column awk '{sum += $1} END {print sum}' file.txt 6. Calculate average of a column -------------------------------- awk '{sum += $1; count++} END {print sum/count}' filename # Example: Calculate average of values in the 1st column awk '{sum += $1; count++} END {print sum/count}' file.txt 7. Print lines with more than a specific number of fields --------------------------------------------------------- awk 'NF > num' filename # Example: Print lines with more than 3 fields awk 'NF > 3' file.txt 8. Print lines where a field matches a value -------------------------------------------- awk '$1 == "value"' filename # Example: Print lines where the 1st field is "apple" awk '$1 == "apple"' file.txt 9. Print lines where a field is greater than a value ---------------------------------------------------- awk '$1 > value' filename # Example: Print lines where the 1st field is greater than 10 awk '$1 > 10' file.txt 10. Define field separator -------------------------- awk -F 'separator' '{print}' filename # Example: Define comma as field separator and print fields awk -F ',' '{print $1, $2}' file.txt 11. Use a variable ------------------ awk -v var=value '{print var, $1}' filename # Example: Define variable "prefix" and print it with the 1st field awk -v prefix="Item:" '{print prefix, $1}' file.txt 12. Print a range of lines -------------------------- awk 'NR==start, NR==end' filename # Example: Print lines from 2 to 4 awk 'NR==2, NR==4' file.txt 13. Conditional print with an if statement ------------------------------------------ awk '{if ($1 > value) print $0}' filename # Example: Print lines where the 1st field is greater than 10 awk '{if ($1 > 10) print $0}' file.txt 14. Format output ----------------- awk '{printf "Field 1: %s, Field 2: %s\n", $1, $2}' filename # Example: Format output with printf awk '{printf "Field 1: %s, Field 2: %s\n", $1, $2}' file.txt 15. Execute multiple actions ---------------------------- awk '{action1; action2}' filename # Example: Print the 1st field and then the 2nd field awk '{print $1; print $2}' file.txt 16. Use built-in variables (e.g., NR, NF) ----------------------------------------- awk '{print NR, NF, $0}' filename # Example: Print line number, number of fields, and line content awk '{print NR, NF, $0}' file.txt 17. Filter and process with BEGIN and END blocks ------------------------------------------------ awk 'BEGIN {print "Start"} {print $0} END {print "End"}' filename # Example: Print "Start" before processing, then lines, then "End" awk 'BEGIN {print "Start"} {print $0} END {print "End"}' file.txt 18. Use an external script file ------------------------------- awk -f script.awk filename # script.awk contains awk commands # Example: script.awk # {sum += $1} # END {print sum} # Command to run: awk -f script.awk file.txt ########################################################################################################################### # cut syntax: cut [options] filename 1. Cut specific fields by delimiter ----------------------------------- cut -d 'delimiter' -f field_list filename # Example: Cut the 1st and 3rd fields by comma delimiter cut -d ',' -f 1,3 file.txt 2. Cut specific characters by position -------------------------------------- cut -c char_list filename # Example: Cut characters from position 1 to 3 cut -c 1-3 file.txt 3. Cut multiple ranges of characters ------------------------------------ cut -c range1,range2 filename # Example: Cut characters from position 1 to 3 and 5 to 7 cut -c 1-3,5-7 file.txt 4. Cut specific bytes --------------------- cut -b byte_list filename # Example: Cut bytes 1 to 4 cut -b 1-4 file.txt 5. Combine cut with other commands using pipes ---------------------------------------------- command | cut -d 'delimiter' -f field_list # Example: Combine with echo to cut fields from output echo "a,b,c,d" | cut -d ',' -f 2,4 6. Cut fields with tab delimiter (default) ------------------------------------------ cut -f field_list filename # Example: Cut the 1st and 2nd fields (tab-delimited) cut -f 1,2 file.txt 7. Use cut with multiple delimiters (GNU cut) --------------------------------------------- cut --output-delimiter='new_delimiter' -d 'delimiter' -f field_list filename # Example: Replace comma delimiter with semicolon in output cut --output-delimiter=';' -d ',' -f 1,2 file.txt # grep syntax: grep [options] pattern filename 1. Search for a pattern in a file --------------------------------- grep 'pattern' filename # Example: Search for "hello" in file.txt grep 'hello' file.txt 2. Search recursively in directories ------------------------------------ grep -r 'pattern' directory # Example: Search for "hello" in all files in the current directory grep -r 'hello' . 3. Ignore case while searching ------------------------------ grep -i 'pattern' filename # Example: Search for "hello" case-insensitively grep -i 'hello' file.txt 4. Show line numbers of matches ------------------------------- grep -n 'pattern' filename # Example: Search for "hello" and show line numbers grep -n 'hello' file.txt 5. Display count of matching lines ---------------------------------- grep -c 'pattern' filename # Example: Count lines containing "hello" grep -c 'hello' file.txt 6. Invert match to show non-matching lines ------------------------------------------ grep -v 'pattern' filename # Example: Show lines that do not contain "hello" grep -v 'hello' file.txt 7. Search for whole words ------------------------- grep -w 'pattern' filename # Example: Search for whole word "hello" grep -w 'hello' file.txt 8. Use regular expressions for complex patterns ----------------------------------------------- grep -E 'pattern' filename # Example: Search for lines containing "hello" or "world" grep -E 'hello|world' file.txt 9. Print only matched parts of a line ------------------------------------- grep -o 'pattern' filename # Example: Print only the word "hello" from matching lines grep -o 'hello' file.txt 10. Search for multiple patterns -------------------------------- grep -e 'pattern1' -e 'pattern2' filename # Example: Search for "hello" or "world" grep -e 'hello' -e 'world' file.txt 11. Show N lines before and after the match ------------------------------------------- grep -C N 'pattern' filename # Example: Show 2 lines of context around matches grep -C 2 'hello' file.txt 12. Show N lines before the match --------------------------------- grep -B N 'pattern' filename # Example: Show 2 lines before matches grep -B 2 'hello' file.txt 13. Show N lines after the match -------------------------------- grep -A N 'pattern' filename # Example: Show 2 lines after matches grep -A 2 'hello' file.txt 14. Read patterns from a file ----------------------------- grep -f patternfile filename # Example: Search patterns listed in patterns.txt grep -f patterns.txt file.txt 15. Combine grep with other commands using pipes ------------------------------------------------ command | grep 'pattern' # Example: Combine with echo to search for a pattern in output echo "hello world" | grep 'hello' #############################TR############################## command | tr <'old'> <'new'> raj [ ~ ]$ cat /etc/passwd | tr 'root' 'Root' Root:x:0:0:Root:/Root:/bin/bash raj [ ~ ]$ cat /etc/passwd | sort ----> will be used for to arrange in alphabet order bin:x:1:1:bin:/dev/null:/bin/false daemon:x:6:6:Daemon User:/dev/null:/bin/false find . -name "*.pdf" --> find a files in . means current directory / from root directory ====================Network============= ip a ifconfig to add any ip adress to interfaces ip address add ipaddressof traceroute google.com raj [ ~/test ]$ netstat -tnlp ---> What are the ports are running machine (No info could be read for "-p": geteuid()=9527 but you should be root.) Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:8888 0.0.0.0:* LISTEN - tcp 0 0 127.0.0.1:50342 0.0.0.0:* LISTEN - tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN - netstat -nutlp | grep httpd nsloopup: The nslookup command is an older edition of the dig command. Also, it is utilized for DNS related problems. raj [ ~/test ]$ nslookup cloudera.com Server: 168.63.129.16 Address: 168.63.129.16#53 Non-authoritative answer: Name: cloudera.com Address: 96.17.180.42 Name: cloudera.com Address: 96.17.180.51 Name: cloudera.com raj [ ~/test ]$ dig cloudera.com ; <<>> DiG 9.16.48 <<>> cloudera.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 27976 ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1
0 Comments