Custom Search

Unix Basic Commands

<< Back to Main Page                    << Back to UNIX Page


Page1 Page2 Page3


date : display or set the current system date and time.
-u : Input and output values in Coordinated Universal Time (UTC), functionally equivalent to Greenwich Mean Time (GMT), instead of in local time.

Following formatting directives are used with date. + directive must be used.
%a : Abbreviated weekday name. For example, Wed.
%A : Full weekday name. For example, Wednesday.
%b : Abbreviated month name. For example, Jan.
%B : Full month name. For example, January.
%c : Current date and time representation. For example, Wed Oct 22 23:18:55 2008.
%d : Day of the month as a two-digit decimal number [01-31].
%e : Day of the month as a two-character decimal number with leading space fill [" 1"-"31" ].
%E : Year.
%H : Hour (24-hour clock) as a two-digit decimal number [00-23].
%I : Hour (12-hour clock) as a two-digit decimal number [01-12].
%j : Day of the year as a three-digit decimal number [001-366].
%m : Month as a decimal two-digit number [01-12].
%M : Minute as a decimal two-digit number [00-59].
%R : Time as %H:%M
%S : Second as a two-digit decimal number (allows for possible leap seconds) [00-61].
%u : Weekday as a one-digit decimal number [1-7 (Monday-Sunday)].
%U : Week number of the year (Sunday as the first day of the week) as a two-digit decimal number [00-53].
%V : Week number of the year (Monday as the first day of the week) as a two-digit decimal number [00-53].
%w : Weekday as a one-digit decimal number [0-6 (Sunday-Saturday)].
%x : Current date representation. For example, 10/22/08.
%X : Current time representation. For example, 23:18:55.
%y : Year without century as a two-digit decimal number [00-99].
%Y : Year with century as a four-digit decimal number [1970-2037].
%Z : Time zone name. For example, EDT.

example:
date '+Date : %x' = Date : 10/22/08
date '+%m/%d/%Y %H:%M:%S' = 10/22/2008 23:37:31

___________________________________________________________________________________________


sort : Sorts lines of all the named files together and writes the result to the specified output. It also takes standard output as an input.
Various options are available with this command.
-u : Unique: suppress all but one in each set of lines having equal keys.
-f : Fold letters. Prior to being compared, all lowercase letters are effectively converted into their uppercase equivalents, as defined by LC_CTYPE.
-i : In non-numeric comparisons, ignore all characters which are non-printable, as defined by LC_CTYPE.
For the ASCII character set, octal character codes 001 through 037 and 0177 are ignored.
-r : Reverse the sense of comparisons.
-t char : Use char as the field separator character; char is not considered to be part of a field (although it can be included in a sort key).
-b : Ignore leading blanks when determining the starting and ending positions of a restricted sort key.
If the -b option is specified before the first -k option, it is applied to all -k options.
-k keydef : The keydef argument defines a restricted sort key. space is considered as field here.
The format of this definition is
    field_start[type][,field_end[type]]

example:
1) sort -k 2,3 test.csv - this command will sort test.csv starting from field 2 to field 3.

2) test.txt contains following data
test1 50 p
test2 30 p
test3 40 p
test4 10 f

We want to sort data and get result as below.
test4 10 f
test2 30 p
test3 40 p
test1 50 p

command - sort -k 2 test.txt

___________________________________________________________________________________________


uniq : prints non-repeating and one copy of repeating lines. (Data should be sorted before using uniq command)
Various options are available with this command.
-u : Print only those lines that are not repeated.
-d : print one copy only of each repeated line.
-c : Print default result but each line is preceded by a count of occurence.

example:
data.txt is having following lines.
ABC
DEF
PQR
DEF
ABC

sort data.txt | uniq
ABC
DEF
PQR

sort data.txt | uniq -u
PQR

sort data.txt | uniq -d
ABC
DEF

sort data.txt | uniq -c
2 ABC
2 DEF
1 PQR

___________________________________________________________________________________________


cut : extracts selected fields of each line.
Various options are available with this command.
-c list : Cut based on character positions.
-f list : list is a list of fields assumed to be separated in the file by a delimiter character -d
-d char : The character following -d is the field delimiter. This option is used with -f option.

example:
cut -c 1,4,7 filename returns positions 1, 4, and 7.
cut -c 1-3,8 filename returns positions 1 through 3 and 8.
cut -c -5,10 filename returns positions 1 through 5 and 10.
cut -c 3- filename returns position 3 through last position.

test.txt contains following data.
a,b,c,d,e,f
p,q,r,x,y,z

cut -d, -f1,3 test.txt returns following output.
a,c
p,r

___________________________________________________________________________________________


tr : Translate characters.
-d : Deletes all occurrences of input characters or string.
[] : represents a group of characters
examples:
echo "abcde" | tr "b" "x" returns axcde
echo "abcde" | tr "bd" "xy" returns axcye
echo "abcde" | tr "[a-z]" "[A-Z]" returns ABCDE (translate all a to z into A to Z)
echo "ab12c" | tr "[1-9]" "*" returns ab*2c (translate 1 into *, remaining numbers are not converted as only 1 replacement character is specified)
echo "ab123" | tr "[2-9]b" "xy" returns ab1xy (translates 2 and 3 into x and y. numbers into x and b into y)

