生信人的Linux1-13

Linux Day1: cd/pwd/mkdir/rmdir

1.目录的相关操作:

比较特殊的目录:

  • . 代表此层目录
  • .. 代表上一层目录
  • -代表前一个工作目录
  • ~ 代表“目前用户身法”所在的文件夹
  • ~account 代表 account这个用户的主文件夹(account是个账户的名称)

几个常见的处理目录的命令:

  • cd:切换目录(Change DIrectory)
    例:cd dirname:切换至dirname路径下
    cd 没有加上任何命令,也还是代表回到自己主文件夹的意思(同 cd ~)

  • pwd:显示当前目录(Print Working Directory),以绝对路径的方式显示当前工作目录。
  • 绝对路径:由根目录(/)开始写起的文件名或目录名称,例如/home/dmtsai/.bashrc。
  • 相对路径: 相对于当前路径的文件名写法。例如./home/dmtsai 或 ../../home/dmtsai/。
  • 反正开头不是/就属于相对路径的写法。

  • mkdir :新建一个新的目录
  • mkdir有两个重要的参数:
  1. -m:配置文件案的权限。
  2. -p:帮助你直接将所需的目录(包含上层目录)递归创建起来

对比touch、nano

例:利用cd切换到tmp文件夹cd /tmp,在tmp下创建文件夹testmkdir test,也可以用到-p在tmp下面创建分层文件夹mkdir -p test1/test2/test3


  • rmdir:删除一个空的目录
  • 相对于mkdir,rmdir也有参数:-p:连同上层“空的”目录一起删除。

例:现在我们想要删除上面tmp下的test文件夹,原理同mkdir。


  • tar:压缩/解压缩(后续章节会讲到)

Linux Day2: ls/cp/rm/mv

2.1 查看文件与目录: ls

  • 需要结合不同参数反复练习。

2.2 复制、删除与移动:cp,rm,mv

cp:复制+创建连接文件(快捷方式)
mv:移动+ 重命名(rename)
rm:删除


  • cp (复制文件或目录)
    例1cp ~/.bashrc /tmp/bashrc表示:将 .bashrc 复制到 /tmp 文件下,并更名为bashrc。
cp -i  ~/.bashrc /tmp/bashrc
cp:是否覆盖'/tmp/bashrc'? y  

例2:切换目录到 /tmp,并将var/log/wtmp 复制到 /tmp 且查看属性。
cp /var/log/wtmp .最后的“空格 ."不要忘。
查看属性:ls -l /var/log/wtmp wtmp
(ls -l :列出长数据串,包含文件的属性与权限等数据)

rw-rw-r-- 1 root utmp 6528 10月  3 10:45 /var/log/wtmp
-rw-rw-r-- 1 lc   lc   6528 10月  3 10:53 wtmp

cp 在不加任何参数的情况下,文件的某些属性/权限会改变(如上 wtmp的时间发生量改变); 那么考虑以下命令:
cp -a /var/log/wtmp wtmp_2这里用到了 cp 的参数 -a :相当于 -pdr 的意思。


例子3:复制 /etc/ 这个目录下的所有内容到 /tmp 下面。
cp /etc/ /tmp提示cp: 略过目录'/etc/',由于目录不能直接复制的原因。
考虑:cp -r /etc/ /tmp,但是我的电脑etc里面的文件打不开,提示权限不够。
(-r :递归持续复制,用于目录的复制行为,但是文件与目录的权限可能会改变)也可以用cp -a /etc/ /tmp来执行命令,尤其是在备份的情况下。
例4:建立例1 bashrc的连接档(symbolic link)
cp的更多用法参考鸟哥的例子(5/6)
总之-再复制时,需要考虑到以下:
1.是否需要完整的保留来源档案的信息?
2.来源档案是否为连接档(symbolic link file)?(需要后面完善连接档的学习)
3.来源档是是否为特殊的文档,例如FIFO,socket(后面理解)?
4.来源文件是否为目录?


  • rm(移除档案或目录)
    选项和参数:
  1. -f:就是force的意思,忽略不存在的 档案,不会警告讯息;
  2. -i:互动模式,在删除前会询问使用者是否动作(常用这个吧)。
  3. -r :递归删除,最常用在目录的删除
    4.考虑以下两个命令:rm -rf;rm -rf /删除根目录。
    例1:通过通配符的帮忙,将/tmp底下以bashrc开头档名通通删除。
    代表的是0到无穷多个任意字符)
    rm -i bashrc.
    例2:删除一个带有-开头的档案。
    首先用touch命令建立一个空档案,touch ./-aaa-,查看刚创立的文件ls -l,提示为:-rw-rw-r-- 1 ywu ywu 0 Oct 3 18:31 -aaa-(档案大小为零)。
    试着用rm命令删除文件,rm -aaa-,
    提示为:
    Try 'rm ./-aaa-' to remove the file '-aaa-'.为什么会提示这个命令不太明白,因为rm有移动的意思?
    Try 'rm --help' for more information.因为”-“是选项,所以系统会误判,所以尽量避免首字母是”-“是的方法,在目录上加上[ ./ ]。(”-“需要进一步理解其意义)

  • mv (移动档案与目录,或更名)
    选项和参数:
  1. -f:就是force的意思,如果档案已经存在,不会询问而直接覆盖;
  2. -i:互动模式,若目标档案(destination)已经存在,不会询问而直接覆盖。
  3. -u :若目标档案(destination)已经存在,且source比较新,才会更新(update)。
    例1:建立目录wy,并在wy目录下分别建立文件mvtest,bashrc,通过mv命令将bashrc移动到mvtest。mkdir wy,利用pwd确认是否创建成功,切换到cd wy,分别建立文件mvtest,bashrc。再通过mv bashrc mvtest.
    mv命令

Linux Day3: cat/nl/head/tail

2.3 档案内容查阅

  • cat 由第一行开始显示档案内容。
  • tac 从最后一行开始显示(可以看出tac是cat的倒着写。)
  • nl 显示的时候,顺着输出行号!
  • head 只看头几行
  • tail 只看尾巴就行
  • od 以二进制的方式读取档案内容!(二进制需要了解)
  • 直接检视档案内容可以使用 cat/tac/nl 这几个命令!

  • cat (concatenate连续)
    -n: 打印出行号,连同空白行也会有行号,与 -b 的选项不同;
    -b: 列出行号,仅针对非空白行做行号显示,空白行不标行号!
    例1:检阅/etc/issue 这个档案的内容
cat /etc/issue 
Ubuntu 16.04.3 LTS \n \l

给上面的例子加印行号:
用 -n查看:

 cat -n /etc/issue
     1  Ubuntu 16.04.3 LTS \n \l
     2  

用-b查看:

cat -b /etc/issue
     1  Ubuntu 16.04.3 LTS \n \l
  • cat配合more/less 效果更好。

  • tac(反向列示)
    tac 与 cat 是相反的两个例子。tac是由最后一行到第一行,cat由第一行到最后一行显示。

  • nl(添加行号打印)
$ nl /etc/issue
     1  Ubuntu 16.04.3 LTS \n \l
       
$ nl -b a /etc/issue
     1  Ubuntu 16.04.3 LTS \n \l
     2  
$ nl -b a -n rz /etc/issue
000001  Ubuntu 16.04.3 LTS \n \l
000002  
$ nl -b a -n rz -w3 /etc/issue
001 Ubuntu 16.04.3 LTS \n \l
002 
$ nl -b a -n rz -w2 /etc/issue
01  Ubuntu 16.04.3 LTS \n \l
02  

