0% found this document useful (0 votes)
50 views21 pages

Touch, Cat

The document describes the Linux touch command which is used to create empty files, change file timestamps, and reference other file timestamps. Touch can create single or multiple empty files, sequentially numbered files, and change access, modify, or change timestamps on existing files by specifying options like -d, -a, -m, or -r. Examples are provided showing how to use touch to create files, change timestamps, and set the timestamp of a new file to match an existing file.

Uploaded by

Girsawale
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)
50 views21 pages

Touch, Cat

The document describes the Linux touch command which is used to create empty files, change file timestamps, and reference other file timestamps. Touch can create single or multiple empty files, sequentially numbered files, and change access, modify, or change timestamps on existing files by specifying options like -d, -a, -m, or -r. Examples are provided showing how to use touch to create files, change timestamps, and set the timestamp of a new file to match an existing file.

Uploaded by

Girsawale
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/ 21

File Creation

cmd --> touch

it is use to create empty files , you can also create multiple files.

and it is also use to change the time stamp of files and directories

syntax:

# touch <filename/path>  create a empty file

# touch <file> <file2> <file3>  create multiple files

# touch <file name/path>{start..stop}  create sequence file

# touch <option> <filename/path>

options:

-d  change time stamp

-a change access time only

-m change modify time only

-r  (referance) use this file's time instead of current time

eg:

[root@localhost ~]# touch test

[root@localhost ~]# pwd

/root

[root@localhost ~]# ls

anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates Videos

Desktop Downloads Music Public test

[root@localhost ~]# touch test1.txt text2.txt test3.txt

