LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-22-2014, 01:54 PM   #1
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Rep: Reputation: Disabled
General approach to command line commands


Is there a general structure for various commands?

For instance, it seems like -- always means the option is a word, and - means the option is a single character. Is this always the case? Also, does capital verse under-case represent anything?

Also, some commands have a secondary command. For instance, "yum install foo" has the secondary command "install". Do these always go after the primary command? Furthermore, is there a specific order like command, then subject, then options, then???

Lastly, some commands like iptables describes "targets", "tables", and "options". For the options, they are further broken down as "commands", "parameters", and "other options". Is some structure like this used on other commands, and what does the order mean?

Last edited by NotionCommotion; 03-22-2014 at 02:07 PM.
 
Old 03-22-2014, 02:59 PM   #2
redd9
Member
 
Registered: Nov 2013
Location: Canada
Distribution: Ubuntu
Posts: 80
Blog Entries: 4

Rep: Reputation: 42
Well, I'm no expert, but I think this is right. Someone please correct me if I'm wrong.

Yes, there is a pattern that is generally followed. For your first question, yes, that's always the case. The single letter options are sometimes just short for the whole option. For example:
Code:
foo --bar
may mean the same thing as
Code:
foo -b
When a command has capital and undercase options, it usually just means that there are a lot of options, and that there are two that would logically start with that letter. For example, the man page of ls:
Code:
       -i, --inode
              print the index number of each file

       -I, --ignore=PATTERN
              do not list implied entries matching shell PATTERN
Third question: for yum (and many other commands) it may be helpful to think of them as programs. Install would be one thing you could do with that program, and would always go after yum. There is a specific order, which you can look at by going to the man page of the command and looking under 'Synopsis'. For the 'ping' command, the format is command-option-option with argument-argument of option-argument of command. However, it's not writ in stone, you can do the option(s) with arguments first. For example:
Code:
 ping -a -c 3 google.com
I honestly don't know about the rest. Sorry.

Last edited by redd9; 03-22-2014 at 03:03 PM.
 
Old 03-22-2014, 03:26 PM   #3
jefro
Moderator
 
Registered: Mar 2008
Posts: 15,388

Rep: Reputation: 2199Reputation: 2199Reputation: 2199Reputation: 2199Reputation: 2199Reputation: 2199Reputation: 2199Reputation: 2199Reputation: 2199Reputation: 2199Reputation: 2199
Many of the commands are very old and have no common design. I doubt they will ever be recoded to be common.

As noted, using the man page is the best step.
 
Old 03-22-2014, 04:50 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Use the man pages...

Now some attempt to standardize has been attempted, but not all commands use the getopt function.

The general convention is that a "-" at the beginning of a parameter is to be interpreted as a flag for one or more options that follow (this is the "option is a single character" mode). As such, a "cmd -abc" indicates that the "cmd" has options a, b and c. These can ALSO be specified by "cmd -a -b -c" as being equivalent.

IF one or more options take a parameter (such as an input file) then saying "cmd -abc file" where option b takes a file, is the usual. This does show where some confusion gets in, so usually people avoid that by using "cmd -a -b file -c" or "cmd -ab file -c" to help associate the "file" with option b.

Some command have "long names", usually indicated by a "--". This is only a convention, as "--" is also used to indicate the end of parameters for some commands. The "startx" utility is one of these. startx is used to initiate an X server when no X server is running, OR you want a special one running. In which case you use something like "startx cmd -abc -- xservertorun optionstoxserver"...

The tar utility is an exmple of an utility that has both long and short options. The old standard options allow for a simple quick backup file - "tar -cf ../file.tar ." where the "-c" is to create an archive file, the f (or -f) specifies the file ("../file.tar") to create, and the "." is the current directory to be archived. It also allows long names to be used. the "-A" option directs tar to append to an existing archive file... But to make scripts a little more self documenting, the long name can be used instead "--concatenate". For "tar" I believe all short options have long equivances, the "-c" option has a long name "--create". Tar also has long names for options that don't have a short name (--delete directs tar to delete a file from an archive, but the obvious short name "-d" historically means "diff" to identify differences between an archive and what is on disk.

The most unique command is "dd". This command has neither short, nor long name options that follow this convention. This utility goes back to the original UNIX where it meant "dump data". It was originally used to make copies of entire disks, or to copy data directly from a tape into a file, or onto a disk...

