Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views15 pages

Cat, Echo

The document provides an overview of the 'cat' and 'echo' commands in Linux, detailing their usage, syntax, and examples. The 'cat' command is used for reading, creating, and manipulating files, while the 'echo' command is primarily for printing text and adding it to files. Various operators for redirecting output and appending data are also explained.

Uploaded by

Ritesh Meshram
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)
16 views15 pages

Cat, Echo

The document provides an overview of the 'cat' and 'echo' commands in Linux, detailing their usage, syntax, and examples. The 'cat' command is used for reading, creating, and manipulating files, while the 'echo' command is primarily for printing text and adding it to files. Various operators for redirecting output and appending data are also explained.

Uploaded by

Ritesh Meshram
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/ 15

cat command

cat command is used to read the data from files.

and it is also used to create files with data input

SYNTAX:

#cat <operator> <file name>  create a file with data input

<text> <enter>

ctrl+d  to save and exit

#cat <file name>  read data of file

#cat -n <file name>  read file data with line no.

OPERATOR:

> single input redirector  overwrite

>> double input redirector  append

EG:

[root@server ~]# cat anaconda-ks.cfg  read the file

# Generated by Anaconda 34.25.0.29

# Generated by pykickstart v3.32

#version=RHEL9

# Use graphical install

graphical

repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-cdrom/AppStream

%addon com_redhat_kdump --enable --reserve-mb='auto'

[root@server ~]#

[root@server ~]# cat -n anaconda-ks.cfg

1 # Generated by Anaconda 34.25.0.29

2 # Generated by pykickstart v3.32


3 #version=RHEL9

4 # Use graphical install

5 graphical

6 repo --name="AppStream" --baseurl=file:///run/install/sources/mount-0000-


cdrom/AppStream

8 %addon com_redhat_kdump --enable --reserve-mb='auto'

10 %end

11

12 # Keyboard layouts

13 keyboard --xlayouts='us'

[root@server ~]# cat /root/.bashrc

# .bashrc

# Source global definitions

if [ -f /etc/bashrc ]; then

. /etc/bashrc

fi

# User specific environment

if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]

then

PATH="$HOME/.local/bin:$HOME/bin:$PATH"

fi

export PATH

# Uncomment the following line if you don't like systemctl's auto-paging feature:

# export SYSTEMD_PAGER=
# User specific aliases and functions

alias rm='rm -i'

alias cp='cp -i'

alias mv='mv -i'

[root@server ~]# ls

abc1 class1sectionB class3sectionB Music redhat Videos

abc{1...3} class1sectionC class3sectionC newfile redhat.txt xyz1.txt

abc2 class2sectionA Desktop Pictures rhel xyz2.txt

abc3 class2sectionB Documents Public sample xyz3.txt

anaconda-ks.cfg class2sectionC Downloads red Templates

class1sectionA class3sectionA file1 'red abc' test

[root@server ~]# cat > sample1.txt

HELLO world

Press (ctrl+d)  save and exit

[root@server ~]# ls

abc1 class1sectionB class3sectionB Music redhat test

abc{1...3} class1sectionC class3sectionC newfile redhat.txt Videos

abc2 class2sectionA Desktop Pictures rhel xyz1.txt

abc3 class2sectionB Documents Public sample xyz2.txt

anaconda-ks.cfg class2sectionC Downloads red sample1.txt xyz3.txt

class1sectionA class3sectionA file1 'red abc' Templates

[root@server ~]#

[root@server ~]# cat sample1.txt

HELLO world

-------------------------------------------------------------------------------------

[root@server ~]# cat >> sample1.txt  use to append


HELLO INDIA

Press (ctrl+d)  save and exit

[root@server ~]# cat sample1.txt

HELLO world

HELLO INDIA

-------------------------------------------------------------------------------------

[root@server ~]# cat > sample1.txt  use to overwrite

REDHAT CERTIFIED

