Day 5 - Basic Commands of Linux (pipe, less, more, head, tail)

Day 5 - Basic Commands of Linux (pipe, less, more, head, tail)

  • | (Pipe) -> Pipe is used to combine two or more commands such that output of one command serves as input to the next. In short, the output of each process directly as input to the next one like a pipeline.

  • less -> less is a Linux command used for filtering and viewing text files one screen page at a time. It is more advanced than the more and most commands.

    With the less command, you can read really long text files in segments without having to load the whole file. It offers a lot of options and interactive features to make your experience more satisfying.

syntax: less [options] file_path

Options

-E -> less automatically exits upon reaching the end of file.

-f -> Forces less to open non-regular files.

-F -> Exit less if the entire file can be displayed on the first screen.

-g -> Highlights the string last found using search. By default, less highlights all strings matching the last search command.

-G -> Removes all highlights from strings found using search.

-i -> Ignores case sensitivity during search.

-J -> Displays a status column on the left side of the screen. The status column shows the lines that matched the current search and any lines marked using the m or M command.

-m -> Instructs less to prompt verbosely (similar to more), showing the percentage into the file. By default, less prompts with a colon.

-M -> Instructs less to prompt even more verbosely than more.

-n -> Removes line numbers from the screen.

-N -> Displays line numbers at the beginning of each line.

-o[file_name] -> Causes less to copy its input to the specified file. This option applies only when the input file is a pipe (|), not an ordinary file. For existing files, less asks for confirmation before overwriting the file.

-p[pattern] -> Instruct less to start at the first occurrence of the specified pattern in the input file.

-Q -> Enforces quiet operation that silences the terminal bell.

-s -> Merges consecutive blank lines into a single blank line.

-X -> Disable clearing the screen after quitting less.
-z[n] -> Changes the default scrolling window size to the specified n lines.

Show Line Numbers With the less Command:

To move forward a line at a time, press the Down key or Space, q for quit.

To move backward by a line, press the Up key.

To move forward by a page, press B. To move forward several lines, hit B, then type the number of lines.

To move backward by a page, press D. To move backward by a number of lines, type D, then the number of lines you want to go back by.

  • more -> The result looks just like that of the less command. There is a difference though, at the bottom left of the screen you will notice that more displays the percentage of the text file, and that number increases or decreases as you move across the file.

Navigating the file with the more command is similar to the less command. You use the Enter key to move to the next line, D to move to a new page, and B to go back by one page.

Display the First N Lines of a File Just like the head command in Linux, you can use more to view the first few parts of a file.

  • head -> head command is a command-line utility, which prints the first 10 lines of the specified files.By default, it prints the first 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name. We can change the number of lines the head command prints by using the -n command line option.

syntax: head [option] file_name

-n num: Prints the first num lines instead of first 10 lines. num is mandatory to be specified in command otherwise it displays an error.

-c num: Prints the first num bytes from the file specified. Newline count as a single character, so if head prints out a newline, it will count it as a byte.

if we need 1-15 line print and use pipe | for manipulate with numbers

-v option: By using this option, data from the specified file is always preceded by its file name.

-q option: It is used if more than 1 file is given. Because of this command, data from each file is not precedes by its file name.

  • tail command: It is the complementary of head command.The tail command, as the name implies, print the last N number of data of the given input. By default it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is precedes by its file name.

Syntax: tail [option] file_name

Without any option it display only the last 10 lines of the file specified.

  • -n num option: Prints the last ‘num’ lines instead of last 10 lines. num is mandatory to be specified in command otherwise it displays an error. This command can also be written as without symbolizing ‘n’ character but ‘-‘ sign is mandatory.

Tail command also comes with an ‘+’ option which is not present in the head command. With this option tail command prints the data starting from specified line number of the file instead of end. For command: tail +n file_name, data will start printing from line number ‘n’ till the end of the file specified.

**-c num ->**Prints the last ‘num’ bytes from the file specified. Newline count as a single character, so if tail prints out a newline, it will count it as a byte. In this option it is mandatory to write -c followed by positive or negative num depends upon the requirement. By +num, it display all the data after skipping num bytes from starting of the specified file and by -num, it display the last num bytes from the file specified.
Note: Without positive or negative sign before num, command will display the last num bytes from the file specified.

-q option -> It is used if more than 1 file is given. Because of this command, data from each file is not precedes by its file name.

-f option -> This option is mainly used by system administration to monitor the growth of the log files written by many Unix program as they are running. This option shows the last ten lines of a file and will update when new lines are added.

-v option -> By using this option, data from the specified file is always preceded by its file name.

if you want 20-25 lines print


Thanks for reading to the end; I hope you gained some knowledge.❤️🙌

PARAMVEER SINGH