Linux(二)常用命令chmod nohup sed等

Linux(二)常用命令chmod/nohup/sed等

1 chmod命令

​ Linux常用命令chmod:修改文件权限 777和754

​ Linux系统中,每个用户的角色和权限划分得很细致、严格,每个文件、目录都设有访问许可权限,利用这种机制来决定某个用户通过某种方式对文件、目录进行读、写、执行等操作。

​ 操作文件或目录的用户,有3种不同类型:文件所有者、群组用户、其他用户。最高位表示文件所有者的权限值,中间位表示群组用户的权限值,最低位则表示其他用户的权限值。chmod 777中,三个数字7分别对应上面三种用户,权限值都为7。

​ 文件或目录的权限分为3种:只读、只写、可执行。如下表:

权限 权限数值 二进制 具体作用
r 4 0100 read,读取。当前用户可以读取文件内容,当前用户可以浏览目录
w 2 0010 write,写入。当前用户可以新增或修改文件内容,当前用户可以删除、移动目录或目录内的文件
x 1 0001 execute,执行。当前用户可以执行文件,当前用户可以进入目录

依照上面的表格,权限组合就是对应权限值求和,如下:

7 = 4 + 2 + 1 读、写、运行权限

5 = 4 + 1 读、运行权限

4 = 4 只读权限

示例:

# 将data目录的权限就被修改为777,即可读可写可执行
chmod 777  文件或目录
chmod 777 /opt/data
# 将filename文件的读写运行权限赋予文件所有者,把读和运行的权限赋予群组用户,把读的权限赋予其他用户
chmod 754 filename

更多官方的解释,可以使用下面命令查看:

chmod  --help   
或者
man  chmod

2 nohup、&、 2>&1

​ 定义:nohup 命令运行由 Command 参数和任何相关的 Arg 参数指定的命令,忽略所有挂断(SIGHUP)信号。在注销后使用 nohup 命令运行后台中的程序。要运行后台中的 nohup 命令,添加 & ( 表示“and”的符号)到命令的尾部。

​ 简言之:1、nohup 其实就是 no hang up(不挂断) 的缩写。2、nohup执行作业后,用户根目录下会有一个 nohup.out 的文件来存放相关作业的输出(即项目报错与日志)。

​ nohup 不挂起(no hang up)放在命令的开头,即关闭终端或者退出某个账号,进程也继续保持运行状态,一般配合&符号一起使用:

nohup  Command  &

nohup 和 & 的区别

​ nohup : 不挂断的运行,免疫session的SIGHUP信号。

​ & :在后台运行,免疫(Ctrl + C)SIGINT信号。

​ 在项目上线,为了确保项目运行不受操作影响,需要同时免疫两个信号的干扰,即nohup和&同时使用。

如某个网站,项目上线启动命令为:

nohup java -jar com.nml-1.0.jar &

& 放在命令到结尾,表示后台运行,防止终端一直被某个进程占用,这样终端可以执行别到任务,配合 >file 2>&1可以将log保存到某个文件中,但如果终端关闭,则进程也停止运行:

command > file.log 2>&1 &

基本概念:0 表示stdin标准输入;1 表示stdout标准输出;2 表示stderr标准错误

1>file # 表示将标准输出输出到file中,也就相当于 >file
2>error.log #表示将错误输出到error.log文件中
2>&1 #表示将错误重定向到标准输出上
2>&1 >file #表示将错误输出到终端,标准输出重定向到文件file,等于 > file 2>&1(标准输出重定向到文件,错误重定向到标准输出)

3 sed 命令

​ sed命令依照脚本的指令来处理、编辑文本文件。主要用来自动编辑一个或多个文件,简化对文件的反复操作,编写转换程序等等。

语法

sed [-hnV][-e<script>][-f<script文件>][文本文件]

参数说明

  • -e< script >或–expression=< script > 以选项中指定的script来处理输入的文本文件。
  • -f<script文件>或–file=<script文件> 以选项中指定的script文件来处理输入的文本文件。
  • -h或–help 显示帮助
  • -n或–quiet或–silent 仅显示script处理后的结果
  • -V或–version 显示版本信息
[root@hadoop1 deployShell]# sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  --follow-symlinks
                 follow symlinks when processing in place; hard links
                 will still be broken.
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied).
                 The default operation mode is to break symbolic and hard links.
                 This can be changed with --follow-symlinks and --copy.
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode.
                 While this will avoid breaking links (symbolic or hard), the
                 resulting editing operation is not atomic.  This is rarely
                 the desired mode; --follow-symlinks is usually enough, and
                 it is both faster and more secure.
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
      --help     display this help and exit
      --version  output version information and exit

If no -e, --expression, -f, or --file option is given, then the first
non-option argument is taken as the sed script to interpret.  All
remaining arguments are names of input files; if no input files are
specified, then the standard input is read.

GNU sed home page: <http://www.gnu.org/software/sed/>.
General help using GNU software: <http://www.gnu.org/gethelp/>.
E-mail bug reports to: <[email protected]>.
Be sure to include the word ``sed'' somewhere in the ``Subject:'' field.
[root@hadoop1 deployShell]# 