nl 可以将输出的档案内容自动的补上行号!其预设的结果与cat -n 有点不太一样 , nl 可以将行号做比较多的显示设计,包括位数与是否补齐 0 等等功能。


  • head(取出前面几行)
    选项与参数:
    1.-n :后面接数字,代表显示几行的意思。默认的情况下,显示前面十行:(空格也算是一行)
$ head /etc/inputrc 
# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.

# Be 8 bit clean.
set input-meta on
set output-meta on

# To allow the use of 8bit-characters like the german umlauts, uncomment
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.

若要显示前面20行,head -n 20 /etc/inputrc;

$ head -n 20 /etc/inputrc
# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.

# Be 8 bit clean.
set input-meta on
set output-meta on

# To allow the use of 8bit-characters like the german umlauts, uncomment
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.

# set convert-meta off

# try to enable the application keypad when it is called.  Some systems
# need this to enable the arrow keys.
# set enable-keypad on

# see /usr/share/doc/bash/inputrc.arrows for other codes of arrow keys

# do not bell on tab-completion

问题: 好像也没有20行?

2.后面100行如果不打印:head -n -100/etc/man.config(注意-100)。


tail(取出后面几行)
选项与参数:

  1. -n:后面数字,代表显示几行的意思。
  2. -f: 表示持续侦测后面所接的 档名,要等到按下【ctrl】-c 才会结束tail的侦测。
  3. 与head相比较记忆:tail -n +100 /etc/man.config代表从改档案100行以后都会被列出来。
  4. 我的tail与head有同样的问题存在样。
  • 需要显示 /etc/man.config 的11行到20行?
  • head -n 20 /etc/man.config l tail -n 10需要用到管道命令,后面学习。

