Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Misc
Reload this Page Need help with linux command?
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Misc Any questions or Linux discussion that does not fit in any of the other technical areas.

Reply
 
Thread Tools Display Modes
Old 06-07-2008   #1 (permalink)
fm_hyudin
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.

fm_hyudin is offline   Reply With Quote
Old 06-07-2008   #2 (permalink)
khafa
Linux Enthusiast
 
khafa's Avatar
 
Join Date: Apr 2008
Location: Tokyo, Japan
Posts: 719
to create a zero size file named operating_system you do
Code:
touch operating_system
__________________
Linux and me it's a love story
khafa is offline   Reply With Quote
Old 06-08-2008   #3 (permalink)
i92guboj
Linux Engineer
 
Join Date: Nov 2007
Location: Córdoba (Spain)
Posts: 1,036
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.
i92guboj is offline   Reply With Quote
Old 06-08-2008   #4 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 984
The quickest way to create an empty file in any decent shell (sh, bash, ksh but not csh) is
Code:
>operating_system
You certainly shouldn't be using cat with /dev/null - whatever next?
scm is offline   Reply With Quote
Old 06-09-2008   #5 (permalink)
fm_hyudin
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.
fm_hyudin is offline   Reply With Quote
Old 06-09-2008   #6 (permalink)
Cabhan
Linux Guru
 
Cabhan's Avatar
 
Join Date: Jan 2005
Location: Seattle, WA, USA
Posts: 2,409
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
Cabhan is online now   Reply With Quote
Old 06-09-2008   #7 (permalink)
i92guboj
Linux Engineer
 
Join Date: Nov 2007
Location: Córdoba (Spain)
Posts: 1,036
Quote:
Originally Posted by scm View Post
The quickest way to create an empty file in any decent shell (sh, bash, ksh but not csh) is
Code:
>operating_system
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.
i92guboj is offline   Reply With Quote
Old 06-10-2008   #8 (permalink)
scm
Linux Engineer
 
Join Date: Feb 2005
Posts: 984
Quote:
Originally Posted by i92guboj View Post
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!
scm is offline   Reply With Quote
Old 06-10-2008   #9 (permalink)
i92guboj
Linux Engineer
 
Join Date: Nov 2007
Location: Córdoba (Spain)
Posts: 1,036
Quote:
Originally Posted by scm View Post
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
i92guboj is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 02:49 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0