Results 1 to 9 of 9
Hi..
I need to do my assignment related to linux command by testing them and do a screenshot.
The question that i got stuck is
1. Create a file, namely ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 06-07-2008 #1Just Joined!
- Join Date
- Jun 2008
- Posts
- 5
Need help with linux command?
Hi..
I need to do my assignment related to linux command by testing them and do a screenshot.
The question that i got stuck is
1. Create a file, namely “operating_system” with zero size
I tried using these commands and its failed.I tried ubuntu and opensuse.Plz help me.
I got these information from a website on which command to use for my problem but i still can't get the command to work.
Plz help me..Thanks in advance.
- 06-07-2008 #2
to create a zero size file named operating_system you do
Code:touch operating_system
Linux and me it's a love story
- 06-08-2008 #3Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Or even
Or evenCode:cat /dev/null > operating_system
The latest one ":" is a bash builtin, so, it might not be portable if you use another shell.Code:: > operating_system
- 06-08-2008 #4Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
The quickest way to create an empty file in any decent shell (sh, bash, ksh but not csh) is
You certainly shouldn't be using cat with /dev/null - whatever next?Code:>operating_system
- 06-09-2008 #5Just Joined!
- Join Date
- Jun 2008
- Posts
- 5
thanks
thanks alot for all your reply.i'll try to do as you all said and give it a try.thanks alot again.god bless all.
- 06-09-2008 #6
Just to give a little bit more analysis:
You seem to have worked "operating system" into all of your commands: this is entirely unnecessary. I don't know where you got this from, but you just type the command you want: "ls file", "cat file", etc.
As far as creating a 0-length file, although answering homework questions is against the rules, people have already done so in this thread, so I may as well explain more.
The official way to do this is to use the "touch" command. touch simply updates the timestamp on a file to the current time. However, if the file does not exist, it is created, but since we have nothing to put in it, it is created with no content.
The ": > file" and "> file" approaches are both Bash-specific, so I won't bother with them. But the "cat /dev/null > file" approach is interesting. /dev/null is a special file that will always produce nothing when you try to read it, and will not remember anything when you write to it. It is essentially a dump location. Its most common usage is when you want to discard output from a program:
However, as we see here, trying to read from /dev/null to write the contents to file will produce nothing, again creating this effect.Code:my_program_that_might_produce_debug_information 2> /dev/null
I personally suggest "touch", though.
- 06-09-2008 #7Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
Can you elaborate a bit on the downside of that method? I am not aware of any problem. It's essentially a device which produces nothing, and consumes everything (staying empty no matter what do you dump into it). So, it's usage to create an empty file seems not only possible, but also a logical thing.
What is exactly the problem? I would personally just use "touch", like everyone else, but I don't know of a valid reason to disregard the usage of any of the alternatives that have been mentioned, except those that are specific to a given shell.
- 06-10-2008 #8Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Well, if you're the kind of person who thinks it's logical to employ domestic help but clean the house before they arrive, then it's logical.

Bear in mind that the objective is to create an empty file. The shell has already done that by the output redirection before it's even thought about running the cat command, which will then open a file, read (nothing) from it and write that to its output stream. cat does nothing to achieve the objective so is completely redundant. Cabhan's comment that >filename is bash specific is not true - it's been a feature of the original Bourne shell for over 30 years and any self-respecting shell will be compatible, so if you're going to rely on output redirection creating/truncating a file, there's no need to pad it out with unnecessary and irrelevant commands. At least use "cp" which will create the wanted file!
There's always more than one way to do anything in UNIX/Linux, and no way is necessarily more "right" than another, but my personal preference is to use the most effective commands in the most efficient way, and using the shell to create the file is more efficient than getting it to run a command to do the same thing. I still shudder when I see "cat file | grep pattern" - you can achieve exactly the same effect with "grep pattern <file". cat has its uses and I'd be lost without it, but it's not always the best tool.What is exactly the problem? I would personally just use "touch", like everyone else, but I don't know of a valid reason to disregard the usage of any of the alternatives that have been mentioned, except those that are specific to a given shell.
touch is more appropriate, but is still doing unnecessary work setting timestamps, and also doesn't truncate extant files, which >filename will do. And you're still running one more command than you need to!
- 06-10-2008 #9Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
I don't get that analogy, but maybe it's just me.
I am talking about logic, and not about efficiency. I know how the redirection works. The question, again, is why do you think that redirecting from stdin (which produces nothing) is any better than redirecting from /dev/null, which, indeed, produces nothing as well. Yes, there's the overhead of cat, but I doubt anyone will scream at that. As you said, "touch" is also an overhead, but it's the method most people choose. So, the "overhead excuse" has no meaning at all for me in this situation (it would have a meaning if I wanted to create one trillion empty files).Bear in mind that the objective is to create an empty file. The shell has already done that by the output redirection before it's even thought about running the cat command, which will then open a file, read (nothing) from it and write that to its output stream. cat does nothing to achieve the objective so is completely redundant.
I quite see the point and, as I said, I would not use that form. But I was talking about correctness. And, as far as I can tell, it's correct. Useless typing, for sure. Dumb, I know. Something I'd never do, you can be sure. But, nonetheless: it's correct. The purpose was to illustrate alternatives.
The amazing thing that the linux shells is that you have a wide offer of possibilities to do anything, I think we all are happy about that. Sometimes, the shorter or more efficient command is not the one that most people do use, just because of the mental model of the user. Some people might get the concept better by associating it with previously learned behaviors/commands. Even if the "correct" or "efficient" way is easier for the trained eye. Bear in mind that even the leeter command will be useless if you can't remember it (even if it's much shorter than a piped oneliner or whatever.
I know for sure how to use grep, I use it everyday and I have learned somewhat to use it correctly without piping anything. However, for some reason, that was the first way that fitted into my mind, more than ten years ago. And, even if I now I have re-assimilated most of the stuff I learned those days, I still find myself occasionally typing something like "cat file | grep foo" for no reason. It's something automatic that happens from time to time.
Heck, if you don't like piping you could even use redirection as well, like "grep foo < filename". It's just wonderful


Reply With Quote

