Linux directory structure, Bash's basic features, I/O redirection, and pipelines

Source: Internet
Author: User
Tags aliases

Linux directory structure

The LSB (Linux standardsbase) is a core set of standards that ensure a good combination of Linux distributions and Linux applications, including file system hierarchy standards (FHS Filesystem Hierarchy Standard)

Linux There are many subdirectories under the system root directory, which are referenced by the FHS Agreement

/

├──bin

├──boot

├──dev

├──etc

├──home

├──lib

├──lib64

├──lost+found

├──media

├──mnt

├──opt

├──proc

├──root

├──sbin

├──selinux

├──srv

├──sys

├──tmp

├──usr

└──var

/bin : binary is the basic command for the user to use, and the program that is used by the OS startup

/ Boot: store system boot-related files, such as kernel, INITRD, and Grub (bootloader)

/dev: store device files

Block devices: Random access, data blocks (such as hard disks)

Character Devices: Linear access, by character (e.g. keyboard)

Device number: Main device number (major) and secondary device number (minor)

/ etc: the directory where the configuration files are stored; (Requires regular backup)

/etc/sysconfig: System-level application configuration

/ETC/INIT.D: System service Script

/Home : User's home directory, each user's home directory usually defaults to/home/username

/lib : basic shared library; kernel module

/lib/modules: Kernel module files

Static Library,. A

Dynamic library,. dll,. So (Shared object)

The library file cannot be executed alone, it can only be called because there is no execution entry for the program

/lib64 : library files for x86_64 systems

/lost+found : used to store files that were partially repaired during the fsck process.

/media : mount point directory, typically used to mount a mobile device (typically in this directory to create a subdirectory to mount)

/mnt : mount point directory, typically used to mount additional temporary file systems

/opt : optional Directory, installation directory for third-party programs (typically not used)

/proc : pseudo file system, kernel parameter mapping file (for output kernel and process related information)

/root : administrator's home directory;

/sbin : basic commands for managing classes

/selinux :

/srv : Services, intermediate data used by the service running on the system

/sys : pseudo file system, the output kernel identifies a hardware device-related property mapping file on the current system

/ tmp : Temp file directory

/ usr : Universal Shared read-onlydata Global share read-only data (can be partitioned independently)

/usr/bin:

/usr/sbin:

/usr/lib:

/USR/LIB64:

/usr/include: (header file for the application: header files)

/usr/share: structured, independent data

Doc, man (application's help documentation)

/usr/local: The installation location of the commonly used third-party programs now

/usr/local/bin

/usr/local/sbin

/usr/local/lib

/usr/local/lib64

/usr/local/etc

/usr/local/share

/ var : variable date files (storing data that often needs to be changed)

/var/cache: Application Cache data

/var/lib: status information data for an application

/var/local: Dedicated to storing mutable data for applications under/usr/local

/var/lock: Lock File

/var/log: Log file

/var/opt: Dedicated to/opt

/var/spool: Data Pools for Applications

/var/run: Associated data for a running process: normally using the daemon's PID file

Types of files for Linux:

When you use the LS-L command to list a file in a directory, the first character in the first column of each row indicates the type of the file

[[Email protected]]# ll/usr

total124

dr-xr-xr-x. 2 root root 36864 10:15 bin

-: Normal file

D: Catalog file

B: Block device files

C: Character device file

L: Symbolic Link file (similar shortcut)

P: Named pipe file

S: Socket file

<<=====================================>>

Basic features of Bash (i)

( 1 ) Command History

A list of commands that bash has executed in the past

The command history of the current shell process is saved in the buffer ;

-C: Empty list

-D #: Delete the specified history command

-A: Append the current session's command history to the history file;

-R: Reads the contents of the history file and appends the current session's command history to the history file

-W: Appends the current session's command history to the file

Command history-related environment variables:

The commands in the buffer are saved to the user home directory in the file when the shell exits. Bash_history

History Shortcuts:

!#: #为命令历史列表中的命令编号; can execute the # command;

!! : Execution of the previous order;

!-#: Executes the penultimate # command in the command history list;

!string: Executes the last command in the command history list that starts with string;

!$: Refers to the last argument of the last command; esc-->.

History command

Histsize: The number of commands that can be saved in the command history;

Histfile: command history file;

Histfilesize: The number of commands the command history file can hold;

Histcontrol: Control the generation of command history;

Ignoredups: Ignore record duplicate command, repeat the same command for consecutive;

Ignorespace: Do not log commands that begin with a blank character;

Ignoreboth: Both of the above characteristics;

( 2 ) Command Completion

In a Linux system, if you don't remember the full command when you enter a command, you can enter the first few characters of the command, and then press two tab to list all the available commands that begin with the input character.

[[email protected] ~]# MKD//Click here two times tab keyboard, display the following command

Mkdict mkdir Mkdosfs MKDUMPRD

[[email protected] ~]# Mkdi//Click here to two times tab keyboard, then display the following command, gradually narrow the hit range

Mkdict mkdir

[Email protected] ~]#

