Showing posts with label useful linux commands. Show all posts
Showing posts with label useful linux commands. Show all posts

Saturday, July 26, 2014

find command in linux/unix

  1. Use the find command to find the file new.config.
    
         find . -name new.config -print
         
  2. Now use the find command to find all files with "file" as part of their name. Don't forget to put the wildcard specification in quotes - it won't work otherwise:
    
         find . -name 'file*' -print
         
  3. Try to find only directories with "file" as part of their name. Are there any?
    
         find . -name 'file*' -type d -print
         

rm command in linux/unix

  1. Use the rm command to delete a file. List your directory after the command completes.
    
         rm new.config
         ls
         
  2. cd to another subdirectory say, subdir2. List the directory to view its contents. Then use the "*" wildcard to remove all of the files. NOTE: using rm in this manner can be dangerous! If you are in the wrong directory, you'll remove files you didn't mean to remove. You may want to use the -i option to protect yourself from accidents.
    
         cd subdir2
         ls
         rm -i *
         ls
         
  3. Get out of the subdir2 subdirectory by using the command cd .. Now try to use rm to remove a directory. What happens?
    
         cd ..
         rm subdir3
         
  4. This time, include the -r option when you try to remove a directory. For example, try removing some subdirectory subdir3. What happens?
    
         rm -r subdir3
         ls
         

file command in linux/unix

  1. Use the file command to determine a file's type:
    
         file file1
         
  2. Now try it with a directory say, dir4:
    
         file dir4
         
  3. Finally, try it with a wildcard character;
    
         file *
         

mv command in linux/unix

  1. The mv command can be used for renaming files. Try this command and then list your files to prove that the command worked:
    
         mv my.config  new.config
         ls
         
  2. mv can be used to rename directories also. Try this command and then list your files to prove that the command worked:
    
         mv subdir4  dir4  
         ls
         
  3. The mv command is also used for moving files. Use the command below to move new.sample into a new location, then list your files to prove that the command worked:
    
         mv new.sample  dir4  
         ls
         ls dir4
         
  4. Like many other UNIX commands, mv recognizes wildcard characters. Try the command below and then list your files to prove that the command worked:
    
         mv *env* dir4  
         ls
         ls dir4
         

Copying files in Linux/Unix

  1. Copy an existing file in your current directory to another file in the current directory and then list your directory to prove that it was done:
    
         cp  file1  file2 
         ls
         
  2. Copy a file from a different directory to your current directory and then list your directory to prove that it was done:
    
         cp /etc/ifconfig my.config 
         ls
         
  3. Use the copy command with the "inquire" option. What happens when you try to copy one file to an already existing file?
    
         cp -i file1  file2  
         
  4. Use the recursive option to copy an entire subdirectory to a new subdirectory and then list both directories to prove that it worked:
    
         cp -R subdir1 subdir4 
         ls subdir1 subdir4
         
  5. The copy command accepts "wildcard" characters. Try the command below. What did it do? List the subdir1 subdirectory to find out.
    
         cp samp* subdir1 
         ls subdir1
         
  6. If you are copying a file from another location to the current directory and want its name to remain the same, you can use the shorthand "." to indicate the current directory. Try this for example and note what happens:
    
         cp /etc/ifconfig  . 
         ls
         

References

cut command in linux/unix

A filter, such as the UNIX cut command, is a program that processes an input stream of data to produce an output stream of data. The input data may be fed into the program's standard input or read from a file, and the output data may be sent to standard output or sent to a file.

The UNIX cut command is used to extract a vertical selection of columns (character position) or fields from one or more files. The syntax for extracting a selection based on a column number is:

$ cut -c n [filename(s)]

Here c tells us that we have to slice a column, and n is the number of the column.
Note : that column number start with number 1 rather than 0.

Example  1 - Cut column 1

 $ cat class

 A       Johnson    Sara
 D       Smith      Tom
 B       Jones      Mindy
 D       Anderson   Bob

         

The following UNIX cut example will extract the first column of the class file:

 $ cut -c 1 class

   A
   D
   B
   D

       
Extracting the field number
The syntax for extracting a selection based on a field number is:

 $ cut -f n [filename(s)]