[root@localhost ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates test1.txt text2.txt

Desktop Downloads Music Public test test3.txt Videos

[root@localhost ~]# touch file1.txt /file2.txt /tmp/file2.txt

[root@localhost ~]# ls

anaconda-ks.cfg Downloads Music Templates test3.txt

Desktop file1.txt Pictures test text2.txt

Documents initial-setup-ks.cfg Public test1.txt Videos

[root@localhost ~]# cd /

[root@localhost /]# ls

bin dev file2.txt lib media opt root sbin sys usr

boot etc home lib64 mnt proc run srv tmp var

[root@localhost /]# cd

[root@localhost ~]# ls /

bin dev file2.txt lib media opt root sbin sys usr

boot etc home lib64 mnt proc run srv tmp var

[root@localhost ~]# ls /tmp

file2.txt

systemd-private-b2133f7679e1412da15d36e37f3d9653-bluetooth.service-aN1b4t

systemd-private-b2133f7679e1412da15d36e37f3d9653-colord.service-A6eg9E

systemd-private-b2133f7679e1412da15d36e37f3d9653-fwupd.service-LxZ1LE

systemd-private-b2133f7679e1412da15d36e37f3d9653-geoclue.service-GZmpsk

systemd-private-b2133f7679e1412da15d36e37f3d9653-ModemManager.service-d5hX8v

systemd-private-b2133f7679e1412da15d36e37f3d9653-rtkit-daemon.service-OkQvLy
[root@localhost ~]# touch apple{1..5}.txt

[root@localhost ~]# ls

anaconda-ks.cfg apple3.txt Desktop file1.txt Pictures test text2.txt

apple1.txt apple4.txt Documents initial-setup-ks.cfg Public test1.txt Videos

apple2.txt apple5.txt Downloads Music Templates test3.txt

[root@localhost ~]# touch myfile{A..C}

[root@localhost ~]# ls

anaconda-ks.cfg apple4.txt Downloads myfileA Public test3.txt

apple1.txt apple5.txt file1.txt myfileB Templates text2.txt

apple2.txt Desktop initial-setup-ks.cfg myfileC test Videos

apple3.txt Documents Music Pictures test1.txt

[root@localhost ~]# touch myfile{10..7}

[root@localhost ~]# ls

anaconda-ks.cfg apple4.txt Downloads myfile10 myfileA Public test3.txt

apple1.txt apple5.txt file1.txt myfile7 myfileB Templates text2.txt

apple2.txt Desktop initial-setup-ks.cfg myfile8 myfileC test Videos

apple3.txt Documents Music myfile9 Pictures test1.txt

[root@localhost ~]# touch linux{1..3}redhat{1..3}

[root@localhost ~]# ls

anaconda-ks.cfg Desktop linux1redhat2 linux3redhat2 myfile9 Templates

apple1.txt Documents linux1redhat3 linux3redhat3 myfileA test

apple2.txt Downloads linux2redhat1 Music myfileB test1.txt

apple3.txt file1.txt linux2redhat2 myfile10 myfileC test3.txt

apple4.txt initial-setup-ks.cfg linux2redhat3 myfile7 Pictures text2.txt


apple5.txt linux1redhat1 linux3redhat1 myfile8 Public Videos

[root@localhost ~]# touch linux{1..2}redhat{1..2}centos{1..2}

[root@localhost ~]# ls

anaconda-ks.cfg file1.txt linux2redhat1 linux3redhat3 Pictures

apple1.txt initial-setup-ks.cfg linux2redhat1centos1 Music Public

apple2.txt linux1redhat1 linux2redhat1centos2 myfile10 Templates

apple3.txt linux1redhat1centos1 linux2redhat2 myfile7 test

apple4.txt linux1redhat1centos2 linux2redhat2centos1 myfile8 test1.txt

apple5.txt linux1redhat2 linux2redhat2centos2 myfile9 test3.txt

Desktop linux1redhat2centos1 linux2redhat3 myfileA text2.txt

Documents linux1redhat2centos2 linux3redhat1 myfileB Videos

Downloads linux1redhat3 linux3redhat2 myfileC

[root@localhost ~]# date

Fri Jan 28 20:07:52 IST 2022

[root@localhost ~]# touch sample.txt

[root@localhost ~]# ls

anaconda-ks.cfg file1.txt linux2redhat1 linux3redhat3 Pictures

apple1.txt initial-setup-ks.cfg linux2redhat1centos1 Music Public

apple2.txt linux1redhat1 linux2redhat1centos2 myfile10 sample.txt

apple3.txt linux1redhat1centos1 linux2redhat2 myfile7 Templates

apple4.txt linux1redhat1centos2 linux2redhat2centos1 myfile8 test

apple5.txt linux1redhat2 linux2redhat2centos2 myfile9 test1.txt

Desktop linux1redhat2centos1 linux2redhat3 myfileA test3.txt

Documents linux1redhat2centos2 linux3redhat1 myfileB text2.txt

Downloads linux1redhat3 linux3redhat2 myfileC Videos


[root@localhost ~]# ls -l sample.txt

-rw-r--r--. 1 root root 0 Jan 28 20:08 sample.txt

[root@localhost ~]# stat sample.txt

File: sample.txt

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: fd00h/64768d Inode: 34657887 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Context: unconfined_u:object_r:admin_home_t:s0

Access: 2022-01-28 20:08:10.212063266 +0530

Modify: 2022-01-28 20:08:10.212063266 +0530

Change: 2022-01-28 20:08:10.212063266 +0530

Birth: -

[root@localhost ~]# touch -d "20 jan 2022 12:12:12" mydata.txt

[root@localhost ~]# ls

anaconda-ks.cfg initial-setup-ks.cfg linux2redhat1centos2 myfile10 Templates

apple1.txt linux1redhat1 linux2redhat2 myfile7 test

apple2.txt linux1redhat1centos1 linux2redhat2centos1 myfile8 test1.txt

apple3.txt linux1redhat1centos2 linux2redhat2centos2 myfile9 test3.txt

apple4.txt linux1redhat2 linux2redhat3 myfileA text2.txt

apple5.txt linux1redhat2centos1 linux3redhat1 myfileB Videos

Desktop linux1redhat2centos2 linux3redhat2 myfileC

Documents linux1redhat3 linux3redhat3 Pictures

Downloads linux2redhat1 Music Public

file1.txt linux2redhat1centos1 mydata.txt sample.txt

[root@localhost ~]# stat mydata.txt


File: mydata.txt

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: fd00h/64768d Inode: 34657888 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Context: unconfined_u:object_r:admin_home_t:s0

Access: 2022-01-20 12:12:12.000000000 +0530

Modify: 2022-01-20 12:12:12.000000000 +0530

Change: 2022-01-28 20:12:08.773067955 +0530

Birth: -

[root@localhost ~]# touch -a mydata.txt

[root@localhost ~]# stat mydata.txt

File: mydata.txt

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: fd00h/64768d Inode: 34657888 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Context: unconfined_u:object_r:admin_home_t:s0

Access: 2022-01-28 20:16:22.428072941 +0530

Modify: 2022-01-20 12:12:12.000000000 +0530

Change: 2022-01-28 20:16:22.428072941 +0530

Birth: -

[root@localhost ~]# touch -m mydata.txt

[root@localhost ~]# stat mydata.txt

File: mydata.txt

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: fd00h/64768d Inode: 34657888 Links: 1


Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Context: unconfined_u:object_r:admin_home_t:s0

Access: 2022-01-28 20:16:22.428072941 +0530

Modify: 2022-01-28 20:19:55.880077136 +0530

Change: 2022-01-28 20:19:55.880077136 +0530

Birth: -

[root@localhost ~]# touch -d "01 jan 2022 " mydata.txt

[root@localhost ~]# stat mydata.txt

File: mydata.txt

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: fd00h/64768d Inode: 34657888 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Context: unconfined_u:object_r:admin_home_t:s0

Access: 2022-01-01 00:00:00.000000000 +0530

Modify: 2022-01-01 00:00:00.000000000 +0530

Change: 2022-01-28 20:20:41.342078030 +0530

Birth: -

-r  referance of file time,

eg:

[root@localhost ~]# touch -d "31 dec 2021 10:10:10" redhat.txt

[root@localhost ~]# stat redhat.txt

File: redhat.txt

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: fd00h/64768d Inode: 34657889 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)


