 |
06-07-2008
|
#1 (permalink)
| | Just 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 (permalink)
| | Linux Engineer
Join Date: Apr 2008 Location: Tokyo, Japan
Posts: 770
| 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
|
#3 (permalink)
| | Linux Engineer
Join Date: Nov 2007 Location: Córdoba (Spain)
Posts: 1,159
| Or even Code: cat /dev/null > operating_system
Or even Code: : > operating_system
The latest one ":" is a bash builtin, so, it might not be portable if you use another shell. |
| |
06-08-2008
|
#4 (permalink)
| | Linux Engineer
Join Date: Feb 2005
Posts: 1,003
| 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? |
| |
06-09-2008
|
#5 (permalink)
| | Just 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 (permalink)
| | Trusted Penguin
Join Date: Jan 2005 Location: Seattle, WA, USA
Posts: 2,472
| 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: Code: my_program_that_might_produce_debug_information 2> /dev/null
However, as we see here, trying to read from /dev/null to write the contents to file will produce nothing, again creating this effect.
I personally suggest "touch", though.
__________________
DISTRO=Gentoo
Registered Linux User #388732
Gentoo Linux, 410 GB HD, 1.2 GB RAM, Fluxbox, These are a Few of my Favorite Things
|
| |
06-09-2008
|
#7 (permalink)
| | Linux Engineer
Join Date: Nov 2007 Location: Córdoba (Spain)
Posts: 1,159
| Quote:
Originally Posted by scm 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? | 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
|
#8 (permalink)
| | Linux Engineer
Join Date: Feb 2005
Posts: 1,003
| Quote:
Originally Posted by i92guboj 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. | 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! Quote: |
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.
| 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.
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
|
#9 (permalink)
| | Linux Engineer
Join Date: Nov 2007 Location: Córdoba (Spain)
Posts: 1,159
| Quote:
Originally Posted by scm 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.  | I don't get that analogy, but maybe it's just me. Quote: |
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 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).
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  |
| | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | Free Magazines | Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business. subscribe | Systems Management News, the newspaper for IT systems administration and data center managers!
Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field. subscribe | The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business. subscribe | Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company. subscribe | Total Telecom Total Telecom is "The Economist of the communications industry". subscribe | | More free magazines » | All times are GMT. The time now is 04:34 AM. |
| |