动作说明

  • a :新增, a 的后面可以接字符串,而这些字符串会在新的一行出现(当前行的下一行)
  • c :取代, c 的后面可以接字符串,这些字符串可以取代 n1到n2 之间的行
  • d :删除,因为是删除,所以 d 后面通常没有内容
  • i :插入, i 的后面可以接字符串,而这些字串会在新一行出现(当前行的上一行)
  • p :打印,将某个选择的数据打印出来。通常 p 会与命令sed -n 一起运行
  • s :取代,通常搭配正规表示式,sed ‘s/要被取代的字串/新的字串/g’,如 sed ‘s/^.*an//g’

实操

新建testfile,内容如下:

[root@hadoop1 deployShell]# vi testfile
[root@hadoop1 deployShell]# cat testfile
aa
bb
cc
dddd
i love xuanmeier

在testfile文件的第四行后添加一行,并将结果输出到标准输出,在命令行提示符下输入命令,输出结果如下:

[root@hadoop1 deployShell]# sed -e 5a\forever testfile
aa
bb
cc
dddd
i love xuanmeier
forever

新增和删除

给每行加序号

[root@hadoop1 deployShell]# nl testfile
     1	aa
     2	bb
     3	cc
     4	dddd
     5	i love xuanmeier

新增

在第4行前新增一行

[root@hadoop1 deployShell]# nl testfile| sed '4i da me me da'
     1	aa
     2	bb
     3	cc
da me me da
     4	dddd
     5	i love xuanmeier

在第4行前新增多行

[root@hadoop1 deployShell]# nl testfile| sed '4i da me me da \
> hei xiu.
> '
     1	aa
     2	bb
     3	cc
da me me da 
hei xiu.
     4	dddd
     5	i love xuanmeier

删除1-2行

[root@hadoop1 deployShell]# nl testfile | sed '1,2d'
     3	cc
     4	dddd
     5	i love xuanmeier

以行为单位的替换与显示

将第1-3行的内容取代成为No 1-3 :

[root@hadoop1 deployShell]# nl testfile | sed '1,3c No 1-3'
No 1-3
     4	dddd
     5	i love xuanmeier

数据的搜寻并显示

搜索testfile有aa关键字的行

[root@hadoop1 deployShell]# nl testfile | sed '/aa/p'
     1	aa
     1	aa
     2	bb
     3	cc
     4	dddd
     5	i love xuanmeier

如果aa找到,除输出所有行,还会输出匹配行。

使用-n的时候将只打印包含模板的行。

[root@hadoop1 deployShell]# nl testfile | sed -n '/aa/p'
     1	aa

数据的搜寻并删除

删除testfile所有包含aa的行,其他行输出

[root@hadoop1 deployShell]# nl testfile | sed '/aa/d'
     2	bb
     3	cc
     4	dddd
     5	i love xuanmeier
#第一行的匹配aa已经删除了

数据的搜寻并执行命令

​ 搜索testfile找到aa对应的行,执行后面花括号中的一组命令,每个命令之间用分号分隔,这里把a替换为abc,再输出这行。最后的q是退出。

[root@hadoop1 deployShell]# nl testfile | sed '/aa/{s/a/abc/;p;q}'
     1	abca
     1	abca

数据的搜寻并替换

除整行的处理模式外,sed 还可以用行为单位进行部分数据的搜寻并取代。sed 的搜寻与替代基本上 与vi类似!

sed 's/要被取代的字串/新的字串/g'

删除第5行an及an之前的内容

[root@hadoop1 deployShell]# nl testfile | grep 'i'
     5	i love xuanmeier
[root@hadoop1 deployShell]# nl testfile | grep 'i' | sed 's/^.*an//g'
meier

直接修改文件内容(慎用)

​ sed 可以直接修改文件的内容,不必使用管道命令或数据流重导向!由于此动作会直接修改到原始的文件,千万不要拿系统配置来测试!

这里使用testfile 文件来测试。testfile文件内容如下:

[root@hadoop1 deployShell]# cat testfile
aa
bb
cc
dddd
i love xuanmeier

利用 sed 直接在 testfile 最后一行加入 I am apple

[root@hadoop1 deployShell]# sed -i '$a I am apple' testfile 
[root@hadoop1 deployShell]# cat testfile
aa
bb
cc
dddd
i love xuanmeier
I am apple

$ 代表最后一行, a 的动作是新增,因此该文件最后新增 !

sed 的 -i 选项可以直接修改文件内容,这功能非常有帮助!举例来说,如果你有一个 100 万行的文件,你要在第 100 行加某些文字,此时使用 vim 可能会疯掉!因为文件太大了!那怎办?就利用 sed 啊!透过 sed 直接修改/取代的功能,你甚至不需要使用 vim 去修订!

将每行最后以c结尾的换为!

[root@hadoop1 deployShell]# sed -i 's/c$/\!/g' testfile 
[root@hadoop1 deployShell]# cat testfile
aa
bb
c!
dddd
i love xuanmeier
I am apple

4 参考文献
https://blog.csdn.net/pythonw/article/details/80263428
https://blog.csdn.net/lovewebeye/article/details/82934049

发布了38 篇原创文章 · 获赞 46 · 访问量 1411

猜你喜欢

转载自blog.csdn.net/weixin_45568892/article/details/105468570
今日推荐