Linux Bash Scripting Tutorial
Regular Expression is a sequence of characters that have certain patterns of text (with meta-characters), which will be searched from larger text or file. Patterns of text is a combination of characters and meta characters. Regular expression searches a given pattern in a file or files,output mateched lines of text.Bash Regular expressions uses greedy,non-greedy algorithms to find a pattern in a given file. Regular Expressions in Bash mostly used with find,grep,awk and sed commands.
^ (caret) Beginning of the line $ (dollar) End of the line . (dot) Match single character * (asterisk) Match any number of character i.e zero or more characters [] (brackets) Enclose character set to match \ (backslash) Escape. interpret following character literally \<..\> (angel brackets, escaped) word boundaryCharacter matches
\{..\} Escaped curly brackets [::] POSIX character classes
"alnum", "alpha", "ascii", "blank", "cntrl", "digit", "graph", "lower", "print", "punct", "space", "upper", "word" or "xdigit".
In basic regular expressions the metacharacters "?", "+", "{", "|", "(", and ")" lose their special meaning; instead use the backslashed versions "\?", "\+", "\{", "\|", "\(", and "\)". s expressions.
grep command searches the files for lines containing a match for a given patterns.When it finds the pattern in a line, it copies that matched line to terminal(by default). String matching or searching algorithms try to find places where one or several strings (also called patterns) are found within a larger string (searched text)
grep command syntax:grep [OPTION]... PATTERNS [FILE]PATTERNS
PATTERNS can be a simple text or it can be a combination of text and meta characters.
FILEFILE can be single text file or multiple text files or even a directory
grep options -w searching whole word grep -w 'hello' file grep '\' file matches whole word not Ohello grep 'hello/>' file searches only for words ending in 'hello' so it matches 'Ohello' -l listing only file names grep -l 'main' *.c search file names in the current directory whose has word 'main' -r recursive search grep -r 'hello' path -C context around the matching line grep -C 2 'hello' prints 2 lines of context around each matching line. -H /dev/null how do i force to print the name of the file. grep 'fire' /etc/passwd /dev/null grep -H /etc/passwd /etc/passwd:firebird:x:977:974::/:/sbin/nologin fgrep,egrep fgrep stands for fixed grep egrep stands for extended grep
find command, finds or searches for files in the specified directories based on search condition. if search condition is ommitted displays are files in the directory and its sub directories. search condition can be a file type, time,name of the file with wild cards etc., for ex: find all graphic files, find all log files, find all files which modified yesterday etc.,
You can search a system for any type of file, including filesm directories,links,sockets,block special files,character special files and many other.You can search the system that are world-writable,world-readable,files with sticky bit set,as well as perform an action on each file found.
find command syntax:Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]default path is current directory, default expression is -print
positional options (always true): -daystart -follow -regextype
normal options (always true, specified before other expressions): -depth --help -maxdepth LEVELS -mindepth LEVELS -mount -noleaf --version -xautofs -xdev -ignore_readdir_race -noignore_readdir_race
expression may consists of operators, options, tests and actions
operators
tests
actions
Find all files modified in last seven days
find . -mtime -7 | tra -cvf /dev/rmt0
Find all files that have sticky bit set by using -perm -1000 -type f
find / -perm 1000 -type f This find command searches the entire system, starting in the root directory, for files of type file that have minimum permissions of the sticky bit se
Find all files that are world-writable
find / -perm -2 -exec ls -l {} \; Notice that we ended the -exec switch option with a blank space followed by a backslash-semicolon, \; . This is a requirement to terminate the -exec switch option
ADS