How to use command line linux

How to Use The Command Line

A First Lesson

The command line (i.e., all-text display mode) is a key part of any truly modern computer operating system 1 .

Because graphical user interfaces (GUIs) have become very easy to use in the past decade or so, it is no longer common for most computer users to utilize the command line, especially for simple tasks such as word processing, searching the web and sending e-mail. Thus, the command line is frequently perceived as being unnecessary, intimidating and even obsolete. A GUI is a display mode that contains images, windows and menus and which is manipulated primarily with a mouse.

However, the command line, also referred to as the shell (although the shell is actually a program the provides the command line), can be quite easy to learn and use, and its value soon becomes apparent after a little practice. Even a basic familiarity with it can make computers easier to use and facilitate performing tasks that might be difficult or impossible with a GUI. Such familiarity can also lead to an improved understanding of how computers actually work.

Many people who are new to the Linux command line will be familiar with the command line used in MS-DOS and think that the two are similar. However, the similarities are largely just superficial, and there are vast differences. The Linux command line is far more powerful (i.e., it is much more flexible and can do many more things), and in some ways it is much more user friendly.

Studying the Linux command line provides an education in much more than just how to use a specific operating system. One reason is that the Linux command line is virtually identical to the command line used on every other Unix-like operating system, e.g., Solaris, FreeBSD and Mac OS X. Thus, multiple operating systems are being learned simultaneously.

Studying the command line also provides insight into how computers really work. This is because the command line is much closer to the internal functioning of computers than are GUIs, which are generally just front ends for the commands used on the command line. Moreover, the underlying commands typically have greater flexibility than their GUI counterparts, they can easily be combined with other commands, and they can be used in situations where a GUI is not available (e.g., making system repairs) or is not functioning properly.

Accessing the Command Line

A first step in learning about the command line is accessing it. Usually, the easiest way to do this is to open a terminal window, which is an all-text window in a GUI. This can be accomplished by merely clicking on the appropriate menu item or icon (i.e., small image). For example, in the case of Red Hat Linux, all that is necessary is to select the item called Terminal which appears in the System Tools menu. On some distributions (i.e., versions) of Linux, a terminal window can be opened by clicking an appropriate icon that sits on the toolbar (i.e., strip of icons) along the bottom of the screen.

Another way to open the command line is to switch to a console, which is a display mode in which the entire screen is text only and there are no buttons, windows or other GUI objects. The easiest way to accomplish this is to simultaneously press the CRTL, ALT and F1 keys while in a GUI. The GUI disappears and the entire screen becomes black except for a few white characters (which constitute the command prompt) in the upper left-hand corner. Fortunately, the GUI can be restored at any time on a typical system by simultaneously pressing the CTRL, ALT and F8 keys.

The pwd Command

A good first command to learn is pwd, which stands for present working directory. This command shows the name and location of the current directory, which is the directory (also called a folder on some operating systems) in which the user is currently working. All that is necessary in order to use this command is to type the word pwd in at the keyboard as follows and then press the ENTER key:

If the terminal window or console has just been opened, what will typically be shown on the next line on the monitor screen will be /home followed by another forward slash and then the name of the user’s home directory (which is usually the same as the user name). For example, if the user has a user name of john, the line would say /home/john. This is because a user begins working at the command line in its home directory, home directories are located in the directory named /home, and a user’s home directory typically has the same name as the user name.

Commands (and virtually everything else) in Unix-like operating systems are case sensitive, in contrast to MS-DOS, which is case insensitive. That is, lower case and upper case letters are treated as completely different characters. Thus, for example, pwd must always be written in all lower case letters, not as PWD or Pwd 2 .

pwd becomes more useful after the current directory has been changed a few times, because then the location and name of the current directory are not always as obvious.

The ls Command

ls is another of the most basic and frequently used commands on Unix-like operating systems. It is equivalent to DIR on MS-DOS systems, and it lists the contents of a directory. When used just by itself, it provides a list of the names of the objects (i.e., files, directories and links) in the current directory, i.e.,

Читайте также:  Linux process ports used