Context: unconfined_u:object_r:admin_home_t:s0

Access: 2021-12-31 10:10:10.000000000 +0530

Modify: 2021-12-31 10:10:10.000000000 +0530

Change: 2022-01-28 20:23:14.344081037 +0530

Birth: -

[root@localhost ~]# touch -r redhat.txt newfile.txt

[root@localhost ~]# ls

anaconda-ks.cfg initial-setup-ks.cfg linux2redhat1centos2 myfile10 redhat.txt

apple1.txt linux1redhat1 linux2redhat2 myfile7 sample.txt

apple2.txt linux1redhat1centos1 linux2redhat2centos1 myfile8 Templates

apple3.txt linux1redhat1centos2 linux2redhat2centos2 myfile9 test

apple4.txt linux1redhat2 linux2redhat3 myfileA test1.txt

apple5.txt linux1redhat2centos1 linux3redhat1 myfileB test3.txt

Desktop linux1redhat2centos2 linux3redhat2 myfileC text2.txt

Documents linux1redhat3 linux3redhat3 newfile.txt Videos

Downloads linux2redhat1 Music Pictures

file1.txt linux2redhat1centos1 mydata.txt Public

[root@localhost ~]# stat newfile.txt

File: newfile.txt

Size: 0 Blocks: 0 IO Block: 4096 regular empty file

Device: fd00h/64768d Inode: 34657890 Links: 1

Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)

Context: unconfined_u:object_r:admin_home_t:s0

Access: 2021-12-31 10:10:10.000000000 +0530

Modify: 2021-12-31 10:10:10.000000000 +0530

Change: 2022-01-28 20:24:29.883082522 +0530


Birth: -

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@localhost ~]# cat > grras.txt

hello world

welcome to grras

[root@localhost ~]# ls

anaconda-ks.cfg grras.txt linux2redhat1centos1 mydata.txt Public

apple1.txt initial-setup-ks.cfg linux2redhat1centos2 myfile10 redhat.txt

apple2.txt linux1redhat1 linux2redhat2 myfile7 sample.txt

apple3.txt linux1redhat1centos1 linux2redhat2centos1 myfile8 Templates

apple4.txt linux1redhat1centos2 linux2redhat2centos2 myfile9 test

apple5.txt linux1redhat2 linux2redhat3 myfileA test1.txt


Desktop linux1redhat2centos1 linux3redhat1 myfileB test3.txt

Documents linux1redhat2centos2 linux3redhat2 myfileC text2.txt

Downloads linux1redhat3 linux3redhat3 newfile.txt Videos

file1.txt linux2redhat1 Music Pictures

[root@localhost ~]# cat grras.txt

hello world

welcome to grras

[root@localhost ~]# cat >> grras.txt # append

hello user

Welcome to redhat

[root@localhost ~]# cat grras.txt

hello world

welcome to grras

hello user

Welcome to redhat

[root@localhost ~]# cat > grras.txt # overwrite

Redhat Enterprise Linux

[root@localhost ~]# cat grras.txt

Redhat Enterprise Linux


[root@localhost ~]# cat > grras1

hello world

Welcome to redhat

[root@localhost ~]# cat grras1

hello world

Welcome to redhat

[root@localhost ~]# cat > grras2

hello user

Welcome to redhat

[root@localhost ~]# cat grras2

hello user

Welcome to redhat