where f stands for field and n represents the number of the field to extract. The default field delimiter is the tab character.

Example of field
This cut example will extract the second field of the class file and redirect standard output to the file class.lastname:

$ cut -f 2 class > class.lastname
Now seeing the file class.lastname
$ cat class.lastname
Johnson
Smith
Jones
Anderson

Changing the delimitter
We know that default delimitter is tab - what if we want to have a ":",  then we can use -d option.
Eg.:
cut -d: blah blah

More Examples

We will see the usage of cut command by considering the below text file as an example


> cat file.txt
unix or linux os
is unix good os
is linux good os

1. Write a unix/linux cut command to print characters by position?

The cut command can be used to print characters in a line by specifying the position of the characters. To print the characters in a line, use the -c option in cut command

cut -c4 file.txt
x
u
l

The above cut command prints the fourth character in each line of the file. You can print more than one character at a time by specifying the character positions in a comma separated list as shown in the below example

cut -c4,6 file.txt
xo
ui
ln

This command prints the fourth and sixth character in each line.

2.Write a unix/linux cut command to print characters by range?

You can print a range of characters in a line by specifying the start and end position of the characters.

cut -c4-7 file.txt
x or
unix
linu

The above cut command prints the characters from fourth position to the seventh position in each line. To print the first six characters in a line, omit the start position and specify only the end position.

cut -c-6 file.txt
unix o
is uni
is lin

To print the characters from tenth position to the end, specify only the start position and omit the end position.

cut -c10- file.txt
inux os
ood os
good os

If you omit the start and end positions, then the cut command prints the entire line.

cut -c- file.txt

3.Write a unix/linux cut command to print the fields using the delimiter?

You can use the cut command just as awk command to extract the fields in a file using a delimiter. The -d option in cut command can be used to specify the delimiter and -f option is used to specify the field position.

cut -d' ' -f2 file.txt
or
unix
linux

This command prints the second field in each line by treating the space as delimiter. You can print more than one field by specifying the position of the fields in a comma delimited list.

cut -d' ' -f2,3 file.txt
or linux
unix good
linux good

The above command prints the second and third field in each line.

Note: If the delimiter you specified is not exists in the line, then the cut command prints the entire line. To suppress these lines use the -s option in cut command.

4. Write a unix/linux cut command to display range of fields?

You can print a range of fields by specifying the start and end position.

cut -d' ' -f1-3 file.txt

The above command prints the first, second and third fields. To print the first three fields, you can ignore the start position and specify only the end position.

cut -d' ' -f-3 file.txt

To print the fields from second fields to last field, you can omit the last field position.

cut -d' ' -f2- file.txt

5. Write a unix/linux cut command to display the first field from /etc/passwd file?

The /etc/passwd is a delimited file and the delimiter is a colon (:). The cut command to display the first field in /etc/passwd file is

cut -d':' -f1 /etc/passwd

6. The input file contains the below text

> cat filenames.txt
logfile.dat
sum.pl
add_int.sh

Using the cut command extract the portion after the dot.

First reverse the text in each line and then apply the command on it.

rev filenames.txt | cut -d'.' -f1

References

Comparing files in Linux/Unix - diff, sdiff


diff and sdiff


  1. Read/scan the man page for diff with the command:
    
         man diff
         
  2. Use the diff command to determine the differences between two files:
    
         diff names1 names2
         
  3. Use the diff command to determine the differences between two directories.
    
         diff subdir1 dir4
         
  4. Try using sdiff to display the differences between names1 and names2:
    
         sdiff names1 names2
         
  5. Now try sdiff specifying that the screen width is 80 characters...not the default 130:
    
         sdiff -w 80 names1 names2
         

Sort command in unix


head and tail in Linux/Unix

head
  1. head displays first lines of a file.
       - defaults to first 10 lines
       - argument allows different number to be shown
  2. Display the top of a file with the command below. How many lines do you count?
    
         head  filename 
         
  3. Now try this command and note how many lines are displayed this time?
    
         head -5  filename  
         

tail
  1. tail displays the last lines of the file.
         - defaults to 10 lines, or you can specified a number.
  2. Display the same file with the tail command. How is it different and how many lines do you count?
    
         tail  filename  
         
  3. Now try this command and note how many lines are displayed this time?
    
         tail -5  filename 
         