It can be seen that various names are listed but that no additional information is provided about them. This is often sufficient, but in many cases it is desired to obtain additional information about each directory object. Obtaining such information can be easily accomplished by using ls together with one or more of its options. An option is a letter (or in some cases a word) that follows the command after a space and that tells the command how to behave. Multiple options can usually be used together, and they are usually preceded directly by a hyphen (without any intervening space). The options available can differ substantially according to the specific command.

For example, the -a option tells ls to show all objects in the directory, inclusive of hidden files or hidden directories. Hidden objects are those whose names are normally not visible either in the command line or when examining the contents of a directory in a GUI. Their names are preceded directly by a period, such as .file1. Thus to see the names of all objects in the current directory, the following command should be typed in and then the ENTER key pressed:

Many commands have numerous options, and ls is no exception. However, typically only a few of them are frequently used. Another commonly employed option for ls is -l, which provides a long listing, i.e., it provides much information about each object in addition to just its name. This additional information includes the type of object (e.g., file, directory or link), its permissions (i.e., who has access to it for reading, writing and/or executing), its owner (which is by default the same as its creator), and the date and time of creation.

When used together with both its -a and -l options, the command would be written as

Another common option for ls is -s, which shows the size of each file (but not directory 3 ) in kilobytes. This option is often used together with the above two options as

Like most commands, but unlike pwd, ls can accept input data, which is referred to as an argument. In the case of ls, arguments are the names of directories about which it is desired to obtain information. Any number of directories can be listed as arguments. They all follow ls and any options, and they are separated by at least one blank space.

For example, if it is desired to see the names of all the objects in both the directories /bin and /home, the following command would be used:

As is the case with /home, /bin is a standard first tier directory in the root directory, which is the single directory that contains all other directories and their subdirectories on a Unix-like operating system and which is represented by a forward slash ( / ). Like all other first-tier directories in the root directory, their names begin with a forward slash, in part so that they will not be confused with directories with similar names that are not first tier directories of the root directory.

The cd Command

The last of what are perhaps the three most basic commands on Unix-like operating systems is cd, which is used to change the current directory. This is equivalent to the CD and CHDIR commands in MS-DOS.

Thus, for example, to change the current directory to the /bin directory, the following would be used:

Likewise, to change to the root directory, the following would be used:

Any user can return to its home directory by using a tilde as the argument for cd. A tilde is a short, wavy, horizontal line that represents the home directory of the current user, and this character is located in the upper left hand corner on most standard English language keyboards. That is, any user can return immediately to its home directory by typing the following and then pressing the ENTER key:

This is easier than typing the full name of the user’s home directory as an argument, for example /home/josephine in the case of a user named josephine. It is just one of the numerous shortcuts that help make the command line on Unix-like operating systems so easy to use 4 .

For additional introductory information on the use of the command line, see How to Use the Command Line, A Second Lesson.

________
1 Microsoft downgraded the command line in its operating systems subsequent to Microsoft Windows 95 in an attempt to allow virtually all operations to be performed in the GUI. However, this has not been successful in the opinion of many computer experts, and the company has thus been developing a command line (code named Monad) similar to those in Unix-like systems for use in its future operating systems.

2 For purposes of clarity and consistency, the case of commands, keywords, etc. that are always written in lower case is maintained when discussing them in text in The Linux Information Project, even when they are used at the beginning of sentences or in titles (although this may at first appear odd or even erroneous).

3 In Unix-like operating systems everything is considered to be a file, and that includes directories, which are just a special type of file. However, there are some differences in behavior, of which this is an example.

4 Among the others are command completion, which is the automatic completion of the names of commands and their arguments that have only partially been typed by a user, command history, which allows the user to view, edit and reissue previously executed commands, and wildcards, which can represent any group of files and directories.

Читайте также:  Безопасный активатор windows 10 pro x64

Created August 9, 2005. Updated April 17, 2006.
Copyright © 2005 — 2006 The Linux Information Project. All Rights Reserved.

Источник

Ubuntu Documentation

Introduction

Even though Ubuntu is a newbie friendly and polished graphical distribution, there are still situations where a significant amount of time and mouse-clicking can be spared by typing a bit. I don’t think this is a bad thing at all; no matter what you do, Linux has one of its real strengths in the Command Line!