Bash commands fall into two categories: (for example, commands are both internal and external, and the internal directory location is preferred)

Internal command: System built-in commands

External command: There is an executable program under a path in the system

★ Use the Type command to identify which class the command belongs to, and use the-a option to find the executable file for the command

[Email protected]~]# type history

History is a shell builtin

[[Email protected]~]#

[[Email protected]~]# type-a ls

LSIs aliased to ' ls--color=auto '

LS Is/bin/ls

[[Email protected]~]#

Command Search mechanism:

Path environment variables: paths separated by colons, search from left to right

[Email protected] ~]# echo $PATH

/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

Command search does not search the path path every time, but searches for a hash to achieve greater efficiency.

hash: Caches previously searched paths into memory

Kv:key-value

LS--/bin/ls

[[email protected] ~]# hash

Hits command

5/bin/cat

1/usr/bin/man

3/bin/ls

8/usr/bin/tree

3/usr/bin/clear

( 3 ) path completion (avoid errors, improve efficiency)

Path completion: The same concept as command completion, when the user enters the beginning character of the pathname, the TAB key is searched continuously, and the directory of the specified string header is search, and the completion path is complete.

If the pathname is unique: direct completion

Otherwise: Tab two times to list

( 4 ) command-line expansion features

Bash command line expansion to change a special character to another character

~ Expand to the current user's home directory (home directory)

~username Expand the home directory for the specified user

{,} can host a comma-separated list and expand it to multiple paths

For example:

/var/{log,cache,run} =/var/log/var/cache/var/run

How to create Directories X_n, X_m, Y_n, Y_m

[[email protected]/]# Mkdir-pv/zjj/{x,y}_{n,m}

mkdir:created directory '/ZJJ '

mkdir:created directory '/zjj/x_n '

mkdir:created directory '/zjj/x_m '

mkdir:created directory '/zjj/y_n '

mkdir:created directory '/zjj/y_m '


Directory Management Commands:

mkdir: Creating a Directory

- P: Create parent directory

-V: Show creation process

RmDir: Delete Empty directory

Tree: Displaying a table of contents in a structured way

- D: Show only hierarchical directory files

- L level: show only a few levels

( 5 ) The execution status result of the command, as determined by the Bash Tracking and Saving

There are two results of the command execution:

return value of the command execution result

Status return value after command execution

Bash uses special variable $?  To save the status result of the most recent command, use echo $? Show

BASH: The status return value for each command.

Success: 0

Failure: 1-255, (where 1, 2, 127 are reserved by the system)

$?:

Instance:

[Email protected] tmp]#

[Email protected] tmp]# LS/USR

Bin etc Games include Lib Lib64 libexec local sbin share src tmp

The above is the return value of the command execution result

[[email protected] tmp]# echo $?

0//This is the status return value after the command executes, and a value of 0 indicates that the command executed successfully

[Email protected] tmp]#

[Email protected] tmp]# LS/USRR

Ls:cannot Access/usrr:nosuch file or directory

The above is a deliberately wrong directory name, so command execution failed

