A Beginner's Guide to the Linux Command Line

A Beginner's Guide to the Linux Command Line N814tDK

Linux is a free open source operating system (OS) based on UNIX that was created in 1991 by Linus Torvalds. This tutorial begins with an introduction to Linux shells (the programs that accept and interpret text-mode commands) and many of the basic commands and procedures you can use from a shell.

As with many key software components, Linux provides a range of options for shells. A complete list would be quite long, bash the GNU Bourne Again Shell (bash) is based on the earlier Bourne shell for Unix but extends it in several ways. In Linux, bash is the most common default shell for user accounts, and it’s the one emphasized in this tutorial.

When you first boot your Linux system and log in, your default shell is loaded. You can identify which shell you’re using by entering env at the current shell prompt. The env command lists all the environment variables for the currently logged-in user.

A Beginner's Guide to the Linux Command Line QJQPbGw

In the screenshot above, the bash shell is the default shell for the rtracy user account, as indicated by the SHELL variable.

If you want to switch to a different shell, simply enter the shell command at the prompt. For example, if you are currently using the bash shell and want to use zsh instead, simply enter zsh at the prompt.

To stop using the new shell and revert back to the original shell, simply enter exit.

A terminal and a shell whilst often used in conjunction with each other are very different beasts. A terminal is a program which enables you to access a shell. You can do pretty much anything in a terminal window that you can achieve in a more graphical environment but you do need to know the commands that are available.

For the most part, Linux commands are external that is, they're separate programs from the shell. A few commands are internal to the shell.

If you log into Linux using a text-mode login screen, you'll be dropped directly into default shell, the shell is what presents the prompt and accepts subsequent commands. If you log into Linux using a graphical user interface (GUI) login screen, though, you’ll have to start a shell manually. Some GUIs provide a menu option to start a program called a terminal, xterm, Konsole, or something similar. These programs enable you to run text-mode programs within Linux, and by default they come up running your shell. Launch a terminal from your desktop’s application menu and you will see the bash shell. There are other shells, but most Linux distributions use bash by default.

A Beginner's Guide to the Linux Command Line ZovZJ0M

Commands are given to the operating system on the bash shell. The dollar sign is called a prompt, and it’s the mechanism Linux uses to tell you that it’s ready to accept a command. At the shell, you can enter a single command or a combination of commands and options, the sum of which is called a command line. You can use commands directly to do something, such as moving and copying files. You can also use commands to run other programs. Linux has a specific set of commands, so if you type in something that doesn’t match one of its commands, it tells you it can’t find the command. There are literally hundreds of commands in the Linux operating system.




Running Commands at the Shell Prompt

Running a program or command from the shell prompt is relatively easy. You just type the command or program file name at the shell prompt and press Enter key.

The ls command prints a listing of files and directories within the current directory on screen.

A Beginner's Guide to the Linux Command Line PWZJ6cL

It writes results to standard output. The ls command supports showing a variety of information about files, sorting on a range of options and recursive listing.

There are a number of other options available to do with time formatting. You can read about all the other options by reading the ls Linux Manual Page.

Code:

man ls


A Linux command normally consists of three parts: the command itself, the command options, and its arguments. For instance, the following example shows what a Linux command looks like:

Code:

useradd -m -G sales linda


This example consists of three parts, useradd, which is the command; -m and -G sales, which are both options; and linda, which is a generic argument. Notice that the word "sales" behind the -G is also a special item, it is an argument to the option -G.


Most commands have options as their second part. By using these options, you modify the behavior of the commands. Options are a part of the program code, they are fixed and the software programmer that created the program has provided the available options. For instance, the ls command just shows the names of files in the current directory. If you want to see details, such as the file size, the permissions that are set for it, and information about the creation date, you can add the option -l.

A Beginner's Guide to the Linux Command Line LKL8gBe

