Command Line Skills

Part 1: Command Line Interface(CLI)

The Command Line Interface (CLI), is a text-based interface to the computer, where the user types in a command and the computer then executes it. The CLI environment is provided by an application on the computer known as a terminal.

The terminal accepts what the user types and passes to a shell. The shell interprets what the user has typed into instructions that can be executed by the operating system. If output is produced by the command, then this text is displayed in the terminal. If problems with the command are encountered, then an error message is displayed.

1. Prompt

A terminal window displays a prompt; the prompt appears when no commands are being run and when all command output has been printed to the screen. The prompt is designed to tell the user to enter a command.

The structure of the prompt may vary between distributions, but will typically contain information about the user and the system. Below is a common prompt structure:

[[email protected] ~]

The previous prompt provides the name of the user that is logged in (sysadmin), the name of the system (localhost) and the current directory (~). The ~ symbol is used as shorthand for the user‘s home directory (typically the home directory for the user is under the /home directory and named after the user account name, for example: /home/sysadmin).

2. Shell

A shell is the interpreter that translates commands entered by a user into actions to be performed by the operating system. The Linux environment provides many different types of shells, some of which have been around for many years.

The most commonly used shell for Linux distributions is called the BASH shell. It is a shell that provides many advanced features, such as command history, which allows you to easily re-execute previously executed commands.

The BASH shell also has other popular features:

  • Scripting: The ability to place commands in a file and execute the file, resulting in all of the commands being executed. This feature also has some programming features, such as conditional statements and the ability to create functions (AKA, subroutines).
  • Aliases: The ability to create short "nicknames" for longer commands.
  • Variables: Variables are used to store information for the BASH shell. These variables can be used to modify how commands and features work as well as provide vital system information.

Note: The previous list is just a short summary of some of the many features provided by the BASH shell.

3. Formatting Commands

Many commands can be used by themselves with no further input. Some commands require additional input to run properly. This additional input comes in two forms: options and arguments.

The typical format for a command is as follows:

command [options] [arguments]

Options are used to modify the core behavior of a command while arguments are used to provide additional information (such as a filename or a username). Each option and argument is normally separated by a space, although options can often be combined together.

Keep in mind that Linux is case senstive. Commands, options, arguments, variables and filenames must be entered exactly as shown.

The ls command will provide useful examples. By itself, the ls command will list the files and directories contained in your current working directory:

The ls command will be covered in complete detail in a later chapter. The purpose of introducing this command now is to demonstrate how arguments and options work. At this point you shouldn‘t worry about what the output of the command is, but rather focus on understanding what an argument and an option is.

An argument can also be passed to the ls command to specify which directory to list the contents of. For example, the command ls /etc/ppp will list the contents of the /etc/ppp directory instead of the current directory:

Since the ls command will accept multiple arguments, you can list the contents of multiple directories at once by typing the ls /etc/ppp /etc/sshcommand:

4. Working with Options

Options can be used with commands to expand or modify the way a command behaves. Options are often single letters; however, they sometimes will be "words" as well. Typically, older commands use single letters while newer commands use complete words for options. Single-letter options are preceded by a single dash (-). Full-word options are preceded by two dashes (--).

For example, you can use the -l option with the ls command to display more information about the files that are listed. The ls -l command will list the files contained within the current directory and provide additional information, such as the permissions, the size of the file and other information:

In most cases, options can be used in conjunction with other options. For example, the ls -l -h or ls -lh command will list files with details, but will display the file sizes in human readable format instead of the default value (bytes):

Note that the previous example also demonstrated how you can combine single letter options: -lh. The order of the combined options isn‘t important.

The -h option also has a full-word form: --human-readable.

Options can often be used with an argument. In fact, some options require their own arguments. You can use options and arguments with the ls command to list the contents of another directory by executing the ls -l /etc/pppcommand:

5. Command history

When you execute a command in a terminal, the command is stored in a "history list". This is designed to make it easy for you to execute the same command later since you won‘t need to retype the entire command.

To view the history list of a terminal, use the history command:

Pressing the Up Arrow key will display the previous command on your prompt line. You can press up repeatedly to move back through the history of commands you have run. Pressing the Enter key will run the displayed command again.

When you find the command that you want to execute, you can use the Left arrow keys and Right arrow keys to position the cursor for editing. Other useful keys for editing include the HomeEndBackspace and Delete keys.

If you see a command you wish to run in the list that the history command generates, you can execute this command by typing an exclamation point and then the number next to the command, for example:

!3

Some additional history examples:

Example

Meaning

history 5 Show the last five commands from the history list
!! Execute the last command again
!-5 Execute the fifth command from the bottom of the history list
!ls Execute the most recent ls command

6. Introducing BASH shell variables

A BASH shell variable is a feature that allows you or the shell to store data. This data can be used to provide critical system information or to change the behavior of how the BASH shell (or other commands) work.

Variables are given names and stored temporarily in memory. When you close a terminal window or shell, all of the variables are lost. However, the system automatically recreates many of these variables when a new shell is opened.

To display the value of a variable, you can use the echo command. The echocommand is used to display output in the terminal; in the example below, the command will display the value of the HISTSIZE variable:

The HISTSIZE variable defines how many previous commands to store in the history list. To display the value of the variable, use a dollar sign $ character before the variable name. To modify the value of the variable, you don‘t use the $ character:

There are many shell variables that are available for the BASH shell, as well as variables that will affect different Linux commands. A discussion of all shell variables is beyond the scope of this chapter, however more shell variables will be covered as this course progresses.

7. PATH variable

One of the most important BASH shell variables to understand is the PATHvariable.

The term path refers to a list that defines which directories the shell will look in for commands. If you type in a command and receive a “command not found” error, it is because the BASH shell was unable to locate a command by that name in any of the directories included in the path. The following command displays the path of the current shell:

Based on the proceeding output, when you attempt to execute a command, the shell will first look for the command in the /usr/lib/qt-3.3/bin directory. If the command is found in that directory, then it is executed. If it isn‘t found, then the shell will look in the /usr/local/bin directory.

If the command is not found in any directory listed in the PATH variable, then you will receive a "command not found" error:

If custom software is installed on your system, you may need to modify thePATH to make it easier to execute these commands. For example, the following will add the /usr/bin/custom directory to the PATH variable:

8. export Command

There are two types of variables used in the BASH shell, local and environment. Environment variables, such as PATH and HOME, are used by BASH when interpreting commands and performing tasks. Local variables are often associated with user based tasks and are lowercase by convention. To create a local variable, simply type:

[email protected]:~$variable1=‘Something‘

To view the contents of the variable, refer to it with a leading $ sign:

[email protected]~:$echo $variable
Something

To view environment variables, use the env command (searching through the output using grep, as shown here, will be discussed in later chapters). In this case, the search for variable1 in the environment variables results in no output:

[email protected]:~$env | grep variable1

After exporting variable1, it is now an environment variable. Notice that this time, it is found in the search through the environment variables:

[email protected]:~$export variable1
[email protected]:~$env | grep variable1
variable1=Something

The export command can also be used to make an environment variable upon its creation:

[email protected]:~$export variable2=‘Else‘
[email protected]:~$env | grep variable2
variable1=Else

To change the value of an environment variable, simply omit the $ when referencing it:

[email protected]:~$ variable1=$variable1 ‘ ‘ $variable2
[email protected]:~$ echo $variable1
Something Else

Exported variables can be removed using the unset command:

[email protected]:~$ unset $variable2

9. which Command

There may be situations where different versions of the same command are installed on a system or where commands are accessible to some users and not others. If a command does not behave as expected or if a command is not accessible that should be, it can be beneficial to know where the shell is finding the command or which version it is using.

It would be tedious to have to manually look in each directory that is listed in thePATH variable. Instead, you can use the which command to display the full path to the command in question:

$which data
/bin/date
$which cal
/usr/bin/cal

The which command searches for the location of a command by searching thePATH variable.

10. type Command