___________________________________________________________________________________________


grep : Search the input text file or standard input for lines matching a pattern.
Various options are available with this command.
-c : Only a count of matching lines is printed.
-i : Ignore uppercase/lowercase distinctions.
-l : Only the names of files with matching lines are listed.
-v : All lines but those matching are printed.
-n : Each line is preceded by its relative line number in the file starting at 1.
The line number is reset for each file searched. This option is ignored if -c, -b, -l, or -q is specified.
-q : quite mode. Does not display any output.

example:
grep "test" data.txt
- returns each line having "test" in data.txt
grep -c "test" data.txt
- returns total count of lines having "test" in file data.txt
grep -i "test" data.txt
- returns each line having "test" or "TEST" in data.txt
grep -l "test" *
- looks for each file in working directory and returns filename that has "test" in line. i.e. data.txt
grep -v "test" data.txt
- returns all lines from data.txt that does not contain "test".
grep -n "test" data.txt
- returns each line having "test" in data.txt. Each line is preceded by relative line number.
i.e. 4:test
11:test

___________________________________________________________________________________________


find : The find command recursively descends the directory hierarchy for each path name seeking files that match the expression.
    Syntax: find pathname [expression]

Various options are available with this command.
-type : Look for specified file type
  f - Regular file
  d - Directory
  l - Symbolic link
-name file : search for file for a given pattern.
-atime n : True if the file access time subtracted from the initialized time is n-1 to n multiples of 24h.
-mtime n : True if the file modification time subtracted from the initialized time is n-1 to n multiples of 24h.
-ctime n : True if time of last change of file status information subtracted from the initialized time is n-1 to n multiples of 24h.

example:
find /home/work -name test.dat (search for test.dat in /home/work directory and subtree)
find . -name "test*" (search for filenames start with test in current working directory and subtree)
find . -type d -name temp (search for temp directory in current working directory and subtree)
find $PWD -type f -name "test*" | grep -v "$PWD/.*/"
-This command will list only files from current directory ($PWD is unix system variable for current directory) that start with test.
find . -type f -name "test*" | grep -v "./.*/"
-This command retuns same result as above.
find . -type f -name "test*" -mtime +10
-This command retuns only files list from current and sub directory that start with test and older than 10 days.
find . -type f -name "test*" -mtime -10
-This command retuns only files list from current and sub directory that start with test and created/modified within 10 days.

___________________________________________________________________________________________


remsh : This command is used to execute commands/scripts from remote machine.
    Syntax: remsh host [-l username] [-n] command

By default, remsh uses the following path when executing the specified command:
 /usr/bin:/usr/ccs/bin:/usr/bin/X11:/usr/contrib/bin:/usr/local/bin
In a non-secure, or traditional environment, the remote account name must be equivalent to the originating account;
no provision is made for specifying a password with a command.

Username connecting to remote shell must be included in $HOME/.rhosts file on remote machine.

example:
machine1 : m1user
machine2 : m2user

To execute command on machine2 from machine1, m1user (user on machine1) has to be added in $HOME/.rhosts files on machine2.

execute remsh command on machine1 to run desire commands on machine2:
remsh machine2 -l m2user -n "ls -l"
remsh machine2 -l m2user -n "/home/work/test.sh"

___________________________________________________________________________________________


split : Split reads file and writes it in pieces (default 1000 lines) onto a set of output files.
The name of the first output file is name with aa appended, and so on up to zz (only ASCII letters are used, a maximum of 676 files).
If no output name is given, x is the default.
Various options are available with this command.
-l line_count : The input file is split into pieces line_count lines in size.
-n : The input file is split into pieces n lines in size. This option is same as -l line_count.
-a suffix_length : suffix_length letters are used to form the suffix of the output filenames. This option allows creation of more than 676 output files.
-b n : The input file is split into pieces n bytes in size.
-b nk : The input file is split into pieces n x 1024 bytes in size.
-b nm : The input file is split into pieces n x 1048576 bytes in size.

example:
split -2 test.txt
- This will split test.txt into multiple files having 2 lines in each file.
  Files will be created as xaa, xab, xac etc.
split -l 2 test.txt
- This is same as above.
split -200 test.txt test.txt.
- This will split test.txt into multiple files having 200 lines in each file.
  Files will be created as test.txt.aa, test.txt.ab, test.txt.ac etc.
split -200 -a 1 test.txt test.txt.
- same as above except, it creates files as test.txt.a, test.txt.b, test.txt.c etc.
split -b10m test.data test.data.
- This will split test.data file into multiple files of 10m size (10 * 1048576 bytes).


Page1 Page2 Page3


<< Back to Main Page                    << Back to UNIX Page