more in linux/unix

  1. Read/scan the man page for more with the command:
    
         man more 
         
  2. Use the more command to read a file:
    
         more   filename 
         
  3. As you are reading, notice the "More" prompt at the bottom of the page.
  4. Try pressing the return key - what happens?
  5. Try pressing the space bar once - what happens?
  6. Type the letter b - what happens?
  7. Use the search forward feature to find a word say, "MARCH" by entering the command:
    
         /MARCH 
         
  8. Now use the search backward feature to find a string of words say, "black hole" by entering the command:
     
         ?black hole 
         
  9. more will continue until the end of the file is reached or until you type q for quit. Try typing q to quit.

Friday, July 25, 2014

Grep command in unix

grep searches files for string.

  1. grep syntax:
    grep [-cilnvw] "RE" [files ...]

    RE - regular expression which is string template
  2. grep options:
    Option
    Meaning
    -c display count of matching lines
    -i case insensitive search
    -l list names of files containing matching lines
    -n precede each line by its line number
    -v only display line that do no match
    -w search for the expression as a word

Regular expressions
^=match expression at the start of a line, as in ^A.
$ =match expression at the end of a line, as in A$.
\ =turn off the special meaning of the next character, as in \^.
[ ] =match any one of the enclosed characters, as in [aeiou]. Use Hyphen "-" for a range, as in [0-9].
[^ ]=match any one character except those enclosed in [ ], as in [^0-9].

.
=match a single character of any value, except end of line.
* =match zero or more of the preceding character or expression.
\{x,y\}=match x to y occurrences of the preceding.
\{x\}=match exactly x occurrences of the preceding.
\{x,\}=match x or more occurrences of the preceding.

Quoting
  • Quoting allows characters to be hidden from the shell.
    $ echo hello    world
    hello world
    
    $ echo "hello     world"
    hello     world
    
    $ echo p*
    pub pint pissed
    
    $echo "p*"
    p*
    
  • " (double quotes) - quotes spaces & wildcards
    ' (single quotes- also quotes $,^,"
    \ (backward slash) - quotes next charcter
  • \ quotes regular expression characters in grep.
 More grep examples

1. Search for the given string in a single file

The basic usage of grep command is to search for a specific string in the specified file as shown below.
Syntax:
grep "literal_string" filename

$ grep "this" demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.

2. Checking for the given string in multiple files.

Syntax:
grep "string" FILE_PATTERN

This is also a basic usage of grep command. For this example, let us copy the demo_file to demo_file1. The grep output will also include the file name in front of the line that matched the specific pattern as shown below. When the Linux shell sees the meta character, it does the expansion and gives all the files as input to grep.
$ cp demo_file demo_file1

$ grep "this" demo_*
demo_file:this line is the 1st lower case line in this file.
demo_file:Two lines above this line is empty.
demo_file:And this is the last line.
demo_file1:this line is the 1st lower case line in this file.
demo_file1:Two lines above this line is empty.
demo_file1:And this is the last line.

3. Case insensitive search using grep -i

Syntax:
grep -i "string" FILE

This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as “the”, “THE” and “The” case insensitively as shown below.
$ grep -i "the" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
And this is the last line.

4. Match regular expression in files

Syntax:
grep "REGEX" filename

This is a very powerful feature, if you can use use regular expression effectively. In the following example, it searches for all the pattern that starts with “lines” and ends with “empty” with anything in-between. i.e To search “lines[anything in-between]empty” in the demo_file.
$ grep "lines.*empty" demo_file
Two lines above this line is empty.
From documentation of grep: A regular expression may be followed by one of several repetition operators:
  • ? The preceding item is optional and matched at most once.
  • * The preceding item will be matched zero or more times.
  • + The preceding item will be matched one or more times.
  • {n} The preceding item is matched exactly n times.
  • {n,} The preceding item is matched n or more times.
  • {,m} The preceding item is matched at most m times.
  • {n,m} The preceding item is matched at least n times, but not more than m times.

5. Checking for full words, not for sub-strings using grep -w

If you want to search for a word, and to avoid it to match the substrings use -w option. Just doing out a normal search will show out all the lines.