The type command can be used to determine information about various commands. Some commands originate from a specific file:

$type which
which is /usr/bin/which

This output would be similar to the output of the which command (as discussed in the previous section, which displays the full path of the command):

$which which
/usr/bin/which

The type command can also identify commands that are built into the bash (or other) shell:

$type echo
echo is a shell builtin

In this case, the output is significantly different from the output of the whichcommand:

$which echo
/usr/bin/which

Using the -a option, the type command can also reveal the path of another command:

$type -a echoecho is a shell builtinehco is /bin/echo

The type command can also identify aliases to other commands:

$type ll
ll is aliased to ‘ls -alF‘
$type ls
ls is aliased to ‘ls --color=auto‘

The output of these commands indicate that ll is an alias for ls -alF, and even ls is an alias for ls --color=auto. Again, the output is significantly different from the which command:

$which ll
$which ls
/bin/ls

The type command supports other options, and can lookup multiple commands simultaneously. To display only a single word describing the echo,ll, and which commands, use the -t option:

$type -t echo ll which
builtin
alise
file

11. Aliases

An alias can be used to map longer commands to shorter key sequences. When the shell sees an alias being executed, it substitutes the longer sequence before proceeding to interpret commands.

For example, the command ls -l is commonly aliased to l or ll. Because these smaller commands are easier to type, it becomes faster to run the ls -lcommand line.

You can determine what aliases are set on your shell with the alias command:

The aliases that you see from the previous examples were created by initialization files. These files are designed to make the process of creating aliases automatic and will be discussed in more detail in a later chapter.

New aliases can be created by typing alias name=command where name is the name you want to give the alias and command is the command you want to have executed when you run the alias.

For example, you can create an alias so that lh displays a long listing of files, sorted by size with a “human friendly” size with the alias lh=‘ls -Shl‘command. Typing lh should now result in the same output as typing the ls -Shl command:

Aliases created this way will only persist while the shell is open. Once the shell is closed, the new aliases that you created will be lost. Additionally, each shell has its own aliases, so if you create an alias in one shell and then open another shell, you won‘t see the alias in the new shell.

12. Globbing

Glob characters are often referred to as “wild cards”. These are symbols that have special meaning to the shell.

Unlike commands that the shell will run, or options and arguments that the shell will pass to commands, glob characters are interpreted by the shell itself before it attempts to run any command. This means that glob characters can be used with any command.

Globs are powerful because they allow you to specify patterns that match filenames in a directory, so instead of manipulating a single file at a time, you can easily execute commands that will affect many files. For instance, by using glob characters it is possible to manipulate all files with a certain extension or with a particular filename length.

Keep in mind that these globs can be used with any command because it is the shell, not the command that expands with globs into matching filenames. The examples provided in this chapter will use the echo command for demonstration.

Asterisk(*)

The asterisk character is used to represent zero or more of any character in a filename. For example, suppose you want to display all of the files in the /etc directory that begin with the letter "t":

The pattern "t*" means "match any file that begins with the character t and has zero or more of any character after the t".

You can use the asterisk character at any place within the filename pattern. For example, the following will match any filename in the /etc directory that ends with ".d":

In the next example, all of the files in the /etc directory that begin with the letter "r" and end with ".conf" will be displayed:

Question Mark(?)

The question mark represents any one character. Each question mark character matches exactly one character, no more and no less.

Suppose you want to display all of the files in the /etc directory that begin with the letter "t" and have exactly 7 characters after the "t" character:

Glob characters can be used together to find even more complex patterns. Theecho /etc/*???????????????????? command will print only files in the/etc directory with twenty or more characters in the filename:

The asterisk and question mark could also be used together to look for files with three-letter extensions by running the echo /etc/*.??? command:

Brackets []

Brackets are used to match a single character by representing a range of characters that are possible match characters. For example, echo /etc/[gu]* will print any file that begins with either a “g” or “u” character and contains zero or more additional characters:

Brackets can also be used to a represent a range of characters. For example, the echo /etc/[a-d]* command will print all files that begin with any letter between and including “a” and “d”:

The echo /etc/*[0-9]* command would display any file that contains at least one number:

The range is based on the ASCII text table. This table defines a list of characters, arranging them in a specific standard order. If you provide an invalid order, no match will be made:

Exclamation Point (!)

The exclamation point is used in conjunction with the square brackets to negate a range. For example, the command echo [!DP]* will display any file thatdoes not begin with a “D” or “P”.

13. Quoting

There are three types of quotes that have special significance to the Bash shell: double quotes ("), single quotes (‘), and back quotes (`). Each set of quotes indicates to the shell that it should treat the text within the quotes differently than it would normally be treated.

Double Quotes

Double quotes will stop the shell from interpreting some metacharacters, including glob characters. Within double quotes an asterisk is just an asterisk, a question mark is just a question mark, and so on. This means that when you use the second echo command below, the BASH shell doesn‘t convert the glob pattern into filenames that match the pattern:

This is useful when you want to display something on the screen that is normally a special character to the shell:

Double quotes still allow for command substitution (discussed later in this chapter), variable substitution and permit some other shell metacharacters that haven‘t been discussed yet. For example, in the following demonstration, you will notice that the value of the PATH variable is displayed:

Single Quotes

Single quotes prevent the shell from doing any interpreting of special characters. This includes globs, variables, command substitution and other metacharacter that have not been discussed yet.

For example, if you want the $ character to simply mean a $, rather than it acting as an indicator to the shell to look for the value of a variable, you could execute the second command displayed below:

Backslash Chracter(\)

You can use an alternative technique to essentially single quote a single character. For example, suppose you want to print the following: "The services costs $100 and the path is $PATH". If you place this in double quotes, $1 and $PATH are considered variables. If you place this in single quotes, $1 and $PATH are not variables. But what if you want to have $PATH treated as a variable and $1 not?

If you place a backslash ( \ ) character in front of another character, it treats the other character as a "single quoted" character. The third command below demonstrates using the \ character while the other two demonstrate how the variables would be treated within double and single quotes:

Back Quotes

Back quotes are used to specify a command within a command, a process called command substitution. This allows for very powerful and sophisticated use of commands.

While it may sound confusing, an example should make things more clear. To begin, note the output of the date command:

Now note the output of the echo Today is date command line:

In the previous command the word “date” is treated as regular text and the shell simply passes "date" to the echo command. But, you probably want to execute the date command and have the output of that command sent to the echocommand. To accomplish this, you would run the echo Today is `date`command line:

14 Control Statements

Control statements allow you to use multiple commands at once or run additional commands, depending on the success of a previous command. Typically these control statements are used within scripts, but they can also be used on the command line as well.

Semicolon

The semicolon can be used to run multiple commands, one after the other. Each command runs independently and consecutively; no matter the result of the first command, the second will run once the first has completed, then the third and so on.

For example, if you want to print the months of January, February and March of 2014, you can execute cal 1 2014; cal 2 2014; cal 3 2014 on the command line:

Double Ampersand (&&)

The double ampersand ( && ) acts as a logical “and”; if the first command is successful, then the second command (to the right of &&) will also run. If the first command fails, then the second command will not run.

To better understand how this works, consider first the concept of failure and success for commands. Commands succeed when they work properly and fail when something goes wrong. For example, consider the ls /etc/xmlcommand line. The command will succeed if the /etc/xml directory is accessible and fail if it isn‘t.

For example, the first command will succeed because the /etc/xml directory exists and is accessible while the second command will fail because there is no/junk directory:

The way that you would use the success or failure of the ls command in conjunction with && would be to execute a command line like the following:

In the first example above, the echo command executed because the ls command succeeded. In the second example, the echo command wasn‘t executed because the ls command failed.

Double Pipe

The double pipe ( || ) is a logical “or”. It works in a similar way to the double ampersand; depending on the result of the first command, the second command will either run or be skipped.

With the double pipe, if the first command runs successfully, the second command is skipped; if the first command fails, then the second command will be run. In other words, you are essentially telling the shell, “Either run this first command or the second one”.

In the following example, the echo command will only execute if the lscommand fails:

时间: 2024-12-22 08:08:32

Command Line Skills的相关文章

10 Interesting Linux Command Line Tricks and Tips Worth Knowing

I passionately enjoy working with commands as they offer more control over a Linux system than GUIs(Graphical User Interfaces) applications, therefore am always on the look out to discover or figure out interesting ways and ideas to make Linux so eas

Can't use Subversion command line client: svn

使用Intellij IDEA的svn时提示出错:Can't use Subversion command line client: svn. 当我在使用svn,Checkout一个项目后,然后将其导入到Intellij idea中,出现这样的报错!经过google后,发现了问题,我的问题是:我安装的TortoiseSVN工具,本身是带有command-line功能的(我没有安装)如图: 所以报这个错误.如果安装的TortoiseSVN工具,本身是不带有command-line功能的,必须要安装

解决MySQL5.6 Warning: Using a password on the command line interface can be insecure

MySQL5.6在使用名文的密码登陆时,会出现:Warning: Using a password on the command line interface can be insecure 当然这样对于平常的登陆会无所谓,如果在脚本里使用使用的话,就会有问题: 解决这种问题的方法是需要在my.cnf中配置即可: 在my.cnf中加入如下配置 [mysqladump] user=my_name password=my_pass 重启MySQL 即可 以后再使用mysqldump命令就不需要加上任

How to build .apk file from command line(转)

How to build .apk file from command line Created on Wednesday, 29 June 2011 14:32 If you don’t want to install a number of programs for building your Android project, this article is for you. You will need only JDK, the Android SDK platform tools and

Command line option syntax error.type Command /? for help

电脑装思维导图的时候,报错显示"Command line option syntax error.Type Command /? for help."就查了一下,原来是系统没有C++2005,需要安装,就上网下载了一个vcredist_x86.exe,但是双击安装,仍然出现这个错误. 没办法,接着上网查吧,是什么原因呢?网上说是因为该文件安装不支持中文安装路径,然后我就把文件夹改成了英文名称的,但是双击还是出现这个错误.可能有的人用这种方法成功了吧~本着没有解决不了的问题的思想,接着奋

How To use RHEVM Command Line?

本文简单的描述下如何连接rhev shell以及简单的使用.关于更详细的用法请参考官方文档. 1.如何连接到rhevm? 要想连接到rhevm,必须拥有一个有效的证书.此证书一般安装完rhevm后会自动产生.下面是如何获取到证书. [[email protected] ~]# wget -O rhevm.cer http://rhevm.xzxj.edu.cn/ca.crt --2014-05-02 09:29:34-- http://rhevm.xzxj.edu.cn/ca.crt Resol

【转载】Data Science at the Command Line

Data Science at the Command Line Data Science at the Command Line is a new book written by Jeroen Janssens. This website contains information about the upcoming workshop in London, the webcast from August 20th, instructions on how to install the Data

Mac OS X Command Line

关于 man 命令 虽然有上千条命令,每条命令还有许多可选参数和具体的使用方式,但是你却不需要记住这些命令.你只需要记住一个:man 大多数命令都会包含一个使用指南,会告诉你任何你需要知道的关于这个命令的所有细节,在命令行中输入 man command-name 即可获取.例如,你想知道ls这个命令怎么使用,输入man ls即可进入使用指南页面. 使用指南往往很长,所以你可以使用▲(上箭头)或▼(下箭头)来上下移动,使用 来翻页,输入/和关键字来按照关键字搜索,按Q来退出使用指南页面. 那么——

MySQL5.6 Using a password on the command line interface can be insecure

最近把MySQL从5.5升到5.6以后,mysqldump居然不好用了,提示:  代码如下 复制代码 [[email protected] ~]# /usr/local/mysql/bin/mysqldump  -uroot -proot db > bak.sqlWarning: Using a password on the command line interface can be insecure. 翻译过来是:在命令行界面上使用密码可以是不安全的. 这让人有点郁闷,5.5用的一直都很爽,