[root@localhost ~]# cat grras.txt grras1 grras2

Redhat Enterprise Linux

hello world

Welcome to redhat

hello user

Welcome to redhat
[root@localhost ~]# cat grras1 grras2 grras.txt

hello world

Welcome to redhat

hello user

Welcome to redhat

Redhat Enterprise Linux

[root@localhost ~]# cat grras1 grras2 grras.txt > class.txt #merge

[root@localhost ~]# ls

anaconda-ks.cfg grras1 linux1redhat3 Music Public

apple1.txt grras2 linux2redhat1 mydata.txt redhat.txt

apple2.txt grras.txt linux2redhat1centos1 myfile10 sample.txt

apple3.txt grras.xt linux2redhat1centos2 myfile7 Templates

apple4.txt initial-setup-ks.cfg linux2redhat2 myfile8 test

apple5.txt linux1redhat1 linux2redhat2centos1 myfile9 test1.txt

class.txt linux1redhat1centos1 linux2redhat2centos2 myfileA test3.txt

Desktop linux1redhat1centos2 linux2redhat3 myfileB text2.txt

Documents linux1redhat2 linux3redhat1 myfileC Videos

Downloads linux1redhat2centos1 linux3redhat2 newfile.txt

file1.txt linux1redhat2centos2 linux3redhat3 Pictures

[root@localhost ~]# cat class.txt

hello world

Welcome to redhat
hello user

Welcome to redhat

Redhat Enterprise Linux

[root@localhost ~]# cat -n class.txt

1 hello world

2 Welcome to redhat

4 hello user

5 Welcome to redhat

7 Redhat Enterprise Linux

[root@localhost ~]# cat -t class.txt  shows tab spaces

hello world

Welcome to redhat

hello ^Iuser

Welcome^I^Ito ^Iredhat

Redhat Enterprise Linux

[root@localhost ~]# cat -E class.txt  shows end of line

hello world$

Welcome to redhat$
$

hello user$

Welcome to redhat$

Redhat Enterprise Linux$

[root@localhost ~]# cat -En class.txt

1 hello world$

2 Welcome to redhat$

3 $

4 hello user$

5 Welcome to redhat$

6 $

7 Redhat Enterprise Linux$

8 $

redirection

[root@localhost ~]# ls

anaconda-ks.cfg grras1 linux1redhat3 Music Public

apple1.txt grras2 linux2redhat1 mydata.txt redhat.txt

apple2.txt grras.txt linux2redhat1centos1 myfile10 sample.txt

apple3.txt grras.xt linux2redhat1centos2 myfile7 Templates

apple4.txt initial-setup-ks.cfg linux2redhat2 myfile8 test

apple5.txt linux1redhat1 linux2redhat2centos1 myfile9 test1.txt

class.txt linux1redhat1centos1 linux2redhat2centos2 myfileA test3.txt

Desktop linux1redhat1centos2 linux2redhat3 myfileB text2.txt


Documents linux1redhat2 linux3redhat1 myfileC Videos

Downloads linux1redhat2centos1 linux3redhat2 newfile.txt

file1.txt linux1redhat2centos2 linux3redhat3 Pictures

[root@localhost ~]# cat anaconda-ks.cfg > new.txt

[root@localhost ~]# cat new.txt

#version=RHEL8

# Use graphical install

graphical

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

%packages

@^graphical-server-environment

kexec-tools

%end

# Keyboard layouts

keyboard --xlayouts='us'

# System language

lang en_US.UTF-8

# Network information

network --hostname=localhost.localdomain

# Use CDROM installation media


cdrom

# Run the Setup Agent on first boot

firstboot --enable

ignoredisk --only-use=sda

autopart

# Partition clearing information

clearpart --none --initlabel

# System timezone

timezone Asia/Kolkata --isUtc --nontp

# Root password

rootpw --iscrypted
$6$QI32zDcsNgXNL.6D$oagwMH1jqoGQ50BJ6wz.4v4waex6lBNBKcKOO55XSJr1GnNMIqY7.ki.zsdSwz
IyP1gvni3rOFa2DmSpzVGQ3/

user --groups=wheel --name=student --


password=$6$0GYE/8DzFyg8AFvQ$vKeC1KOgxIxCOJD78wPs.7K0Ps1T36Wi77ViBf6sgFNWLCMrtbivA
XvdOUm4mn.rUOyU.VYiopVxARdMbvSyn1 --iscrypted --gecos="student"

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

%end

%anaconda

pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty

pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok

pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty


%end

[root@localhost ~]# ls

anaconda-ks.cfg grras1 linux1redhat3 Music Pictures

apple1.txt grras2 linux2redhat1 mydata.txt Public

apple2.txt grras.txt linux2redhat1centos1 myfile10 redhat.txt

apple3.txt grras.xt linux2redhat1centos2 myfile7 sample.txt

apple4.txt initial-setup-ks.cfg linux2redhat2 myfile8 Templates

apple5.txt linux1redhat1 linux2redhat2centos1 myfile9 test

class.txt linux1redhat1centos1 linux2redhat2centos2 myfileA test1.txt

Desktop linux1redhat1centos2 linux2redhat3 myfileB test3.txt

Documents linux1redhat2 linux3redhat1 myfileC text2.txt

Downloads linux1redhat2centos1 linux3redhat2 newfile.txt Videos

file1.txt linux1redhat2centos2 linux3redhat3 new.txt

[root@localhost ~]# ls 1> outputfile  redirect output to specified file

[root@localhost ~]# ls

anaconda-ks.cfg grras1 linux1redhat3 Music outputfile

apple1.txt grras2 linux2redhat1 mydata.txt Pictures

apple2.txt grras.txt linux2redhat1centos1 myfile10 Public

apple3.txt grras.xt linux2redhat1centos2 myfile7 redhat.txt

apple4.txt initial-setup-ks.cfg linux2redhat2 myfile8 sample.txt

apple5.txt linux1redhat1 linux2redhat2centos1 myfile9 Templates

class.txt linux1redhat1centos1 linux2redhat2centos2 myfileA test

Desktop linux1redhat1centos2 linux2redhat3 myfileB test1.txt

Documents linux1redhat2 linux3redhat1 myfileC test3.txt

Downloads linux1redhat2centos1 linux3redhat2 newfile.txt text2.txt


file1.txt linux1redhat2centos2 linux3redhat3 new.txt Videos

[root@localhost ~]# cat outputfile

anaconda-ks.cfg

apple1.txt

apple2.txt

apple3.txt

apple4.txt

apple5.txt

class.txt

Desktop

Documents

Downloads

file1.txt

grras1

grras2

grras.txt

grras.xt

initial-setup-ks.cfg

linux1redhat1

linux1redhat1centos1

linux1redhat1centos2

linux1redhat2

linux1redhat2centos1

linux1redhat2centos2

linux1redhat3

linux2redhat1
linux2redhat1centos1

linux2redhat1centos2

linux2redhat2

linux2redhat2centos1

linux2redhat2centos2

linux2redhat3

linux3redhat1

linux3redhat2

linux3redhat3

Music

mydata.txt

myfile10

myfile7

myfile8

myfile9

myfileA

myfileB

myfileC

newfile.txt

new.txt

outputfile

Pictures

Public

redhat.txt

sample.txt

Templates

test
test1.txt

test3.txt

text2.txt

Videos

[root@localhost ~]# ls abcdef

ls: cannot access 'abcdef': No such file or directory

[root@localhost ~]# ls abcdef 2> errorfile  redirect error to the specified file

[root@localhost ~]# ls

anaconda-ks.cfg grras1 linux2redhat1 myfile10 redhat.txt

apple1.txt grras2 linux2redhat1centos1 myfile7 sample.txt

apple2.txt grras.txt linux2redhat1centos2 myfile8 Templates

apple3.txt grras.xt linux2redhat2 myfile9 test

apple4.txt initial-setup-ks.cfg linux2redhat2centos1 myfileA test1.txt

apple5.txt linux1redhat1 linux2redhat2centos2 myfileB test3.txt

class.txt linux1redhat1centos1 linux2redhat3 myfileC text2.txt

Desktop linux1redhat1centos2 linux3redhat1 newfile.txt Videos

Documents linux1redhat2 linux3redhat2 new.txt

Downloads linux1redhat2centos1 linux3redhat3 outputfile

errorfile linux1redhat2centos2 Music Pictures

file1.txt linux1redhat3 mydata.txt Public

[root@localhost ~]# cat errorfile

ls: cannot access 'abcdef': No such file or directory


[root@localhost ~]# echo $?  show exit status of previous run command

[root@localhost ~]# man cat

[root@localhost ~]# info cat

[root@localhost ~]# ls abcdef

ls: cannot access 'abcdef': No such file or directory

[root@localhost ~]# echo $?

[root@localhost ~]# man ls

[root@localhost ~]#

You might also like