Tuesday 11 February 2014

Basic commands for linux

Linux commands are very useful for file handling, here I am discussing some basic commands for linux. Please download printable version here.


***** cut ****
cut command is used to extract bytes/characters/fields (separated by the delimiter) from each line when input used as a file.

Syntax
cut [-b] [-c] [-f ] [-d] [file]    (User can use any of the option with or without delimiter)

-b:  give the range of the bytes which will be returned
-c: to print characters of line by position you can use the particular position or range for example
                cut -c2 file    (Outputs the second character of every line of the file)
                cut -c1,4 file (Outputs the first & fourth character from the every line of the file)
                cut -c2-5 file (Outputs the second to fifth character of every line of the file)
                cut -c-5 file (Outputs the first five characters, b/c only last position defined )
                cut -c2- file (Outputs from second to last, because only first position defined)
-f: to print specific fields separated by delimiter like space(' '), tab('\t'), :, ; etc
-d : option for delimiter
                cut -d' ' -f3 file (Outputs third field in each line by treating space as a delimiter)
                cut -d' ' -f3,4 file (Outputs more fields, by specifying the fields positions)
                cut -d' ' -f2-5 file (Outputs fields range, by specifying the fields positions)
                cut -d' ' -f-5 file (Outputs the first five fields, b/c only last position defined )
                cut -d' ' -f2- file (Outputs second to last field, b/c only first position defined )

**** comm ****
comm command is used for comparing the two randomly sorted files line by line.

Syntax
comm [options] file1 file2

                comm file1 file2  (Outputs three column, first column contains unique in first, second column contains unique in second, and third column contains common in both files)
                comm -12 file1 file2 (Output common in both files, here -12 suppress the first and second column)
                comm -13 file1 file2 (Output unique in second file, here -13 suppress the first and third column)
                comm -23 file1 file2 (Output unique in first file, here -23 suppress the second and third column)

**** paste and cat ****
These two commands used for combining two or more files.

                paste file1 file2 file3.... (Output content of all files in a single file pasted side by side)
                cat file1 file2 file3 ...    (Output content of all files in a single file pasted below the file1 in the same order)

**** rm ****
rm command is used for removing the files.

Syntax
rm [options] file

            rm file (remove file from the file system)
            rm -i file (prompt before removing the file)
            rm -I file1 file2 file3 ... (prompt once before removing more than three files)
            rm -fr file (remove entire thing recursively)
            rm -r directory/ (remove directories and their contents recursively)

******* The End ******


No comments:

Post a Comment