0% found this document useful (0 votes)
101 views44 pages

Head

The document provides information about basic Linux commands including ls, ll, cat, grep, head, tail, less, more, and echo. It describes what each command is used for and provides examples of common syntax and options. The ls command lists files and directories. The ll command is typically an alias for ls -l. The cat command concatenates and prints file contents. The grep command searches for patterns in files. The head and tail commands print the first/last lines of files. The less and more commands page through file contents. The echo command prints arguments and is used in shell scripts.

Uploaded by

Tripti Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views44 pages

Head

The document provides information about basic Linux commands including ls, ll, cat, grep, head, tail, less, more, and echo. It describes what each command is used for and provides examples of common syntax and options. The ls command lists files and directories. The ll command is typically an alias for ls -l. The cat command concatenates and prints file contents. The grep command searches for patterns in files. The head and tail commands print the first/last lines of files. The less and more commands page through file contents. The echo command prints arguments and is used in shell scripts.

Uploaded by

Tripti Gupta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Linux

basic
commands
Check File/Folder/Content
LS TAIL
LL LESS
CAT MORE
GREP - ECHO
HEAD
LS
ls command
The ls command is the basic command used to list
files and directories within the Linux and other
Unix-based of the current directory by default.
ls
you will not be able to see file types, dates, and
permissions.
ls options ls --help
ls -l
ls path
ls -1
LL
Linux ll is not a command provided by the linux
system. It is usually the command alias we set.
Generally, the ls -l command is set to ll.
seshadri@seshadri:~$ which ll
seshadri@seshadri:~$ which ls
/usr/bin/ls
seshadri@seshadri:~$ type ll
ll is aliased to `ls -alF'
seshadri@seshadri:~$ type ls
ls is aliased to `ls --color=auto'
CAT
The cat (short for “concatenate“) command is one
of the most frequently used commands in
Linux/Unix-like operating systems. It is also
command utility in Linux. One of its most common
usages is to print the content of a file onto the
standard output stream. Other than that, the cat
command also allows us to write some texts into
a file.
cat [OPTION] [FILE]
# cat /etc/passwd

write some texts into a file


cat > filename
This is a cat test
ctrl+d

This will save the text


cat >> filename
This is appending to the existing filename
ctrl+d
This will add the text to exsting file

seshadri@seshadri:~$ cat > test << EOF


> This is a eof test
> EOF
seshadri@seshadri:~$
With the -n option you could see the line numbers of
a file song.txt in the output terminal.
# cat -n song.txt

1 "Heal The World"


2 There's A Place In
3 Your Heart
4 And I Know That It Is Love
5 And This Place Could

Multiple Files at Once


# cat test; cat test1; cat test2
GREP
grep stands for Global search for Regular
Expression and Print out
The grep filter searches a file for a particular
pattern of characters, and displays all lines
that contain that pattern. The pattern that is
searched in the file is referred to as the
regular expression.
grep [options] pattern [files]

Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the
filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the
pattern
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular
expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.
-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the
result.
-C n : Prints searched line and n lines after before
the result.
For instance, to search for a filename that contains a
string “test“, the command would be
$ ls -l | grep -i “string”
$ ls –l | grep –i test
This command lists all the files that contain the string
“test”.

$ grep “string” filename


$ grep “employee” testfile1
For instance, to search for a string “test” in a file
named testfile1, we have used the following
command
To Search Multiple Files
grep filename sample sample2 sample3
HEAD
The Linux head command reads and prints the first
N lines to standard output. By default, it prints out
the first ten lines of a file to standard output.
However, this can be modified by passing additional
arguments on the command-line. The ‘head’
command is the opposite of the tail command that
prints out the last N lines of a given file. In this
guide, we focus on the Linux head command and
feature a few use cases of the command.
Options available for Head Command in Linux
1. -n, –lines=[-]num: Displays the first num lines instead of the first
10; with the leading ‘-‘, displays all but the last num lines of each file.

2. -v, –verbose: Always display the header name when file is identified.

3. –version: For version.

4. -c, –bytes=[-]num: Displays the first num bytes of each file; with a
leading ‘-‘, displays all but the last num bytes of each file.

5. -q, –quiet, –silent: It restricts the printing of header name when file
identified.

6. –help: For help.


$ head asian_countries.txt

Using the -v flag, you can display the filename tag


before printing out the lines in the file as follows
$ head -v asian_countries.txt

Display output from multiple files


$ head asian_countries.txt europe_countries.txt

$ head -n 5 asian_countries.txt

Redirect output to a text file


