uname is a command-line utility that prints basic
information about the operating system name and system hardware.
uname Command
The uname tool is most commonly used to determine the
processor architecture, the system hostname and the version of the kernel
running on the system.
The syntax of the uname command takes the following form:
uname [OPTIONS]...
The options are as follows:
-s, (--kernel-name) - Prints the kernel name.
-n, (--nodename) - Prints the system’s node name (hostname).
This is the name the system uses when communicating over the network. When used
with the -n option, uname produces the same output as the hostname command.
-r, (--kernel-release) - Prints the kernel release.
-v, (--kernel-version) - Prints the kernel version.
-m, (--machine) - Prints the name of the machine’s hardware
name.
-p, (--processor) - Prints the architecture of the
processor.
-i, (--hardware-platform) - Prints the hardware platform.
-o, (--operating-system) - Print the name of the operating
system. On Linux systems that is “GNU/Linux”
-a, (--all) - When the -a option is used, uname behaves the
same as if the -snrvmo options have been given.
When invoked without any options, uname prints the kernel
name, as if the -s option had been specified:
uname
As you already know, the name of the kernel is “Linux”:
Linux
You don’t have to remember all the command line options.
Usually, the uname command is used with the -a option to print all available
information:
uname -a
Linux dev.linuxize.com 4.19.0-6-amd64 #1 SMP Debian
4.19.67-2+deb10u1 (2019-09-20) x86_64 GNU/Linux
The options can be combined with each other to produce the
desired output. For example, to find out what version of the Linux kernel is
running on your system, you would type the following command:
uname -srm
Linux 4.19.0-6-amd64 x86_64
When multiple options are used the information contained in
the output is in the same order as provided by the -a option. The position of
the given options doesn’t matter. Both uname -msr and uname -srm produces the
same output.
Conclusion
The uname command is used to print basic system information.
It is usually invoked with the -a option to display all available information.
If you have any questions or feedback, feel free to leave a
comm
more command is used to view the text
files in the command prompt, displaying one screen at a time in case the file
is large (For example log files). The more command also allows the user do
scroll up and down through the page. The syntax along with options and command
is as follows. Another application of more is to use it with some other command
after a pipe. When the output is large, we
can use more command to see output one by one.
Syntax:
more
[-options] [-num] [+/pattern] [+linenum] [file_name]
·[-options]: any
option that you want to use in order to change the way the file is displayed.
Choose any one from the followings: (-d, -l, -f, -p, -c, -s, -u)
·[-num]: type
the number of lines that you want to display per screen.
·[+/pattern]: replace
the pattern with any string that you want to find in the text file.
·[+linenum]: use the
line number from where you want to start displaying the text content.
·[file_name]: name of
the file containing the text that you want to display on the screen.
While
viewing the text file use these controls:
Enter
key: to scroll down line by line.
Space bar: To go to the next page.
b key: To go to back one page.
Options:
-d : Use this command in order
to help the user to navigate. It displays “[Press space to continue, ‘q’
to quit.]” and displays “[Press ‘h’ for instructions.]” when wrong key is
pressed.
Example:
more -d sample.txt
-f : This option does not wrap
the long lines and displays them as such.
Example:
more -f sample.txt
-p : This option clears the
screen and then displays the text.
Example:
more -p sample.txt
-c : This command is used to
display the pages on the same area by overlapping the previously displayed
text.
Example:
more -c sample.txt
-s : This option squeezes
multiple blank lines into one single blank line.
Example:
more -s sample.txt
-u : This option omits the
underlines.
Example:
more -u sample.txt
+/pattern : This option is used to
search the string inside your text document. You can view all the
instances by navigating through the result.
Example:
more +/reset sample.txt
+num : This option displays the
text after the specified number of lines of the document.
Example:
more +30 sample.txt
Using
more to Read Long Outputs: We use more command after a pipe to see long outputs. For example,
seeing log files, etc.
cat a.txt | more
Hope you understand about cat command if you have
any query you can comment me below in comment section. Thanks for watching see
you again in next video. Bye
Less is a command line utility that displays the contents of
a file or a command output, one page at a time. It is similar to more, but has
more advanced features and allows you to navigate both forward and backward
through the file.
When starting less doesn’t read the entire file which
results in much faster load times compared to text editors like vim or nano .
The less command is mostly used for opening large files .
How to Use Less
The general syntax for the less program is as follows:
less [OPTIONS] filename
For example, to view the content of the
/usr/share/common-licenses/GPL-3 file you would type:
less /usr/share/common-licenses/GPL-3
Less Command
You can also redirect the output from a command to less
using a pipe. For example, to view the output of the ps command page by page
you would type:
ps aux | less
Navigating Through the File Content
When opening a file which content is too large to fit in one
page, you will see a single colon (:).
To go forward to the next page press either the f key or
Space bar. If you want to move down for a specific number of lines, type the
number followed by the space or f key.
You can press either the Down arrow or Enter to scroll
forward by one line and Up arrow scroll backward by one line.
To go back to the previous page hit the b key. Move up for a
specific number of lines, by typing the number followed by the b key.
If you want to search for a pattern, type forward slash (/)
followed by the pattern you want to search. Once you hit Enter less will search
forward for matches. To search backwards use (?) followed by the search
pattern.
When the end of the file is reached, the string (END) is
shown at the bottom of the screen.
To quit less and go back to the command line press q.
Less Options
If you want less to shows line numbers launch the program
with the -N option:
less -N filename
By default, when less exits, the file contents will be
cleared from the screen. To leave file contents on screen, use the -X option:
less -X filename
The +F option tells less to watch the file contents for
changes. This is useful when opening log files.
less +F /var/log/messages
When
launched with +F, less will behave pretty much the same as tail -f .
Less Commands
The less program includes a number of commands that allows
you to navigate through the file content and search for strings. To view a full
list of all commands type h.
Most of
the commands that you can enter from the keyboard are based on those used by
both more and vi. The same action can be performed using different keys.
Below are
some of the most frequently used commands to navigate through the file content
when viewed by less:
Command
Action
Down arrow, Enter, e, or j
Move
forward one line.
Up arrow,y or k
Move
backward one line.
Space bar or f
Move
Forward one page.
b
Move
Backward one page.
/pattern
Search
forward for matching patterns.
?pattern
Search backward
for matching patterns.
n
Repeat
previous search.
N
Repeat
previous search in reverse direction.
g
Go to
the first line in the file.
Ng
Go to
the N-th line in the file.
G
Go to
the last line in the file.
p
Go to
the beginning of fthe ile.
Np
Go to N
percent into file.
h
Display
help.
q
Exit less.
Conclusion
By now you should have a good understanding of how
to use the less command.
For a complete list of all options and commands
type man
less in your terminal.
In Linux, getting a portion of text from input files is a
common operation.
There are two basic and widely used command-line utilities
to output some parts of the text from the input: the head command and the tail
command.
In this tutorial, we will discuss the typical usages of
these two commands through examples.
2. Introduction to the head and the tail Commands
Both the head and the tail commands are members of the GNU
coreutils package. They are, by default, installed in all Linux distributions.
As their names imply, the head command will output the first
part of the file, while the tail command will print the last part of the file.
Both commands write the result to standard output.
In later sections, we’ll take a closer look at each command
and learn how to use them through examples.
3. The head Command
The syntax of the head command is pretty straightforward:
head [OPTIONS] FILES
Let’s prepare a file (numbers_en.txt) as the input example
to understand the command better:
$ cat numbers_en.txt
one: 1
two: 2
three: 3
four: 4
...
ninety-seven: 97
ninety-eight: 98
ninety-nine: 99
one hundred: 100
The file contains English words of numbers from 1 to 100.
Thus, the file has 100 lines.
The head command will, by default, write the first ten lines
of the input file to the standard output:
$ head numbers_en.txt
one: 1
two: 2
three: 3
four: 4
five: 5
six: 6
seven: 7
eight: 8
nine: 9
ten: 10
3.1. Output a Specific Number of Lines
With the -n option, we can let the head command output the
first n lines instead of the default 10.
For example, if we want to have the first seven lines
printed to standard out, we’d use -n 7:
$ head -n 7 numbers_en.txt
one: 1
two: 2
three: 3
four: 4
five: 5
six: 6
seven: 7
If we pass the -n option together with a number following
the –, for example -n -x, the head command will print all lines but the last x
lines of the file.
For instance, if we want to ignore the last 97 lines from
the file, we’d do -n -97:
$ head -n -97 numbers_en.txt
one: 1
two : 2
three: 3
3.2. Output a Specific Number of Bytes
In addition to displaying text by line, the head command can
also print the file content by byte if we pass the -c option.
The usage of the -c option is the same as the -n option
except for displaying text byte-wise instead of line-wise.
Let’s see an example of displaying only the first word “one”
(3 bytes) from the file:
$ head -c 3 numbers_en.txt
one
3.3. Output Multiple Files
The head command can also handle multiple files. To see this,
let’s first prepare another input file numbers_de.txt. This file is very
similar to the numbers_en.txt. The only difference is that the words in the
file are in German instead of English:
$ cat numbers_de.txt
eins: 1
zwei: 2
drei: 3
vier: 4
...
siebenundneunzig : 97
achtundneunzig: 98
neunundneunzig: 99
(ein)hundert: 100
Now let’s output the first five lines from both files in one
shot:
$ head -n 5 numbers_en.txtnumbers_de.txt
==> numbers_en.txt <==
one: 1
two: 2
three: 3
four: 4
five: 5
==> numbers_de.txt <==
eins: 1
zwei: 2
drei: 3
vier: 4
fünf: 5
4. The tail Command
The syntax of using the tail command is quite
straightforward, too:
tail [OPTIONS] FILES
The tail command will by default write the last ten lines of
the input file to the standard output:
$ tail numbers_en.txt
ninety-one: 91
ninety-two: 92
ninety-three: 93
ninety-four: 94
ninety-five: 95
ninety-six: 96
ninety-seven: 97
ninety-eight: 98
ninety-nine: 99
one hundred: 100
4.1. Output a Specific Number of Lines
With the -n option, we can let the tail command output the
last n lines instead of the default 10.
This example shows how to get the last seven lines from the
input file:
$ tail -n 7 numbers_en.txt
ninety-four: 94
ninety-five: 95
ninety-six: 96
ninety-seven: 97
ninety-eight: 98
ninety-nine: 99
one hundred: 100
If we pass the -n option together with a number following
the “+”, for example “-n +x”, the tail command will print starting with the
x-th line till the end of the file.
Let’s print from 95th line till the end of the
numbers_en.txt file:
$ tail -n +95 numbers_en.txt
ninety-five: 95
ninety-six: 96
ninety-seven: 97
ninety-eight: 98
ninety-nine: 99
one hundred: 100
4.2. Output a Specific Number of Bytes
Similar to the head command, if we pass -c x option to the
tail command, it will output only the last x bytes from the input file.
Let’s get the last number 100 from the input file:
$ tail -c 4 numbers_en.txt
100
In the example above, we passed 4 instead of 3 to the -c
option. This is because there is an ending linebreak in the last line, and this
linebreak occupied 1 byte.
4.3. Output Multiple Files
Like the head command, the tail command can output from
multiple input files, too.
Let’s have a look at an example of printing the last five
lines from two input files:
$ tail -n 5 numbers_en.txt numbers_de.txt
==> numbers_en.txt <==
ninety-six: 96
ninety-seven: 97
ninety-eight: 98
ninety-nine: 99
one hundred: 100
==> numbers_de.txt <==
sechsundneunzig: 96
siebenundneunzig : 97
achtundneunzig: 98
neunundneunzig: 99
(ein)hundert: 100
4.4. Watch a File for Changes
Sometimes the input file we want to check is changing. For
example, a running application may append its output to a log file.
If we execute the tail command with the -f option on the
changing file, all newly added lines will be appended to standard out.
Next, let’s see a demonstration of this feature.
First, we’ll create a simple shell script append_ts.sh.
Every second it appends the current timestamp to a log file
(/tmp/timestamp.log), it will append to the log file ten times:
$ cat ./append_ts.sh
#!/bin/bash
for times in {1..10}
do
echo $(date)
>> /tmp/timestamp.log
times=$(($times+1))
sleep 1
done
Now, let’s start watching the log file by the tail command,
then we execute the script above and observe if the newly append timestamps
come to tail‘s output:
As we expected, all newly appended lines are printed out.
To exit the tail command, we can press Ctrl-C.
5. Use the head and the tail Together
We’ve learned that the head command can give us the first
part of a file, while the tail command can output the last part of the input
file. But what if we want to get some part in the middle of a file?
To solve this problem, we can combine the two simple
commands.
Let’s say we want to get from the 51st to the 55th line from
an input file:
$ tail -n +51 numbers_en.txt | head -n 5
fifty-one: 51
fifty-two: 52
fifty-three: 53
fifty-four: 54
fifty-five: 55
We can also get the same output by combining the head
command and the tail command in a different way:
$ head -n 55 numbers_en.txt | tail -n 5
6. Conclusion
The head and the tail are two simple and straightforward
Linux command-line utilities.
head and tail command in linux tutorialspoint,
head and tail command in linux geeksforgeeks,
combine head and tail command in linux,
difference between head and tail command in linux,