Options provide a method that is defined within the command code to modify the behavior of the command. Some commands don’t have any options, and other commands can have more than 50. The man command normally gives you a complete list of all options that are available. Alternatively, many commands support the option --help, which will show a summary of all available options.

Apart from options, many Linux commands have arguments. These are additional specifications that you can add to the command to tell it more precisely what to do, but the argument is typically not defined in the command code itself. For example, consider the command ls -l /etc/hosts. In this example, /etc/hosts is the argument. You can use any other file name instead of /etc/hosts, and this is typical for arguments. They are not fixed, and you can use any argument you like as long as it is relevant in the context of the command.

You should be aware that not only commands have arguments, but also options have arguments as well.

For example, consider the following command:

Code:

mail -s hello root


This command consists of four different parts:

  • mail: The command itself

  • -s: The option that tells the mail command what subject it should use

  • hello: The argument of the option -s, which specifies what exactly you want to do with the option -s

  • root: The argument of the command, which in this case makes clear to whom to send the mail message


As a rule of thumb, arguments at the end of the command are normally command arguments, and arguments for options are placed right next to the options.

Whenever you’re running a shell, you’re working in a specific directory. When you refer to a file without providing a complete path to the file, the shell works on the file in the current working directory. (Similar rules apply to many programs.) The cd command changes the current working directory. For instance, typing cd /home/sally changes to the /home/sally directory. The tilde (~) character is a useful shortcut; it stands for your home directory, so typing cd ~ will have the same effect as cd /home/sally if your home directory is /home/sally.

The pwd command displays the current working directory.

The echo command displays the text you enter; for instance, typing echo Hello causes the system to display the string Hello.

The exec command runs an external program that you specify, as in exec myprog to run myprog. In most cases, this is better accomplished by typing the name of the program you want to run. The exec command has one special feature, though: Rather than create a new process that runs alongside the shell, the new process replaces the shell. When the new process terminates, it’s as if you terminated the shell.

The exit and logout commands both terminate the shell. The exit command terminates any shell, but logout command terminates only shells, that is, those that are launched automatically when you initiate a text-mode login as opposed to those that run in xterm windows or the like.

When you type a command that’s not recognized by the shell as one of its internal commands, the shell checks its path to find a program by that name to execute it. The path is a list of directories in which commands can be found. It’s defined by the PATH environment variable, as described shortly in “Using Environment Variables.” A typical user account has about half a dozen or a dozen directories in its path. You can adjust the path by changing the PATH environment variable in a shell configuration file, as described in “Exploring Shell Configuration.”

You can run programs that aren’t on the path by providing a complete path on the command line. For instance, typing ./myprog runs the myprog program in the current directory, and typing /home/arthur/thisprog runs the thisprog program in the /home/ arthur directory.

Linux shells include various tools that can help speed up typing operations. The first of these is command completion: Type part of a command or (as an option to a command) a filename, and then press the Tab key. The shell tries to fill in the rest of the command or the filename. If just one command or filename matches the characters you’ve typed so far, the shell fills it in and adds a space after it. If the characters you’ve typed don’t uniquely identify a command or filename, the shell fills in what it can and then stops. Depending on the shell and its configuration, it may beep.

Linux provides a text-based help system known as man. This command’s name is short for manual, and its entries (its man pages) provide succinct summaries of what a command, file, or other feature does. For instance, to learn about man itself, you can type man man command. The result is a description of the man command.

The man utility uses the less pager to display information. This program displays text a page at a time. Press the spacebar to move forward a page, Esc followed by V to move back a page, the arrow keys to move up or down a line at a time, the slash (/) key to search for text, and so on.  When you’re done, press Q to exit less and the man page it’s displaying.

Some programs have moved away from man pages to info pages. The basic purpose of info pages is the same as that for man pages, but info pages use a hypertext format so that you can move from section to section of the documentation for a program. Type info info to learn more about this system.


An A-Z Index of the Bash command line for Linux

What is the Best Linux Distributions for Beginners?

Did you find this tutorial helpful? Don’t forget to share your views with us.