Find Grep and Vi in Aix
Find Grep and Vi in Aix
Remember one of the field for ls -l output is, the number of links the file have? How many links to the file has essentially determines by how many names the file is accessible. When a file has 2 links, it is not physically present at 2 places but can be referred by either of the names. Use?? If you accidentally delete a file with only one link, there is no brining it back. So if your file has two links, your file is safe even if one of the links get severed. How to create a link? $ ln file_name link_file_name One more advantage: If a file need to be shared between so many users, instead of giving each user a copy, create links of the file in each users directory. This avoids un-necessary duplication. unBy default any new file created has one link, but for a new directory has 2 links. Why? Because whenever we create a directory two entries for this directory are made, one in its parent directory and the second entry in the new directory itself in the form of .
Diff command Take the different between two files. $diff -bw fl1 fl2 To know the login name of user: $logname To know the user identity and group identity values: $id To find the name of Unix system: $uname $uname Also try uname n (tells hostname) ; uname a What this gives? To see who all are currently logged in: $ who What does who am I gives? To display current date and time: $ date
Want more variety on the display format: $ date +DATE: %d-%m-%y %n TIME: %H:%M:%S %d-%m%D=>date as mm/dd/yy %a=>abbreviated weekday (Sun-Sat) (Sun%h=>abbreviated month (Jan-Dec) (Jan%j=>day of year (001-366) (001%w=>day of week (0-6) Sunday is 0 (0%T=>time as HH:MM:SS
Commands Cond
grep: grep command searches specified input globally for a match with the supplied pattern and display it.
grep points citi_lsp grep points citi_lsp amex_lsp # Search a pattern in more than one file grep total points citi_lsp # if pattern comprises of more than one word use grep -i points amex_lsp # Ignores the case grep -n points amex_lsp #Print numbers of lines in which pattern was matched grep points *lsp # searches in all the files end with lsp grep [Rr]esponse citi_lsp # Searches for response or Response grep p??r citi_lsp #Search for all 4 letter words start with p and end with r grep v points amex_lsp #Searches for line without the pattern points. grep c pts mylsp #Prints only the number of matches, without quoting the text grep l pts *lsp #Prints only the files which has match without quoting the text
Commands Cond
find command: Helps in locating the files which meet the search criteria
find . name mylsp print # Search the file mylsp from current dir, downwards find . -name [ab]* find /usr -type d -name backup # Search for backup dir from /usr downwards find /usr type f name backup # Search for backup file from /usr downwards find /usr/learner links 2 # files with 2 links find /usr/learner links +2 # Files with more than 2 links find . perm 744 #All files from the current dir downwards with permissions 744 find . atime 7 print # All files from the current dir downwards which were accessed exactly 7 days back find . atime +7 print # All files from the current dir downwards which were accessed more than 7 days ago find . mtime +7 print #Files which have not been modified since last 7 days (or in other words, which have been modified more than 7 days ago) find . name [ab]* -exec grep values {} \; # What is this?
Commands Cond..
Head command: Use to view the lines of a file from the begin.
head file_name # By default prints the first 10 lines head -15 file_name # Prints first 15 lines head -1 emp_list | wc c What will be output? tail file_name # By default prints the last 10 lines tail -15 file_name # Prints last 15 lines Monitoring the growth of a file: tail f <file_name> tr abc def < myfile: Translation will take place character by character. tr /| : < emplist | head -3 What is the output? head -3 emplist | tr [a-z] [A-Z] [a- [A$ who | grep shailesh $ ls | grep file1 $ who | wc l $ cat poetry | grep love
Tail command: Counterpart of head command. View the lines from the end
Tr command: Translates the characters. Does not accept file name as arg.
To change your login password: $ passwd To print a message in large letters: $ banner welcome to unix Count the number of lines, words and characters in a file:
wc -l file_name Gives the number of lines in the file wc -w file_name Gives the number of words in the file wc -c file_name Gives the number of words in the file
Please store the following in a file:emp_list 1023|Sourav Ganguly |10|General Manager |45000 1024|Sachin Tendulkar |20|Vice President |50000 1000|Kapil Dev |10|Marketing Head |34000 4235|Rahul Dravid |30|Insurance VP |28000 2345|Anil Kumble |40|Administration VP |46000 6345|V.V.S Laxman |30|Marketing Head |34000
VI (Visual) Editor
Very fast and powerful editor. To create a new file or open existing file: vi filename vi has two modes: the command mode and the insert mode. It is mode. essential that you know which mode you are in at any given point in time. When you are in command mode, letters of the keyboard will be interpreted as commands. When you are in insert mode the same letters of the keyboard will type or edit text. vi always starts out in command mode. Entering text: To begin entering text in an empty file, you must first change from the command mode to the insert mode. To do this, type the letter i. Any time to come back to command mode press ESC button. Moving One Character at a Time h: left one space l: right one space j: down one space k: up one space
VI Cond
w: moves the cursor forward one word. So what 5w does? b: moves the cursor backward one word (if in the middle of a word, b will move you to the beginning of the current word). What 12b will do? e: moves to the end of a word.
Shotcuts: Two short cuts for moving quickly on a line include the $ and the 0 (zero) keys. The $ key will move you to the end of a line, while the 0 will move you quickly to the beginning of the line. Screen movement:
ctrlctrl-f: scrolls down one screen ctrlctrl-b: scrolls up one screen ctrlctrl-u: scrolls up a half a screen ctrlctrl-d: scrolls down a half a screen
VI Cond..
Searching for text: When you are in command mode, type a / followed the text you wish to search for. When you press Return , the cursor will move to the first incidence of that string of text. You can repeat the search by typing n or search in a backwards direction by using N . Basic Editing:
Deleting Characters, Words and Lines: x: deletes the character under the cursor. What 10x does? X: deletes the character to the left of your cursor. dw: deletes from the character selected to the end of the word. dd: deletes all the current line. What 5dd does? D: deletes from the current character to the end of the line. Pasting text using Put: The Put command will paste in the last portion of text that was deleted since deleted text is stored in a buffer. To use this command, place the cursor where you wish the deleted text to appear. Then use p to reinsert the text. What is I use P?
VI Cond
"[a"[a-z]nyy: yank next n lines into named buffer [a-z] [a"[a"[a-z]p/P: place the contents of selected buffer below/above the current line r: replaces the current character with the next character you enter/type. Once you enter the character you are returned to command mode. R: puts you in overtype mode until you hit ESC which will then return you to command mode. cw: changes and replaces the current word with text that you type. A dollar sign marks the end of the text you're changing. Pressing ESC when you finish will return you to command mode. mode.
Joining Lines: place your cursor on the first line to be joined and type J . As with other commands, you can precede J with a number to join multiple lines (4J joins 4 lines)
VI Cond
Undoing: When you make a mistake you can undo it. DO NOT move the cursor from the line where you made the change. Then try using one of the following two commands:
u: undoes the last change you made anywhere in the file. Using u again will "undo the undo". U: undoes all recent changes to the current line. You can not have moved from the line to recover the original line. :w to save your file but not quit vi (this is good to do periodically in case of machine crash!). :q to quit if you haven't made any edits. :wq to quit and save edits (Can use ZZ also). :q! wipes out all edits and allows you to exit from vi. :e! reads the original file back in so that you can start over.
Repeating a command: Use dot(.) to repeat the last basic cmd issued.
VI Cond
Customizing vi environment: We can customize several options within vi using :set command.
:set wrapmargin=10 specify that you wish to have a right margin of 10. :set number . This command causes vi to display line numbers in the file you are working on. :set ic Ignore the case.
To add a blank line in between the file, place the cursor on the line where you want to insert the blank line after that, press o and type Return. Return. Appending text:
a Begin appending text, one character to the right of current cursor location A Begin appending text at the end of the current line
VI Cond
/ [string] search forward for string ? [string] search backwards for string n repeat last search N repeat search in opposite direction cw change the contents of the current word, (use ESC to stop replacement mode) c$ Replace all content to the right of cursor (exit replacement mode with ESC) c0 Replace all content to the left of cursor (exit with ESC) :1,$s/s1/s2/g global replacement of string1 with string2 r replace current character with next character typed
VI Cond..
Some Fancy Stuff: :1,10w file write lines 1 through 10 to file newfile :340,$w >> file write lines 340 through the end of the file and append to file newfile :sh escape temporarily to a shell ^d return from shell to VI :![command] execute UNIX command without leaving VI :r![command] read output of command into VI :r[filename] read filename into VI :^g list current line number