Prerequisites

This assumes that you are running any version of Ubuntu Linux and have a desire to learn its inner workings. We will proceed by briefly describing the command line interface and giving some history.

The impatient can move right on to the Command Syntax section.

What is it?

A Command Line is, in all simplicity, a user interface based on lines of commands. You can say that it is a textual direct serial processor. Most commonly, the user interacts directly with the computer by typing one line (although it can be more than one), which triggers actions from the computer based on the syntax of the current processor.

History

In the early days of computers, there was only the Command Line. The concept of a Graphical User Interface (GUI) after which most GUI are modeled was developed by engineers at Xerox’s Palo Alto Research Center (PARC). A bit later, Apple paid a whole bunch of money to be allowed to «study» their GUI idea. And, after a while, Apple had their own GUI.

Not until 1986 did UNIX get its first GUI, developed by the MIT Project. They named it X. Linux, however, had to wait ten more years before XFree86 was released. XFree86 was (and remains) a free adaptation of the original X server.

As mentioned earlier, the CLI (Command Line Interface) was the only way to communicate with computers before the GUI was invented. In 1969, Bell Telephone Laboratories released V1 of the UNIX Timeshare System. UNIX had a shell called sh, which was the only means of communicating with the computer, and it would stay that way for quite some time.

Later on, there came derivatives of UNIX: HP-UX, 1BSD, Solaris, OpenVMS, IRIX, SCO XENIX, etc. As time progressed, GNU/Linux emerged. However, the history of Linux itself is way off the scope of this HOWTO. Suffice to say that alternative CLI to sh emerged: zsh, ksh, bourne shell, etc.

POSIX

The Wikipedia defines POSIX as the following:

  • POSIX is the collective name of a family of related standards specified by the IEEE to define the application program interface (API) for software designed to run on variants of the Unix OS. They are formally designated as IEEE 1003 and the international standard name is ISO/IEC 9945. The standards emerged from a project, begun circa 1985. The term POSIX was suggested by Richard Stallman in response to an IEEE request for a memorable name; before that the standards effort was called IEEE-IX. POSIX is a near acronym for Portable Operating System Interface, with the X signifying the Unix heritage of the API.

POSIX is the underlying standard and functionality of how your CLI responds.

Advantages of using the command line

So, CLI preceded GUI as a computer interface. Since GUIs are common now, why should we still care about CLI? Some advantages of using the command line are:

  • It can save you time.
  • It can help when you are unable to use the GUI, such as a system crash or a configuration issue.
  • It can enable you to use Linux in ways that using a GUI exclusively can not (such as scripting repetitive tasks).

For example, you have been called by the systems administrator that you have used too much space. You want to quickly work out where the most space is used, so using a graphical interface, start your timer — go. Now, go to a command line and type: du | sort -n (we will describe more later). See? It is faster to do some things on the command line (and other times, easier for graphical).

How to invoke it

Methods of launching a CLI can vary by desktop environment:

Unity (Ubuntu 14.04)

Dash -> Search for Terminal
Dash -> More Apps -> ‘See More Results’ -> Terminal
Dash -> More Apps -> Accessories -> Terminal

Gnome Desktop (Ubuntu GNOME)

Activities -> Search for Terminal

KDE Desktop (Kubuntu)

KDE Menu | Applications | System | Konsole Terminal Program

XFCE Desktop (Xubuntu)

Applications | System | Terminal

An alternative way to invoke the command line, only using keyboard shortcuts (since on the command line, you would mostly be interacting only through the keyboard) is:

On Unity (Ubuntu): Ctl + Alt + T
On GNOME (Ubuntu): Alt + F2 -> (
Type within the text box) gnome-terminal (Press return)
On KDE (Kubuntu): Alt + F2 -> (
Type within the text box) konsole (Press return)

Basic structure and concepts

The first thing that you should notice is something like:

What you see here is called the prompt. It signifies that the computer is ready and awaiting user input. In my case, dud is the user that I’m logged in as. shadowplay is the computer’s hostname, and

is the current directory (the user’s home directory).

