Creating File using the CAT Command

Hello ,

I am newbie to UNIX platform .
I have read that there are two ways of creating files that is using
1.) Cat 2.) touch .

With Cat Command i am unable to create a File , i is saying No file or Directory exists

I logged in with root as user .

please help

cat > sample

now, type data in
you can press Enter/Return
when done, press

Control and d keys

cat is not a command for creating files. cat is a method for concatenating files. (see man cat.) It creates files the same way as any command capable of producing any output whatsoever -- shell redirection allows you to put its output into a file.

# Echo prints to stdout
$ echo asdf
asdf
# Echo prints to new empty file 'newfile'
$ echo asdf > newfile
# printf prints to stdout
$ printf "%02d\n" 9
09
# printf prints to new empty file 'newfile'
$ printf "%02d\n" 9 > newfile
# printf appends to file 'newfile'
$ printf "%02d\n" 9 >> newfile
# etc...

touch on the other hand is a command for creating new files(and updating their timestamps).

At first it is very important in *nix environments to have an eye on case sensitvity. It is a difference to use cat Cat or CAT. Only the 1st will work, but that just by the way.

cat is for showing the content of an already existing file. It alone will not create a file - you will have to redirect output with a > sign into a new file. Maybe this is more appropriate:

# writes text into the file "newfile"; overwrites it's content if it already exists
echo "Content of my new file" > newfile
# writes text into the file "newfile"; appends content if it already exists
echo "Content of my new file" >> newfile
# creates empty file named "newfile"
> newfile
# creates empty file named "newfile"
touch newfile

Thank you very much .

Great Forum with instant replies .

Thanks to the site creator .:b:

The equivalent of "touch new_file_name" is:

cat /dev/null > new_file_name

But only for a new file.

Where just

> new_file_name

would do.

Corona688 is right. Using redirection with any tool will create a file, so the question is ultimately futile.

Agreed.
However I carefully lifted the example from "man cat" which on my system also strangely shows examples not involving "cat".