Friday, 1 September 2017

Unix Shell Scripting Tutorial - page 6

Unix Shell Scripting Tutorial - page 6

Question: How can we search in file using Regular expression?
We use grep command.
grep "search string" filename.txt



Question: How to search a string in file?
grep "search string" filename.txt

search string is text searched in filename.txt. By Default its case sensitive.


Question: How to search a case-insensitive string in file?
use -i to search case insensitive.
grep -i "search string" filename.txt



Question: How to search a string in mutiple file?
separate the filename by space.
grep -i "search string" filename.txt filename2.txt filename3.txt 



Question: How to search a string in file using regular expression? Use regex character for example .* used to search all
grep -i "textname .*" filename.txt 

It will return all after the textname.


Question: What are the basic Regex characters?
  1. ? The preceding item is optional and matched at most once.
  2. * matched zero or more times.
  3. + matched one or more times.
  4. {n} matched exactly n times.
  5. {n,} matched n or more times.
  6. {,m} matched at most m times.
  7. {n,m} matched at least n times, but not more than m times.



Question: How to search a words in file?
Use -w to search words.
grep -iw "textname" filename.txt 

It will not search textnameA.


Question: How to search and return next line?
grep -A 3 -i "textname" filename.txt

It will search and return next 3 lines after search.
You can use -B to return Before 3 lines.


Question: How to search recursively in folder? Use -r to search recursive.
grep -r "textname" 



It will search in current folder and sub-folder.


Question: How to find number of matches ? Use -c to return number of matches.
grep -c "textname" fiename.txt

It will return number of matches in number.


Question: What are the Performance Tools in unix?
  1. nice/renice: Runs a program with modified scheduling priority.
  2. netstat: Prints network connections, routing tables and interface statistics.
  3. time: give resource usage
  4. uptime: This is System Load Average.
  5. ps: Current process list
  6. vmstat: Virtual Memory stats
  7. top: Top Task List



Question: What is syslog?
Syslog is a way for network devices to send event messages to a server. Also known as Syslog server.
SysLog is stored in /etc/syslogd OR /etc/syslog.
syslog.conf have following configuration.
*.err;kern.debug;auth.notice /dev/console
daemon,auth.notice           /var/log/messages
lpr.info                     /var/log/lpr.log
mail.*                       /var/log/mail.log
ftp.*                        /var/log/ftp.log
mark.*                       /dev/console



Question: What is command for logging?
logger [-i] [-f file] [-p priority] [-t tag] [message]



Thursday, 31 August 2017

Unix Shell Scripting Tutorial - page 5

Unix Shell Scripting Tutorial - page 5

Question: What are the directory structure of unix?
  1. /: This is the root directory which should contain only the directories.
  2. /bin: Executable files are located here.
  3. /dev: Device drivers
  4. /etc: configuration files, valid user lists, groups, ethernet and hosts etc
  5. /lib: shared library files and sometimes other kernel-related files.
  6. /boot: files for booting the system.
  7. /home: home directory for users and other accounts
  8. /mnt: Used to mount other temporary file systems like CD/pendrive
  9. /proc: Contains all processes marked as a file by process number.
  10. /tmp: Holds temporary files used between system boots.
  11. /user: Used for miscellaneous purposes.
  12. /var: Typically contains log and print files.
  13. /sbin: Contains binary (executable) files.
  14. /kernel: kernel files



Question: How to create a new Group?
groupadd mynewgroup



Question: How to modify group name?
groupmod -n new_modified_group_name old_group_name



Question: How to delete group?
groupdel new_modified_group_name



Question: How to add a existing user to a group?
usermod -a -G mynewgroup username

-G Its for secondary group.
-g Its for primary group.


Question: How to change a User's Primary Group?
usermod -g groupname username  



Question: How to list groups of loggined user?
groups



Question: How to list groups of other user?
groups username



Question: How to Create a New User and Assign a Group?
useradd -G examplegroup username



Question: How to assign a user multiple groups?
usermod -a -G group1,group2,group3 username