The following example is the regular grep where it is searching for “is”. When you search for “is”, without any option it will show out “is”, “his”, “this” and everything which has the substring “is”.
$ grep -i "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
This Line Has All Its First Character Of The Word With Upper Case.
Two lines above this line is empty.
And this is the last line.

The following example is the WORD grep where it is searching only for the word “is”. Please note that this output does not contain the line “This Line Has All Its First Character Of The Word With Upper Case”, even though “is” is there in the “This”, as the following is looking only for the word “is” and not for “this”.
$ grep -iw "is" demo_file
THIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

6. Displaying lines before/after/around the match using grep -A, -B and -C

When doing a grep on a huge file, it may be useful to see some lines after the match. You might feel handy if grep can show you not only the matching lines but also the lines after/before/around the match.

Please create the following demo_text file for this example.
$ cat demo_text
4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

 * e - go to the end of the current word.
 * E - go to the end of the current WORD.
 * b - go to the previous (before) word.
 * B - go to the previous (before) WORD.
 * w - go to the next word.
 * W - go to the next WORD.

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

 * 192.168.1.1 - single WORD
 * 192.168.1.1 - seven words.

6.1 Display N lines after match

-A is the option which prints the specified N lines after the match as shown below.
Syntax:
grep -A  "string" FILENAME

The following example prints the matched line, along with the 3 lines after it.
$ grep -A 3 -i "example" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

6.2 Display N lines before match

-B is the option which prints the specified N lines before the match.
Syntax:
grep -B  "string" FILENAME

When you had option to show the N lines after match, you have the -B option for the opposite.
$ grep -B 2 "single WORD" demo_text
Example to show the difference between WORD and word

* 192.168.1.1 - single WORD

6.3 Display N lines around match

-C is the option which prints the specified N lines before the match. In some occasion you might want the match to be appeared with the lines from both the side. This options shows N lines in both the side(before & after) of match.
$ grep -C 2 "Example" demo_text
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD

7. Highlighting the search using GREP_OPTIONS

As grep prints out lines from the file by the pattern / string you had given, if you wanted it to highlight which part matches the line, then you need to follow the following way.

When you do the following export you will get the highlighting of the matched searches. In the following example, it will highlight all the this when you set the GREP_OPTIONS environment variable as shown below.
$ export GREP_OPTIONS='--color=auto' GREP_COLOR='100;8'

$ grep this demo_file
this line is the 1st lower case line in this file.
Two lines above this line is empty.
And this is the last line.

8. Searching in all files recursively using grep -r

When you want to search in all the files under the current directory and its sub directory. -r option is the one which you need to use. The following example will look for the string “ramesh” in all the files in the current directory and all it’s subdirectory.
$ grep -r "ramesh" *

9. Invert match using grep -v

You had different options to show the lines matched, to show the lines before match, and to show the lines after match, and to highlight match. So definitely You’d also want the option -v to do invert match.

When you want to display the lines which does not matches the given string/pattern, use the option -v as shown below. This example will display all the lines that did not match the word “go”.
$ grep -v "go" demo_text
4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:

WORD - WORD consists of a sequence of non-blank characters, separated with white space.
word - word consists of a sequence of letters, digits and underscores.

Example to show the difference between WORD and word

* 192.168.1.1 - single WORD
* 192.168.1.1 - seven words.

10. display the lines which does not matches all the given pattern.

Syntax:
grep -v -e "pattern" -e "pattern"

$ cat test-file.txt
a
b
c
d

$ grep -v -e "a" -e "b" -e "c" test-file.txt
d

11. Counting the number of matches using grep -c

When you want to count that how many lines matches the given pattern/string, then use the option -c.
Syntax:
grep -c "pattern" filename

$ grep -c "go" demo_text
6

When you want do find out how many lines matches the pattern
$ grep -c this demo_file
3

When you want do find out how many lines that does not match the pattern
$ grep -v -c this demo_file
4

12. Display only the file names which matches the given pattern using grep -l

If you want the grep to show out only the file names which matched the given pattern, use the -l (lower-case L) option.

When you give multiple files to the grep as input, it displays the names of file which contains the text that matches the pattern, will be very handy when you try to find some notes in your whole directory structure.
$ grep -l this demo_*
demo_file
demo_file1

