More about ls command - ls command allows you to specify wildcards - everything matches * - * can be put as many times as you want in an expression - * matches any sequence of characters of any length - ? any single character - a set of characters can be specified using [....], where .... is a set of characters. [....] matches any characters from the set - to get a list of all files ending with net ls *net - to get a list of all files starting with form followed by any (single) character followed by .doc ls form?.doc - Files starting with an uppercase alphabet ls [A-Z]* - Files starting with a digit ls [0-9]* - Files starting with digits in the range 1 to 4 ls [1234]* ls [1-4]* - Files starting with digits in the range 1 to 4 and 6 to 9 ls [1-46-9]* - Files starting with a digit followed by a lower case alphabet followed by a digit followed by any sequence ls [0-9][a-b][0-9]* - it is geberally not desirable to have filenames with unprintable characters. But if you have them by accident, you should use the od command to find out what the filename looks like - don't use filenames such as -g - command du can be used to look at the disk space taken up by a particular branch in a directory structure Regular expression - Similar to wild cards - If you are searching for a pattern, you may want to specify things like at the beginning of the line there is a string From - Any string is a regular expression itself - grep From mail/sent-mail - From is a regular expression - To get lines that start with From you type - grep "^From" mail/sent-mail - ^ indicates beginning of line - $ stands for the end of the line - The following expression look for blank lines - grep "^$" mail/sent-mail - . matches a single character similar to ? in ls - Let us store ls in a file so that we can try grep on that file ls > templist - The following command is looking for an expression which has the string form followed by any character followed by . followed by doc grep form.\.doc templist - \. to suppress special meaning of . - similarly special meaning of $ can be suppressed by \$ and so on - we can use a set of characters just like ls - [A-Z] means uppercase characters between - r* means zero or more occurrences of r - r+ means one or more occurrences of r - r? means zero or one occurrences of r - Other filters that use regular expressions are: sed and awk sed 's/^[a-z]*/xxx/' /etc/passwd | more - The above command is going to modify the passwd file at command line. Note that the passwd file doesn't get modified but the modified version is printed on the screen - sed command accepts a single line program enclosed in single quotes The program here is trying to substitute a string at the beginning of the file (^) which starts with zero or more occurrences of lowercase characters ([a-z]*) by xxx in the passwd file - sed utility is good for some quick modifications - instead of putting the program on the command line you can also put it in a file and use the file as the program sed -f program.sed input-file - Let us look at a possible use of sed in the following script sed 's/xxx/4jul220dhv/g' < 22aadt.template.cfg > 22dhv.cfg snnsbat 22dhv.cfg 22dhv.log >& 22junk rm 22dhv.cfg rm 22junk - There is a template file with xxx as a dummy string - We want to replace xxx with name of a data set to create a cfg file - s/xxx/datasetname/g allows us to replace xxx by the dataset name. The g at the end stands for global substitution - sed goes through the input file line by line and applies the program to each line - sed is useful for editing large files - or editing during a batch program - awk allows you to - 1.txt has an example of how awk can be useful ls -1 | awk '{print "chown",$1,$1}' | sh - we were in the dir /extra/city - ls -1 gave list of all the user dir in single column - awk program printed the string chown followed by first field followed by first field one more time - sample output is: chown brenda brenda - this output was directly sent to sh for execution - awk also executes the given program for each line - Each line is separated into fields. By default, the field separator is space and tab characters - common variables are: $0 - entire line $1,$2,$3,.... first,second,third field, .. NF number of fields on the current line $NF is last field on the line NR is the line number - field separator can be changed using -F option wascana/extra/pawan 128 >awk -F: '{print NR,$1}' /etc/passwd | more 1 root 2 bin 3 daemon 4 adm 5 lp 6 sync 7 shutdown