Creating a file / /dev/null


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Creating a file / /dev/null
# 1  
Old 10-15-2003
Question Creating a file / /dev/null

First of all, hello there cOmMuNity ! Smilie

Well, I've got two basic questions:

1) In how many ways it's possible to create a file ?
I know one manner using "touch", other typing something like echo "" > file ...
The fact is that I need to overwrite the file if it exists, and touch isn't capable of doing this (actually, touch isn't that suitable for creating files, is it ? as it's destined to change the access date and time of a file...). So, is the echo "" > file the most elegant way for this purpose ?


2) What is exactly the /dev/null... It isn't the same thing as a NULL value, is it ?
Suppose I've chosen to create a file by the echo "" > file method.
The file starts with a blank line. To avoid this, I've seen
cat /dev/null > file

Is this again the only way ?
I heard also that I could use the -n option of echo, which removes the newline character...
It would be something like echo -n "" > file
It should work out.

Well, that's all for now.
Thank you anyway !!!

435 Gavea - bRaZiL - tHE HEll iS hERe ! Smilie
# 2  
Old 10-15-2003
1. Depends on your shell. Also a matter of opinion. People can reasonably disagree on "most elegant". With sh, ksh, or bash, my vote goes to:
> file


2. /dev/null is a data trashcan. Use it to discard stuff that you don't want. Suppose that you have a program that updates a data file. But it prints out messages like: "getting starting" "half way done" "all done" "closing file" "exiting". And also suppose that the program does not have an option to sut it up. Just use:
program > /dev/null

Also errors go to file descriptor 2. So you can use 2>/dev/null to discard error messages.
# 3  
Old 10-15-2003
/dev/null

But sending files to /dev/null acts as if I was deleting these files ?



By the way, thanks for the prompt answer !




435 Gavea - bRaZiL - ThE HeLL iS hERe !!! Smilie
# 4  
Old 10-15-2003
No you can't send a file to /dev/null. You could display a files contents by doing:
cat filename
by that doesn't use up the file. It's still there. So
cat filename > /dev/null
just wastes time.

To delete a file, do
rm filename
# 5  
Old 10-15-2003
I'm trying to understand what you said about /dev/null...

Looking your example, send this file (that prints irrelevant stuff)to /dev/null would be the same as using "cut" command to cut out this "garbage" ?
# 6  
Old 10-15-2003
no, if your actually talking about file contents then this is different. you can redirect output to /dev/null as an example. your console is actually referenced to as a file in unix, same as the printer and everything else. so if you have a program that outputs "hello world" you could, instead of sending it to the console, redirect it to the printer so it prints, or to /dev/null. the out put sent to /dev/null does not get stored in a file, nor does it get output to anywhere. its gone.

your 'cat /dev/null > file' example would just create a file with no data in it what so ever, its size would be zero.
# 7  
Old 10-15-2003
Maybe a couple more examples will make it clear.

echo hello
echo hello > garbage.txt
echo hello > /dev/tty
echo hello > /dev/null

The first one will display hello for you to see, and the second one will send the word hello into a file. I think that you got that much.

The third one is explicitly doing for the shell did automatically. /dev/tty is not your ordinary file, it is a "special file". Instead of being stored in a file on disk, a special little program called a driver is going to get that "hello" string. The tty driver will figure out how to make the characters appear on your screen. And then it will tell the OS, ok...that "hello" string went to the device.

The fourth one is also going to a special file and again a driver will get the string "hello". But this driver is just going to tell the OS, ok...that "hello" string went to the device. But there is no device and the null driver did not try to do anything. It just tosses the characters away.

You can read from /dev/tty. And the driver will wait for you to type something and read those characters and send them on.

You can read from /dev/null too, but the driver will always claim that there is no data.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Size of file /dev/./null 2>&1

Hello Can I just remove/delete flile ? rw-r--r-- 1 root system 2385088512 Jun 30 21:25 /dev/null 2>&1 size of this flile is 2274.75 m and fill up my filesystem: Filesystem MB blocks Used Free %Used Mounted on /dev/hd4 2560.00 2558.59 1.41 100% / (10 Replies)
Discussion started by: primo102
10 Replies

2. AIX

/dev/null file issue

Hi Experts, I Have a query. In one of my server I just came to know that there was /dev/null file which is a not a character file. Its just a normal file. I know the command to create the character file (/dev/null) but what is the procedure. Like should i delete /dev/null and create or... (7 Replies)
Discussion started by: jayadeava
7 Replies

3. Shell Programming and Scripting

Redirecting standard out to /dev/null goes to file "/dev/null" instead

I apologize if this question has been answered else where or is too elementary. I ran across a KSH script (long unimportant story) that does this: if ; then CAS_SRC_LOG="/var/log/cas_src.log 2>&1" else CAS_SRC_LOG="/dev/null 2>&1" fithen does this: /usr/bin/echo "heartbeat:... (5 Replies)
Discussion started by: jbmorrisonjr
5 Replies

4. UNIX for Dummies Questions & Answers

/dev/null a file using xargs

Hi, I'm currently using the following command to wipe clean a log file which can't be straight out RM'd: cat /dev/null > server.log I'm building this into a script and I'm current working on a command to run on each machine to do this automatically however I have multiple files so I need... (11 Replies)
Discussion started by: Deehem
11 Replies

5. UNIX for Dummies Questions & Answers

/dev/null 2>&1 Versus /dev/null 2>1

How are these two different? They both prevent output and error from being displayed. I don't see the use of the "&" echo "hello" > /dev/null 2>&1 echo "hello" > /dev/null 2>1 (3 Replies)
Discussion started by: glev2005
3 Replies

6. Shell Programming and Scripting

/dev/null what is the use of it?

when do you use the path /dev/null (3 Replies)
Discussion started by: webmunkey23
3 Replies

7. Solaris

What is /dev/tty /dev/null and /dev/console

Hi, Anyone can help My solaris 8 system has the following /dev/null , /dev/tty and /dev/console All permission are lrwxrwxrwx Can this be change to a non-world write ?? any impact ?? (12 Replies)
Discussion started by: civic2005
12 Replies

8. UNIX for Advanced & Expert Users

/dev/NULL can't open this file ??

when i write the following two statements : cp /dev/NULL /clocal/mqbrkrs/user/mqsiadm/sanjay/dspmq_temp cat /dev/NULL > /clocal/mqbrkrs/user/mqsiadm/sanjay/dspmq_temp its gives me errors like : cp: /dev/NULL: A file or directory in the path name does not exist. cat : /dev/NULL can't open... (2 Replies)
Discussion started by: varungupta
2 Replies

9. Shell Programming and Scripting

> /dev/null

hello all, In many shell scripts i found '> /dev/null' , i am not able to get this, will any one please explain why we are using this. thanks sudha (2 Replies)
Discussion started by: rrs
2 Replies

10. Solaris

URGENT - setup port and dump all output to /dev/null or a file

Please help urgently. I need to setup up some sort of service on a solaris server on a port. I dont need it do anything special, anything that is sent to this port from an external server should be dump to /dev/null or a flat file.. Can you help urgently? (1 Reply)
Discussion started by: frustrated1
1 Replies
Login or Register to Ask a Question