The dd command used named options - where "name=value" is done, and the parameter is parsed using the "=" character to identify the option and its associated value. An example is "bs=10k", where for dd this means "use a Buffer Size of 10k bytes". To specify an input file you would use "if=infile" where "if" stands for "Input File", and the "infile" here could just as easily be a normal file or a device (disk/tape/...). A similar option was "of=tofile" where "of" is interpreted as "Output File" and the "tofile" is where to put the result.

This is why you have to refer the man pages relatively frequently.

I still do, a lot because there are thousands of commands available, though not all are necessarily installed, and each may have a different way of handling things, the letters used can mean different things... -f as an option is USUALLY used to specify a file name... but in image processing/camera processing it could mean the fstop, or a filter matrix. The only way to know for sure is to read the manpage or see if the utility knows what -h (help) or --help may show. Though you do have to be careful (shutdown -h means "shutdown the system and halt", so don't expect to get help information).
 
3 members found this post helpful.
Old 03-22-2014, 08:07 PM   #5
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
Thanks jpollard,

Great post. I read it in detail, and will need to read it several times more.

Yes, I know, use the man! But, I am sure you and others know what is typical and look for the exceptions. For me, it is still spaghetti.

I never new that -abc was the same as -a -b -c. This will keep me for looking for option abc which obviously does not exist.

You also describe how options can take parameters, and that the parameter "should" but doesn't necessarily have to follow the option. How did you know that typical commands should be "cmd -a -b file -c" and not "cmd -a file -b -c" where "file" is a parameter to option b? I know (now) that it isn't, and I am sure the individual man's describes this, but is their any beginner instruction manual or blog or FAQ in this forum that describes what is obvious to you experts?

Thanks again!!!
 
Old 03-22-2014, 09:08 PM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Quote:
Originally Posted by redd9 View Post
Yes, there is a pattern that is generally followed. For your first question, yes, that's always the case.
Sorry, but no, this is not always the case. It is totally up to the developers if they follow any convention at all. Just have a look at the find command, which has options in the long format, but only uses one -.
 
Old 03-22-2014, 09:21 PM   #7
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TobiSGD View Post
Sorry, but no, this is not always the case. It is totally up to the developers if they follow any convention at all. Just have a look at the find command, which has options in the long format, but only uses one -.
Hi TobiSGD, I have learned the hard way that that there is nothing absolute, and there will always be exceptions. But if you knew nothing about a given command, would you assume anything? Is there a 80/20% rule? I've looked for these rules of thumb, but haven't found any (that being said, I promise I will always consult the man!)
 
Old 03-22-2014, 09:44 PM   #8
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Debian, Mint, OpenBSD
Posts: 11,352
Blog Entries: 12

Rep: Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731Reputation: 2731
The great failing of man pages is that examples are not part of the default format. Often, doing a websearch for linux [command] examples can be helpful.

For example, he said recursively, "linux fdisk examples" yielded this.
 
Old 03-22-2014, 09:49 PM   #9
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Quote:
Originally Posted by NotionCommotion View Post
Hi TobiSGD, I have learned the hard way that that there is nothing absolute, and there will always be exceptions. But if you knew nothing about a given command, would you assume anything? Is there a 80/20% rule? I've looked for these rules of thumb, but haven't found any (that being said, I promise I will always consult the man!)
If I know nothing about a given command I assume nothing, I have a look at the available documentation, usually man- or info-pages or documentation in tutorials and the developers website. Dealing with computers is a field where you usually do not come wide when you guess things, the correct way is to actually learn what you need to know.
 
Old 03-22-2014, 10:10 PM   #10
sgosnell
Senior Member
 
Registered: Jan 2008
Location: Baja Oklahoma
Distribution: Debian
Posts: 1,054

Rep: Reputation: 276Reputation: 276Reputation: 276
Most of the time, using "cmd --help" will tell you what you need to know. If it doesn't, then use the man pages. Assumptions can be dangerous. I would be very afraid of using syntax that had a 20% chance of being wrong, so an 80/20 rule would be useless to me. Linux is what it is, and there is no person or organization that has the authority to enforce any rules. Developers are free to use whatever conventions they choose, and some can be rather surprising, at least to me. When in doubt read the man pages, and then read them again.
 
Old 03-22-2014, 11:01 PM   #11
NotionCommotion
Member
 
Registered: Aug 2012
Posts: 536

Original Poster
Rep: Reputation: Disabled
Thanks guys (and maybe gals if there are any),

I understand your position and agree. But you know what is "normal" (at lest in your perception of normal) and what to look for when it isn't normal. But I see a million totally unrelated man pages and am trying to memorize each step as I go and am having a hard time. Maybe it is just me, but I do better by having a concept in my head and memorizing differences. Perhaps a pipe dream (oh yea, piping, that is an important concept!).

PS. I agree, Frank, I like an example now and then.
 
Old 03-22-2014, 11:04 PM   #12
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by TobiSGD View Post
Sorry, but no, this is not always the case. It is totally up to the developers if they follow any convention at all. Just have a look at the find command, which has options in the long format, but only uses one -.
I said generally, and pointed out that not all follow that convention.

If I remember right, find doesn't use the getopt function, it has its own way of parsing parameters, just as dd does. For find the major options are things like -depth (where -d is provided as an extension for compatibility with BSD variants), -print, -exec. If they were following the getopt standard, these couldn't be used... To make things really tricky find has a logical expression allowing various options to be combined with "and", "or", and "not" operations. In that case, order of the parameters becomes important.

The -- long form is only the GNU extension to the getopt. The standard getopt is defined by XOpen UNIX standards. GNU has a getopt_long and getopt_long_only that function a bit differently.

Last edited by jpollard; 03-22-2014 at 11:16 PM.
 
Old 03-22-2014, 11:07 PM   #13
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by NotionCommotion View Post
Thanks jpollard,

Great post. I read it in detail, and will need to read it several times more.

Yes, I know, use the man! But, I am sure you and others know what is typical and look for the exceptions. For me, it is still spaghetti.

I never new that -abc was the same as -a -b -c. This will keep me for looking for option abc which obviously does not exist.

You also describe how options can take parameters, and that the parameter "should" but doesn't necessarily have to follow the option. How did you know that typical commands should be "cmd -a -b file -c" and not "cmd -a file -b -c" where "file" is a parameter to option b? I know (now) that it isn't, and I am sure the individual man's describes this, but is their any beginner instruction manual or blog or FAQ in this forum that describes what is obvious to you experts?

Thanks again!!!
It isn't obvious for which option takes the parameter - that is why the possible -h option (brief help) is sometimes useful, other times you have to refer to the man pages.

The only thing that does occur is that if two options take a parameter, then the order of the parameters SHOULD the same as the order the options are specified. So "cmd -abc file1 file2" where "a" is the option for a parameter, and "c" is another, then file1 goes to the "a" option, and file2 goes to the "c" option. If the options are in a different order such as "cmd -cba file2 file1" would then be specifying the same thing. It is confusing which is why most uses separate the options such that you get "cmd -a file1 -b -c file2".

I repeat the "SHOULD" because it is up to the developers of the individual commands as to how their options are interpreted.

Last edited by jpollard; 03-22-2014 at 11:13 PM.
 
Old 03-24-2014, 05:35 AM   #14
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
I'd also add that not all *nix have the same cmds available and(!) a given cmd can have different options/behaviour on different *nixes.
In other words, just because a cmd has the same name on different *nixes, it doesn't mean its constituent code is the same on each platform ...

Don't even try to memorize all the cmds you come across, let alone all the options as well; its just not possible.
Just get used to the common options on the cmds you use the most and read the man pages (and google for examples) for the rest.

Last edited by chrism01; 03-24-2014 at 05:37 AM.
 
Old 03-24-2014, 06:23 AM   #15
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,129

Rep: Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271Reputation: 2271
Quote:
Originally Posted by NotionCommotion View Post
Is there a 80/20% rule? I've looked for these rules of thumb, but haven't found any (that being said, I promise I will always consult the man!)
You should never assume any flag, argument or command line parameter. That will definitely cause an "unexpected" error somewhere later and you will never know where - and where is it coming from (and you will hardly find that your own assumption was the original failure).

This will be your own 20%.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Where are command line commands stored Otto-1062 Linux - General 12 03-03-2014 07:59 AM
Entering Command Line commands johnmtb Linux - Newbie 5 01-06-2009 02:26 AM
command line commands bcbotha Linux - Newbie 15 12-01-2008 09:05 AM
Java Commands for Command Line AceTech Programming 4 08-20-2006 03:55 AM
Time commands from command line ToothlessRebel Linux - Newbie 1 03-09-2005 11:52 AM


All times are GMT -5. The time now is 10:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration