0% found this document useful (0 votes)
9 views

Unix Commands

Commands of Unix Operating System
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Unix Commands

Commands of Unix Operating System
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Head Command

The head command in Unix is used to display the beginning of a file or the first few lines of output
from a command. By default, it shows the first 10 lines, but you can adjust this behavior with
options.

Basic Syntax

head [OPTION]... [FILE]...

Common Options

• -n NUM: Outputs the first NUM lines. For example, head -n 5 filename.txt will display the
first 5 lines of filename.txt.

• -c NUM: Outputs the first NUM bytes of each file. For example, head -c 20 filename.txt
will display the first 20 bytes of filename.txt.

Examples

1. Display the first 10 lines of a file:

head filename.txt

2. Display the first 5 lines of a file:

head -n 5 filename.txt

3. Display the first 20 bytes of a file:

head -c 20 filename.txt

4. Head command with a pipe to display the first 10 lines of a command's output:

ls -l | head

Tail Command

The tail command in Unix is used to display the last part of a file or the last lines of output from a
command. By default, it shows the last 10 lines of a file, but this can be customized with
options.

Basic Syntax

tail [OPTION]... [FILE]...

Common Options

• -n NUM: Outputs the last NUM lines. For example, tail -n 5 filename.txt will display the
last 5 lines of filename.txt.

• -c NUM: Outputs the last NUM bytes of each file. For example, tail -c 20 filename.txt will
display the last 20 bytes of filename.txt.
• -f: Outputs appended data as the file grows (commonly used to monitor log files in real
time). For example, tail -f logfile.log will display the end of logfile.log and update as new
entries are added.

Example

1. Display the last 10 lines of a file:

tail filename.txt

2. Display the last 5 lines of a file:

tail -n 5 filename.txt

3. Display the last 20 bytes of a file:

tail -c 20 filename.txt

4. Monitor a file (e.g., log file) in real time:

tail -f logfile.log

5. Tail command with a pipe to display the last 10 lines of a command's output:

ls -l | tail

Head and Tail Command Together using Pipe

You can use the head and tail commands together with a pipe (|) in Unix to extract specific parts
of a file or output. By combining them, you can view a range of lines from the middle of a file or
output stream.

Common Use Case: Extracting Lines from the Middle of a File

To extract lines from the middle of a file, you can first use head to get the first N lines, and then
tail to get the last M lines from that output.

Example: Extract Lines 5 to 10 from a File

head -n 10 filename.txt | tail -n 6


Process Control Commands in Unix/Linux

Process control commands in Unix are:

bg - put suspended process into background

fg - bring process into foreground

bg Command

bg is a process control command that resumes suspended process while keeping them running
in the background.

User can run a job in the background by adding a “&” symbol at the end of the command.

Syntax :

bg [job]

Example:

bg %1

Output:

The stopped job will resume operation, but remain in the background.

It will not receive any input from the terminal while it's in the background, but it will keep
running.

fg Command

fg command moves a background job in the current shell environment into the foreground.

Use the job ID parameter to indicate a specific job to be run in the foreground.

If this parameter is not supplied, the fg command uses the job most recently suspended, placed
in the background, or run as a background job.

Syntax :

fg [ %job]

Example

$ fg

Output:

It will resume the most recently suspended or background job.

$ fg 1

Output:

It brings the job with the id 1 into the foreground, resuming it if it was suspended.
PS1 and PS2 Variables:
The characters that the shell displays as your command prompt are stored in the variable PS1. You can change
this variable to be anything you want. As soon as you change it, it'll be used by the shell from that point on.

For example, if you issued the command:

$PS1='=>'
=>
=>
=>

Your prompt would become =>. To set the value of PS1 so that it shows the working directory, issue the command:

=>PS1="[\u@\h \w]\$"
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$

The result of this command is that the prompt displays the user's username, the machine's name (hostname), and
the working directory.

There are quite a few escape sequences that can be used as value arguments for PS1; try to limit yourself to the
most critical so that the prompt does not overwhelm you with information.

Escape Sequence Description

\t Current time, expressed as HH:MM:SS.

\d Current date, expressed as Weekday Month Date

\n Newline.

\s Current shell environment.

\W Working directory.

\w Full path of the working directory.

\u Current user.s username.

\h Hostname of the current machine.

Command number of the current command. Increases with each new


\#
command entered.

If the effective UID is 0 (that is, if you are logged in as root), end the prompt
\$
with the # character; otherwise, use the $.
You can make the change yourself every time you log in, or you can have the change made automatically in PS1
by adding it to your .profile file.

When you issue a command that is incomplete, the shell will display a secondary prompt and wait for you to
complete the command and hit Enter again.

The default secondary prompt is > (the greater than sign), but can be changed by re-defining the PS2shell variable:

Following is the example which uses the default secondary prompt:

$ echo "this is a
> test"
this is a
test
$

Following is the example which re-define PS2 with a customized prompt:


$ PS2="secondary prompt->"
$ echo "this is a
secondary prompt->test"
this is a
test
$

Environment Variables:
Following is the partial list of important environment variables. These variables would be set and accessed as
mentioned above:

Variable Description

DISPLAY Contains the identifier for the display that X11 programs should use by default.

Indicates the home directory of the current user: the default argument for the
HOME
cd built-in command.

Indicates the Internal Field Separator that is used by the parser for word
IFS
splitting after expansion.

LANG expands to the default system locale; LC_ALL can be used to override
LANG this. For example, if its value is pt_BR, then the language is set to (Brazilian)
Portuguese and the locale to Brazil.

On many Unix systems with a dynamic linker, contains a colon-separated list of


LD_LIBRARY_PATH directories that the dynamic linker should search for shared objects when
building a process image after exec, before searching in any other directories.

Indicates search path for commands. It is a colon-separated list of directories in


PATH
which the shell looks for commands.

PWD Indicates the current working directory as set by the cd command.

RANDOM Generates a random integer between 0 and 32,767 each time it is referenced.

Increments by one each time an instance of bash is started. This variable is


SHLVL useful for determining whether the built-in exit command ends the current
session.

TERM Refers to the display type

TZ Refers to Time zone. It can take values like GMT, AST, etc.

UID Expands to the numeric user ID of the current user, initialized at shell startup.

You might also like