od:非纯文本文件
选项或参数:

  1. -t: 后面可以接各种【类型(TYPE0】的输出,例如:
    a :利用默认的字符来输出;(有点多,显示不完)
    c :使用ASCⅡ字符来输出。

Linux Day4: more/less/touch

1. more 一页一页的显示档案内容。

  • 空格键 (space):代表向下翻一页;
  • Enter :代表向下翻『一行』;
  • /字符串 :代表在这个显示癿内容弼中,向下搜寻『字符串』这个关键词;
  • :f :立刻显示出文件名以及目前显示癿行数;
  • q :代表立刻离开 more ,不再显示该档案内容。
  • b 或者 [ctrl]-b :代表往回翻页,不过这劢作叧对档案有用,对管线无用。
$ more /etc/inputrc
# /etc/inputrc - global inputrc for libreadline
# See readline(3readline) and `info rluserman' for more information.

# Be 8 bit clean.
set input-meta on
set output-meta on

# To allow the use of 8bit-characters like the german umlauts, uncomment
# the line below. However this makes the meta key not work as a meta key,
# which is annoying to those which don't need to type in 8-bit characters.

# set convert-meta off

# try to enable the application keypad when it is called.  Some systems
# need this to enable the arrow keys.
# set enable-keypad on

# see /usr/share/doc/bash/inputrc.arrows for other codes of arrow keys

# do not bell on tab-completion
# set bell-style none
# set bell-style visible

# some defaults / modifications for the emacs mode
$if mode=emacs

# allow the use of the Home/End keys
"\e[1~": beginning-of-line
"\e[4~": end-of-line

# allow the use of the Delete/Insert keys
"\e[3~": delete-char

最后一行
more命令

对于52%的说明:more 后面接的档案内容行数大亍屏幕输出的行数时, 就会出现类似上面的图示。最后一行会显示出目前显示癿百分比, 而且还可以在最后一行输入一些有用的指令:
按下enter:

enter

2. less 与 more 类似,但是比 more 更好的是,他可以往前翻页,也可以向前搜索!

其命令有:

  • 空格键 :向下翻劢一页;
  • [pagedown]:向下翻劢一页;
  • [pageup] :向上翻劢一页;
  • /字符串 :向下搜寻『字符串』癿功能;
  • ?字符串 :向上搜寻『字符串』癿功能;
  • n :重复前一个搜寻 (不 / 戒 ? 有关!)
  • N :反向癿重复前一个搜寻 (不 / 戒 ? 有关!)
  • q :离开 less 这个程序;

这个为使用less搜索文件后的:less /etc/inputrc

less

3.修改档案时间或建置新档: touch

选项与参数:
-a :仅修订 access time;
-c :仅修改档案的时间,若该档案不存在则不建立新档案;
-d :后面可以接欲修订的日期而不用目前的日期,也可以使用 --date="日期或时间"
-m :仅修改 mtime ;
-t :后面可以接欲修订的间而不用目前的时间,格式为[YYMMDDhhmm]

linux 底下都会记录许多的时间参数, 其实是有三个主要的变动时间,那么三个时间的意义是什么呢?

  • modification time (mtime):
    当该档案的『内容数据』变更时,就会更新这个时间!内容数据指的是档案的内容,而不是档案的属性或权限喔!
  • status time (ctime):
    当该档案的『状忞 (status)』改变时,就会更新这个时间,举例来说,像是权限不属性被更改了,都会更新这个时间啊。
  • access time (atime):
    当『该档案癿内容被取用』时,就会更新这个读取时间 (access)。举例来说,我们使用 cat 去读取/etc/inputrc, 就会更新该档案的 atime 了。
    范例一:新建一个空的档案并观察时间
$ cd /tmp
$ pwd
/tmp
$ touch testtouch
$ ls -l testtouch
-rw-rw-r-- 1 ywu ywu 0 Oct  5 18:49 testtouch

这个档案的大小是 0 呢!在预设的状忞下,如果 touch 后面有接档
案, 则该档案的三个时间 (atime/ctime/mtime) 都会更新为目前的时间。若该档案不存在,则会建立一个新的空的档案喔!
范例二:修改案例二的testtouch档案,将日期调整为两天前

$ touch -d "2 days ago" testtouch
$ ls -l testtouch
-rw-rw-r-- 1 ywu ywu 0 Oct  3 18:56 testtouch

范例三:将上个范例的testtouch 日期改为 2018/10/05 2:02

$ touch -t 1810050202 testtouch
$ ls -l testtouch
-rw-rw-r-- 1 ywu ywu 0 Oct  5 02:02 testtouch

Linux Day5:操作系统与基础

Linux操作系统与基础。

  • Linux的基本原则:
  1. 由目的单一的小程序组成:组合小程序完成负责任务。
  2. 一切皆文件;
  3. 尽量避免捕获用户接口;
  4. 配置文件保存为纯文本格式;

  • GUI接口:
  • ClI接口:
  • 命令提示符:# root;$ 普通用户。

  • 命令格式:
    命令 选项 参数
  • 选项:
    1.短选项:-(多个选项可以组合:-a -b = -ab);
    2.长选项: --
    3.某些选项是可以带参数的,
  • 参数:命令的作用对象。

  • su : switch user
    (# su [-1] 用户名)全切换的意思。

  • passwd:
$ passwd
Changing password for ywu.
(current) UNIX password: 
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully

Linux Day6: 路径、权限

一些概念:

  • 目录:working directoy、current + directory;目录也是一种文件,映像路径(FHS:文件系统层级标准。)

  • 路径:从指定起始点到目的地所经过的位置。

  • 文件系统:file system。
    马哥:文件名是不是文件的数据?

  • 命令类型:
    内置命令(shell内置),内部,内建
    外部命令:在文件系统的某个路径下有一个与命令对应的可执行文件。

  • 环境变量:PATH:使用冒号分隔的路径。
    printenv显示出工作环境:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/

相对路径与绝对路径:(树状图说明)

路径.PNG
  • 绝对路径:想要查找😊这个文件,如果从☀(根目录)开始查找,通过☁,找到😊;这个就叫绝对路径。
  • 相对路径:如果你目前所处☁这个文件,那么想要查找😊这个文件,无需要回到而根目录,可以直接开始从☁向下查找。(相对路径:相对于目前所处位置的路径)
  • 那么目前多处位置为🌙,想要查找😊这个文件,就不能用相对路径,需要先回到☀到😊的某个节点上开始查找。

  • list:ls(列出,列表)
$ ls -l
total 12
-rw-rw-r-- 1 ywu  ywu     0 Oct  3 18:31 -aaa-
-rw-r--r-- 1 root root  207 Oct  1 09:45 readme.txt
-rw-rw-r-- 1 ywu  ywu     0 Oct  5 02:02 testtouch
drwxrwxr-x 2 ywu  ywu  4096 Oct  3 17:22 tmp
drwxrwxr-x 3 ywu  ywu  4096 Oct  5 18:56 wy

鸟哥:-rw-rw-r--第一个字符代表这个档案是『目彔、档案戒链接文件等等』:

  • 当为[ d ]则是目彔,例如上表档名为『.gconf』的那一行;
  • 当为[ - ]则是档案,例如上表档名为『install.log』那一行;
  • 若是[ l ]则表示为连结档(link file);
  • 若是[ b ]则表示为装置文件里面的可供储存的接口讴备(可随机存取装置);
  • 若是[ c ]则表示为装置文件里面的串行端口讴备,例如键盘、鼠标(一次性读取装置)

文件类型:(马哥简洁版)
-:普通文件(f)
d:目录文件
b:块设备文件(block)
c: 字符设备文件(character)
l:符号连接文件(symbolic link file)
p:命令管道文件(pipe)
s:套接字文件(socket)

  • 鸟哥:接下来的字符中,以三个为一组,且均为『rwx』 的三个参数的组合其中,[ r ]代表可读(read)、 [ w ]代表可写(write)、 [ x ]代表可执行(execute)。 要注意的是,这三个权限的位置不会改变,如果没有权限,就会出现减号[ - ]而已。
    先将整个类型不权限数据分开查阅,并将十个字符整理成为如下所示:
    [-][rwx][r-x][r--]
    1 234 567 890
    1 为:代表这个文件名为目彔戒档案,本例中为档案(-);
    234 为:拥有者的权限,本例中为可读、可写、可执行(rwx);
    567 为:同群组用户权力,本例中为可读可执行(rx);
    890 为:其他用户权力,本例中为可读(r)
    见下图:
    文件属性.鸟哥
-rw-rw-r-- 1 ywu  ywu     0 Oct  5 02:02 testtouch
ls -l.鸟哥

ls的选项与参数:(鸟哥)
-a :全部的档案,连同隐藏档( 开头为 . 的档案) 一起列出来(常用)
-A :全部的档案,连同隐藏档,但不包括 . 不 .. 这两个目录
-d :仅列出目录本身,而不是列出目弽内癿档案数据(常用)
-f :直接列出结果,而丌迚行排序 (ls 预讴会以档名排序!)
-F :根据档案、目弽等信息,给予附加数据结构,例如:
*:代表可执行文件; /:代表目弽; =:代表 socket 档案; |:代表 FIFO 档案;
-h :将档案容量以人类较易读癿方式(例如 GB, KB 等等)列出杢;
-i :列出 inode 号码,inode 癿意义下一章将会介绍;
-l :长数据串行出,包吨档案癿属性不权限等等数据;(常用)
-n :列出 UID 不 GID 而非使用者不群组癿名称 (UID 不 GID 会在账号管理提
到!)
-r :将排序结果反向输出,例如:原本档名由小到大,反向则为由大到小;
-R :连同子目弽内容一起列出杢,等亍该目弽下癿所有档案都会显示出杢;
-S :以档案容量大小排序,而丌是用档名排序;
-t :依时间排序,而丌是用档名。
--color=never :丌要依据档案特性给予颜色显示;
--color=always :显示颜色
--color=auto :讥系统自行依据讴定杢判断是否给予颜色
--full-time :以完整时间模式 (包吨年、月、日、时、分) 输出
--time={atime,ctime} :输出 access 时间戒改变权限属性时间 (ctime)
而非内容变更时间 (modification time)


Linux Day7: Date/echo/print

学习man,结合date一起学习:

date:时间管理
linux:rtc

硬件时间(clock),系统时钟(date)
hwclock
-w:
-s:
我的用户好像显示不出clock时间。

获得命令的帮助:

  • 内部命令:help COMMAND
  • 外部命令:COMMAND --help
  • 命令手册:manual(man COMMAND)

man 分章节:
1.用户命令
2.系统调用
3.库用户
4.特殊软件(设备文件)
5.文件格式(配置文件的语法)
6.游戏
7.杂项
8.管理命令(/sbin,/usr/sbin,/usr/local/sbin)

基本上,man page 大致分成底下这几个部分:


man命令.鸟哥

举例:man time

DATE(1)                             User Commands                             DATE(1)

NAME
       date - print or set the system date and time

SYNOPSIS
       date [OPTION]... [+FORMAT]
       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION
       Display the current time in the given FORMAT, or set the system date.

       Mandatory arguments to long options are mandatory for short options too.

       -d, --date=STRING
              display time described by STRING, not 'now'

       -f, --file=DATEFILE
              like --date; once for each line of DATEFILE

       -I[FMT], --iso-8601[=FMT]
等等...

同more/less一样,man最后也有个光标供你翻页:


man 翻页.PNG

man page常用的按键有:


man 按键,鸟哥.PNG

那么上面synopsis:说明可以设置的时间格式
date一下目前的时间:

date
Sun Oct  7 17:41:41 CST 2018

如果想要修改时间,那么可以按照[MMDDhhmm[[CC]YY][.ss]]的格式修改时间。按照DESCRIPTION中介绍:

$ date +%y
18
$ date +%Y
2018
$ date +%Y/%m/%d
2018/10/07
$ date +%H:%M
18:18
$ date +%T
18:21:58
$ date +%F
2018-10-07
  • 显示日历的指令
  • 简单好用的计算机:bc

练习echo、printf命令:
1.echo命令是内部命令还是外部命令?

$ type echo
echo is a shell builtin
  1. 作用?
$ man echo
 echo - display a line of text

3.如何换行显示

$ echo -e "this year is 2018.\n today is 08"
this year is 2018.
 today is 08
$ echo -e "this year is 2018.\n today is 08"
this year is 2018.
 today is 08
$ echo -e "this year is 2018./b today is 08"
this year is 2018./b today is 08
$ echo -e "this year is 2018.\b today is 08"
this year is 2018 today is 08
$ echo -e "this year is 2018.\t today is 08"
this year is 2018.   today is 08
$ echo -e "this year is 2018.\v today is 08"
this year is 2018.
                   today is 08

printf:

man printf
NAME
       printf - format and print data
$ printf "The year is 2018"
The year is 2018

$ printf "The year is 2018.\n Today is 08"
The year is 2018.
 Today is 08$ 

Linux Day8: 目录管理

学习寄言:时间相关函数在生信用的少,环境变量,还有目录路径需要加强理解。

$ ls /
bin   dev   initrd.img  lost+found  opt     root  snap  tmp   umac  vmlinuz
boot  etc   lib     media       proc    run   srv   trainee   usr   wyx
data  home  lib64   mnt     public  sbin  sys   trainee2  var
  • /boot:系统启动相关的文件,如内核,initrd,以及grub(bootloader)
  • /dev:设备文件
    1.块设备:随机访问,数据块(如有ABCD四个文件,想要访问C,可直接访问C,无需经过AB)
    2.字符设备:线性访问,按字符为单位
    3.设备号:主设备号,次设备号
  • /etc:配置文件
  • /home:用户的家目录;每个用户的家目录默认为/home/USERNAME。
  • /root:管理员的家目录。
  • lib:库文件
    1.静态库:.a
    2.动态库:.dll,
    3./lib/modules:内核模块文件
  • media:挂载点目录,移动设备
  • /mnt:挂载点目录,额外的临时文件系统
  • /opt:可选目录,第三方程序的安装目录。
  • /proc:伪文件系统,内核映射文件。
  • /sys:伪文件系统,跟硬件设备相关的属性映射文件。
  • /tmp:临时文件
  • /var:可变化的文件。
  • /bin:可执行文件,用户命令
  • /sbin:管理命令
  • /usr:shared,read-only

文件命令规则:

1.长度不超过255个字符
2.不能使用/当文件名
3.严格区分大小写

目录管理:

  • ls
  • cd
  • pwd
  • mkdir:创建空目录,/root/x/y/z创建z目录,只有x/y路径存在时才能创建z文件。
mkdir -pv test/m/n/1
$ tree test
test
└── m
    └── n
        └── 1

3 directories, 0 files

$ #创建a_b,a_c,d_b,d_c
$ #思想为:(a+b)(b+c)=ab+ac+db+dc
$ mkdir -pv test/test2/{a,d}_{b,c}
mkdir: created directory 'test/test2'
mkdir: created directory 'test/test2/a_b'
mkdir: created directory 'test/test2/a_c'
mkdir: created directory 'test/test2/d_b'
mkdir: created directory 'test/test2/d_c'

$ mkdir test/test1/{x/m,y} -pv
mkdir: created directory 'test/test1'
mkdir: created directory 'test/test1/x'
mkdir: created directory 'test/test1/x/m'
mkdir: created directory 'test/test1/y'
# { }展开作用。
  • rmdir:删除空文件
$ rmdir test/test1
rmdir: failed to remove 'test/test1': Directory not empty

文件创造

  • touch:可用来创作空文件
$ touch a
$ file a
a: empty
NAME
       touch - change file timestamps

SYNOPSIS
       touch [OPTION]... FILE...

DESCRIPTION
       Update the access and modification times of each FILE to the current time.

       A  FILE argument that does not exist is created empty, unless -c or -h is sup‐
       plied.

       A FILE argument string of - is handled specially and causes  touch  to  change
       the times of the file associated with standard output.

       Mandatory arguments to long options are mandatory for short options too.

       -a     change only the access time

       -c, --no-create
              do not create any files
$ ls
-aaa-  readme.txt  testtouch  tmp  wy
$ touch a
$ ls 
a  -aaa-  readme.txt  testtouch  tmp  wy
# 出现了a文件
a  -aaa-  readme.txt  testtouch  tmp  wy
$ touch -c c
$ ls
a  -aaa-  readme.txt  testtouch  tmp  wy
# 未出现c文件

touch是用来改时间窗的:

$ stat a
  File: 'a'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd71h/64881d    Inode: 27266337    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1091/     ywu)   Gid: ( 1092/     ywu)
Access: 2018-10-09 20:08:28.189589153 +0800
Modify: 2018-10-09 20:08:28.189589153 +0800
Change: 2018-10-09 20:08:28.189589153 +0800
 Birth: -
$ touch a
$ stat a
  File: 'a'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: fd71h/64881d    Inode: 27266337    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1091/     ywu)   Gid: ( 1092/     ywu)