Concepts:

  • A terminal is a «physical» (direct) interface to your Linux Operating System.
  • A terminal emulator is what we’ll be using here. This is a CLI wrapped within your running GUI. Any applications running in a terminal emulator will be killed if you close the terminal emulator.
  • A shell is an interpreter for commands entered into the terminal.
  • A command is usually a small utility that the shell will execute for you.
  • Output is what a command returns; most often this is returned on the terminal.
  • Input consists of the arguments or data that any given command will take. Input will change the way a given command acts.
  • A process is a running application on your computer. It can be active, sleeping, or in a number of other states.

Command Syntax

This section will try to give you a good rundown of the basic usage for the bash shell, which is the default user shell in Ubuntu.

Single Command

The command syntax will vary with each command. Here are some of the basics.

The simplest way to use some commands is to type just the command.

command

The above example displays the contents of the current working directory, while other commands may require one or more arguments.

The above example shows you the content of the file1.txt file by adding the filename as the argument for the cat command. Almost all commands, whether they have arguments or not, have options.

command -option

Using the previous example of showing the current directory, we have added the -r option. As you can see, the listing of the current working directory has been displayed in the reverse order.

It is also possible to pass more than one argument or option to the command, depending on whether the program accepts more or not. If you want to pass two arguments or options consecutively, then you would use the following syntax:

argument1 argument2

-option1 -option2

Multiple Commands

Sometimes the desired task may require the use of more than one command to be completed. Here is the syntax for the use of multiple commands.

If you want to execute two commands consecutively, then you would use the following syntax:

command1 ; command2

In the example above, command1 and command2 are executed. However, if you need command1 to complete successfully before executing command2, then you would use the following syntax:

command1 && command2

In the example above, you will notice nothing happened when the first command did not complete successfully. If you want command2 to execute only if command1 fails, then you would use the following syntax:

command1 || command2

In the example above, you will notice command2 was only executed when command1 failed.

Wildcards

Wildcards are a useful feature that allows an unknown value or values to be used with another command. This becomes very useful with commands such as «ls» allowing only a range of filenames to be displayed.

There are three operators used with wildcards — «*», «?» and «[x-y]».

Specifing a single character

The «?» is used to represent a single unknown character, consider we have a folder containing four files: file1.pdf, file2.pdf, file2.mp3 and file23.pdf. If wanted to know which PDF filenames contained numbers, then we could use:

Specifying multiple characters

Using the same files as the previous example, if we wanted to search for all files called «file2» of any type we could:

Specifying a range

If we wanted to know all PDF filenames beginning with «file» and a number between 2 and 23 then we use:

Control Flow

Commands read input from the keyboard (standard input, or stdin) and write to output (standard out, or stdout). There is also a special output category for error messages called standard error (or stderr). These three locations are created automatically for each program.

We can redirect input and output to and from a command.

Redirection

If you wanted the output of a command to go to a file instead of the terminal, then you would use the following syntax:

command > filename

The above example will create file4.txt if it is not found. NOTE: If file4.txt exists already, the above command will overwrite its contents. If you want to add to the end of a existing file, then you would use the following syntax:

command >> filename

In the example, you will notice the file was appended with the new information.

But bash takes it even further. You will have noticed that ls can produce error messages if a directory ls is suppose to list does not exist or the user lacks access rights. In bash, Error messages and the «regular results» (i.e. dir listing) are 2 separate streams. To ease use of results only find a detailed explanation of bash.

Now we are going to do a different redirection: We are going to take the input from a file for the command to be executed. Here is the syntax for this redirection:

As you can see from this example, we used the file4.txt as input into the sort command.

When you need the output from command 1 for the input into command 2, then you would use pipe character ‘|’. Here is the syntax for the pipe character:

command | command

The above example is using the output from ls as input to the sort command. You will notice the list has been sorted.

As you can see, the command line is an easy and powerful way of completing many tasks. If you want more information on using the command line, then look at the further reading section of this document.

Further reading

BasicCommands has a list of basic commands.

AdvancedCommandlineHowto has some advanced command line features such as scripting.

CommandlineHowto (последним исправлял пользователь ckimes 2017-09-11 20:21:18)

The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details

Источник

Оцените статью