$ head -n 4 asian_countries.txt > output.txt
TAIL
The tail command is typically used to list
the last ten lines in a file. It is just opposite
to what HEAD command does. If more
than one files are specified on the
command or syntax, then it give 10
results from each of them with mentioning
of file name before respectively.
TAIL [option] file(s)
Options Description
1 -n Number of line on output.
2 -c Characters bytes you want to display.
3 -f Follows -mainly used while using LOGS.
4 -v Verbose enable or disable.
5 -q Quiet.
$ tail -n 3 state.txt

+ option tail command prints the data starting


from specified line number
tail +25 state.txt

$ tail -f logfile

$ tail –help

keyword –v, the data in the file is always


displayed with the filename
LESS
The Linux less command is a command-line
tool that displays a file line by line or one
page at a time. It has faster access because
if file is large it doesn’t access the complete
file, but accesses it page by page. The less
tool is more advanced and versatile than
other terminal pagers, such as more.
Options
-E : causes less to automatically exit the first time it reaches end of
file.
-f : forces non-regular file to open.
-F : causes less to exit if entire file can be displayed on first screen
-g : highlight the string which was found by last search command
-G : suppresses all highlighting of strings found by search commands
-i : cause searches to ignore case
-n : suppresses line numbers
-p pattern : it tells less to start at the first occurrence of pattern in the
file
-s : causes consecutive blank lines to be squeezed into a single blank
line
Shortcuts Action
Down Arrow, Enter, e, j One line forward.
Up Arrow, y, k One line backward.
Space bar, Page Down One page forward.
Page Up, b One page backward.
Right Arrow Scroll right.
Left Arrow Scroll left.
Home, g Jump to the beginning of the file.
End, G Jump to the end of the file.
/[string] Search forward for the specified string.
?[string] Search backward for the specified string.
n Next match during a search.
N Previous match during a search.
q Quit less.
less [options] file_path

less /etc/updatedb.conf

less -N /etc/init/mysql.conf
It will show output along with line numbers

dmesg | less -N
To view the output of another command with less, redirect the
output from that command using a pipe

following command finds all items containing the "ERROR"


string in the mysql.conf file
less -p ERROR /etc/init/mysql.conf
MORE
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.
more [options] file [...]

Options Function
more -num Limits the line displayed per page.
more -d Displays user message at right corner.
more -s Squeeze blank lines.
more +/string name It helps to find the string.
more +num Used to display the content from a
specific line.
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.

Type a ? when you're between pages to


show a brief reminder of your options at
this prompt.
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.
ECHO
The echo command is a built-in Linux feature
that prints out arguments as the standard
output. echo is commonly used to display text
strings or command results as messages.
Echo is very frequently used with code that is
written in the form of shell scripts. You can
make use of this command to display
anything on your screen.
echo command uses the following options
-n: Displays the output while omitting the newline after it.
-E: The default option, disables the interpretation of
escape characters.
-e: Enables the interpretation of the following escape
characters:
\\: Displays a backslash character (\).
\a: Plays a sound alert when displaying the output.
\b: Creates a backspace character, equivalent to
pressing Backspace.
\c: Omits any output following the escape character.
\e: The escape character, equivalent to pressing Esc.
\f: The form feed character, causes the printer to
automatically advance to the start of the next page.
\n: Adds a new line to the output.
\r: Performs a carriage return.
\t: Creates horizontal tab spaces.
\v: Creates vertical tab spaces.
\NNN: Byte with the octal value of NNN.
\xHH: Byte with the hexadecimal value of HH.
echo -e "\033[1;37mWHITE"
echo -e "\033[0;30mBLACK"
echo -e "\033[0;34mBLUE"
echo -e "\033[0;32mGREEN"
echo -e "\033[0;36mCYAN"
echo -e "\033[0;31mRED"
echo -e "\033[0;35mPURPLE"
echo -e "\033[0;33mYELLOW"
echo -e "\033[1;30mGRAY"
Use cases of echo command

To display text or string which are passes as


arguments
Print the variable with a value
Only print the value as discussed above
Display number of files of the same format
Remove all spaces and so on.
echo [option] [string]

$ echo “Hello World”

$ echo \'Hello World\'

$ echo '"Hello World"'

-e: Enable the interpretation of backslash escape


sequences. A few examples of escape sequences
that can be used with -e
-n: to omit new line
$ echo -e "Linux for \bdeivces"

$ echo -e "Linux \nfor \ndevices"

$ echo -e "Linux \t for \t Devices"

$ echo -e "Linux \vfor \vDevices"

$ echo -e "Linux \afor Devices"

$ echo *$
$ echo -e 'Hello, \vWorld, \vthis \vis \vPNAP!'
$ echo *.txt

Printing Variable values


$ x=10

$ echo The value of x is = $x

Store data in a file

$ echo "hello world" > hello.txt

$ cat hello.txt

You might also like