Access: 2018-10-09 20:11:24.229993592 +0800
Modify: 2018-10-09 20:11:24.229993592 +0800
Change: 2018-10-09 20:11:24.229993592 +0800
 Birth: -

创建文件,可以使用文件编辑器:

ASCII:美国国家信息交换代码

  • cp:复制:
$ cp /etc/passwd /tmp/
# 将passwd复制到tmp目录下
$ cp /etc/passwd /tmp/test
# 将passwd复制到tmp目录下:如果test不存在,那么passwd将更名为test;
如果test为目录,那么psswd将放在test目录下;
如果为文件,则覆盖test。
$ cp /etc/init.d/ /tmp/
cp: omitting directory '/etc/init.d/'
# cp默认情况下是不会复制目录的
# -R, -r, --recursive
              copy directories recursively
  • mv:移动文件(原则基本类似cp);并更改文件名字
  • install:copy files and set attributes。
    -d:创建目录/文件;

Linux Day9: cut/sort/uniq/wc/tr/history

  • 目录管理:
    ls、cd、pwd、mkdir、tree
  • 文件管理:
    touch、stat、file、rm、cp、mv、nano
  • 日期时间:
    date、clock、hwclock、cal
  • 查看文本:
    cat、more、tac、less、head、tail
  • 文本处理:
    cut、join、sed、awk
    三驾马车awk、sed、grep

cut

-d: 指定字段分隔符,默认空格
-f: 指定要显示的字段
> + -f 1,3:显示1,3字段
> + -f 1-3:显示1-3字段
  • 做个小练习
$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
...

# 如果剪切第一行:
$ cut -d : -f1 /etc/passwd
root
daemon
bin
sys
sync
...

文本排序:sort

  • 参数及其选项
 不影响源文件排序,只影响显示排序
 -n:数值排序
 -r :降序
 -t: 字段分隔符
 -k以那个字段为关键字进行排序
 排序后相同的行只显示一次
$ sort -t: -k3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
jmzeng:x:1000:1000:,,,:/home/jmzeng:/bin/bash
systemd-timesync:x:100:102:systemd Time Synchronization,,,:/run/systemd:/bin/false
spguo:x:1001:1003:,,,:/home/spguo:/bin/bash
fzhao:x:1002:1004:,,,:/home/fzhao:/bin/bash
zgxu:x:1003:1005:,,,:/home/zgxu:/bin/bash
myshen:x:1004:1006:,,,:/home/myshen:/bin/bash

uniq:report or omit repeated lines

-d:只显示重复的行
-c: 显示文件中行重复的次数。

文本统计:wc (word count)

$ wc /etc/fstab
  9  54 541 /etc/fstab
$ wc -l /etc/fstab
9 /etc/fstab
$ wc -w /etc/fstab
54 /etc/fstab

字符处理命令:

tr:转换/删除字符

tr [OPTION]... SET1 [SET2]

$ tr 'ab' 'AB'
abc
ABc
able
ABle
acd
Acd

`tr 'a-z' 'A-Z' < /etc/passwd```将/etc/passwd文件中的a-z换成A-Z
-d:删除出现在字符集中的所有字符

$ tr -d 'ab'
helloab
hello
helloba
hello
are
re

