Creating File using the CAT Command


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Creating File using the CAT Command
# 1  
Old 01-12-2010
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
# 2  
Old 01-12-2010
Tools creating file with cat command

Code:
cat > sample

now, type data in
you can press Enter/Return
when done, press
Code:
Control and d keys

# 3  
Old 01-12-2010
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.

Code:
# 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).
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 01-12-2010
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:
Code:
# 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

# 5  
Old 01-12-2010
Thank you very much .

Great Forum with instant replies .

Thanks to the site creator .Smilie
# 6  
Old 01-12-2010
The equivalent of "touch new_file_name" is:

Code:
cat /dev/null > new_file_name

# 7  
Old 01-12-2010
Quote:
Originally Posted by methyl
The equivalent of "touch new_file_name" is:

Code:
cat /dev/null > new_file_name

But only for a new file.

Where just
Code:
> new_file_name

would do.

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

Last edited by Scott; 01-12-2010 at 12:31 PM.. Reason: rewording
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Creating file and passing argument to a command

Hi All, I am having command to run which will take argument as input file. Right now we are creating the input file by cat and executing the command ftptransfer -i input file cat >input file file1 file2 cntrl +d Is there a way I can do that in a single command like ... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

2. Shell Programming and Scripting

Cat command not working to display Mac file in Ubuntu

Hi, Recently I got a .txt file from Mac user. when I try to open it in my Ubuntu machine using cat command it is not displaying any content of file however I can see the content using vi. Anyone know How to see its content using cat as I have to process it in my shell script. Thanks in... (4 Replies)
Discussion started by: diehard
4 Replies

3. UNIX for Dummies Questions & Answers

Cat command drops lines in output file

I use the cat command to concatenate text files, but one of the rows I was expecting doesn't display in the output file. Is there a verbose mode\logging mechanism for the cat command to help me investigate where the lines I was expecting are going?? cat 7760-001_1_*_06_*.txt | grep -v... (1 Reply)
Discussion started by: Xin Xin
1 Replies

4. Shell Programming and Scripting

Cat Command on File not printing "Blank" Lines?

Hello All, I have a bash script and in it at some point I call an Expect Script that does some stuff and saves its output in a ".txt" file. Example "/path/to/my/file/Expect_Output.txt" file: notice the 2nd line is empty in the file... Data for Host-1 (192.168.1.110) Checking the... (2 Replies)
Discussion started by: mrm5102
2 Replies

5. UNIX for Dummies Questions & Answers

format values in a text file using cat command

Hello... Is it possible that we can change the format of the values we entered in a text file using cat? Like for example, I will create a text file names.txt using cat and as I save the names and view the text file... the format will be like this ... (5 Replies)
Discussion started by: kpopfreakghecky
5 Replies

6. UNIX for Dummies Questions & Answers

stuck in editing file with cat command

Hi, While editing a small text file with cat command i pressed ctrl-d to send eof, instead of coming out of cat command it echoed ^D to the screen. Same thing is happening to ctrl-c. After googling i found this is because of trap. The problem is i m stuck in editing mode and cannot get the... (3 Replies)
Discussion started by: TITANIUM
3 Replies

7. Shell Programming and Scripting

Extra control characters being added when I create a file using cat command

Hi, I am using Cygwin.I created a new file and type into it using cat > newfile. When I open this using vi editor, it contains loads of extra control characters. Whats happening? (1 Reply)
Discussion started by: erora
1 Replies

8. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

9. UNIX for Dummies Questions & Answers

Creating a file that contains output from a command, and then displays itself

hey, I'm trying to create the command that will create a file named user.txt that contains the output of the command cut -d: -f1,5 /etc/passwd, and displays itself afterwards. I don't know how to bridge cat > user.txt with cut -d: -f1,5 /etc/passwd, or how display it afterwards. Any help would... (2 Replies)
Discussion started by: raidkridley
2 Replies

10. Shell Programming and Scripting

creating file of 1MB using shell command?

Hi everybody in the forum, I want to create an empty file of say some 1MB ,i mean at the command line itself.How is this possible??????EEK! (4 Replies)
Discussion started by: vijaya2006
4 Replies
Login or Register to Ask a Question