13. Show only the matched string

By default grep will show the line which matches the given pattern/string, but if you want the grep to show out only the matched string of the pattern then use the -o option.

It might not be that much useful when you give the string straight forward. But it becomes very useful when you give a regex pattern and trying to see what it matches as
$ grep -o "is.*line" demo_file
is line is the 1st lower case line
is line
is is the last line

14. Show the position of match in the line

When you want grep to show the position where it matches the pattern in the file, use the following options as
Syntax:
grep -o -b "pattern" file

$ cat temp-file.txt
12345
12345

$ grep -o -b "3" temp-file.txt
2:3
8:3

Note: The output of the grep command above is not the position in the line, it is byte offset of the whole file.

15. Show line number while displaying the output using grep -n

To show the line number of file with the line matched. It does 1-based line numbering for each file. Use -n option to utilize this feature.
$ grep -n "go" demo_text
5: * e - go to the end of the current word.
6: * E - go to the end of the current WORD.
7: * b - go to the previous (before) word.
8: * B - go to the previous (before) WORD.
9: * w - go to the next word.
10: * W - go to the next WORD.

Using sed Stream Editor in Linux/Unix

Sed is a Stream Editor used for modifying the files in unix (or linux). Whenever you want to make changes to the file automatically, sed comes in handy to do this. Most people never learn its power; they just simply use sed to replace text. You can do many things apart from replacing text with sed. Here I will describe the features of sed with examples.

Consider the below text file as an input.


>cat file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.

Sed Command Examples


1. Replacing or substituting string

Sed command is mostly used to replace the text in a file. The below simple sed command replaces the word "unix" with "linux" in the file.

>sed 's/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

Here the "s" specifies the substitution operation. The "/" are delimiters. The "unix" is the search pattern and the "linux" is the replacement string.

By default, the sed command replaces the first occurrence of the pattern in each line and it won't replace the second, third...occurrence in the line.

2. Replacing the nth occurrence of a pattern in a line.

Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line. The below command replaces the second occurrence of the word "unix" with "linux" in a line.

>sed 's/unix/linux/2' file.txt
unix is great os. linux is opensource. unix is free os.
learn operating system.
unixlinux which one you choose.

3. Replacing all the occurrence of the pattern in a line.

The substitute flag /g (global replacement) specifies the sed command to replace all the occurrences of the string in the line.

>sed 's/unix/linux/g' file.txt
linux is great os. linux is opensource. linux is free os.
learn operating system.
linuxlinux which one you choose.

4. Replacing from nth occurrence to all occurrences in a line.

Use the combination of /1, /2 etc and /g to replace all the patterns from the nth occurrence of a pattern in a line. The following sed command replaces the third, fourth, fifth... "unix" word with "linux" word in a line.

>sed 's/unix/linux/3g' file.txt
unix is great os. unix is opensource. linux is free os.
learn operating system.
unixlinux which one you choose.

5. Changing the slash (/) delimiter

You can use any delimiter other than the slash. As an example if you want to change the web url to another url as

>sed 's/http:\/\//www/' file.txt

In this case the url consists the delimiter character which we used. In that case you have to escape the slash with backslash character, otherwise the substitution won't work.

Using too many backslashes makes the sed command look awkward. In this case we can change the delimiter to another character as shown in the below example.

>sed 's_http://_www_' file.txt
>sed 's|http://|www|' file.txt

6. Using & as the matched string

There might be cases where you want to search for the pattern and replace that pattern by adding some extra characters to it. In such cases & comes in handy. The & represents the matched string.

>sed 's/unix/{&}/' file.txt
{unix} is great os. unix is opensource. unix is free os.
learn operating system.
{unix}linux which one you choose.

>sed 's/unix/{&&}/' file.txt
{unixunix} is great os. unix is opensource. unix is free os.
learn operating system.
{unixunix}linux which one you choose.

7. Using \1,\2 and so on to \9

The first pair of parenthesis specified in the pattern represents the \1, the second represents the \2 and so on. The \1,\2 can be used in the replacement string to make changes to the source string. As an example, if you want to replace the word "unix" in a line with twice as the word like "unixunix" use the sed command as below.

