- Linux Command Line Tutorial: the commands you need to know
- Learn advanced BASH commands and scripts
- What is the Linux Command Line?
- Brief History of Linux Command Line
- Introduction
- Prerequisites
- What is it?
- History
- POSIX
- Advantages of using the command line
- How to invoke it
- Basic structure and concepts
- Command Syntax
- Single Command
- Multiple Commands
- Wildcards
- Specifing a single character
- Specifying multiple characters
- Specifying a range
- Control Flow
- Redirection
- Pipe
- Further reading
Linux Command Line Tutorial: the commands you need to know
The Linux command line, also called a terminal, shell, or console, is a text interface for your computer. With this computer program, you can interpret commands and write your own scripts.
It may look intimidating at first, but once you get used to it, it is easy to use. This article will walk you through some of the basics of the Linux command line, along with some real-world examples. We assume you have little or no prior knowledge.
Each Linux distribution comes with a command line. This article uses Ubuntu 20.04, the commands should work with most distributions.
Here’s what we will be covering today:
Learn advanced BASH commands and scripts
This course is for anyone interested in understanding the BASH shell. You’ll learn all the intricacies of advanced BASH scripting.
What is the Linux Command Line?
The Linux terminal gives you an interface where you can input commands and see the result printed as text. You can use the terminal to accomplish tasks like moving files or navigating a directory, without the use of a GUI. Terminals just transfer information. You need a shell for the operating system to understand this information.
With the Linux command line, you can execute commands manually by typing on the terminal. You can also automate commands using Shell Scripts.
Brief History of Linux Command Line
Let’s briefly go through the Linux command line history. Back in the early days of computing, there was an operating system called Unix, which was designed to run on mainframe computers as a multi-user system. Users connected to it remotely through terminals.
These terminals were very basic, as they could only send keystrokes and display data on the screen.
Since text is light on resources, users could interact quickly and efficiently. Every task the users performed was through this textual interface. To coordinate the execution of different programs, users would connect to single master programs.
The user commands were wrapped in “shell” programs. To make complex tasks easier, users could write shell scripts that automated a long series of shell commands.
Linux is a descendant of Unix. Its core part is designed to behave similarly to a Unix system. The Bourne Again Shell (BASH) was developed by Stephen Bourne and released in 1979 in the Version 7 Unix release. Over time, it has become the default shell for Linux systems.
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
- 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
- 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:
- Dash -> Search for Terminal
Dash -> More Apps -> ‘See More Results’ -> Terminal
Dash -> More Apps -> Accessories -> 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:
dud@shadowplay:~ $ or [dud@shadowplay ~]$
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).
- 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.
dud@shadowplay:~ $ ls file1.txt file2.pdf file3.mp3 file1.pdf another_file.txt Yet-Another_file.txt file-with_other-NAME.TXT
dud@shadowplay:~ $ cat file1.txt Roses are red. Violets are blue, and you have the bird-flu!
dud@shadowplay:~ $ ls -r file-with_other-NAME.TXT Yet-Another_file.txt another_file.txt file1.pdf file3.mp3 file2.pdf file1.txt
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.
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.
dud@shadowplay:~ $grep red file1.txt ; grep blue file1.txt Roses are red, Violets are blue,
dud@shadowplay:~ $ grep red file1.txt && grep blue file1.txt Roses are red, Violets are blue, dud@shadowplay:~ $ grep purple file1.txt && grep blue file1.txt dud@shadowplay:~ $
dud@shadowplay:~ $ grep red file1.txt || grep blue file1.txt Roses are red, dud@shadowplay:~ $ grep purple file1.txt || grep blue file1.txt Violets are blue, dud@shadowplay:~ $
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:
dud@shadowplay:~ $ ls file?.pdf file1.pdf file2.pdf
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:
dud@shadowplay:~ $ ls file2.* file2.pdf file2.mp3
Specifying a range
If we wanted to know all PDF filenames beginning with «file» and a number between 2 and 23 then we use:
dud@shadowplay:~ $ ls file2.pdf file2.pdf file23.pdf
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
dud@shadowplay:~ $ ls > file4.txt dud@shadowplay:~ $ cat file4.txt file1.txt file2.pdf file3.mp3 file1.pdf another_file.txt Yet-Another_file.txt file-with_other-NAME.TXT file4.txt
dud@shadowplay:~ $ ls >> file4.txt dud@shadowplay:~ $ cat file4.txt file1.txt file2.pdf file3.mp3 file1.pdf another_file.txt Yet-Another_file.txt file-with_other-NAME.TXT file4.txt file1.txt file2.pdf file3.mp3 file1.pdf another_file.txt Yet-Another_file.txt file-with_other-NAME.TXT file4.txt
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.
dud@shadowplay:~ $ sort another_file.txt another_file.txt file1.txt file1.txt file2.pdf file2.pdf file3.mp3 file3.mp3 file4.txt file4.txt file-with_other-NAME.TXT file-with_other-NAME.TXT Yet-Another_file.txt Yet-Another_file.txt
As you can see from this example, we used the file4.txt as input into the sort command.
Pipe
dud@shadowplay:~ $ ls | sort another_file.txt file1.pdf file1.txt file2.pdf file3.mp3 file-with_other-NAME.TXT Yet-Another_file.txt
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
- UsingTheTerminal
- 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