[root@server ~]# cat sample1.txt

REDHAT CERTIFIED

-------------------------------------------------------------------------------------

[root@server ~]# cat >> sample2.txt

SYSTEM ADMINISTRATOR

Press (ctrl+d)  save and exit

[root@server ~]# cat sample2.txt

SYSTEM ADMINISTRATOR

[root@server ~]# cat sample1.txt sample2.txt  read multiple files

REDHAT CERTIFIED

SYSTEM ADMINISTRATOR
[root@server ~]# cat sample2.txt sample1.txt

SYSTEM ADMINISTRATOR

REDHAT CERTIFIED

-------------------------------------------------------------------------------------

[root@server ~]# cat sample1.txt sample2.txt > sample3 merge multiple file into
single

[root@server ~]# ls

abc1 class1sectionC Desktop Public sample1.txt xyz2.txt

abc{1...3} class2sectionA Documents red sample2.txt xyz3.txt

abc2 class2sectionB Downloads 'red abc' sample3

[root@server ~]# cat sample3

REDHAT CERTIFIED

SYSTEM ADMINISTRATOR

-------------------------------------------------------------------------------------

[root@server ~]# cat >> sample3

HELLO WORLD  tab space has been used

HELLO INDIA

[root@server ~]# cat sample3

REDHAT CERTIFIED
SYSTEM ADMINISTRATOR

HELLO WORLD

HELLO INDIA

[root@server ~]# cat -n sample3  print with line no.

1 REDHAT CERTIFIED

4 SYSTEM ADMINISTRATOR

6 HELLO WORLD

7 HELLO INDIA

[root@server ~]# cat -E sample3  $ shows end of line

REDHAT CERTIFIED$

SYSTEM ADMINISTRATOR$

HELLO WORLD$

HELLO INDIA$

[root@server ~]# cat -T sample3  show tab space used in file

REDHAT CERTIFIED
SYSTEM ADMINISTRATOR

HELLO^IWORLD

HELLO INDIA

-------------------------------------------------------------------------------------

REDIRECTOR

> (single input) --> overwrite

>> (dounle input) --> append

1>  output redirector (overwrite)

1>>  output redirector (append)

2>  error redirector (overwrite)

2>>  error redirector (append)

[root@server ~]# ls

abc1 class1sectionC Desktop Public sample1.txt xyz2.txt

abc{1...3} class2sectionA Documents red sample2.txt xyz3.txt

abc2 class2sectionB Downloads 'red abc' sample3

abc3 class2sectionC file1 redhat Templates

anaconda-ks.cfg class3sectionA Music redhat.txt test

class1sectionA class3sectionB newfile rhel Videos

class1sectionB class3sectionC Pictures sample xyz1.txt

[root@server ~]#

[root@server ~]# ls /

afs data etc home lib64 mnt opt redhat sample sys var

bin dev file.txt kali media net proc root sbin tmp web.sh

boot dhparams.pem grras lib misc nfsdata red run srv usr
[root@server ~]# ls / /abcd  there are two arguments for ls command
one is giving output and the another is giving error

ls: cannot access '/abcd': No such file or directory  error

/:  output

afs data etc home lib64 mnt opt redhat sample sys var

bin dev file.txt kali media net proc root sbin tmp web.sh

boot dhparams.pem grras lib misc nfsdata red run srv usr

[root@server ~]# # ls / /abcd > lsoutput # output redirector

-------------------------------------------------------------------------------------

[root@server ~]# ls / /abcd 1> lsout  output redirector redirects the


output in file as an data

ls: cannot access '/abcd': No such file or directory

[root@server ~]# ls

abc3 class2sectionC Public sample1.txt xyz2.txt

anaconda-ks.cfg class3sectionA file1 red sample2.txt xyz3.txt

class1sectionA class3sectionB lsout 'red abc' sample3

class1sectionB class3sectionC lsoutput redhat Templates

[root@server ~]# cat lsout

