Entering Commands

From the command line, everything is done from the keyboard. You type in a “command,” and Linux does what you tell it. For example, want to know the date? Use the date command, and Linux will tell you the date. If you'd like to try that right now, you can; go to the Applications->Accessories->Terminal menu, to open a terminal. Then type date, and press Enter. Linux will then tell you the date, similar to the following.

$ date
Mon Mar 10 21:15:42 EDT 2008

So what is a “command?” Actually, when you get right down to it, a command is simply an instruction to execute a particular program. In the example above, when we typed in date, date is actually the name of a program. So you tell Linux the name of the program you want it to run, and it runs it.

In most cases, you don't need to think about the fact that you're “executing a program” — you just think of it as entering a command. And it is fine to think that way; I'm sure most people do. The instructions on this site will often refer to “executing a command,” rather than “running a program.”

Many, if not most, of the progams we'll be talking about, regarding the command line, are text-based. This means that they don't have a graphical user interface (GUI), they just accept input from your keyboard, and show characters on the screen. When you run date, it displays the date on the screen, and exits.

However, you can execute any program from the command line, if you know its name. For example, when I want to edit a text file, I usually use the Applications->Accessories->Text Editor menu, to open it. But if I happened to already be working from the command line, I could also open it by entering gedit, which would execute the program.

Unfortunately, if I were to do so, I wouldn't be able to enter any more commands, until gedit closed — the command prompt will wait until the program exits, before allowing me to enter another command.


We'll see later on how we can have our cake and eat it too, by allowing a program to run in the background, but in the meantime, you can always open up another terminal window, and start another command prompt, if you want to enter commands while gedit is still open.


Sometimes, a program needs more information before it can do its work. Or perhaps you can make it work slightly differently, depending on your needs. For example, when we typed in the date command, above, it gave us the date in the format “Mon Mar 10 21:15:42 EDT 2008.” What if you don't need quite that much information? You could modify the way that the date application displays the date, by giving it parameters. Just need to know the day of the week? Enter the following:

$ date +"%A"

As you can see, we typed in the name of the program, date, just like we did before, but then we put in a space, and some more text. The extra stuff we typed, after the name of the program, is a parameter. A parameter is information that is passed to the application, which the application can decide to use while its running. What parameters are available, and what can they do? That depends on the command. Each command has its own parameters, which means that you have to learn the details about each command: what parameters are allowed (and what parameters are optional vs. mandatory), and how to specify them.

In this case, I passed a string telling the application how to format the date, because the date command knows how to interpret that parameter. We can also be more complex about it, if we wish:

$ date +"%A, %B %d%n%C%g"
Monday, March 10

Some commands/programs — in fact, most of the ones we'll be looking at — can take multiple parameters. Each parameter would be separated by a space1. Let's look at another example; if you want to see a calendar of the current month, you can use the cal command.

$ cal
     March 2008     
Su Mo Tu We Th Fr Sa
 2  3  4  5  6  7  8
 9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

By default, cal will show the current month. (When I executed this command, it was March of 2008.) And, although you can't see it in the example above, the current day will be highlighted. However, you can tell cal to show any month, from any year. What if you want to know what day of the week May 7th2 will be? You can do this:

$ cal 5 2008
      May 2008      
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31


In this case, we executed the cal command, and then we passed two parameters: 5 and 2008. We entered 5 because we wanted to see the calendar for May — the fifth month — and then the year.

The parameters for some commands/programs are very simple, and some are extremely complex. In many, if not most, cases, you can find out more information about a command by entering the --help parameter. For example, if you were to look into all of the various options for the date command, you could easily lose an entire afternoon trying to figure it all out:

$ date --help
Usage: date [OPTION]... [+FORMAT]
  or:  date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.

  -d, --date=STRING         display time described by STRING, not `now'
  -f, --file=DATEFILE       like --date once for each line of DATEFILE
  -r, --reference=FILE      display the last modification time of FILE
  -R, --rfc-2822            output date and time in RFC 2822 format
      --rfc-3339=TIMESPEC   output date and time in RFC 3339 format.
                            TIMESPEC=`date', `seconds', or `ns' for
                            date and time to the indicated precision.
  -s, --set=STRING          set time described by STRING
  -u, --utc, --universal    print or set Coordinated Universal Time
      --help     display this help and exit
      --version  output version information and exit

FORMAT controls the output.  The only valid option for the second form
specifies Coordinated Universal Time.  Interpreted sequences are:

  %%   a litreal %
  %a   locale's abbreviated weekday name (e.g., Sun)
  %A   locale's full weekday name (e.g., Sunday)
  %b   locale's abbreviated month name (e.g., Jan)
  %B   locale's full month name (e.g., January)
  %c   locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
  %C   century; like %Y, except omit last two digits (e.g., 21)
  %d   day of month (e.g, 01)
  %D   date; same as %m/%d/%y
  %e   day of month, space padded; same as %_d
  %F   full date; same as %Y-%m-%d
  %g   last two digits of year of ISO week number (see %G)
  %G   year of ISO week number (see %V); normally useful only with %V
  %h   same as %b
  %H   hour (00..23)
  %I   hour (01..12)
  %j   day of year (001..366)
  %k   hour ( 0..23)
  %l   hour ( 1..12)
  %m   month (01..12)
  %M   minute (00..59)
  %n   a newline
  %N   nanoseconds (000000000..999999999)
  %p   locale's equivalent of either AM or PM; blank if not known
  %P   like %p, but lower case
  %r   locale's 12-hour clock time (e.g., 11:11:04 PM)
  %R   24-hour hour and minute; same as %H:%M
  %s   seconds since 1970-01-01 00:00:00 UTC
  %S   second (00..60)
  %t   a tab
  %T   time; same as %H:%M:%S
  %u   day of week (1..7); 1 is Monday
  %U   week number of year, with Sunday as first day of week (00..53)
  %V   ISO week number, with Monday as first day of week (01..53)
  %w   day of week (0..6); 0 is Sunday
  %W   week number of year, with Monday as first day of week (00..53)
  %x   locale's date representation (e.g., 12/31/99)
  %X   locale's time representation (e.g., 23:13:48)
  %y   last two digits of year (00..99)
  %Y   year
  %z   +hhmm numeric timezone (e.g., -0400)
  %:z  +hh:mm numeric timezone (e.g., -04:00)
  %::z  +hh:mm:ss numeric time zone (e.g., -04:00:00)
  %:::z  numeric time zone with : to necessary precision (e.g., -04, +05:30)
  %Z   alphabetic time zone abbreviation (e.g., EDT)

By default, date pads numeric fields with zeroes.
The following optional flags may follow `%':

  - (hyphen) do not pad the field
  _ (underscore) pad with spaces
  0 (zero) pad with zeros
  ^ use upper case if possible
  # use opposite case if possible

After any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either
E to use the locale's alternate representations if available, or
O to use the locale's alternate numeric symbols if available.

Report bugs to <bug-coreutils@gnu.org>.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.