Get the App
SLTechnology News&Howtos  ›  Servers  › 

Environment variables and file operations

Shulou Source: shulou.com Published: 2022-06-03 06:16:16 10月23日 Update

I. Environmental variables

Environment variables generally refer to some parameters used in the operating system to specify the operating environment of the operating system, such as: temporary folder location and system folder location.

An environment variable is an object with a specific name in the operating system that contains information that one or more applications will use. For example, the path environment variable in Windows and DOS operating systems. When the system is asked to run a program without telling it the full path where the program is located, the system should look for the program in addition to the current directory. The user sets environment variables to run processes better.

View the current system environment variables with the echo command:

[root@server02 ~]# echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin[root@server02 ~]#

You can modify the PATH environment variable by defining:

[root@server02 ~]# PATH=$PATH:/tmp/[root@server02 ~]# echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/[root@server02 ~]#

PATH modified in the above way is only valid under the current window, and will be directly invalid after restarting or switching to other windows. By editing the/etc/profile file, redefining PATH at the end of the file will take effect permanently.

II. File operation

cp

1. Command function

Copy one file to another, or multiple files to another directory

2. Command format

cp [options] source dest

cp [options] source directory

3. Parameters

-r Copy directory. If the target directory exists, it will be placed under the target directory; if the target directory does not exist, it will be copied and renamed.

-i comes with parameters. If the target already exists, confirmation is required for operation. The command/usr/bin/cp does not require confirmation.

4. Examples

[root@server02 test]#cp1.txt 2.txtcp: Overwrite "2.txt"? n[root@server02 test]# which cpalias cp='cp -i' /usr/bin/cp[root@server02 test]# /usr/bin/cp1.txt 2.txt[root@server02 test]#[root@server02 test]# cp test1 test2cp: skip directory "test1"[root@server02 test]# cp -r test1 test2[root@server02 test]#

mv

1. Command function

Move one file to another, or multiple files to another directory

2. Command format

mv [options] source dest

mv [options] source directory

3. Parameters

-i comes with parameters. If the target already exists, confirmation is required for operation. The command/usr/bin/mv does not require confirmation.

4. Examples

[root@server02 test]# mv test1 test2[root@server02 test]# which mvalias mv='mv -i' /usr/bin/mv[root@server02 test]# [root@server02 test]# mv 1.txt 2.txtmv: Overwrite "2.txt"? n[root@server02 test]#

cat/tac

1. Command function

cat View the entire contents of the file sequentially

tac View the entire contents of the file in reverse order

2. Command format

cat [options] file

tac [options] file

3. Parameters

-A Displays all characters in the file.

-n Displays file contents with line numbers.

4. Examples

[root@server02 test]# cat 1.txtroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@server02 test]# cat -A 1.txtroot:x:0:0:root:/root:/bin/bash$bin:x:1:1:bin:/bin:/sbin/nologin$daemon:x:2:2:daemon:/sbin:/sbin/nologin$adm:x:3:4:adm:/var/adm:/sbin/nologin$lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin$[root@server02 test]# cat -n 1.txt 1 root:x:0:0:root:/root:/bin/bash 2 bin:x:1:1:bin:/bin:/sbin/nologin 3 daemon:x:2:2:daemon:/sbin:/sbin/nologin 4 adm:x:3:4:adm:/var/adm:/sbin/nologin 5 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@server02 test]# tac 1.txtlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinbin:x:1:1:bin:/bin:/sbin/nologinroot:x:0:0:root:/root:/bin/bash[root@server02 test]#

more/less

1. Command function

more/less One screen to view the contents of the file, use the space bar to turn pages

2. Command format

more [options] file

less [options] file

3. Usage

less support "/" sequential search highlight,"? "Reverse search highlighted.

Less can jump to the beginning of a file with g and to the end with G.

head/tail

1. Command function

head View the entire contents of the file sequentially, displaying 10 lines by default

tail View all the contents of the file in reverse order, showing 10 lines by default

2. Command format

head [options] file

tail [options] file

3. Parameters

-n 2 The number of rows displayed is 2.

-f tail command parameters. Dynamic display of file content. It is mainly used for dynamic observation of log changes.

4. Examples

[root@server02 test]# head -2 1.txtroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologin[root@server02 test]# tail -2 1.txtmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologin[root@server02 test]# tail -f 1.txtroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologinsync:x:5:0:sync:/sbin:/bin/syncshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologin

Tags: File command environment parameter directory system variable content function format target operating system copy program multiple reverse order run location dynamic Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MariaDB MySQL OPPO Reno Shulou Tech Info