LInux Day10: bash特性

bash及其特性:

  • 光标跳转:
Ctrl+a:跳到命令行首
+e:行尾
+u:删除光标至命令行首的内容
+k:删除光标至命令行尾的内容
+l:清屏 clear
  • 命令历史:
查看命令历史:history
-c : 清空命令历史
-d OFFSET [n] :删除指定位置的命令
-w:保存历史命令至历史文件中
命令历史的使用技巧:
!n:执行命令历史中第n条命令
!-n:执行命令历史中倒数第n条命令
!!:执行上一个命令
! string:执行命令历史中最近一个以string开头的命令
! $应用上个命令的最后 一个参数;ESC, .  及 Alt+.(本地)

Tab

  • 命令补全:Tab(命令搜索路径下)
  • 路径补全全:Tab

环境变量:

PATH:命令搜索路径
HISTSIZE:命令历史缓冲区大小

命令别名:

alias:在shell中定义的别名仅在当前shell的生命周期中有效,有效范围为当前shell进程;
alias的基本使用方法为:alias COMMANDS=‘COMMANDS [option] [ arguments]’

$ cls
-sh: cls: command not found
$ alias cls=clear
  • 撤销命令别名,可以使用unalias命令
$ cls
-sh: cls: command not found
$ alias cls=clear
$ unalias cls
$ cls
-sh: cls: command not found

命令替换:

把命令中的某个子命令替换为执行结果的过程
格式为:
$(COMMAND)或COMMAND(反引号)
例子1:

$ echo "The current directory is $(pwd)"
The current directory is /umac/ht1T/home//ywu
$ pwd
/umac/ht1T/home//ywu

例2:date +%F命令可以查看今天的日期,我们若想建一个新文件,以今天的日期命名,则可以使用以下命令:

$ date +%F-%H-%M
2018-10-11-13-12
$ touch ./file-$(date +%F-%H-%M).txt
$ ls
a  -aaa-  file-2018-10-11-13-13.txt  readme.txt  sort.test.save  testtouch  tmp  wy
  • bash支持的引号:
    :命令替换
    " ":弱引用,可以实现变量替换
    ' ':强引号,不完全变量替换

文件名通配:

用法:

*:匹配任意长度的任意字符;(可以为零)
?:匹配任意单个字符;
[]:匹配指定范围内的任意单个字符;
[^]:匹配指定范围外的任意单个字符;
[::]:中括号和冒号中间加某些字母,可以表示某个范围的字符,外面再加一个中括号可表示匹配;
[[:space:]]:空白字符;
[[:punct:]]:标点符号;
[[:lower:]]:小写字母;
[[:upper:]]:大写字母;
[[:alpha:]]:大小写字母;
[[:digit:]]:数字;
[[:alnum:]]:数字和大小写字母;
[^[:space:]]:取非空白字符;
  • 小练习:
$ touch a123 abc xyz x12 xyz123
$ ls
a123  abc  x12  xyz  xyz123
$ ls a*
a123  abc
$ ls a*3
a123
$ ls *y*
xyz  xyz123
$ touch y123
$ ls *y*
xyz  xyz123  y123
$ ls ?y*
xyz  xyz123
$ ls [a-z]*[0-9]
a123  x12  xyz123  y123
$ ls [^0-9]*
a123  abc  x12  xyz  xyz123  y123

Linux Day11/12:文件权限

学习寄言:
文件名通配,跟正则表达式的区别(在后面学习了正则表达式了,一起解决吧)

文件权限

  • 文件的权限是文件的一项重要属性,它可以限定不同用户对该文件的操作,
  • 对于一个文件来说:
r: 可读;
w:可写,可编辑或删除;
x: 可以执行;
  • 而对于一个目录:
r: 可用ls列出目录内所有文件;
w: 可在此目录中创建文件;
x: 可以使用cd切换进此目录,也可以使用ls -l查看内部文件详细信息;
文件的权限也可用八进制或二进制数字表示:
  • 八进制表示方法:
0 000 ---
1 001 --x
2 010 -w-
3 011 -wx
4 100 r--
5 101 r-x
6 110 rw-
7 111 rwx

#rwx-xr-x-rw-r-----:640
drwxrwxr-x 4 ywu  ywu  4096 Oct  9 20:03 wy
八进制表示为:775
7 rwx:ywu
7 rwx:ywu
5 r-x:其他用户

用户:UID,/etc/shadow
组:/etc/gshadow
用户组
用户类别:

  • 管理员:0 (内在设定的)
  • 普通用户:普通用户的uid范围是1-65535,它又可以分为系统用户和一般用户。
    1.系统用户:uid范围1-499,保障系统的运行,一般没有登录密码。
    2.一般用户:也就是我们使用账号和密码登入的用户啦。uid范围500-60000

文件组的概念

用户组:
管理员组:
普通组:系统组+一般组

passwd.马哥
加密方法.马哥

用户相关的两个重要文件:

/etc/passwd和/etc/shadow。

这两个文件对整个系统中的所有用户和密码进行管理,是linux系统中最重要的文件之一。如果它们出了问题,轻则某些用户无法登陆,重则整个系统无法启动。

/etc/passwd
该文件记录了系统中所有的用户信息,由“:”分割成7个字段。

$ cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin

每字段的具体含义如下:

  1. account: 用户名。代表用户账号的字符串,如root, sys等;用来对应 UID 的。例如 root 的 UID 对应就是 0 (第三字段);
  2. password: 密码。用户的登录密码。Linux中密码都存放在/etc/shadow中,此处以x代替;
$ cat /etc/shadow
cat: /etc/shadow: Permission denied
# 用root账户cat一下
  1. UID: 用户名ID。通常 Linux 对于 UID 有几个限制需要说给您了解一下:


    UID.PNG

    UID 为 0 的时候,就是 root 呦!所以请特别留意一下你的/etc/passwd 档案

  2. GID:基本组ID。用户的基本组,具体的组信息存放在/etc/group文件中; /etc/group的观念与 /etc/passwd 差多,只是他是用来规范组名与 GID的对应而已!
  3. comment: 用户注释信息,可以注释用户的一些信息如地址电话等,通常为空;
  4. HOME DIR:家目录。这里可以看到,root的家目录是/root,普通用户的家目录是/home/USER;如果你有个账号的使用空间特删的大,你想要将该账号的家目录移动到其他盘?可以在这个字段进行修改呦!默认的用户家目录在
    /home/yourIDname
  5. shell: 用户的默认shell。CentOS发行版的默认shell是bash,此处root和sxy等用户的默认shell是/bin/bash,而系统用户的是/sbin/nologin,这代表该用户无法登录。

/etc/shadow
普通用户是没有权限查看该文件的,我们切换到管理员查看该文件的内容。

shadow 鸟哥

