Linux v Unix and Useful Unix Commands

Coding CS Learning Study

Unix is a proprietary operating system, meaning that it requires a license to use. Linux is an open source, free to use operating system derived from Unix and it is a continuation of the basis of Unix design. Linux distributions are the most famous and healthiest example of direct Unix derivatives. BSD (Berkley Software Distribution) is also an example of a Unix derivative. Linux has several features similar to Unix, but still has some key differences. While discussing the commands in Unix and Linux, they are not the same but are very much similar. Below some useful Unix commands are given.

Who :– Identifies the users currently logged in.

Description

The who command displays information about all users currently on the local system. The following information is displayed: login name, tty, date and time of login. Typing who am i or who am I displays your login name, tty, date and time you logged in. If the user is logged in from a remote machine, then the host name of that machine is displayed as well.

Example:–

Note:– x.t.e.r is IP address

bash-3.00$ who
user1 pts/5 Jun 20 11:45 (x.t.e.r)
user2 pts/6 Jun 22 12:30 (x.t.e.r)

bash-3.00$ who am i
user2 pts/24 Jun 22 13:06 (x.t.e.r)

Uname :— Displays the name of the current operating system.

Description

The uname command writes to standard output the name of the operating system that you are using.

Example:–
bash-3.00$ uname -a
UNIXTYPE CCC 3 5 123456784C00

Clear :— Clears the terminal screen.

Description

The clear command clears your screen, if possible.
bash-3.00$ clear

Cd :— Changes the current directory.

Description

The cd command sets the current working directory of a process. The user must have execute(search) permission in the specified directory.

Example:–
bash-3.00$ pwd
/system/in/byte-man

1)
bash-3.00$ cd ..
bash-3.00$ pwd
/system/in

2)
bash-3.00$ cd /DC
bash-3.00$ pwd
/DC

Ls:— Displays the contents of a directory.

Description

The ls command writes to standard output the contents of each specified Directory parameter or the name of each specified File parameter, along with any other information you ask for with the flags. If you do not specify a File or Directory parameter, the ls command displays the contents of the current directory.

bash-3.00$ pwd
/system/developer/byte-man

bash-3.00$ ls
02020 Crt.sql FILE1.txt javajar
1.ou BYTE-MAN a.sql.gz javajar.jar
1.txt query.sql text2.txt text3.txt
2.txt zipfine.gz first.txt

bash-3.00$ ls -ltr
total 7032
-rw-r–r– 1 developer data 1580 Mar 03 03:03 zipfine.gz
-rw-r–r– 1 developer data 43604 Mar 03 12:22 javajar.jar
-rw-r–r– 1 developer data 67 Apr 11 17:02 query.sql
-rw-r–r– 1 developer data 1 Jun 14 13:58 text2.txt
-rw-r–r– 1 developer data 33041 Jun 14 14:21 FILE1.txt
-rw-r–r– 1 developer data 474 Jun 14 18:59 Crt.sql
-rw-r–r– 1 developer data 0 Jun 15 15:07 1.ou
drwxr-xr-x 4 developer data 256 Jun 18 17:30 javajar
drwxrwxrwx 2 developer data 256 Jun 20 15:12 BYTE-MAN
drwxrwxrwx 2 developer data 256 Jun 20 15:12 text3.txt
-rw-r–r– 1 developer data 0 Jun 20 17:12 02020
-rw-r–r– 1 developer data 53 Jun 22 14:29 1.txt
-rw-r–r– 1 developer data 38 Jun 22 18:19 2.txt
-rw-r–r– 1 developer data 38 Dec 12 2011 first.txt

Kill :— Sends a signal to running processes.

Description

The kill command sends a signal (by default, the SIGTERM signal) to a running process. This default action normally stops processes. If you want to stop a process, specify the process ID (PID) in the ProcessID variable. The shell reports the PID of each process that is running in the background (unless you start more than one process in a pipeline, in which case the shell reports the number of the last process). You can also use the ps command to find the process ID number of commands.

Example:–

bash-3.00$ kill -9 28180
Process containing PID 28180 is killed

Grep :— find pattern parameter

Description

The grep command searches for the pattern specified by the Pattern parameter and writes each matching line to standard output. The patterns are limited regular expressions in the style of the ed or egrep command.
The grep command uses a compact non-deterministic algorithm.

Example:–

1)
bash$ ps -aef|grep processname

2)
bash-3.00$ ls -ltr|grep filename.txt
-rw-r–r– 1 system data 38 Jun 22 18:19 filename.txt

Echo :– Writes character strings to standard output.

Description

The echo command writes character strings to standard output. Strings are separated by spaces, and a new-line
character follows the last String parameter specified. If no String parameter is specified,
a blank line (new-line character) is displayed.

bash-3.00$ echo hi
hi

Echo command with single quote and double quote.