>sed 's/\(unix\)/\1\1/' file.txt
unixunix is great os. unix is opensource. unix is free os.
learn operating system.
unixunixlinux which one you choose.

The parenthesis needs to be escaped with the backslash character. Another example is if you want to switch the words "unixlinux" as "linuxunix", the sed command is

>sed 's/\(unix\)\(linux\)/\2\1/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxunix which one you choose.

Another example is switching the first three characters in a line

>sed 's/^\(.\)\(.\)\(.\)/\3\2\1/' file.txt
inux is great os. unix is opensource. unix is free os.
aelrn operating system.
inuxlinux which one you choose.

8. Duplicating the replaced line with /p flag

The /p print flag prints the replaced line twice on the terminal. If a line does not have the search pattern and is not replaced, then the /p prints that line only once.

>sed 's/unix/linux/p' file.txt
linux is great os. unix is opensource. unix is free os.
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.
linuxlinux which one you choose.

9. Printing only the replaced lines

Use the -n option along with the /p print flag to display only the replaced lines. Here the -n option suppresses the duplicate rows generated by the /p flag and prints the replaced lines only one time.

>sed -n 's/unix/linux/p' file.txt
linux is great os. unix is opensource. unix is free os.
linuxlinux which one you choose.

If you use -n alone without /p, then the sed does not print anything.

10. Running multiple sed commands.

You can run multiple sed commands by piping the output of one sed command as input to another sed command.

>sed 's/unix/linux/' file.txt| sed 's/os/system/'
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.

Sed provides -e option to run multiple sed commands in a single sed command. The above output can be achieved in a single sed command as shown below.

>sed -e 's/unix/linux/' -e 's/os/system/' file.txt
linux is great system. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you chosysteme.

11. Replacing string on a specific line number.

You can restrict the sed command to replace the string on a specific line number. An example is

>sed '3 s/unix/linux/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

The above sed command replaces the string only on the third line.

12. Replacing string on a range of lines.

You can specify a range of line numbers to the sed command for replacing a string.

>sed '1,3 s/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

Here the sed command replaces the lines with range from 1 to 3. Another example is

>sed '2,$ s/unix/linux/' file.txt
linux is great os. unix is opensource. unix is free os.
learn operating system.
linuxlinux which one you choose.

Here $ indicates the last line in the file. So the sed command replaces the text from second line to last line in the file.

13. Replace on a lines which matches a pattern.

You can specify a pattern to the sed command to match in a line. If the pattern match occurs, then only the sed command looks for the string to be replaced and if it finds, then the sed command replaces the string.

>sed '/linux/ s/unix/centos/' file.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
centoslinux which one you choose.

Here the sed command first looks for the lines which has the pattern "linux" and then replaces the word "unix" with "centos".

14. Deleting lines.

You can delete the lines a file by specifying the line number or a range or numbers.

>sed '2 d' file.txt
>sed '5,$ d' file.txt

15. Duplicating lines

You can make the sed command to print each line of a file two times.

>sed 'p' file.txt

16. Sed as grep command

You can make sed command to work as similar to grep command.

>grep 'unix' file.txt
>sed -n '/unix/ p' file.txt

Here the sed command looks for the pattern "unix" in each line of a file and prints those lines that has the pattern.

You can also make the sed command to work as grep -v, just by using the reversing the sed with NOT (!).

>grep -v 'unix' file.txt
>sed -n '/unix/ !p' file.txt

The ! here inverts the pattern match.

17. Add a line after a match.

The sed command can add a new line after a pattern match is found. The "a" command to sed tells it to add a new line after a match is found.

>sed '/unix/ a "Add a new line"' file.txt
unix is great os. unix is opensource. unix is free os.
"Add a new line"
learn operating system.
unixlinux which one you choose.
"Add a new line"

18. Add a line before a match

The sed command can add a new line before a pattern match is found. The "i" command to sed tells it to add a new line before a match is found.

>sed '/unix/ i "Add a new line"' file.txt
"Add a new line"
unix is great os. unix is opensource. unix is free os.
learn operating system.
"Add a new line"
unixlinux which one you choose.

19. Change a line

The sed command can be used to replace an entire line with a new line. The "c" command to sed tells it to change the line.