基本上, shadow 同样以『:』作为分隔符,如果数一数,会发现共有九个字段啊,这九个字段的用途是这样的:
每个字段的具体含义如下:

  1. account:用户名;
  2. encrypted password:加密密码;
    使用md5加密,分为以$隔开的3个字段,第2个字段为杂质,在加密前将杂质加入密码中,之后再进行加密,第3个字段为加密后的md5码;
    md5是常用的单向加密方式,可以由明文取得密文,但反之不行,可以使用md5sum获取md5码。
  3. 上次更改密码的时间:在linux系统中经常会用到linux时间,即距离1970年1月1日0时0分0秒的时间。这个字段的数字就是上次修改密码的时间距离1970年1月1日的总天数。
  4. 修改密码后再次修改密码需要经过多少天:默认是0,即不需等待,可连续修改密码;
  5. 密码的有效期:即密码修改后在多少天内有效,超过一定时间后必须重新设置密码。默认99999,可以认为永不到期,嗯,除非你有意把密码世代相传;
  6. 密码到期前警告:此处为7,即密码到期7天前,系统会给予警告;
  7. 账号失效期限:假如此处为7,则密码到期7天后用户还未修改密码则会被锁定。我们的系统中此处为空;
  8. 账号的有效期限:同样按照距离1970年1月1日的天数计算。在此天数内账号有效,超过日期则失效;
  9. 保留用,无意义。

/etc/group

dbjiang:x:1086:
ztsong:x:1087:
sstian:x:1088:
hhwang:x:1089:
yxliu:x:1090:
zwbao:x:1091:
ywu:x:1092:

这个档案每一行代表一个群组,也是以冒号『:』作为字段的分隔符,共分为四栏,每一字段的意义是:(参考鸟哥)

  1. 组名:
    就是组名啦!
  2. 群组密码:
    通常不需要训定,这个训定通常是给『群组管理员』使用, 同样的,密码已经秱劢刡 /etc/gshadow 去,因此这个字段叧会存在一个『x』而已;
  3. GID:
    就是群组的 ID 啊。我们 /etc/passwd 第四个字段使用的 GID 对应的群组名,就是由这里对应出来的!
  4. 此群组支持癿账号名称:
    我们知道一个账号可以加入多个群组,那某个账号想要加入此群组时,将该账号填入这个字段即可。 如果我想要让 dmtsai 也加入 root 这个群组,那么在第一行的最后面加上
    『,dmtsai』,注意不要有空格, 使成为『root:x:0:root,dmtsai 』就可以啰~

Linux Day13:useradd及相关

  • 新增与移除使用者: useradd, 相关配置文件, passwd, usermod, userdel

useraddd

  • useradd [-u UID] [-g 初始群组] [-G 次要群组] [-mM] [-c 说明栏] [-d 家目录绝对路径] [-s shell] 使用者账号名


    useradd选项
  • 使用『useradd 账号 』来建立使用者即可。 CentOS 这些默认值主要会帮我们处理几个项目:
    1.在 /etc/passwd 里面建立一行与账号相关的数据,包括建立 UID/GID/家目录等;
    2.在 /etc/shadow 里面将此账号的密码相关参数填入,但是尚未有密码;
    3.在 /etc/group 里面加入一个不账号名称一模一样的组名;
    4.在 /home 底下建立一个不账号同名的目录作为用户家目录,且权限为 700
# 创建user1用户
root@VM-0-3-ubuntu:~# useradd user1
root@VM-0-3-ubuntu:~# tail -1 /etc/passwd
user1:x:1000:1000::/home/user1:
root@VM-0-3-ubuntu:~# #在创建用户的时候,没有为用户指定组,将创造同用户相同UID的组。
# 如果两者不一样,则会出现特殊用户。
#创建新用户并加入到mygroup中
root@VM-0-3-ubuntu:~# useradd -g mygroup user2
useradd: group 'mygroup' does not exist
# 说名这个组要事先存在才行
# 将user2加入到刚刚创建的user1中去
root@VM-0-3-ubuntu:~# useradd -g user1 user2
root@VM-0-3-ubuntu:~# tail -1 /etc/passwd
user2:x:1001:1000::/home/user2:
root@VM-0-3-ubuntu:~# 其实,这个user1的UID为1000,在创建新用户的时候,系统默认根据/etc/passwd的最后一位UID自动创建下一位,就是这里的1001.
# 创建user3,并制定其附加组为user1
root@VM-0-3-ubuntu:~# tail /etc/group
lxd:x:110:
messagebus:x:111:
uuidd:x:112:
mlocate:x:113:
ssh:x:114:
ubuntu:x:500:
lpadmin:x:115:ubuntu
sambashare:x:116:ubuntu
ntp:x:117:
user1:x:1000:
root@VM-0-3-ubuntu:~# useradd -G user1 user3
root@VM-0-3-ubuntu:~# tail /etc/group
messagebus:x:111:
uuidd:x:112:
mlocate:x:113:
ssh:x:114:
ubuntu:x:500:
lpadmin:x:115:ubuntu
sambashare:x:116:ubuntu
ntp:x:117:
user1:x:1000:user3
user3:x:1002:
# 指定名称和家目录
root@VM-0-3-ubuntu:~# useradd -c "wuyi" -d /home/wuyi user5
root@VM-0-3-ubuntu:~# tail -1 /etc/passwd
user5:x:1004:1004:wuyi:/home/wuyi:
# 默认情况下创建的用户:
root@VM-0-3-ubuntu:~# echo $SHELL
/bin/bash

userdel:删除用户

  • 用法:userdel [option] USERNAME目的在删除用户的相关数据,而用户的数据有:
    1. 用户账号/密码相关参数:/etc/passwd, /etc/shadow
     2.使用者群组相关参数:/etc/group, /etc/gshadow
     3.用户个人档案数据: /home/username, /var/spool/mail/username
#删除用户user4,默认情况下不会删除其家目录,要想删除-r
root@VM-0-3-ubuntu:~# userdel user4
# 删除user5及其家目录
root@VM-0-3-ubuntu:~# userdel -r user5
userdel: user5 mail spool (/var/mail/user5) not found
userdel: user5 home directory (/home/wuyi) not found
# user5创建了家目录的,为什么提示找不到?

id: 查看用户账号的属性

SYNOPSIS
       id [OPTION]... [USER]

DESCRIPTION
       Print  user  and group information for the specified USER, or (when USER omit‐
       ted) for the current user.

       -a     ignore, for compatibility with other versions

       -Z, --context
              print only the security context of the process

       -g, --group
              print only the effective group ID

       -G, --groups
              print all group IDs

       -n, --name
  • 上面几个option都可以与-n一起用
$ #查看当前用户的id信息
$ id
uid=1091(ywu) gid=1092(ywu) groups=1092(ywu),1019(student)
$ #id user1查看user1的账户信息
$ id user1
id: ‘user1’: no such user
$ id -n
id: cannot print only names or real IDs in default format
$ id -u -n ywu
ywu
$ id -G -n ywu
ywu student

finger 查看用户账户信息

  • finger 的中文字面意义是:『手指』戒者是『挃纹』的意思。这个 finger 可以查阅很多用户相关的信息喔! 大部分都是在 /etc/passwd 这个档案里面的信息啦
root@VM-0-3-ubuntu:~# finger user1
Login: user1                    Name: 
Directory: /home/user1                  Shell: /bin/sh
Never logged in.
No mail.
No Plan.

修改账户信息:

usermod:

  • 选项:


    uermod.PNG

chsh:修改用户的默认的shell

root@VM-0-3-ubuntu:~# chsh user2
Changing the login shell for user2
Enter the new value, or press ENTER for the default
    Login Shell []: /bin/tcsh
chsh: Warning: /bin/tcsh does not exist
root@VM-0-3-ubuntu:~# chsh user2
Changing the login shell for user2
Enter the new value, or press ENTER for the default
    Login Shell [/bin/tcsh]: /bin/bash
root@VM-0-3-ubuntu:~# finger user2
Login: user2                    Name: 
Directory: /home/user2                  Shell: /bin/bash
Never logged in.
No mail.
No Plan.

