Unix Commands
Unix Commands
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
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
head filename.txt
head -n 5 filename.txt
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
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
tail filename.txt
tail -n 5 filename.txt
tail -c 20 filename.txt
tail -f logfile.log
5. Tail command with a pipe to display the last 10 lines of a command's output:
ls -l | tail
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.
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.
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:
$ 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.
$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.
\n Newline.
\W Working directory.
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:
$ echo "this is a
> 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.
RANDOM Generates a random integer between 0 and 32,767 each time it is referenced.
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.