1K Linux Commands

From wiki.ucalgary.ca
Revision as of 04:58, 27 March 2014 by JohnAdams71 (talk | contribs) (minor updates)
Jump to: navigation, search

1024 Commands: An Introduction to the Linux Command Line

A shell is just a program stored on a computer. When executed, the shell provides an interactive process for issuing commands. For users accustomed to interacting with a computer through a GUI, mouse, or touchscreen, a command line can seem daunting, particularly since the "prompt" (a terse snippit of characters indicating the input location) does very little prompting and often spits back obtuse error messages.

This brief tutorial demonstrates some of the basics of the Linux command line environment using the Bash shell. A reader should have no trouble using another shell as this tutorial largely avoids shell-specific features. Instead, it focuses on a general set of Linux commands.

The CPSC Department Tech Staff have a quick start command line guide at:

https://www.cpsc.ucalgary.ca/tech_support/help/unix_commands

The Basics

A shell is a program that helps a user interact with a computing environment. The core functionality of most shells, particularly in a Unix environment, is to locate and execute programs on behalf of the user. The user types a command; commands typically include the name of a program and some arguments or data for that program to operate on. The shell locates the named program, supplies the arguments to the program, and waits for the program to complete.

It is important to realize that the shell typically acts like a dispatcher -- except in the cases where it uses its own built-in functionality (or in cases where the user invokes shell-specific scripting commands), the shell merely looks up the location of the named program and asks the operating system to execute that program. Commands (most of which are separate programs) are typically stored in a few well-known locations in the file system, such as /bin, /usr/bin, /sbin, /usr/sbin and the user's own bin/ directory in their home directory. For example, the /bin directory of a recent distribution of Linux includes more than 100 commands, including ones like `arch', `cat', `cp', and `date'. It has over 1800 commands in the /usr/bin directory, 280 in /sbin, and 375 in /usr/sbin. This amounts to more than 2500 possible programs to run.

Unix and Linux environments come with a large variety of commands. Commands are typically external programs, although some functionality is built into the shell because it is simple.

Takeaway Message: the shell is a program that waits for your input. You provide input by typing commands. Commands start with the name of some program and have optional arguments or parameters.

Getting Oriented

So your prompt is waiting for you. Patiently. Before we tell it to do something, let's give you a fallback so you know where to get help --- right in your shell (without resorting to digging through Google results or bugging your friends or system administrator).

The first place to start is knowing how to access documentation. Most Linux shell environments come equipped with a command to do this, the `man' (short for "manual" command). You can supply the `man' command with a number of arguments to control its behavior in various ways, but the simplest form is by giving it just the name of a program whose functionality you'd like to determine. For example, typing the following command and hitting <enter>:

[michael@host 1k]$ man man

causes the manual page for the `man' command to be displayed in the terminal window. Man pages provide the name and brief explanation of the command, a short synopsis, and a list of accepted arguments to the command. You can stop viewing the manual page by typing the letter `q'.

Takeaway Message: Use the manual pages to explore the rich functionality of the many different Linux commands available on a typical system. When in doubt, read the man page!

1024 Commands

This tutorial is based on a history of 1024 commands I gathered from one of my terminal windows (I have multiple open, each with their own extensive history). The history of each shell sessions is maintained in memory by the shell during runtime and written to the file .bash_history when the shell exits.

One thousand commands didn't seem like enough, and 1024 is a nice power of two.

So, what have I been typing for the last one-thousand twenty four commands? The simplest way to find out is to ask the shell with the `history' command:

[michael@host 1k]$ history
1  clear
2  more disorder.c
3  more ../include/disorder.h
4  more disorder.c
5  clear
6  cat disorder.c
7  clear
8  grep 8080 /etc/services
...
[michael@host 1k]$

What the shell displays is a numbered list of all commands it has kept track of. Already we can see a few different commands in play: `clear', `more', `cat', and `grep' -- and these commands seem to be operating on some additional data.