chfn:修改注释信息

root@VM-0-3-ubuntu:~# chfn user2
Changing the user information for user2
Enter the new value, or press ENTER for the default
    Full Name []: wuyi^H^H^H^H
    Room Number []: chongqing
    Work Phone []: 5^H023
    Home Phone []: 15870000000
    Other []: nothing
chfn: name with non-ASCII characters: ''uyi
chfn: invalid work phone: '023'

密码管理:

passwd:

root@VM-0-3-ubuntu:~# tail /etc/shadow
user2:!:17825:0:99999:7:::

root@VM-0-3-ubuntu:~# passwd user2
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@VM-0-3-ubuntu:~# tail /etc/shadow
user2:$6$TyWhVhuR$wDzvvJx8GlArSe9OigIgdNejH3M.dYsly79ebgtuLqsK9a2KSTBVb4rTvMydrnF.eWX0np7xP2zsLbdT45C4Z.:17825:0:99999:7:::

pwck:检查用户账号的完整性

root@VM-0-3-ubuntu:~# pwck
user 'lp': directory '/var/spool/lpd' does not exist
user 'news': directory '/var/spool/news' does not exist
user 'uucp': directory '/var/spool/uucp' does not exist
user 'www-data': directory '/var/www' does not exist
user 'list': directory '/var/list' does not exist
user 'irc': directory '/var/run/ircd' does not exist
user 'gnats': directory '/var/lib/gnats' does not exist
user 'nobody': directory '/nonexistent' does not exist
user 'systemd-resolve': directory '/run/systemd/resolve' does not exist
user 'syslog': directory '/home/syslog' does not exist
user '_apt': directory '/nonexistent' does not exist
user 'ntp': directory '/home/ntp' does not exist
user 'user1': directory '/home/user1' does not exist
user 'user2': directory '/home/user2' does not exist
user 'user3': directory '/home/user3' does not exist
pwck: no changes

chage:

-d :最近一次的修改时间
-E:过期时间
-I:非活动时间
-m:最短使用期限
-M: 最长使用权限
-W: 警告时间

组管理:

创建组:groupadd:

root@VM-0-3-ubuntu:~# groupadd -r wechat
root@VM-0-3-ubuntu:~# tail -1 /etc/group
wechat:x:999:
# -r, --system
           Create a system group.
# 按照马哥来说的话是<500,为什么这里为999?
  • groupmod:修改

  • groupdel:删除

  • gpasswd:给组加密码

newgrp:切换到新的组

root@VM-0-3-ubuntu:~# useradd hadoop
root@VM-0-3-ubuntu:~# su - hadoop
No directory, logging in with HOME=/
$ cd /tmp
$ touch a.hadoop
$ ls -l
total 12
-rw-rw-r-- 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop
-rw-r--r-- 1 root   root   140 Oct 21 12:30 net_affinity.log
-rw-r--r-- 1 root   root    26 Oct 21 12:30 nv_gpu_conf.log
-rw-r--r-- 1 root   root   191 Oct 21 12:30 setRps.log
$ #一个用户创建的文件属于用户的基本组

$ id
uid=1003(hadoop) gid=1003(hadoop) groups=1003(hadoop)
$ #将一个文件的基本组切换到其他组去
$ newgrp user1
Password: 
# 如果想要回到原来的组,则使用exit

马哥作业.PNG

Linux Day14:chgrp/chown/chmod

权限管理:

复习:

 [ r ]代表可读(read)
 [ w ]代表可写(write)
 [ x ]代表可执行(execute)
  • 三类用户
  1. u:属主
  2. g:属组
  3. o:其它用户
  • 第一组为『档案拥有者的权限』,以『install.log』那个档案为例, 该档案的拥有者可以读写,但不可执行;
  • 第二组为『同群组的权限』;
  • 第三组为『其他非本群组的权限』。

  • 我们现在知道档案权限对于一个系统的安全重要怅了,也知道档案的权限对于使用者不群组的相关性,那么如何修改一个档案的属怅与权限呢?又!有多少档案的权限我们可以修改呢? 其实一个档案的属怅与权限有很多!我们先介绍几个常用于群组、拥有者、各种身份的权限的修改的挃令,如下所示:
     chgrp :改变档案所属群组
     chown :改变档案拥有者
     chmod :改变档案的权限, SUID, SGID, SBIT 等等的特性

chown:改变文件属主(只有管理员可以使用此命令)

SYNOPSIS
       chown [OPTION]... [OWNER][:[GROUP]] FILE...
       chown [OPTION]... --reference=RFILE FILE...

root@VM-0-3-ubuntu:~# ls /tmp -l
total 12
-rw-rw-r-- 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop
-rw-r--r-- 1 root   root   140 Oct 21 12:30 net_affinity.log
-rw-r--r-- 1 root   root    26 Oct 21 12:30 nv_gpu_conf.log
-rw-r--r-- 1 root   root   191 Oct 21 12:30 setRps.log
# 修改文件a.hadoop的属主为root
root@VM-0-3-ubuntu:~# chown root /tmp/a.hadoop
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rw-rw-r-- 1 root hadoop 0 Oct 21 23:12 /tmp/a.hadoop
# -R:修改目录及其内部文件的属性
# 通常改变目录的属主,但是其中的文件属主并没有改变,可以-R进行修改

# --reference=/path/to/somefile file ,...(属主,属组一起改的)
ubuntu@VM-0-3-ubuntu:~$ ls -l /tmp
total 12
-rw-rw-r-- 1 root hadoop   0 Oct 21 23:12 a.hadoop
-rw-r--r-- 1 root root   140 Oct 21 12:30 net_affinity.log
-rw-r--r-- 1 root root    26 Oct 21 12:30 nv_gpu_conf.log
-rw-r--r-- 1 root root   191 Oct 21 12:30 setRps.log
root@VM-0-3-ubuntu:~# chown --reference=/tmp/a.hadoop /tmp/setRps.log
root@VM-0-3-ubuntu:~# ls -l /tmp
total 12
-rw-rw-r-- 1 root hadoop   0 Oct 21 23:12 a.hadoop
-rw-r--r-- 1 root root   140 Oct 21 12:30 net_affinity.log
-rw-r--r-- 1 root root    26 Oct 21 12:30 nv_gpu_conf.log
-rw-r--r-- 1 root hadoop 191 Oct 21 12:30 setRps.log
# chown USRNAME(属主):GRPNAME(属组) file,...
root@VM-0-3-ubuntu:~# chown hadoop.hadoop /tmp/a.hadoop
root@VM-0-3-ubuntu:~# chown hadoop:hadoop /tmp/a.hadoop
root@VM-0-3-ubuntu:~#ls -l /tmp/a.hadoop
-rw-rw-r-- 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop
`chown :GRPNAME file,...`(只改变属组)
`chown USRNAME.GRPNAME file,...`(也是同时改变属主、属组,但是在不同的文件中有不同的用法)
chown --reference=/tmp/abc  /tmp/test
# 表示将/tmp/test的属主和属组改成和/tmp/abc/一样

chgrp改变文件所属组

-R:递归修改文件内的属组
-- reference=path to /somefile

# 表示修改a.hadoop的属组为root
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rw-rw-r-- 1 root hadoop   0 Oct 21 23:12 a.hadoop
root@VM-0-3-ubuntu:~# chgrp root /tmp/a.hadoop
root@VM-0-3-ubuntu:~#ls -l /tmp/a.hadoop