afs data etc home lib64 mnt opt redhat sample sys var

bin dev file.txt kali media net proc root sbin tmp web.sh

boot dhparams.pem grras lib misc nfsdata red run srv usr
-------------------------------------------------------------------------------------

[root@server ~]# ls / /abcd 2> error.txt  error redirector redirects the error in
file

/:

afs data etc home lib64 mnt opt redhat sample sys var

bin dev file.txt kali media net proc root sbin tmp web.sh

boot dhparams.pem grras lib misc nfsdata red run srv usr

[root@server ~]# ls

.txt

abc3 class2sectionC error.txt Public sample1.txt xyz2.txt

class1sectionA class3sectionB lsout 'red abc' sample3

class1sectionB class3sectionC lsoutput redhat Templates

[root@server ~]# cat error.txt

ls: cannot access '/abcd': No such file or directory

-------------------------------------------------------------------------------------

[root@server ~]# cat rehat.txt

cat: rehat.txt: No such file or directory  error of command cat rehat.txt

[root@server ~]# cat rehat.txt 2>> error.txt  appending the new error to existing
error file

[root@server ~]# cat error.txt

ls: cannot access '/abcd': No such file or directory

cat: rehat.txt: No such file or directory


echo command

echo command is use to print. it is also use to add text to file and create a file. mostly used is shell
scripting.

syntax:

#echo <string/sentance>  used to print

#echo <string/sentance> <operator> <file name>  use to add data in file

operator:

>  overwrite

>>  append

eg:

[root@server ~]# echo redhat

redhat

[root@server ~]# echo "Redhat Certified System Administrator"

Redhat Certified System Administrator

[root@server ~]# echo "Redhat:user"

Redhat:user

[root@server ~]# echo {1..10}

1 2 3 4 5 6 7 8 9 10

[root@server ~]# echo {A..Z}

ABCDEFGHIJKLMNOPQRSTUVWXYZ

[root@server ~]# echo "Redhat:user@SYSTEM#admin"


Redhat:user@SYSTEM#admin

[root@server ~]# echo Redhat:user@SYSTEM #admin

Redhat:user@SYSTEM

[root@server ~]# echo "Redhat:user@SYSTEM #admin

Redhat:user@SYSTEM #admin

[root@server ~]# echo "Redhat:user@SYSTEM #admin"

Redhat:user@SYSTEM #admin

[root@server ~]# echo Redhat:user@SYSTEM \#admin   need to print # in the


sentence so user backslash (\) before the #

Redhat:user@SYSTEM #admin

-------------------------------------------------------------------------------------

[root@server ~]# echo Redhat:user@SYSTEM \#admin > echofile  create the with data

[root@server ~]# ls

abc2 class2sectionB Downloads newfile rhel Videos

abc3 class2sectionC echofile Pictures sample xyz1.txt

anaconda-ks.cfg class3sectionA error.txt Public sample1.txt xyz2.txt

[root@server ~]# cat echofile

Redhat:user@SYSTEM #admin

-------------------------------------------------------------------------------------

[root@server ~]# echo {A..Z} >> echofile  append

[root@server ~]# cat echofile

Redhat:user@SYSTEM #admin

ABCDEFGHIJKLMNOPQRSTUVWXYZ

-------------------------------------------------------------------------------------
[root@server ~]# echo {1..100} > echofile  overwrite

[root@server ~]# cat echofile

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

-------------------------------------------------------------------------------------

ECHO WITH ESCAPE SEQUENCE

-e  escape sequence

\n  new line

\t  horizontal tab

\c  continue

\v  vertical tab

\b  back space

EG:

[root@server ~]# echo "HELLO USER, YOU ARE REDHAT CERTIFIED"

HELLO USER, YOU ARE REDHAT CERTIFIED

[root@server ~]# echo "HELLO USER,\nYOU ARE \nREDHAT CERTIFIED"