How can I save this history into a file so I can work with it? Fortunately, the shell provides input and output redirection. As you noted, the `history' command dumps its output to the terminal screen (named stdout for "standard output"). Using the output redirection character > we can send this output to a new file named 1k.dat.

[michael@host 1k]$ history 1024 > 1k.dat
[michael@host 1k]$

Here, we've asked the shell to print the history of the last 1024 commands, but instead of sending the output to the stdout, we've asked the shell to take that output and write it to a file named `1k.dat' (there is nothing special about this file name, and nothing special about the extension ".dat" -- we could have easily named the file just `1k'...file names in Unix do not require extensions.

One thing you'll notice about the command line is that it isn't very chatty -- when things work, it just returns a prompt to you. When things don't, it often returns a terse error message:

[michael@host 1k]$ foo
bash: foo: command not found
[michael@host 1k]$

Here, the shell (named `bash') tried to lookup and find the program named `foo', but did not find it (how this lookup proceeds is outside the scope of this tutorial, but the shell basically searches some common locations like /bin and /usr/bin).

Now that we have our last 1024 commands saved in a file, let's find out what those commands actually were. One way to do this would be to spit out the file to the standard output again using the `cat' command or scroll through it a pageful at a time with the `more' command. I could even peek at the front few lines or last few lines using `head' or 'tail', respectively.

[michael@host 1k]$ head -4 1k.dat
1  clear
2  more disorder.c
3  more ../include/disorder.h
4  more disorder.c
[michael@host 1k]$

But these approaches aren't really helpful, particularly since we begin to see that I use some commands quite frequently. What I am really after, then, is a summary of this command history. What is the relative frequency of commands I use?

One solution is to whip up a quick AWK script to process the saved history file. Fro ... \n

PAUL: Another shutdown stickup

Rand Paul Washington Times October 19, 2013 During the shutdown, 85 percent of government stayed open despite the hoopla reported in the media. Government is now 100 percent open. Debt-ceiling deadlines have been averted, but the real problem remains: a $17 trillion debt and a president who continues to pile on new debt at a rate of $1 million a minute. The government ...
[PAUL: Another shutdown stickup ]

['.GetDomainName().' - news, stories, articles]

Invasive: Obamacare exchange mistakenly debting bank accounts

ELISA HAHN KING 5 News December 15, 2013 For the second week in a row, the Washington Healthplanfinder website is down, and its causing problems for people who are dealing with billing issues. Some of them say the website is mistakenly debiting their accounts. Shannon Bruner of Indianola logged on to her checking account Monday morning, and found she was almost 800 ...
[Invasive: Obamacare exchange mistakenly debting bank accounts ]

['.GetDomainName().' - news, stories, articles]

Markets Tumbling All Around The World Economy Set To Slow Debt Ceiling Crisis Grows

JOE WEISENTHAL Business Insider October 7, 2013 Good morning! Markets are tumbling around the world. US futures are set to open sharply lower this morning after a weekend of heightened debt ceiling rhetoric. Not only was there absolutely no progress on any kind of deal but on the Sunday shows, John Boehner insisted that the US was on the path to default if Obama...
[Markets Tumbling All Around The World Economy Set To Slow Debt Ceiling Crisis Grows ]

['.GetDomainName().' - news, stories, articles]

Credit Card Company Cuts Ties With Countrys Largest Gun Shop Over Gun Sales

The countrys largest gun store will lose its credit card processor this week, for allegedly violating a section of their service agreement that barred them from selling firearms. After four years in business with the Visa owned Authorize.net, Larry Hyatt, owner of Hyatt Gun Shop, received an email indicating that the company would no longer provide its services due...
[Credit Card Company Cuts Ties With Countrys Largest Gun Shop Over Gun Sales]

['.GetDomainName().' - news, stories, articles]

English school threatens to label kids racist for YEARS unless they attend Islam field trip

Eric Owens Daily Caller November 23, 2013 The headmistress at a primary school in a small town in England tried to order parents to send their children to a Nov. 27 workshop focused on Islam or else have the kids permanently labeled as racists for the rest of their academic careers. On Wednesday, parents of children aged 8 to 11 received the stark warning in the form of a ...
[English school threatens to label kids racist for YEARS unless they attend Islam field trip ]

['.GetDomainName().' - news, stories, articles]