About Shell basics, Grep and Find commands

~Ganesh, Ranjith

Shell – also called as command interpreter

Interactive use: reads command lines from a terminal

Shell script: When we put command lines into a file, that file is called a shell script or shell program.

 

Broad types of shell – bash and csh

C shell (csh):

Especially good for working on a terminal

 

Bourne Shell (sh):

Probably used more often for shell programming.

Newer version “Bourne-again” shell (bash) combines handy interactive C shell−like features with Bourne shell syntax, and is preferred choice.

 

which :

It takes one or more arguments. For each of its arguments it prints to stdout the full path of the executables that would have been executed when this argument had been entered at the shell prompt. It does this by searching for an executable or script in the directories listed in the environment variable PATH

e.g.

which ls

/usr/bin/ls

 

To see which shell I am running…

echo $SHELL

/bin/bash  – tells which shell we are using (bash here).

 

env :

env is used to print environment variables.

Most environment variables are in capital

 

 

 

env

SHELL=/bin/bash

WSL_DISTRO_NAME=Ubuntu-20.04

NAME=DESKTOP-HABEOE2

PWD=/mnt/e/unix

LOGNAME=ganesh

MOTD_SHOWN=update-motd

HOME=/home/ganesh

LANG=C.UTF-8

 

HOME:

Gives our home directory

echo $HOME

/home/ganesh

 

PATH:

Tells all the directories in which binary files can be extracted

e.g.

echo $PATH

 

Find Command :

Used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions

find -maxdepth 2 -iname “pledge.*” -type f

./pledge.txt

./pledge.txt.bak

-maxdepth <num> ; at most <num> searches files/directories in hierarchy.

-iname searches irrespective of the case.

-type f specifies the input type is a file.

-mindepth <num> ; at least <num> searches files/directories in hierarchy

-group <gname> ; find files/directories in which group have access permission

-user <uname> ; find files/directories in which user have access permission

-size ; find files/directoriesbased on size

-delete ; to delete the found files/directories

-atime -<min> | +<min> | <min> ; find the file which accessed in at given minutes <min> | less than given minutes (-<min>) | greater than given minutes (+<min>)

-atime ; similarly for days

-ctime, -cmin ; similarly for changed files

-mtime ; similarly for modified files


-exec ; to execute shell command on founded files/directories

find -maxdepth 2 -iname “pledge.*” -type f -exec cat {} \;

find -maxdepth 2 -iname “pledge.*” -type f | xargs cat


find -empty:

./cat

./empty_file.txt

finds all empty folders and files in the entered directory or sub-directories.

 

Grep:

grep searches for PATTERNS in each FILE.  PATTERNS is one or more patterns separated by newline characters, and grep prints each line that matches a pattern.  Typically, PATTERNS should be quoted when grep is used in a shell command.

A FILE of “-” stands for standard input.   If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input.

grep <Option> <SearchText/Reg Expression> <Target file/ Path > ; general form

options:

-E ; extended regular expression.

-i ; ignore case

-v ; invert

-l ; list files which has the text/pattern

-r ; recursive search on all files in given <path>

-R ; recursive dereference (open symbolic link).

-c ; count matched text/pattern

-n ; print output with line number

-e ; search for multiple pattern

Example: grep -e “-e” -e “[a-zA-Z]*nary” <target> ; search for “-e” and string end with “nary”

-B <number> ; print <number> lines before matched line

-A <number> ; print <number> lines after matched line

-C <number> ; print <number> lines before and after matched line

-f ; take each line in a file as pattern è Example : grep -f <pattern file> <Target file/ Path>