chomd;修改文件权限

  • 修改三类用户的权限
    chmod MODE file,...
    -R:
    --reference=
  • Linux 档案的基本权限就有九个,分别是 owner/group/others 三种身份各有自己的read/write/execute 权限, 先复习一下刚刚上面提到的数据:档案的权限字符为:『-rwxrwxrwx』, 这九个权限是三个三个一组的!其中,我们可以使用数字来代表各个权限,各
    权限的分数对照表如下:
r:4
w:2
x:1
  • 每种身份(owner/group/others)各自的三个权限(r/w/x)分数是需要累加的,例如当权限为: [-rwxrwx---] 分数则是:
owner = rwx = 4+2+1 = 7
group = rwx = 4+2+1 = 7
others= --- = 0+0+0 = 0
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rw-rw-r-- 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop

# 修改文件a.hadoop的权限为750
root@VM-0-3-ubuntu:~# chmod 750 /tmp/a.hadoop
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rwxr-x--- 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop

# 考虑chmod 75 /tmp/a.hadoop
root@VM-0-3-ubuntu:~# chmod 75 /tmp/a.hadoop
root@VM-0-3-ubuntu:~#ls -l /tmp/a.hadoop
----rwxr-x 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop
  • 还有一个改变权限的方法呦!仍以前的介绍中我们可以发现,基本上就九个权限分别是(1)user(2)group (3)others 三种身份啦!那举我们就可以藉由 u, g, o 来代表三种身份的权限!此外,a 则代表 all 亦即全部的身份!那举读写的权限就可以写成 r, w, x 啰!也就是可以使用底下的方式来看:


    鸟哥
  • 修改某类用户的权限:
root@VM-0-3-ubuntu:~#ls -l /tmp/a.hadoop
----rwxr-x 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop
root@VM-0-3-ubuntu:~# chmod u=rwx /tmp/a.hadoop
root@VM-0-3-ubuntu:~#ls -l /tmp/a.hadoop
-rwxrwxr-x 1 hadoop hadoop   0 Oct 21 23:12 a.hadoop

root@VM-0-3-ubuntu:~# chmod go=rx /tmp/a.hadoop
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rwxr-xr-x 1 hadoop hadoop 0 Oct 21 23:12 /tmp/a.hadoop
root@VM-0-3-ubuntu:~# chmod g=r,o=rx /tmp/a.hadoop
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rwxr--r-x 1 hadoop hadoop 0 Oct 21 23:12 /tmp/a.hadoop
  • 修改某类用户的某位或某些位权限
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rwxr--r-x 1 hadoop hadoop 0 Oct 21 23:12 /tmp/a.hadoop
root@VM-0-3-ubuntu:~# chmod u-x,g+x /tmp/a.hadoop
root@VM-0-3-ubuntu:~# ls -l /tmp/a.hadoop
-rw-r-xr-x 1 hadoop hadoop 0 Oct 21 23:12 /tmp/a.hadoop

练习:

$ ls -l xyz
-rw-rw-r-- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod u=rwx xyz
$ ls -l xyz
-rwxrw-r-- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod u=rx xyz
$ ls -l xyz
-r-xrw-r-- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod 664 xyz
$ ls -l xyz
-rw-rw-r-- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod g=rwx xyz
$ ls -l xyz
-rw-rwxr-- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod g=rwx,o=w xyz
$ ls -l xyz
-rw-rwx-w- 1 ywu ywu 0 Oct 11 13:23 xyz
$ chmod -w xyz
chmod: xyz: new permissions are r--r-x-w-, not r--r-x---
# 这个没有明白?not r--r-x---
捕获.PNG
root@VM-0-3-ubuntu:~# useradd -M openstack
root@VM-0-3-ubuntu:~# finger openstack
Login: openstack                Name: 
Directory: /home/openstack              Shell: /bin/sh
Never logged in.
No mail.
No Plan.
root@VM-0-3-ubuntu:~# ls /home
jmz  ubuntu
root@VM-0-3-ubuntu:~# id openstack
uid=1006(openstack) gid=1006(openstack) groups=1006(openstack)
root@VM-0-3-ubuntu:~# cp -r /etc/skel /home/openstack
root@VM-0-3-ubuntu:~# ls -l /home
total 12
drwxr-xr-x 3 jmz    jmz    4096 Oct 22 23:14 jmz
drwxr-xr-x 2 root   root   4096 Oct 23 21:08 openstack
drwxr-xr-x 5 ubuntu ubuntu 4096 Oct 21 12:23 ubuntu
root@VM-0-3-ubuntu:~# ls -l /home/openstack -a
total 20
drwxr-xr-x 2 root root 4096 Oct 23 21:08 .
drwxr-xr-x 5 root root 4096 Oct 23 21:08 ..
-rw-r--r-- 1 root root  220 Oct 23 21:08 .bash_logout
-rw-r--r-- 1 root root 3771 Oct 23 21:08 .bashrc
-rw-r--r-- 1 root root  655 Oct 23 21:08 .profile
root@VM-0-3-ubuntu:~# chown -R openstack.openstack /home/openstack
root@VM-0-3-ubuntu:~# ls -ld /home/openstack
drwxr-xr-x 2 openstack openstack 4096 Oct 23 21:08 /home/openstack
root@VM-0-3-ubuntu:~# ls -la /home/openstack
total 20
drwxr-xr-x 2 openstack openstack 4096 Oct 23 21:08 .
drwxr-xr-x 5 root      root      4096 Oct 23 21:08 ..
-rw-r--r-- 1 openstack openstack  220 Oct 23 21:08 .bash_logout
-rw-r--r-- 1 openstack openstack 3771 Oct 23 21:08 .bashrc
-rw-r--r-- 1 openstack openstack  655 Oct 23 21:08 .profile

# 修改为组即其它用户不能访问/home/openstack/
root@VM-0-3-ubuntu:~# chmod -R go= /home/openstack/
root@VM-0-3-ubuntu:~# ls -la /home/openstack/
total 20
drwx------ 2 openstack openstack 4096 Oct 23 21:08 .
drwxr-xr-x 5 root      root      4096 Oct 23 21:08 ..
-rw------- 1 openstack openstack  220 Oct 23 21:08 .bash_logout
-rw------- 1 openstack openstack 3771 Oct 23 21:08 .bashrc
-rw------- 1 openstack openstack  655 Oct 23 21:08 .profile
root@VM-0-3-ubuntu:~# su - openstack
openstack@VM-0-3-ubuntu:~$ 
  • 手动添加用户hive,基本组为hive(5000),附加组为mygroup

生信技能树公益视频合辑:学习顺序是linux,r,软件安装,geo,小技巧,ngs组学!
请猛戳下面链接
B站链接:https://m.bilibili.com/space/338686099

YouTube链接:https://m.youtube.com/channel/UC67sImqK7V8tSWHMG8azIVA/playlists

生信工程师入门最佳指南:https://mp.weixin.qq.com/s/vaX4ttaLIa19MefD86WfUA

学徒培养:https://mp.weixin.qq.com/s/3jw3_PgZXYd7FomxEMxFmw

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,366评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,521评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,689评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,925评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,942评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,727评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,447评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,349评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,820评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,990评论 3 337
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,127评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,812评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,471评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,017评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,142评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,388评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,066评论 2 355

推荐阅读更多精彩内容