If you use single quote then it will take whole argument as a string while in double quote it will identify
command put in between `.

Example:–

1)
bash-3.00$ echo ‘today date is date
today date is date
bash-3.00$ echo “today date is date
today date is Wed Jun 22 17:13:16 IST 2011

2)
bash-3.00$ echo ‘i m who am i
i m who am i
bash-3.00$ echo “i m who am i
i m devsss pts/24 Jun 22 17:06 (x.y.z.w)

Find :— Finds files with a matching expression.

Description

The find command recursively searches the directory tree for each specified Path parameter, seeking files that match a Boolean expression written using the terms given in the following text.

Example:—

bash-3.00$ find /system/hst/gets/Files -name filename.html
/system/hst/gets/Files /foldername/filename.html
Note:– How to use find command
$ find path_list -selection_criteria filename

Man :– Displays manual entries online.

Description

The man command provides reference information on topics, such as commands, subroutines, and files.

Example:–
1)
bash-3.00$ man cd
output will be:–
Commands Reference, Volume 1, a – c
cd Command
Purpose
Changes the current directory.
Syntax
cd [directory]
or
cd [directorya directoryb]

2)
bash-3.00$ man kill
output will be
Commands Reference, Volume 3, i – m
kill Command
Purpose
Sends a signal to running processes.
Syntax
To Send Signal to Processes
kill [ -s { SignalName | SignalNumber } ] ProcessID …
Even man man command also works.

Diff :— Compares text files.

Description

The diff command compares text files.

Example:–

Take 3 files
bash-3.00$ cat compareme1.txt
123456
1234
123
1

bash-3.00$ cat compareme2.txt
123456
1234
123
1

bash-3.00$ cat compareme3.txt
123456
1

If no difference i.e both file is same then no output come

bash-3.00$ diff compareme1.txt compareme2.txt

bash-3.00$

But if different then all difference parts are displayed in output

bash-3.00$ diff compareme1.txt compareme3.txt
diff: 0653-827 Missing newline at the end of file compareme1.txt.
2,4c2

< 1234
< 123
< 1

—————–

>1

Cmp :— Compares the contents of two files and reports the first character that differs.

Description

The cmp command compares files designated by the File1 and File2 parameters and writes the results to standard output.

Example:–

Take 3 files
bash-3.00$ cat compareme1.txt
123456
1234
123
1

bash-3.00$ cat compareme2.txt
123456
1234
123
1

bash-3.00$ cat compareme3.txt
123456
1

If no difference i.e both file is same then no output come

bash-3.00$ cmp compareme1.txt compareme2.txt
bash-3.00$

But if different then first difference character position is come
bash-3.00$ cmp compareme1.txt compareme3.txt
compareme1.txt compareme3.txt differ: char 9, line 2

Date :— Displays or sets the date or time.

Description

Attention: Do not change the date when the system is running with more than one user. The date command writes the current date and time to standard output.

Example :—
bash-3.00$ date
Wed Jun 22 18:28:04 IST 2020

Df :— Reports information about space on file systems.

Description

The df command displays information about total space and available space on a file system. The FileSystem parameter specifies the name of the device on which the file system resides, the directory on which the file system is mounted, or the relative path name of a file system. The File parameter specifies a file or a directory that is not a mount point. If the File parameter is specified, the df command displays information for the file system on which the file or directory resides.

Example:—
bash-3.00$ df

Comm :— Selects or rejects lines common to two sorted files.

Description

The comm command reads the File1 and File2 parameters and writes common parts.

Example:—
bash-3.00$ cat compareme1.txt
123456
1234
123
1

bash-3.00$ cat compareme2.txt
123456
1234
123
1

bash-3.00$ cat compareme3.txt
123456
1

bash-3.00$ comm compareme1.txt compareme2.txt
123456
1234
123

bash-3.00$ comm compareme1.txt compareme3.txt
123456
1
1234
123

Cp :— Copies files.

Description

The cp command copies the source file specified by the SourceFile parameter to the destination file specified by the TargetFile parameter. If the target file exists, cp overwrites the contents, but the mode, owner, and group associated with it are not changed. The last access time of the SourceFile and the last modification time of the TargetFile are set to the time the copy was done. If the TargetFile does not exist, cp creates a new file named TargetFile that has the same mode as the source file except that the sticky bit is not set unless it was done by a superuser; the owner and group of the TargetFile is that of the user. When the TargetFile is a link to another file, cp overwrites the destination link with the content of the source file; the links from the TargetFile remains. Also, the cp command can copy the source files specified by the SourceFile parameter (or directories named by the SourceDirectory parameter) to the directory specified by the TargetDirectory parameter.

Example:–

One file “first.txt” is present.
first.txt
12345678910111213
1234 64575
123
12
1

bash-3.00$ cp first.txt 2.txt

bash-3.00$ cat 2.txt
12345678910111213
1234 64575
123
12
1

Chmod:— Changes file modes.

Description

The chmod command modifies the mode bits and the extended access control lists (ACLs) of the specified files or directories.

bash-3.00$ ls -ltr
total 24
-rw-r–r– 1 systm data 17 Jun 27 12:51 compareme2.txt
-rw-r–r– 1 systm data 14 Jun 27 13:03 compareme3.txt
-rw-r–r– 1 systm data 12 Jun 27 13:04 compareme1.txt
bash-3.00$
bash-3.00$
bash-3.00$ chmod 777 compareme2.txt
bash-3.00$ ls -ltr
total 24
-rwxrwxrwx 1 systm data 17 Jun 27 12:51 compareme2.txt
-rw-r–r– 1 systm data 14 Jun 27 13:03 compareme3.txt
-rw-r–r– 1 systm data 12 Jun 27 13:04 compareme1.txt
Note:–
For Read (r) – 4
Write(w)- 2
Execute(x)- 1
Also you can use rwx syntax with chmod
Example:–
bash-3.00$ chmod -rwxrwxrwx compareme1.txt
bash-3.00$ ls -ltr
total 24
-rwxrwxrwx 1 systm data 17 Jun 27 12:51 compareme2.txt
-rwxrwxrwx 1 systm data 14 Jun 27 13:03 compareme3.txt
———- 1 systm data 12 Jun 27 13:04 compareme1.txt
bash-3.00$ chmod +rwxrwxrwx compareme1.txt
bash-3.00$ ls -ltr
total 24
-rwxrwxrwx 1 systm data 17 Jun 27 12:51 compareme2.txt
-rwxrwxrwx 1 systm data 14 Jun 27 13:03 compareme3.txt
-rwxr-xr-x 1 systm data 12 Jun 27 13:04 compareme1.txt

Cut :— Helps split the lines of a file.

Description

The cut command cuts bytes, characters, or fields from each line of a file and writes these bytes, characters, or fields to standard output. If you do not specify the File parameter, the cut command reads standard input.

Example:—
bash-3.00$ cat first.txt
12345678910111213
1234 64575
123
12
1

bash-3.00$ cut -b 1-3 first.txt
123
123
123
12
1

Wc :– Counts the number of lines, words, bytes, or characters in a file.

Description

By default, the wc command counts the number of lines, words, and bytes in the files specified by the File parameter. The command writes the number of newline characters, words, and bytes to the standard output and keeps a total count for all named files.

Example:-

One file “first.txt” is present.
first.txt
12345678910111213
1234 64575
123
12
1

bash-3.00$ wc first.txt
5 6 38 first.txt

Here 5 —> total number of lines
6—> total number of words
38—>total number of bytes used including space and new line character and end of file

Pwd :— Displays the path name of the working directory.

Description

The pwd command writes to standard output the full path name of your current directory (from the root directory). All directories are separated by a / (slash). The root directory is represented by the first /, and the last directory named is your current directory.

Example:– If user is in directory byte-man then,
bash-3.00$ pwd
/system/developement/byte-man

Rm :— Removes (unlinks) files or directories.

Description

The rm command removes the entries for the specified File parameter from a directory. If an entry is the last link to a file, the file is then deleted. If you do not have write permission for a file and the standard input is a terminal, you are prompted with the file name and ask to confirm that you want to delete the file. If you type a y (for yes), the file is deleted, type any other character and the file is not deleted. You do not need read or write permission for the file you want to remove. However, you must have write permission for the directory containing the file.

Example:–

bash-3.00$ ls -ltr
total 24
-rw-r–r– 1 developr data 148 Feb 23 13:53 rmme.txt
-rw-r–r– 1 developr data 1583 May 30 19:05 text1.txt
-rwxrwxrwx 1 developr data 512 Jun 20 15:12 BYTE_MAN
bash-3.00$ rm -rf rmme.txt
bash-3.00$ ls -ltr
total 16
-rw-r–r– 1 developr data 1583 May 30 19:05 text1.txt
-rwxrwxrwx 1 developr data 512 Jun 20 15:12 BYTE_MAN

Sort :— Sorts files, merges files that are already sorted, and checks files to determine if they have been sorted.

Description

The sort command sorts lines in the files specified by the File parameter and writes the result to standard output. If the File parameter specifies more than one file, the sort command concatenates the files and sorts them as one file.

Example:—

bash-3.00$ ls -ltr
total 7032
-rw-r–r– 1 data grp 1580 Mar 03 03:03 a.sql.gz
-rw-r–r– 1 data grp 43604 Mar 03 12:22 jarname.jar
-rw-r–r– 1 data grp 67 Apr 11 17:02 textfilename1.txt
-rw-r–r– 1 data grp 1 Jun 14 13:58 spec.txt
-rw-r–r– 1 data grp 33041 Jun 14 14:21 TEXTFILENAME2.txt
-rw-r–r– 1 data grp 474 Jun 14 18:59 Tbl.sql
-rw-r–r– 1 data grp 0 Jun 15 15:07 1.ou
drwxr-xr-x 4 data grp 256 Jun 18 17:30 jarname
drwxrwxrwx 2 data grp 256 Jun 20 15:12 EARTH
-rw-r–r– 1 data grp 0 Jun 20 17:12 0202020
-rw-r–r– 1 data grp 53 Jun 22 14:29 1.txt
-rw-r–r– 1 data grp 38 Jun 22 18:19 2.txt
drwxrwxrwx 2 data grp 256 Jun 23 18:10 devlpmnt
-rw-r–r– 1 data grp 0 Jun 23 22:16 check.txt
-rw-r–r– 1 data grp 38 Dec 12 2011 first.txt

bash-3.00$ ls -ltr|sort -r
total 7032
drwxrwxrwx 2 data grp 256 Jun 23 18:10 devlpmnt
drwxrwxrwx 2 data grp 256 Jun 20 15:12 EARTH
drwxr-xr-x 4 data grp 256 Jun 18 17:30 jarname
-rw-r–r– 1 data grp 43604 Mar 03 12:22 jarname.jar
-rw-r–r– 1 data grp 33041 Jun 14 14:21 TEXTFILENAME2.txt
-rw-r–r– 1 data grp 1580 Mar 03 03:03 a.sql.gz
-rw-r–r– 1 data grp 474 Jun 14 18:59 Tbl.sql
-rw-r–r– 1 data grp 53 Jun 22 14:29 1.txt
-rw-r–r– 1 data grp 38 Jun 22 18:19 2.txt
-rw-r–r– 1 data grp 38 Dec 12 2011 first.txt
-rw-r–r– 1 data grp 1 Jun 14 13:58 spec.txt
-rw-r–r– 1 data grp 0 Jun 23 22:16 check.txt
-rw-r–r– 1 data grp 0 Jun 20 17:12 0202020
-rw-r–r– 1 data grp 0 Jun 15 15:07 1.ou
bash-3.00$ ls -ltr|sort -n
-rw-r–r– 1 data grp 0 Jun 15 15:07 1.ou
-rw-r–r– 1 data grp 0 Jun 20 17:12 0202020
-rw-r–r– 1 data grp 0 Jun 23 22:16 check.txt
-rw-r–r– 1 data grp 1 Jun 14 13:58 spec.txt
-rw-r–r– 1 data grp 38 Dec 12 2011 first.txt
-rw-r–r– 1 data grp 38 Jun 22 18:19 2.txt
-rw-r–r– 1 data grp 53 Jun 22 14:29 1.txt
-rw-r–r– 1 data grp 474 Jun 14 18:59 Tbl.sql
-rw-r–r– 1 data grp 1580 Mar 03 03:03 a.sql.gz
-rw-r–r– 1 data grp 33041 Jun 14 14:21 TEXTFILENAME2.txt
-rw-r–r– 1 data grp 43604 Mar 03 12:22 jarname.jar
drwxr-xr-x 4 data grp 256 Jun 18 17:30 jarname
drwxrwxrwx 2 data grp 256 Jun 20 15:12 EARTH
drwxrwxrwx 2 data grp 256 Jun 23 18:10 devlpmnt
total 7032

bash-3.00$ ls -ltr|sort -A
-rw-r–r– 1 data grp 0 Jun 15 15:07 1.ou
-rw-r–r– 1 data grp 0 Jun 20 17:12 0202020
-rw-r–r– 1 data grp 0 Jun 23 22:16 check.txt
-rw-r–r– 1 data grp 1 Jun 14 13:58 spec.txt
-rw-r–r– 1 data grp 38 Dec 12 2011 first.txt
-rw-r–r– 1 data grp 38 Jun 22 18:19 2.txt
-rw-r–r– 1 data grp 53 Jun 22 14:29 1.txt
-rw-r–r– 1 data grp 67 Apr 11 17:02 textfilename1.txt
-rw-r–r– 1 data grp 474 Jun 14 18:59 Tbl.sql
-rw-r–r– 1 data grp 1580 Mar 03 03:03 a.sql.gz
-rw-r–r– 1 data grp 33041 Jun 14 14:21 TEXTFILENAME2.txt
-rw-r–r– 1 data grp 43604 Mar 03 12:22 jarname.jar
drwxr-xr-x 4 data grp 256 Jun 18 17:30 jarname
drwxrwxrwx 2 data grp 256 Jun 20 15:12 EARTH
drwxrwxrwx 2 data grp 256 Jun 23 18:10 devlpmnt

Su :— Changes the user ID associated with a session.

Description

The su command changes user credentials to those of the root user or to the user specified by the Name parameter, and then initiates a new session.

Example:–

login as: usrname1
usrname1 ‘s password:
Last unsuccessful login: Thu Jun 23 22:15:07 IST 2020 on /d/s/20from IP
Last login: Thu Jun 23 22:15:16 IST 2020 on /d/s/20from IP
[YOU HAVE NEW MAIL]
$ bash
bash-3.00$ su – devloper
devloper ‘s Password:
[YOU HAVE NEW MAIL]
$ bash
bash-3.00$

Awk :— Finds lines in files that match a pattern and performs specified actions on those lines.

Description

The awk command utilizes a set of user-supplied instructions to compare a set of files, one line at a time, to extended regular expressions supplied by the user. Then actions are performed upon any line that matches the extended regular expressions.

Examples:—

bash-3.00$ date
Wed Jul 6 09:17:27 IST 2020

bash-3.00$ date|awk ‘{print $2 ” ” $6}’
Jul 2020

Banner :— Writes ASCII character strings in large letters to standard output.

Description

The banner command writes ASCII character Strings to standard output in large letters. Each line in the output can be up to 10 uppercase or lowercase characters in length. On output, all characters appear in uppercase, with the lowercase input characters appearing smaller than the uppercase input characters.

Cal:– Displays a calendar.

Description

The cal command displays a calendar of the specified year or month.
The Year parameter names the year for which you want a calendar. Since the cal command can display a calendar for any year from 1 through 9999, you must enter the full year rather than just the last two digits. The Month parameter identifies the month for which you want the calendar. It can be a number from 1 (indicating January) to 12 (indicating December).If you specify neither the Year nor the Month parameter, the cal command displays the current month. If you specify only one parameter, the cal command assumes the parameter is the Year parameter and displays the calendar for the indicated year.

Cat :– Concatenates or displays files.

Description

The cat command reads each File parameter in sequence and writes it to standard output. If you do not specify a file name, the cat command reads from standard input.

Example:-

One file “first.txt” is present.
bash-3.00$ cat first.txt
12345678910111213
1234 64575
123
12
1

But if some special character is present then you can not recognize presence of special character.

Let assume one file is present named 1.txt
1.txt
cdjkbgcvk_vhxfvgjhxcjjhbv bjvbfjvjhvjh_____FDSCADFHCA

bash-3.00$ cat 1.txt
cdjkbgcvkvhxfvgjhxcjjhbv bjvbfjvjhvjhFDSCADFHCA

To overcome this problem new command is used more.

More: — Displays file contents one screen at a time.

Description

The more command reads files and displays the text one screen at a time. The command pauses after each screen and prints the word More at the bottom of the screen. If you then press a carriage return, the more command displays an additional line. If you press the space bar, the more command displays another full screen of text.

Example :–

Take 1.txt file
1.txt
cdjkbgcvk_vhxfvgjhxcjjhbv bjvbfjvjhvjh_____FDSCADFHCA

bash-3.00$ more 1.txt
cdjkbgcvk^^vhxfvgjhxcjjhbv bjvbfjvjhvjh^^^^^^^^^^FDSCADFHCA

So here you will get presence of special character.

Od :– Displays files in a specified format.

Description

The od command displays the file specified by the File parameter in the format specified. If the File parameter is not given, the od command reads standard input.

Specifies the input offset base. The AddressBase variable is one of the following characters:
d —- Offset base is written in decimal.
o—- Offset base is written in octal.
x—- Offset base is written in hexadecimal.
n—- Offset base is not displayed.
One file “first.txt” is present.
first.txt
12345678910111213
1234 64575
123
12
1

bash-3.00$ od first.txt
0000000 030462 031464 032466 033470 034461 030061 030461 031061
0000020 031412 030462 031464 020066 032065 033465 005061 031063
0000040 005061 031012 030412
0000046
for hexadecimal view

bash-3.00$ od -x first.txt
0000000 3132 3334 3536 3738 3931 3031 3131 3231
0000020 330a 3132 3334 2036 3435 3735 0a31 3233
0000040 0a31 320a 310a

Paste :— Joins the lines of different files.

Description

The paste command reads input from the files specified on the command line. The command reads from standard input if a – (minus sign) appears as a file name. The command concatenates the corresponding lines of the given input files and writes the resulting lines to standard output.

Example:–
bash-3.00$ cat 1.txt
cdjkbgcvkvhxfvgjhxcjjhbv bjvbfjvjhvjhFDSCADFHCA
bash-3.00$ cat 2.txt
12345678910111213
1234 64575
123
12
1

bash-3.00$ paste 1.txt 2.txt
cdjkbgcvkvhxfvgjhxcjjhbv bjvbfjvjhvjhFDSCADFHCA 12345678910111213
1234 64575
123
12
1

Ps :— Shows current status of processes.

Description

The ps command writes the current status of active processes

bash-3.00$ ps
PID TTY TIME CMD
1486848 p/20 0:00 bash
2855008 p/20 0:00 ps
1778008 p/20 0:00 -ksh

Touch :— Updates the access and modification times of a file.

Description

The touch command updates the access and modification times of each file specified by the File parameter of each directory specified by the Directory parameter. If you do not specify a value for the Time variable, the touch command uses the current time.
Time is given in MMDDhhmm
MM-month
DD-day
hh-hour
mm-minute

Example:—

bash-3.00$ ls -ltr
total 16
-rw-r–r– 1 smsss data 1583 May 30 19:05 filename.txt
-rwxrwxrwx 1 smsss data 512 Jun 20 15:12 folder
bash-3.00$

bash-3.00$ touch 02020202 filename.txt

bash-3.00$ ls -ltr
total 16
-rw-r–r– 1 smsss data 1583 Feb 02 02:02 filename.txt
-rwxrwxrwx 1 smsss data 512 Jun 20 15:12 folder

Second example:–
If time is not specified with touch command then current time is taken

bash-3.00$ ls -ltr
total 16
-rw-r–r– 1 smsss data 1583 Feb 02 02:02 filename.txt
-rwxrwxrwx 1 smsss data 512 Jun 20 15:12 folder
bash-3.00$

bash-3.00$ touch filename.txt

bash-3.00$ ls -ltr
total 16
-rwxrwxrwx 1 smsss data 512 Jun 20 15:12 folder
-rw-r–r– 1 smsss data 1583 Jun 24 18:47 filename.txt

Third Example:–
If file is not present then touch command will create a file.
bash-3.00$ ls -ltr
total 16
-rwxrwxrwx 1 smsss data 512 Jun 20 15:12 folder
-rw-r–r– 1 smsss data 1583 Jun 24 18:47 filename.txt
bash-3.00$

bash-3.00$ touch 02020202 hi.txt

bash-3.00$ ls -ltr
total 16
-rw-r–r– 1 smsss data 0 Feb 02 02:02 hi.txt
-rwxrwxrwx 1 smsss data 512 Jun 20 15:12 folder
-rw-r–r– 1 smsss data 1583 Jun 24 18:47 filename.txt

Crontab:— Submits, edits, lists, or removes cron jobs.

Description

The crontab command submits, edits, lists, or removes cron jobs. A cron job is a command run by the cron daemon at regularly scheduled intervals.

  1. Crontab Restrictions

You can execute crontab if your name appears in the file /user/library/cron/cron.allow. If that file does not exist, you can use
crontab if your name does not appear in the file /user/library/cron/cron.deny.If only cron.deny exists and is empty,
all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

  1. Crontab Commands

export EDITOR=vi ;to specify a editor to open crontab file.

crontab -e Edit your crontab file, or create one if it doesn’t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)

  1. Crontab file

Crontab syntax :-
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.

  • * * * * command to be executed
  • – – – –
    | | | | |
    | | | | +—– day of week (0 – 6) (Sunday=0)
    | | | +——- month (1 – 12)
    | | +——— day of month (1 – 31)
    | +———– hour (0 – 23)
    +————- min (0 – 59)
  • in the value field above means all legal values as in braces for that column.
    The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges
    shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).

Note: The specification of days can be made in two fields: month day and weekday. If both are specified in an entry,
they are cumulative meaning both of the entries will get executed .

  1. Crontab Example

A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.
30 18 * * * rm /home/someuser/tmp/*

Changing the parameter values as below will cause this command to run at different time schedule below :

Min hour day month day/week Execution time
30 0 1 1,6,12 * 00:30 Hrs on 1st of Jan,June & Dec
0 20 * 10 1-5 8:00 PM every weekday(Mon-Fri) only in Oct
0 0 1,10,15 * * midnight on 1st, 10th & 15th of month
5,10 0 10 * 1 At 12:05, 12:10 every Monday & on 10th of every month

Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.

  1. Crontab Environment

cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/user/bin:/user/sbin:.
SHELL=/user/bin/sh

Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.

  1. Disable Email

By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following
command At the end of the cron job line.

/systemname/null 2>&1

  1. Generate log file

To collect the cron execution execution log in a file :

30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/logname.log

Mailx:— Sends and receives mail.

Description
The mail command invokes the mail utility, enabling you to:

  • Read incoming mail.
  • Send mail.

Examples:–
login as: developer
developer @a.b.c.d’s password:
Last unsuccessful login: Fri Jun 24 17:17:39 IST 2020 on ssh from w.x.y.z.
Last login: Mon Jun 27 13:41:52 IST 2020 on /dev/pts/10 from e.f.g.h


  • *
  • *
  • This is BYTE-MAN Server *
  • *
  • *

[YOU HAVE NEW MAIL]
$
When you login then sometimes message will come “YOU HAVE NEW MAIL”. Reason behind this message is cron. Cron will send message to user for informing him about execution of job.
To view own mailbox command is mailx
bash-3.00$ mailx
Mail [2.2 XYZ] [UNIXTYPE VERSION.X] Type ? for help.
“/XY/AB/mail/developer “: 34 messages 2 new 34 unread
U 1 developer Sun Dec 21 11:35 15/566
U 2 developer Sun Jan 25 11:56 15/570
U 3 developer Sun Jan 25 11:56 15/570
U 4 developer Tue Feb 10 16:39 15/570
U 5 developer Tue Feb 10 16:39 15/570
U 6 developer Mon Mar 16 21:32 15/606
U 7 developer Fri May 22 17:07 15/592
U 8 developer Thu May 28 16:10 15/576
U 9 developer Thu May 28 16:10 15/576
U 10 developer Thu May 28 20:29 15/576
U 11 developer Thu May 28 20:29 15/576
U 12 developer Fri Jul 17 19:52 15/580
U 13 developer Fri Jul 17 19:52 15/580
U 14 developer Fri Jul 17 19:52 15/580
U 15 developer Wed Sep 9 13:00 15/618
U 16 developer Wed Sep 9 13:00 15/616
U 17 developer Wed Sep 9 13:00 15/618
U 18 developer Fri Jan 29 19:04 15/590
U 19 developer Fri Jan 29 19:04 15/590
U 20 developer Sat May 15 15:35 15/568
U 21 developer Fri Jun 4 14:56 15/558
U 22 developer Fri Jun 25 15:10 15/556
U 23 developer Fri Jun 25 15:10 15/556
U 24 developer Wed Aug 4 20:19 15/560
U 25 developer Wed Aug 4 20:19 15/560
U 26 developer Sat Aug 21 23:42 15/580
U 27 developer Sat Aug 21 23:42 15/580
U 28 developer Thu Sep 9 13:56 15/582
U 29 developer Mon Nov 8 20:58 15/586
U 30 developer Mon Nov 8 20:58 15/586
U 31 developer Tue Nov 23 16:17 15/588
U 32 developer Fri Mar 11 14:08 15/616
?
Now U means unread message
Now after ? put number of mail wanted to see.
Example:—
? 31
Message 31:
From developer Tue Nov 23 16:17:09 2010
Date: Tue, 23 Nov 2019 16:17:08 +0530
From: developer
To: developer
A copy of your file logfile.log was saved when the editor was killed.
This file can be retrieved using the recover command of the editor.
An easy way to do this is to give the command:”ex -r logfile.log”.
This works for the “edit” and “vi” editors also.
?
For exit from mailbox put q
? q
Saved 2 messages in /system/developer //mbox
Held 33 messages in /XY/AB/mail/developer
For sending a message to another user assumed “anotherusr”
bash-3.00$ mailx anotherusr
Subject: “Send Mail”
Hi this mail is sending from developer
(Interrupt — one more to kill letter)
bash-3.00$
bash-3.00$
bash-3.00$ mailx anotherusr
Subject: “Send Mail”
Hi this mail is sending from developer
Read all message carefully. (Note:– for stop message use command [Cntr+d]
Cc: developer
In developer account
bash-3.00$ mailx
Mail [2.2 XYZ] [UNIXTYPE VERSION.X] Type ? for help.
“/XY/AB/mail/developer “: 33 messages 1 new 33 unread
U 1 developer Sun Dec 21 11:35 15/566
U 2 developer Sun Jan 25 11:56 15/570
U 3 developer Sun Jan 25 11:56 15/570
U 4 developer Tue Feb 10 16:39 15/570
U 5 developer Tue Feb 10 16:39 15/570
U 6 developer Mon Mar 16 21:32 15/606
U 7 developer Fri May 22 17:07 15/592
U 8 developer Thu May 28 16:10 15/576
U 9 developer Thu May 28 16:10 15/576
U 10 developer Thu May 28 20:29 15/576
U 11 developer Thu May 28 20:29 15/576
U 12 developer Fri Jul 17 19:52 15/580
U 13 developer Fri Jul 17 19:52 15/580
U 14 developer Fri Jul 17 19:52 15/580
U 15 developer Wed Sep 9 13:00 15/618
U 16 developer Wed Sep 9 13:00 15/616
U 17 developer Wed Sep 9 13:00 15/618
U 18 developer Fri Jan 29 19:04 15/590
U 19 developer Fri Jan 29 19:04 15/590
U 20 developer Sat May 15 15:35 15/568
U 21 developer Fri Jun 4 14:56 15/558
U 22 developer Fri Jun 25 15:10 15/556
U 23 developer Fri Jun 25 15:10 15/556
U 24 developer Wed Aug 4 20:19 15/560
U 25 developer Wed Aug 4 20:19 15/560
U 26 developer Sat Aug 21 23:42 15/580
U 27 developer Sat Aug 21 23:42 15/580
U 28 developer Thu Sep 9 13:56 15/582
U 29 developer Mon Nov 8 20:58 15/586
U 30 developer Mon Nov 8 20:58 15/586
U 31 developer Fri Mar 11 14:08 15/616
U 32 developer Thu Jun 23 18:09 15/554

N 33 developer Mon Jun 27 14:47 14/398 “”Send Mail””
In anotherusr mailbox
Mail [2.2 XYZ] [UNIXTYPE VERSION.X]Type ? for help.
“/XY/AB/mail/developer “: 153 messages 1 new 153 unread
U121 daemon Mon Jun 1 00:31 2054/59270 “Output from cron job a/b”
U122 daemon Tue Jun 2 00:31 2054/59273 “Output from cron job a/b”
U123 daemon Wed Jun 3 00:31 2054/59276 “Output from cron job a/b”
U124 daemon Thu Jun 4 00:31 2054/59274 “Output from cron job a/b”
U125 daemon Fri Jun 5 00:31 2054/59276 “Output from cron job a/b”
U126 daemon Sat Jun 6 00:31 2054/59278 “Output from cron job a/b”
U127 daemon Sun Jun 7 00:31 2054/59278 “Output from cron job a/b”
U128 daemon Mon Jun 8 00:31 2054/59278 “Output from cron job a/b”
U129 daemon Tue Jun 9 00:31 2054/59283 “Output from cron job a/b”
U130 daemon Wed Jun 10 00:31 2054/59285 “Output from cron job a/b”
U131 daemon Thu Jun 11 00:31 2054/59277 “Output from cron job a/b”
U132 daemon Fri Jun 12 00:31 2054/59287 “Output from cron job a/b”
U133 daemon Sat Jun 13 00:31 2054/59295 “Output from cron job a/b”
U134 daemon Sun Jun 14 00:31 2054/59295 “Output from cron job a/b”
U135 daemon Mon Jun 15 00:31 2054/59295 “Output from cron job a/b”
U136 daemon Tue Jun 16 00:32 2054/59292 “Output from cron job a/b”
U137 daemon Wed Jun 17 00:32 2054/59292 “Output from cron job a/b”
U138 daemon Thu Jun 18 00:31 2054/59292 “Output from cron job a/b”
U139 daemon Fri Jun 19 00:31 2054/59292 “Output from cron job a/b”
U140 daemon Sat Jun 20 00:31 2054/59291 “Output from cron job a/b”
U141 daemon Sun Jun 21 00:31 2054/59291 “Output from cron job a/b”
U142 daemon Mon Jun 22 00:32 2054/59291 “Output from cron job a/b”
U143 anotherusr Fri Jan 15 21:48 15/616
U144 root Sat Jun 19 00:35 15/536
U145 anotherusr Mon Jun 21 16:33 15/556
U146 anotherusr Mon Jun 21 16:33 15/554
U147 anotherusr Wed Oct 13 15:15 15/552
U148 anotherusr Wed Oct 13 15:15 15/552
U149 anotherusr Tue Feb 8 13:07 15/590
U150 anotherusr Tue Feb 8 13:07 15/588
U151 anotherusr Thu Apr 7 17:12 15/584
U152 developer Tue Jun 14 19:02 17/362 “”BYTE””
N153 developer Mon Jun 27 14:47 14/398 “”Send Mail””
? 153
Message 153:
From developer Mon Jun 27 14:47:04 2020
Date: Mon, 27 Jun 2020 14:47:04 +0530
From: developer
To: anotherusr
Subject: “Send Mail”
Cc: developer
Hi this mail is sending from developer
Read all message carefully.
?
Now sending message with attachment
bash-3.00$ cd /system/developer/byte-man
bash-3.00$ ls -ltr
total 7032
drwxrwxrwx 2 developer data 256 Jun 27 14:30 Folder
-rw-r–r– 1 developer data 38 Dec 12 2020 first.txt
bash-3.00$ mailx anotherusr < first.txt
so here Mail attached with first.txt is gone in anotherusr mailbox without subject.
In anotherusr mailbox,
U150 anotherusr Tue Feb 8 13:07 15/588
U151 anotherusr Thu Apr 7 17:12 15/584
U152 developer Tue Jun 14 19:02 17/362 “”BYTE””
N153 developer Mon Jun 27 14:53 15/344
? 153
Message 153:
From developer Mon Jun 27 14:53:00 2020
Date: Mon, 27 Jun 2020 14:53:00 +0530
From: developer
To: anotherusr
12345678910111213
1234 64575
123
12
1
?
Now second way to send a mail to anotherusr with mail subject and attachment
bash-3.00$ mailx -s “With msgbody” anotherusr < first.txt
Now also put Cc using -c as below shown example.
bash-3.00$ mailx -s “With msgbody” -c developer anotherusr < first.txt

Netstat:— Shows network status.

Description

The netstat command symbolically displays the contents of various network-related data structures for active connections.

Example:—
bash-3.00$ netstat
Active Internet connections
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp4 0 0 XYZ.365 . CLOSED
tcp4 0 0 loopback.24706 loopback.24707 ESTABLISHED
tcp4 0 0 loopback.24707 loopback.24706 ESTABLISHED
tcp4 0 0 loopback.24706 loopback.24712 ESTABLISHED
tcp4 0 0 loopback.24712 loopback.24706 ESTABLISHED
tcp4 0 0 loopback.24783 loopback.24784 ESTABLISHED
tcp4 0 0 loopback.24784 loopback.24783 ESTABLISHED
tcp4 0 0 loopback.24783 loopback.24788 ESTABLISHED
tcp4 0 0 loopback.24788 loopback.24783 ESTABLISHED

Ftp:— Transfers files between a local and a remote host.

Description

The ftp command uses the File Transfer Protocol (FTP) to transfer files between the local host and a remote host or between two remote hosts.

Example:—

bash-3.00$ ftp a.b.c.d
Connected to a.b.c.d
220 SYSTEM FTP server (Version XYZ Fri Jun TIMESSTAMP IST 2020) ready.
Name (W.X.Y.Z:sytem ): was(login id)
331 Password required for was
Password:
230-Last unsuccessful login: Fri Jun 24 TIMESSTAMP IST 2020 on ssh from A.B.C.D
230-Last login: Fri Jun TIMESSTAMP IST 2020 on ftp from ssa
230 User BYTE-MAN logged in.
ftp> cd /filepath
250 CWD command successful.
ftp> get logname1.txt
200 PORT command successful.
150 Opening data connection for logname1.txt(30 bytes).
226 Transfer complete.
31 bytes received in 0.008424 seconds (3.594 Kbytes/s)
local: logname1.txt log remote: logname1.txt
ftp> quit
221 Goodbye.
bash-3.00$ pwd
/sys/development/byte-man/folder
bash-3.00$ ls -ltr
total 32
-rw-r–r– 1 system data 30 Jun 24 19:40 logname1.txt