Table of Contents

Shell Data Processing - Cat command (short for concatenate)

About

cat is a core utility that generates a stream of data from one or more files (ie file descriptor)

It therefore can:

Example

Concatenate files

file 1 - line 1
file 1 - line 2


file 2 - line 1
file 2 - line 2

cat file1.txt file2.txt > fileAll.txt
cat fileAll.txt
file 1 - line 1
file 1 - line 2
file 2 - line 1
file 2 - line 2

Concatenate Output files selected with a glob pattern

Output all files with the pgn extension

cat *.pgn

where: *.pgn is a glob pattern

Concatenate Output files and standard input

Concatenate the output of file and of standard input

cat f - g

where:

Example:

echo 'Hello World' | tee f g

Copy standard input to standard output

Without any argument, cat will copy standard input to standard output

cat

Example:

echo 'Hello World' > helloIn.txt
cat < helloIn.txt > helloOut.txt
cat helloOut.txt
Hello World

Read a file descriptor

# create file descriptor 3 to a file (ie an alias)
exec 3<>/tmp/mycat
# cat works but return nothing
cat /tmp/mycat
# Writes to it
echo "Hello" >&3
# Return Hello
cat /tmp/mycat

Read a named pipe

cd /tmp
# Creates the pipe in the current directory
mkfifo named_pipe
# Reads from the pipe (blocking) until it can read data
cat named_pipe
cd /tmp
# Writes to the pipe (blocking) until the FIFO is emptied
echo "Hi" > named_pipe