[Email protected] tmp]#

[[email protected] tmp]# echo $?

2//This is the status return value after the command executes, and a value of 2 indicates that the command executed successfully

[Email protected] tmp]#

( 6 ) Command alias

Command aliases: Another reference symbol for commands, maintained by the bash program

Enter alias directly to display all the defined command aliases on the current system

Define aliases: Alias alias = ' Original Command '

Example: Alias cls= ' Clear '

Aliases that are undefined: unalias aliases

Example: Unalias CLS

★ If the alias is the same as the original command name: If you want to execute the original command directly, you need to add \ \ Before the command, which means using the command itself instead of the alias

★~/.bashrc# If you want the specified alias to take effect permanently, modify the file to add the command alias to the file content.

Basic features of Bash (ii)

( 1 ) globing

File name wildcard in bash

Wildcard characters:

*: matches any character of any length

?: matches any single character

[^] # (caret) matches any single character outside the specified range

[]: matches any single character within the specified range of characters, not case-sensitive

[ABC], [A-m], [A-z], [A-z], [0-9], [a-za-z], [0-9a-za-z]

[: Space:] white space characters

[:p UNCT:] Punctuation

[: Lower:] lowercase letters

[: Upper:] Uppercase

[: Alpha:] uppercase and lowercase letters

[:d Igit:] Number

[: Alnum:] numbers and uppercase and lowercase letters

# Man 7 glob view globing help manual

( 2 ) key Combinations

There are many shortcuts in bash, and mastering them will improve productivity.

Cursor Jump:

CTRL + A: Skip to the beginning of the command

Ctrl+e: Jump to the end of the command line

CTRL+F: Cursor jumps forward one character

CTRL+B: Cursor Skips one character

Delete or paste:

Ctrl+d: Delete the character of the cursor

Ctrl+u: Cuts the cursor to the beginning of the command line

CTRL+K: Cuts the cursor to the end of the command line

Ctrl+y: Paste the clipped content

Ctrl+l #清屏

CTRL + C #终止命令

CTRL + Z #暂停命令 (run the process in the background)

Enter jobs to view background run commands

Input FG back to foreground run

Ctrl+r #搜索历史

( 3 ) IO Redirects and Pipelines

Input and output redirection

I/O: device, register

Input: Standard input: stdin, 0

Ouput: Standard output: stdout, 1

Standard ERROR: stderr,2

Input redirect:<,<<< p= "" >

<: Input redirection (basic not used, the following command < can be omitted)

Cat </etc/passwd

Tr ' A-Z ' A-Z ' </etc/passwd

<< EOF: Create a file here, here document

Often used to create files or generate menus in scripts;

#!/bin/bash

#

Cat >/tmp.menu.txt << EOF

A:A is asshole

B:b is bastard

C:C is Cadger

D:D is damn

Eof

Output REDIRECT:>,>>

: Overwrite output

>>: Append output

SET-C: Prohibit use of overwrite redirects to existing files;

Set +c: Turn off the above features;

>|: Under the-C feature, the override redirection is enforced;

/dev/null:bit bucket, bit barrel

Error redirection:2>,2>>

2>: Overwrite

2>>: Append

The standard output and error output are also re-determined:

COMMAND >/path/to/outfile 2>/path/to/errfile

REDIRECT standard output or error output to the same file

COMMAND &>/path/to/somefile

COMMAND >/path/to/somefile 2>&1

Pipeline

Pipeline: Passes the execution result of a command to the next command, as a parameter to the next command

COMMAND1 | COMMAND2 | COMMAND3 | ...

You cannot pass a variable in a pipeline, especially if you cannot generate a variable in the preceding command

How to use tee:

Cat/etc/rc.d/rc.sysinit | Tee/tmp/a.out | Wc-l

Fork output: Tee, command will not be processed, will be sent to the next pipeline


This article is from the "My Blog History" blog, please be sure to keep this source http://17460990.blog.51cto.com/4587004/1689987

Linux directory structure, Bash's basic features, I/O redirection, and pipelines

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.