>sed '/unix/ c "Change line"' file.txt
"Change line"
learn operating system.
"Change line"

20. Transform like tr command

The sed command can be used to convert the lower case letters to upper case letters by using the transform "y" option.

>sed 'y/ul/UL/' file.txt
Unix is great os. Unix is opensoUrce. Unix is free os.
Learn operating system.
UnixLinUx which one yoU choose.

Here the sed command transforms the alphabets "ul" into their uppercase format "UL"

xemacs editor for Linux/Unix


ed Line editor in Linux/Unix

http://pic.dhe.ibm.com/infocenter/zos/v1r13/index.jsp?topic=%2Fcom.ibm.zos.r13.bpxa500%2Fed.htm

Thursday, August 26, 2010

cat in linux


cat

  1. Read/scan the man page for cat with the command:
    
         man cat 
         
  2. Use this command to display the contents of a file. What happens?
    
         cat  filename 
         
  3. Now try this command notice the difference. How many lines are in the file?
    
         cat -n  filename 
         
  4. The cat command is more often used for purposes other than just displaying a file. Try these commands to "concatenate" two files into a new, third file:
    
         cat file1                  - first, show file1 
         cat file2                  - then, show file2 
         cat file1 file2 > newfile  - now do the actual concatenate 
         cat newfile                - finally, show the result 
         
OPTIONS:
     

-A Show all.
-b Omits line numbers for blank space in the output.
-e A $ character will be printed at the end of each line prior to a new line.
-E Displays a $ (dollar sign) at the end of each line.
-n Line numbers for all the output lines.
-s If the output has multiple empty lines it replaces it with one empty line.
-T Displays the tab characters in the output.
-v Non-printing characters (with the exception of tabs, new-lines and form-feeds) are printed visibly. 

EXAMPLE:

  1. To Create a new file:
    cat > file1.txt
    This command creates a new file file1.txt. After typing into the file press control+d (^d) simultaneously to end the file.
  2. To Append data into the file:
    cat >> file1.txt
    To append data into the same file use append operator >> to write into the file, else the file will be overwritten (i.e., all of its contents will be erased).
  3. To display a file:
    cat file1.txt
    This command displays the data in the file.
  4. To concatenate several files and display:
    cat file1.txt file2.txt
    The above cat command will concatenate the two files (file1.txt and file2.txt) and it will display the output in the screen. Some times the output may not fit the monitor screen. In such situation you can print those files in a new file or display the file using less command.
    cat file1.txt file2.txt | less
  5. To concatenate several files and to transfer the output to another file.
    cat file1.txt file2.txt > file3.txt
    In the above example the output is redirected to new file file3.txt. The cat command will create new file file3.txt and store the concatenated output into file3.txt.

ls in unix / linux

  1. Use ls without any arguments to display your current directory contents. How many files do you see?
  2. Now use ls with the -a option. How many files do you see this time? Notice that the "new" files all begin with a "dot", which indicates they are "hidden" files.
    
         ls -a
         
  3. This command is useful for distinguishing between directories, ordinary files, and executable files. Notice how its output differs from ls without arguments.
    
         ls -F
         
  4. Use the command ls -l to obtain a "long" listing of your files. Sample output from this command and an explanation of the information it provides appears below.
    
    -rwxr-xr-x   1 jsmith   staff         43 Mar 23 18:14 prog1
    -rw-r--r--   1 jsmith   staff      10030 Mar 22 20:41 sample.f
    drwxr-sr-x   2 jsmith   staff        512 Mar 23 18:07 subdir1
    drwxr-sr-x   2 jsmith   staff        512 Mar 23 18:06 subdir2
    drwxr-sr-x   2 jsmith   staff        512 Mar 23 18:06 subdir3
        1        2   3        4           5       6          7
    
    1 = access modes/permissions
    2 = number of links
    3 = owner
    4 = group
    5 = size (in bytes)
    6 = date/time of last modification
    7 = name of file  
         
  5. Recursive listings can be very useful. Try both of the commands below. What does the output tell you?
    
         ls -R
         ls -Rl
         
  6. Try three options together:
    
         ls -lFa
         