HELLO USER,\nYOU ARE \nREDHAT CERTIFIED

[root@server ~]# echo -e "HELLO USER,\nYOU ARE \nREDHAT CERTIFIED\n"

HELLO USER,

YOU ARE

REDHAT CERTIFIED

[root@server ~]# echo -e "HELLO USER,\tYOU ARE \tREDHAT CERTIFIED\n"

HELLO USER, YOU ARE REDHAT CERTIFIED


[root@server ~]# echo -e "HELLO USER,\vYOU ARE \vREDHAT CERTIFIED\n"

HELLO USER,

YOU ARE

REDHAT CERTIFIED

[root@server ~]# echo -e "HELLO USER,\b YOU ARE\b REDHAT CERTIFIED\b "

HELLO USER YOU AR REDHAT CERTIFIE

[root@server ~]# echo -e "HELLO USER,\v YOU\t ARE\n REDHAT CERTIFIED\b "

HELLO USER,

YOU ARE

REDHAT CERTIFIE

[root@server ~]# echo -n "HELLO USER,\v YOU\t ARE\n REDHAT CERTIFIED\b "

HELLO USER,\v YOU\t ARE\n REDHAT CERTIFIED\b [root@server ~]#

[root@server ~]# echo -e "HELLO USER,\c YOU ARE\c REDHAT CERTIFIED\c "

HELLO USER,[root@server ~]#

-------------------------------------------------------------------------------------

[root@server ~]# echo hello ; echo world

hello

world

[root@server ~]# echo -e "hello \c " ; echo world

hello world

[root@server ~]# echo -e "hello \c" ; echo world

hello world
[root@server ~]# echo -n "hello " ; echo world

hello world

[root@server ~]# echo -n "hello " ; echo world

[root@server ~]# echo -e "hello \c" ; ls

hello abc1 class1sectionC Desktop lsoutput redhat Templates

abc{1...3} class2sectionA Documents Music redhat.txt test

abc2 class2sectionB Downloads newfile rhel Videos

abc3 class2sectionC echofile Pictures sample xyz1.txt

anaconda-ks.cfg class3sectionA error.txt Public sample1.txt xyz2.txt

class1sectionA class3sectionB file1 red sample2.txt xyz3.txt

class1sectionB class3sectionC lsout 'red abc' sample3

[root@server ~]# echo "hello" ; ls

hello

abc1 class1sectionC Desktop lsoutput redhat Templates

abc{1...3} class2sectionA Documents Music redhat.txt test

abc2 class2sectionB Downloads newfile rhel Videos

abc3 class2sectionC echofile Pictures sample xyz1.txt

anaconda-ks.cfg class3sectionA error.txt Public sample1.txt xyz2.txt

class1sectionA class3sectionB file1 red sample2.txt xyz3.txt

class1sectionB class3sectionC lsout 'red abc' sample3

[root@server ~]# echo "hello" ; touch class.txt ; ls ; cd /

hello

abc1 class1sectionC class.txt lsout 'red abc' sample3

abc{1...3} class2sectionA Desktop lsoutput redhat Templates


abc2 class2sectionB Documents Music redhat.txt test

abc3 class2sectionC Downloads newfile rhel Videos

anaconda-ks.cfg class3sectionA echofile Pictures sample xyz1.txt

class1sectionA class3sectionB error.txt Public sample1.txt xyz2.txt

class1sectionB class3sectionC file1 red sample2.txt xyz3.txt

[root@server /]#

[root@server /]#

[root@server /]# cd

-------------------------------------------------------------------------------------

[root@server ~]# echo "hello"

hello

[root@server ~]# echo $0

bash

[root@server ~]# echo $SHELL

/bin/bash

[root@server ~]# echo $HOME

/root

[root@server ~]# echo $HOSTNAME

server.example.com

[root@server ~]# echo $USER

root

[root@server ~]# echo $PWD

/root

[root@server ~]#

You might also like