Linux Basics (3)

Linux directory structure and file basic operation

Linux Directory Structure

Linux is a directory tree structure form to construct the entire system can be understood as a tree directory is a user operable skeletal system . Although in essence, whether the directory structure of the kernel or operating system is stored on the disk, but logically Linux disk is "hung" (mount) on a directory , each directory can not only use local disk partition file system, you can use the file system on the network. For example, you can use the Network File System (Network File System, NFS) server load a particular catalog.

FHS standard

Most Linux directory structure is ordained (FHS standard), are dead, after you have mastered everything you on the inside of the operation will become organized.

The FHS (English: Filesystem Hierarchy Standard Chinese: the file system hierarchy standard), most versions of Linux file organization using this form, the FHS defines the purpose of each system zone, the minimum configuration of files and directories is also given the required the exceptions and contradictions treatment *. *

FHS specification defines two layers, a first layer, /each of the following directory file what data should be placed, for example, /etcshould be placed document set, /binand /sbinthe executable files and the like should be placed.

The second layer is for /usrand /vardefine subdirectories of the two directories. E.g. /var/logplacement system log file, /usr/shareplacing a shared data and the like.

Excuting an order:

$ tree /

sudo apt-get update

sudo apt-get install tree

Here Insert Picture Description

Directory path

path

Use cdthe command to switch directories on Linux which .represents the current directory, ..represent the parent directory (note to the .beginning of the document are hidden files, so these two directories must also be hidden, you can use the ls -acommand to view hidden files), -expressed on a directory, usually represents the current user's home directory. Use pwdcommand to obtain the current location path (absolute path) .

Enter the parent directory:

$ cd ..

Into your home directory:

$ cd ~ 
# 或者 cd /home/<你的用户名> 

Use pwdGet the current path:

$ pwd

Absolute path

About absolute path, simply put, is the root " /" directory is the full path to the starting point to the directory you want to end, forms such as: /usr/local/bin, indicates the root directory usrdirectory localdirectory bindirectory.

relative path

Relative path, which is relative to your current directory path , relative path is the current directory .as a starting point to the directory you want to end, forms such as: usr/local/bin(This assumes your current directory is the root directory). May have noticed, we represent the actual relative path and do not add that represents the current directory ., but directly to the beginning of the directory name, because the usr directory / subdirectory under the directory, you can omit this is a;. If the current directory of the parent directory, you need to use .., such as your current directory under / home / zhangsan directory, root directory should be expressed as ../../representing the parent directory ( homethe directory) of the parent directory ( /the directory) .

Below your home directory as a starting point, respectively, by way of absolute and relative paths into the / usr / local / bin directory:

# 绝对路径
$ cd /usr/local/bin
# 相对路径
$ cd ../../usr/local/bin

I assume that in the current /usr/local/bindirectory, I want to get into one of the localdirectory is to use cd ..easy or cd /usr/localconvenient? And if you want to enter a usrdirectory, then cd /usr, it is more than cd ../..convenient little.

The basic operation of Linux file

New

Create a new blank document

touch Command: Its main role is to change the existing file time stamp (for example, last access time, last modified time), but in the case without any parameters, specify only a file name, you can create a specified file blank file name (do not overwrite existing files of the same name), of course, you can also specify a timestamp of the file at the same time.

New Directory

mkdir(Make directories) command to create an empty directory, create a directory can also be specified privilege attributes simultaneously.

Use -pparameters, creating parent directory (if the parent directory does not exist), follows the same time create a multi-level directory (which is very useful when installing the software, configure the installation path):

$ mkdir -p father/son/grandson

Directory path back to the way an absolute path representation is also possible.

copy

Copy files

cp(Copy) command to copy a file to a specified directory.
Such as copying "test" file to "/ home / zhangsan / father / son / grandson" directory:

$ cp test father/son/grandson
Copy directory

cpPlus -ror -Rparameters; represents a recursive copy;

delete

Delete Files

rm(Remove files or directories) command to delete a file:

$ rm test

You can use -fthe parameters forcibly remove.

Remove directory

Also you need to add -ror -Rparameters.

Move files and rename

Moving Files

Using mv(move or rename files) command to move a file (shear). The file "file1" to move to the Documents directory:
mv 源目录文件 目的目录

$ mkdir Documents
$ touch file1
$ mv file1 Documents
Rename the file

The file "file1" rename "myfile":
mv 旧的文件名 新的文件名

$ mv file1 myfile
Batch rename

You can use commands renameto achieve. But it is to use perl regular expressions as parameters.

$ cd /home/shiyanlou/

# 使用通配符批量创建 5 个文件:
$ touch file{1..5}.txt

# 批量将这 5 个后缀为 .txt 的文本文件重命名为以 .c 为后缀的文件:
$ rename 's/\.txt/\.c/' *.txt

# 批量将这 5 个文件,文件名和后缀改为大写:
$ rename 'y/a-z/A-Z/' *.c

renameThe second argument is to use wildcard matches all suffix .txtof the file, and then use the first argument of the regular expression to match these files .txtsuffix replaced .c.

check the file

Use cat, tacand a nlcommand to view the file : the first two commands are used to print the contents of the file to the standard output (terminal), wherein the sequential display is a positive cat, tac as a reverse display.

Standard input and output: when we execute a shell command line is often automatically open three standard files, i.e., the standard input file (stdin), default keyboard corresponding to the terminal, the standard output file (stdout) and standard error output file (stderr), two files have been redirected to the corresponding screen of the terminal, so that we can directly see the output. Process to get from the standard input file, input data, output normal output data to the standard output file, and the error message to the standard error file.

  • Plus -nparameter display line numbers / nlcommand, add line numbers and print.
$ cat -n passwd

Use moreand lesscommand tab to view files : You can use the Enterkeys to scroll down one line, use the Spacekey to scroll down a screen, press the hdisplay help qexit.

Use headand tailcommand to view the file : The first few lines (the default is 10 lines, all less than 10 lines are displayed) to view files and tail a few lines.
Look even more direct line, with -nparameters, followed by line numbers, such as:

$ tail -n 1 /etc/passwd

View file types

Use filethe command to view the type of file.

Edit the file

Usually we will directly use a special command line in Linux file editor to edit the following example (emacs, vim, nano).

Published 33 original articles · won praise 1 · views 1246

Guess you like

Origin blog.csdn.net/weixin_44783002/article/details/104591881