Linux command line basis

According to this article Linux Command Line Basics & Excaples be adapted

Linux command line overview

AT & T Corp. in the 1970s issued a UNIX system. After years of development, Unix is ​​no longer a name on a specific operating system, but to follow the Unix specification, design and philosophy of a class of operating system collectively. There are some operating systems that follow the design of Unix, and Unix has a similar norms and standards, these operating systems are called Unix-like systems (Unix-like), Linux is one of them.

In the design contains a Unix Unix Shell . It is a command line interpreter ( the CLI ) or Shell , allows the user to interact with the system by entering commands. Unix Shell either directly execute the command input by the user, may be executed from a file read command (shell scripting). The most commonly used Unix Shell is the Bash , almost all Linux distributions have built-Bash. Commonly referred to as Linux command line is Bash Bash command or script.

Linux command line to a powerful and flexible known, use a few commands you can perform many tasks, many tasks can be automated.

Linux command line basis

After Linux starts, it will create a shell session (shell session). shell session is a basic environment, which allows communication between systems and applications, and applications. Multiple sessions can be open at a time, parent-child relationship can exist between sessions, if the current session of the parent session is closed, the current session will be terminated.

 

 

The figure is VSCode remote development mode, connected to Windows10 WSL (Ubuntu18.04.2) screenshot. Before the cursor content format is as follows:

username@hostname:locateCorresponds to the FIG., I.e., the current session of the user name wjchi, hostname is DESKTOP-J2OGSVG, the current directory is ~, i.e. the current user's home directory: / home / wjchi.

Here is a breakdown of some common symbols of Linux:

SYMBOL EXPLANATION EXAMPLES
~ is equal to the current user's home directlry. E.g: /home/someone/ cd ~ ls ~
* A symbol which stands for "everything". Let's say you want to remove all the .jpg files from your Downloads folder which have their name starting with the "E" character, then you can use this symbol to represent all the other letters except E. See the example. rm ~/Downloads/E.jpg ls /etc/c nano /var/log/nginx/*
& Run a command in the background. It will return the PID of the newly running process to you and won't show you the output. sudo apt update &
&& These symbols written together stand for "and". So if you want to run 2 commands together, you can use it. sudo apt update && sudo apt upgrade
\ Allows you to continue writing commands/Bash syntax in new line. sudo \ apt \ update
.. In many cases, especially in navigation, the two dots stand for the parent folder. cd ..
. In navigation or referring to files/folders, the dot stands for the current folder. ls.
# Everything after this symbol in the same line is considered to be a comment, so it won't be processed by the shell. cd # This commands moves you somewhere.
| This is called "Piping", which is the process of redirecting the output of one command to the input of another command. Very useful and common in Linux/Unix-like systems. cat /etc/profile | grep bash
> Take the output of a command and redirect it into a file (will overwrite the whole file). ls ~ > output.txt
< Read the contents of a file into the input of a command. grep bash < /etc/profile
>> Append a text or a command output into the last line of a file. echo "First Line" > output.txt echo "See this is the last line" >> output.txt

We can use manthe command or command plus --helpto see a description of each command, man is short for manual. At the command line: man manthe output follows:

 

 

Linux commonly used navigation commands are as follows:

BASE COMMAND EXPLANATION FAMOUS ARGUMENTS & OPTIONS EXAMPLES
cd This command allows you to move into a different directory on your Linux system, which will make it the current folder of where your shell is running. It's just like as if you open a specific folder in any graphical file manager. . Stands for the current directory. .. Stands for the parent directory. ../.. the parent of the parent directory. cd /etc/
ls Lists the current files and folders in a specific directory. -a shows the hidden files too. -l shows metdata about files and folders, such as permissions, last modification date, size and so on. ls ~
pwd Gives you the current location - -

 

Linux中常用文件/目录操作命令入下:

BASE COMMAND EXPLANATION FAMOUS ARGUMENTS & OPTIONS EXAMPLES
touch Make a new file. - touch text.txt
mkdir Make a new folder -p Make the requested path regardless if the sub-directories exist or not (because normally, mkdir would return an error if you try to make a 3rd-level directory while the 2nd-level directory doesn't exist). mkdir newfolder mkdir something\ with\ spaces mkdir -p newfolder/subfolder/subsubfolder
rm Remove a file or a directory. -rf Adds the ability to remove folders and their contents (because normal rm can't). rm file.txt rm -rf foldername
head Get the first 10 lines of a text file (or the first n lines of a file) -n Specify the number of lines to output from the beginning. head /etc/profile head -n 19 /etc/profile
tail Get the last 10 lines of a text file (or the last n lines of a file). -n Specify the number of lines to output from the end. tail /etc/profile tail -n 18 /etc/profile
cat Output all the contents of a file - cat /etc/profile
grep Output the lines contain a certain string from a file only. - cat /etc/profile | grep "Bash"
chmod Change the permissions of a file. +x Make the file executable 777 Allow the file to accessed, written and executed by everyone (very dangerous!). 755 Allow everyone to read the file, but only the owners to edit and execute it. chmod +x test.sh chmod 755 test.sh

Bash配置文件

Unix设计哲学中包含一条准则:一切皆文件,everything is a file。这意味着,键盘、鼠标、会话、进程、I/O操作等,无论是软件还是硬件都被描述为一个文件存放于文件系统中,你可以像操作普通文件一样操作它们。Bash包含了许多配置文件,你可以通过修改这些配置文件对Bash做些定制,Bash配置文件包含以下内容:

FILE LOCATION EXPLANATION
/etc/profile Executes many startup shell scripts that are located both in /etc/profile.d/ and other paths. This file is system-wide file, you better not change stuff here.
~/.bashrc Commands that are executed once you enter the system. Usually, most people do modify this file according to their needs.
~/.bash_logout Shell commands that are executed when you exit the system.
~/.bash_history All the commands that you execute in your shell are saved in this file, it's like a log file for the commands that you write (You can avoid saving a command here by simply putting a whitespace (or multiple ones) before the command you are entering).

通常我们执行命令:ls -Ali来列出当前目录中的文件信息,但每次执行都需要输入选项-Ali,有些繁琐。我们可以修改bashrc来达到简化的目的。

使用vim打开文件:vim ~./bashrc,在文件中输入alias la='ls -Ali',然后执行source ~/.bashrc让修改立即生效即可:

然后在命令行中输入:la ~ ~/code可以看到列出了家目录及家目录下code文件中的文件信息:

 

 

⚠️ 如果直接执行alias la='ls -Ali',那么在终端关闭后,la命令也不复存在

Bash Scripting基础

Bash脚本通常带有后缀sh(不带也行),写一段脚本如下:

vim demo.sh

 

# 这里是注释
la # 列出当前目录中的文件信息

然后使用source命令读取脚本中的命令并执行:

source demo.sh

source命令的简写形式为半角句号:.

即命令source demo.sh等价于. demo.sh

资料推荐

  1. The Linux Command Line: A Complete Introduction: A famous book to start learning the topic. Made in 544 pages that would explain everything you need about writing Bash commands and scripts. Very recommended to read. (It’s available PDF free from the website).

  2. Linux Command Line Tutorial for Beginners: If you are someone who prefers video content over written one, then this Youtube set of videos is for you. Made in 80 different videos averaging around 10 minutes each, this series takes your hand in explaining various Linux commands beside more advanced topics in writing shell scripts.

  3. ExplainShell.com: Here, let’s say that you encountered a very long command while browsing an online article or a book, and you didn’t know what does it do and how? Just paste it into the website and it will tell you what each part of it does. It’s amazing online website to explain Linux commands.

  4. LearnShell.org: A free online interactive website to learn the shell. You basically write everything and learn everything from inside your browser.

推荐阅读

The Linux Command Line 中文版

Guess you like

Origin www.cnblogs.com/Cwj-XFH/p/11442615.html