More options
-a, --all
do not hide entries starting with .
-A, --almost-all
do not list implied . and ..
--author
print the author of each file
-b, --escape
print octal escapes for nongraphic characters
--block-size=SIZE
use SIZE-byte blocks
-B, --ignore-backups
do not list implied entries ending with ~
-c
with -lt: sort by, and show, ctime (time of last modification of file status information) with -l: show ctime and sort by name otherwise: sort by ctime
-C
list entries by columns
--color[=WHEN]
control whether color is used to distinguish file types. WHEN may be `never', `always', or `auto'
-d, --directory
list directory entries instead of contents
-D, --dired
generate output designed for Emacs' dired mode
-f
do not sort, enable -aU, disable -lst
-F, --classify
append indicator (one of */=@|) to entries
--format=WORD
across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C
--full-time
like -l --time-style=full-iso
-g
like -l, but do not list owner
-G, --no-group
inhibit display of group information
-h, --human-readable
print sizes in human readable format (e.g., 1K 234M 2G)
--si
likewise, but use powers of 1000 not 1024
-H, --dereference-command-line
follow symbolic links on the command line
--indicator-style=WORD append indicator with style WORD to entry names:
none (default), classify (-F), file-type (-p)
-i, --inode
print index number of each file
-I, --ignore=PATTERN
do not list implied entries matching shell PATTERN
-k
like --block-size=1K
-l
use a long listing format (shows permission , owner, size and last modified)
-L, --dereference
when showing file information for a symbolic link, show information for the file the link references rather than for the link itself
-m
fill width with a comma separated list of entries
-n, --numeric-uid-gid
like -l, but list numeric UIDs and GIDs
-N, --literal
print raw entry names (don't treat e.g. control characters specially)
-o
like -l, but do not list group information
-p, --file-type
append indicator (one of /=@|) to entries
-q, --hide-control-chars
print ? instead of non graphic characters
--show-control-chars
show non graphic characters as-is (default unless program is `ls' and output is a terminal)
-Q, --quote-name
enclose entry names in double quotes
--quoting-style=WORD
use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape
-r, --reverse
reverse order while sorting
-R, --recursive
list subdirectories recursively
-s, --size
print size of each file, in blocks
-S
sort by file size
--sort=WORD
extension -X, none -U, size -S, time -t, version -v
status -c, time -t, atime -u, access -u, use -u
--time=WORD
show time as WORD instead of modification time: atime, access, use, ctime or status; use specified time as sort key if --sort=time
--time-style=STYLE
show times using style STYLE: full-iso, long-iso, iso, locale, +FORMAT
FORMAT is interpreted like `date'; if FORMAT is FORMAT1FORMAT2, FORMAT1 applies to non-recent files and FORMAT2 to recent files; if STYLE is prefixed with `posix-', STYLE takes effect only outside the POSIX locale
-t
sort by modification time
-T, --tabsize=COLS
assume tab stops at each COLS instead of 8
-u
with -lt: sort by, and show, access time with -l: show access time and sort by name otherwise: sort by access time
-U
do not sort; list entries in directory order
-v
sort by version
-w, --width=COLS
assume screen width instead of current value
-x
list entries by lines instead of by columns
-X
sort alphabetically by entry extension
-1
list one file per line
--help
display this help and exit
--version
output version information and exit

ps in linux

ps r: Shows only running processes.
ps f: Shows children descended from their parents in an ASCII art tree. I find this very useful when looking at problem processes. Use with the S option to see CPU information from children summed up with parents.

ps e: Shows the command environment for each process. This is useful in a situation where a program works for one user but not for another, or on one machine but not on another.
ps -t pts/3: Shows processes associated with the specified tty. I've found this useful when trying to work out who's doing what on a remote machine, and for how long.
ps u username: Generates much more readable and human friendly output.
ps -l username  : For long listing


Own output format
If you are bored by the regular output, you could simply change the format. To do so use the formatting characters which are supported by the ps command.
If you execute the ps command with the 'o' parameter you can tell the ps command what you want to see:
e.g.
$ ps -o "%u : %U : %p : %a"
RUSER    : USER     :   PID : COMMAND
kinshukc : heyne    :  3363 : bash
kinshukc : heyne    :  3367 : ps -o %u : %U : %p : %a

for more do
$man ps