环境变量、CP命令、MV命令

环境变量

环境变量,或者称为全局变量,它包含了一个或者多个应用程序所将使用到的信息。

  • 输出变量PATH

    [root@yolks ~]# echo $PATH   
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    
  • which命令就是在环境变量中寻找

    [root@yolks ~]# which ls
    alias ls='ls --color=auto'
        /usr/bin/ls
    [root@yolks ~]# ls /usr/bin/ls
    /usr/bin/ls
    
  • 改名实现 ls 命令作用

    • 复制ls命令路径到tmp目录
    [root@yolks ~]# cp /usr/bin/ls /tmp/ls2  
    [root@yolks ~]# /tmp/ls2
    123.txt
    
    • 将tmp目录添加到环境变量
    [root@yolks ~]# PATH=$PATH:/tmp/  
    [root@yolks ~]# echo $PATH
    /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/
    
    • 使用
    [root@yolks ~]# ls2
    123.txt
    [root@yolks ~]# which ls2
    /tmp/ls2
    
    • 保存在文件中,保证其他终端也可使用
    [root@yolks ~]# vi /etc/profile  
    PATH=$PATH:/tmp/
    

CP命令(copy)

用来将一个或多个源文件或者目录复制到指定的目的文件或目录。支持同时复制多个文件,当一次复制多个文件时,目标文件参数必须是一个已经存在的目录,否则将出现错误。

  • r : 复制目录必加选项
  • i : 安全提示
// 约定:拷贝目录需要加r选项,源目录需要加/,目标目录则不用加
cp -r /tmp/shipan/  /tmp/shipan_copy  
tree !$  '上一条命令的第二个参数'

//  源目录和目标目录加/,如果目标存在则将源目录复制到目标目录下,反之则改名
cp -r /tmp/shipan/  /tmp/shipan_copy/

MV命令(move)

用来对文件或目录重新命名,或者将文件从一个目录移到另一个目录中。source表示源文件或目录,target表示目标文件或目录。如果将一个文件移到一个已经存在的目标文件中,则目标文件的内容将被覆盖。

  • 当目标文件是目录时: - 如果目录存在,则会把源文件或目录移动到该目录中。 - 如果目录不存在,则会把源目录重命名为给定的目标目录名
  • 当目标文件是文件时: - 文件存在时,询问是否覆盖 - 文件不存在时,则会把源文件重命名为指定的目标文件名
    '目录的情况'
//  不存在时
[root@pan-128 tmp]# mkdir /tmp/test_mv
[root@pan-128 tmp]# cd /tmp/test_mv
[root@pan-128 test_mv]# mkdir dira  dirb
[root@pan-128 test_mv]# ls
dira  dirb
[root@pan-128 test_mv]# mv dira  dirc
[root@pan-128 test_mv]# ls
dirb  dirc

//  目录存在时
[root@pan-128 test_mv]# mv dirc dirb
[root@pan-128 test_mv]# ls
dirb
[root@pan-128 test_mv]# ls dirb
dirc

文档相关的查看命令

命令cat

  • n : 查看文件同时显示行号
  • a : 显示包含特殊字符的所有内容
//  显示行号(不含特殊符号)
[root@pan-128 tmp]# cat -n filec
     1	ssss
     2	dddd
     3	fff
     4	gg
     5	hh
     6	jj
     7	kkj
     8	hh

//  显示包含特殊符号的内容
[root@pan-128 tmp]# cat -A filec
ssss$
dddd$
fff$
gg$
hh$
jj$
kkj$
hh$
%%%%%%$
shsshs$
$$$
shssj##$

tac命令

倒着显示上面cat文件的内容

[root@pan-128 tmp]# tac filec
shssj##
$$
shsshs
%%%%%%
hh
kkj
jj
hh
gg
fff
dddd
ssss

命令more

支持文件内容比较多的显示,可以空格键翻页

  • ctrl+D : 向上翻屏
  • ctrl+F : 向下翻屏
  • q : 退出
  • / keywords(关键字从上往下搜索) : 关键字查询 - n : 向下一个关键字出现的位置
  • ? keywords(从下面往上搜索) : 关键字查询

命令less

作用和more差不多,比more的功能多一些

  • 空格键翻页
  • j : 向下移动
  • k : 向上移动
  • / keywords : 关键字查询 n
  • ? keywords(从下面往上搜索) : 关键字查询

命令head

用于显示文档前10行

选项:

  • n number(指定行数)
head  -n  5  123.txt

命令tail

默认显示文档最后10行

tail  -n  5  123.txt

选项:

  • f : 动态查看,一般用来动态显示日志最后几行,类似于控制台的作用

Linux文件属性

使用ls命令查看信息时会展示9列内容,下面我们来解释一下

[root@pan-128 tmp]# ls -l filec
-rw-r--r-- 1 root root 55 8月  27 21:23 filec
  • 第1列:表示文件类型 - /- : 普通文件 - d : 目录 - l : 链接文件 - b : 块设备,例如:磁盘分区文件 - c : 串行端口设备文件(字符设备文件),例如:键盘、鼠标、打印机 - s : 套接字文件(socket),用于进程之间的通信 - 紧跟着的9位,3位一组 。用来表示文件或目录的属主、属组、其他用户对应的操作权限
  • 第2列 : 占用的节点(inode),如果是目录,那么该数值与子目录数量有关
  • 第3列 : 所有者
  • 第4列 : 所属组
  • 第5列 : 文件大小
  • 第6、7、8列 : 文件最后一次被修改的时间(mtime) ,依次为月份、日期、时间
  • 第9列 : 文件名

更改文件的权限

命令chgrp(change group)

修改文件的所属组

//  创建新的组
[root@pan-128 tmp]# groupadd testgroup 
[root@pan-128 tmp]# mkdir /tmp/4_6
[root@pan-128 tmp]# cd 4_6
[root@pan-128 4_6]# touch test1
[root@pan-128 4_6]# ls -l test1
-rw-r--r-- 1 root root 0 8月  27 23:18 test1

//  更改文件的所属组
[root@pan-128 4_6]# chgrp testgroup test1
[root@pan-128 4_6]# ls -l test1
-rw-r--r-- 1 root testgroup 0 8月  27 23:18 test1

//  更改目录的所属组  (此用法可使用chown命令代替)
[root@pan-128 4_6]# mkdir dir5
[root@pan-128 4_6]# ls
dir5  test1
[root@pan-128 4_6]# ls -l dir5
总用量 0
[root@pan-128 4_6]# ls -ld dir5
drwxr-xr-x 2 root root 6 8月  27 23:20 dir5
[root@pan-128 4_6]# touch dir5/test2
[root@pan-128 4_6]# ls -l dir5/test2
-rw-r--r-- 1 root root 0 8月  27 23:21 dir5/test2

//  更改目录的所属组,此处只更改了目录的所属组,目录下的文件并没有更改
[root@pan-128 4_6]# chgrp testgroup dir5
[root@pan-128 4_6]# ls -ld dir5
drwxr-xr-x 2 root testgroup 19 8月  27 23:21 dir5
[root@pan-128 4_6]# ls -l dir5
总用量 0
-rw-r--r-- 1 root root 0 8月  27 23:21 test2

//  更改目录及目录下文件的所属组
//  -R 选项
[root@pan-128 4_6]# chgrp -R testgroup   dir5
[root@pan-128 4_6]# ls -l dir5
总用量 0
-rw-r--r-- 1 root testgroup 0 8月  27 23:21 test2

更改所有者和属组 chown(change owner)

[root@pan01 ~]# ls -l /tmp/yum.log
-rw-------. 1 root root 0 5月  31 02:24 /tmp/yum.log
// 1.修改属主
[root@pan01 ~]# chown pan01 /tmp/yum.log
[root@pan01 ~]# !ls
ls -l /tmp/yum.log
-rw-------. 1 pan01 root 0 5月  31 02:24 /tmp/yum.log

// 2.修改属组
[root@pan01 ~]# chgrp pan02 /tmp/yum.log
[root@pan01 ~]# !ls
ls -l /tmp/yum.log
-rw-------. 1 pan01 pan02 0 5月  31 02:24 /tmp/yum.log

// 3.同时修改属主和属组
[root@pan01 ~]# chown pan02:pan01 /tmp/yum.log
[root@pan01 ~]# !ls
ls -l /tmp/yum.log
-rw-------. 1 pan02 pan01 0 5月  31 02:24 /tmp/yum.log

// 4.修改属组
[root@pan01 ~]# chown :root /tmp/yum.log
[root@pan01 ~]# !ls
ls -l /tmp/yum.log
-rw-------. 1 pan02 root 0 5月  31 02:24 /tmp/yum.log
[root@pan01 tmp]# ls -l shipan/2.txt
-rw-r--r--. 1 root root 0 6月   7 22:37 shipan/2.txt
[root@pan01 tmp]# ls -l shipan/
总用量 0
-rw-r--r--. 1 root root 0 6月   7 22:37 2.txt

// 5.修改目录及目录下的属主和属组
[root@pan01 tmp]# chown -R pan02:pan01 shipan
[root@pan01 tmp]# ls -l shipan/
总用量 0
-rw-r--r--. 1 pan02 pan01 0 6月   7 22:37 2.txt
[root@pan01 tmp]# ls -l shipan/2.txt
-rw-r--r--. 1 pan02 pan01 0 6月   7 22:37 shipan/2.txt

文件或目录权限 chmod(change mode)

[root@pan01 ~]# ls -l
总用量 4
// r=4 w=2 x=1
-rw-r--r--. 1 root root    0 6月   7 22:01 123.txt
-rw-------. 1 root root 1422 5月  31 02:27 anaconda-ks.cfg
ps: .受制于selinux,同时创建文件带.说明当前selinux是开启状态
//临时关闭selinux

    setenforce 0

// 永久关闭selinux
    vi /etc/selinux/config  修改为disabled

// 1.使用数字修改权限
[root@pan01 ~]# chmod 700 123.txt
[root@pan01 ~]# ls -l
总用量 4
-rwx------. 1 root root    0 6月   7 22:01 123.txt
-rw-------. 1 root root 1422 5月  31 02:27 anaconda-ks.cfg

// 2.使用字母修改权限
[root@pan01 ~]# chmod u=rx,g=rw,o=r 123.txt
[root@pan01 ~]# ls -l
总用量 4
-r-xrw-r--. 1 root root    0 6月   7 22:01 123.txt
-rw-------. 1 root root 1422 5月  31 02:27 anaconda-ks.cfg

// a:all表示所有,给所有添加x权限
[root@pan01 ~]# chmod a+x 123.txt
[root@pan01 ~]# ls -l
总用量 4
-r-xrwxr-x. 1 root root    0 6月   7 22:01 123.txt
-rw-------. 1 root root 1422 5月  31 02:27 anaconda-ks.cfg

//  全部取消可执行权限
[root@pan01 ~]# chmod a-x 123.txt
[root@pan01 ~]# ls -l
总用量 4
-r--rw-r--. 1 root root    0 6月   7 22:01 123.txt
-rw-------. 1 root root 1422 5月  31 02:27 anaconda-ks.cfg

umask

用来设置限制新建文件权限的掩码。当新文件被创建时,其最初的权限由文件创建掩码决定。用户每次注册进入系统时,umask命令都被执行, 并自动设置掩码mode来限制新文件的权限。用户可以通过再次执行umask命令来改变默认值,新的权限将会把旧的覆盖掉。

[root@pan01 tmp]# umask
0022
一 权限掩码umask
umask是chmod配套的,总共为4位(gid/uid,属主,组权,其它用户的权限),不过通常用到的是后3个,例如你用chmod 755 file(此时这文件的权限是属主读(4)+写(2)+执行(1),同组的和其它用户有读写权限)
二 umask的作用
默认情况下的umask值是022(可以用umask命令查看),此时你建立的文件默认权限是644(6-0,6-2,6-2),建立的目录的默认权限是755(7-0,7-2,7-2),可以用ls -l验证一下哦 现在应该知道umask的用途了吧,它是为了控制默认权限,不要使默认的文件和目录具有全权而设的
三 修改umask值
知道了umask的作用后,你可以修改umask的值了,例如:umask 024则以后建立的文件和目录的默认权限就为642,753了
四 将umask值保存到环境文件
如果使用bash可以更改/etc/bashrc文件中的umask,如果使用csh,可以更改/etc/csh.cshrc

[root @test root]# umask

查看umask值 0022
[root@vbird test]# umask 002 <==后面接 3 个数字  设置umask值
[root@vbird test]# umask

0002

查看 umask 数值为直接输入 umask 即可,而设定就是 umask 之后接三个数字
  • 默认文件夹需要进入,即文件夹具有x权限;
    • 777(rwxrwxrwx)-022(----w--w-)=755(rwxr-xr-x)
  • 文件则默认不需要,即文件不具有x权限
    • 666(rw-rw-rw-)-022(----w--w-)=644(rw-r--r--)

修改文件的特殊属性

  • chattr 选项 - i : 不可操作的权限 - a : 让文件或目录仅供附加用途(只可以追加)
[root@pan01 ~]# ls
123.txt  anaconda-ks.cfg  shipan
'// 1.添加一个不可操作的隐藏i权限'
[root@pan01 ~]# chattr +i 123.txt
[root@pan01 ~]# ls -l 123.txt
-r--rw-r--. 1 root root 0 6月   7 22:01 123.txt
[root@pan01 ~]# vi 123.txt
'// 2.删除i权限'
[root@pan01 ~]# chattr -i 123.txt
[root@pan01 ~]# lsattr 123.txt
---------------- 123.txt
  • lsattr - R 递归显示子目录 - d 只查看目录本身
// 查看隐藏权限
[root@pan01 ~]# lsattr 123.txt
----i----------- 123.txt

特殊权限set_uid:作用在属主

//  1.s权限位表示set_uid,作用在可执行的二进制文件,使普通用户临时获得root权限
[root@yolks ~]# which passwd
/usr/bin/passwd
[root@yolks ~]# ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 '/usr/bin/passwd'
rw's'r-xr-x 

//  2.若给不可执行的文件加s权限,则对应位会变成S
[root@yolks ~]# ls -l /usr/bin/ls
-rwxr-xr-x. 1 root root 117656 Nov  6  2016 /usr/bin/ls
[root@yolks ~]# chmod u-x /usr/bin/ls
[root@yolks ~]# ls -l /usr/bin/ls
-rw-r-xr-x. 1 root root 117656 Nov  6  2016 /usr/bin/ls
[root@yolks ~]# chmod u+s /usr/bin/ls
[root@yolks ~]# ls -l /usr/bin/ls
-rw'S'r-xr-x. 1 root root 117656 Nov  6  2016 /usr/bin/ls


// 1. 添加s权限
    chmod u+s filename
// 2. 去除s权限
    chmod u-s filename

特殊权限set_gid:作用在属组

// 1.作用在文件上表现为属组改变,在目录上则子目录也会继承属组
[root@yolks ~]# chmod g+s /usr/bin/ls
[root@yolks ~]# ls -l /usr/bin/ls
-rwxr-sr-x. 1 root root 117656 Nov  6  2016 /usr/bin/ls

特殊权限stick_bit(防删除位)

// 此权限用来防止其他用户修改或删除自己创建的文件,只有创建者拥有以上权限
[root@yolks ~]# ls -ld /tmp/
drwxrwxrwt. 13 root root 4096 Jun  8 03:18 /tmp/

文件搜索

which查找可执行文件的绝对路径

只能查询PATH环境变量中出现的路径下的可执行文件

//   查询可执行命令的绝对路径
[root@pan-128 4_6]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls

whereis 查找文件

通过预先生成的一个文件列表库查找

  • whereis [-bms] [文件名称]
    • b : 只查找二进制文件
    • m: 只查找帮助文件(在man目录下的文件)
    • s : 只查找源代码文件
[root@pan-128 4_6]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

locate查找

  • 安装
yum install  -y  mlocate
  • 生成或更新文件列表库

注意:服务器正在运行时,增大服务器压力。不适合精准搜索

updatedb

find查询

用来在指定目录下查找文件。任何位于参数之前的字符串都将被视为欲查找的目录名。如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件。并且将查找到的子目录和文件全部进行显示。

使用场景:

  • 查找文件
  • 根据条件删除一些日志文件等。

常用快捷键:

  • ctrl l 清屏,光标定位首行
  • ctrl d 相当于执行exit;logout;
  • ctrl d 行内有内容时一次删一个后面的字符
  • ctrl c 强制中断执行
  • ctrl u 清除剪切光标之前的内容
  • ctrl k 清除剪切光标及光标之后的内容
  • ctrl a||← 光标行首
  • ctrl e||→ 光标行尾

find使用:

1、根据名字查找
find 路径 -name “文件名字 ”或“使用通配符 * ”(需加上)    只知道名字
例:
find /etc/ -name "sshd_config"
find /etc/ -name "sshd*"
2、根据文件类型查找
find 路径 -type 文件类型 = d(目录) 、f(文件)、l(软链接文件)、s、c(字符串设备文件)、b(块设备文件)-name
例:
find /etc/ -type d -name "sshd_config"
find /etc/ -type f -name "sshd*"
3、根据时间查找
 -mtime(文件创建时间或更改时间)、-atime、-ctime +10、(默认单位为天)-mmin +_5 大于5分钟小于5分钟
find使用方法非常广,可以查天,年,月,日,分 还可以按列表形式显示
-o 或者什么
例:
find / -type f -mtime -1
查找,根目录下, f 类型,一天以内mtime有变动的文件(+1:当天以外,-1:当天)
find / -type f -mtime -1 -name "*.conf"
查找,根目录下, f 类型,一天以内mtime有变动的,且文件名以conf结尾的文件
find / -type f -mmin -60 -name "*.conf"
查找,根目录下, f 类型,小于60分钟内改动过,且文件名以conf结尾的文件
find / -type f -o -mtime -1 -o -name "*.conf"
查找,根目录下, f 类型,或者一天以内mtime有变动的,或者文件名以conf结尾的文件
4、查找硬链接
-inum 查找inode号
例:
find / -inum 33583395
查找,根目录下,inode号为33583395的文件
5、查找到文件后,直接显示出结果
-exec 将查找到的文件进行进一步操作
; 做一个循环
例:
find / -type f -mmin -60 -exec ls -l {} ;
查找,根目录下, f 类型,小于60分钟内改动过,把查找到的结果显示出来
find / -type f -mmin -60 -exec mv {} {}.bak ;
查找,根目录下, f 类型,小于60分钟内改动过,把查找的结果更改为以bak结尾的文件
6、根据文件大小查找
-size 可以查看 k(文件大小为k),M(文件大小为兆)
例:
find / -type f -size -10k -exec ls -l {} ;
查找,根目录下, f 类型,小于10k的文件,把查找到的结果显示出来
stat 查看文件的具体信息
stat 2.txt
三个时间属性:
Access time 为最近访问时间
Modify time 为最近创建或更改时间 (更改内容会改变时间)
Change time 为更改文件,更改权限,更改属组,属主时间(更改文件大小,也会改变)
如果只想找到当前目录下(不要子目录以及子目录的子目录)的文件或目录符合条件的需要加什么选项?
 -max depth 1

文件后缀名

  1. 扩展名类型 基本上,Linux的文件是没有所谓的扩展名的,一个Linux文件能不能被执行,与他的第一栏的十个属性有关, 与档名根本一点关系也没有。这个观念跟Windows的情况不相同喔!在Windows底下, 能被执行的文件扩展名通常是 .com .exe .bat等等,而在Linux底下,只要你的权限当中具有x的话,例如[ -rwx-r-xr-x ] 即代表这个文件可以被执行。 不过,可以被执行跟可以执行成功是不一样的~举例来说,在root家目录下的install.log 是一个纯文本档,如果经由修改权限成为 -rwxrwxrwx 后,这个文件能够真的执行成功吗? 当然不行~因为他的内容根本就没有可以执行的数据。所以说,这个x代表这个文件具有可执行的能力, 但是能不能执行成功,当然就得要看该文件的内容. 虽然如此,不过我们仍然希望可以藉由扩展名来了解该文件是什么东西,所以,通常我们还是会以适当的扩展名来表示该文件是什么种类的。底下有数种常用的扩展名: *.sh : 脚本或批处理文件 (scripts),因为批处理文件为使用shell写成的,所以扩展名就编成 .sh *Z, *.tar, *.tar.gz, *.zip, *.tgz: 经过打包的压缩文件。这是因为压缩软件分别为 gunzip, tar 等等的,由于不同的压缩软件,而取其相关的扩展名! *.html, *.php:网页相关文件,分别代表 HTML 语法与 PHP 语法的网页文件。 .html 的文件可使用网页浏览器来直接开启,至于 .php 的文件, 则可以透过 client 端的浏览器来 server 端浏览,以得到运算后的网页结果。 基本上,Linux系统上的文件名真的只是让你了解该文件可能的用途而已,真正的执行与否仍然需要权限的规范才行。例如虽然有一个文件为可执行文件,如常见的/bin/ls这个显示文件属性的指令,不过,如果这个文件的权限被修改成无法执行时,那么ls就变成不能执行。 上述的这种问题最常发生在文件传送的过程中。例如你在网络上下载一个可执行文件,但是偏偏在你的 Linux系统中就是无法执行!呵呵!那么就是可能文件的属性被改变了。不要怀疑,从网络上传送到你的 Linux系统中,文件的属性与权限确实是会被改变的。
  2. Linux文件名长度限制: 在Linux底下,使用预设的Ext2/Ext3文件系统时,针对文件名长度限制为: 单一文件或目录的最大容许文件名为 255 个字符 包含完整路径名称及目录 (/) 之完整档名为 4096 个字符 是相当长的档名!我们希望Linux的文件名可以一看就知道该文件在干嘛的, 所以档名通常是很长很长。
  3. Linux文件名的字符的限制: 由于Linux在文字接口下的一些指令操作关系,一般来说,你在设定Linux底下的文件名时, 最好可以避免一些特殊字符比较好!例如底下这些:
  • ? > < ; & ! [ ] | \ ' " ` ( ) { } 因为这些符号在文字接口下,是有特殊意义的。另外,文件名的开头为小数点“.”时, 代表这个文件为隐藏文件!同时,由于指令下达当中,常常会使用到 -option 之类的选项, 所以你最好也避免将文件档名的开头以 - 或 + 来命名。

链接文件

软链接文件

  • 优点:节省空间,方便使用;
  • 注意:
    • 尽量使用绝对路径;
    • 删除源,快捷也没用
[root@yolks ~]# ln -s /tmp/data /root/git
[root@yolks ~]# ls -l !$
ls -l /root/git
lrwxrwxrwx 1 root root 9 Jun  8 22:15 /root/git -> /tmp/data

硬链接文件

  • 特性:创建文件和其他文件inode号一样,称作相互硬链接
  • 注意: - 删除其中一个其他不受影响 - 不可对目录做硬链接 - 不可跨分区
[root@yolks ~]# ln 123.txt 123_head.txt
[root@yolks ~]# ls -l
total 4
-rw-r--r-- 2 root root    0 Jun  8 22:31 123_head.txt
-rw-r--r-- 2 root root    0 Jun  8 22:31 123.txt
lrwxrwxrwx 1 root root    9 Jun  8 22:15 git -> /tmp/data
drwxr-xr-x 2 root root 4096 Jun  6 21:43 shipan
[root@yolks ~]# ls -i
917527 123_head.txt  917524 git
917527 123.txt       917525 shipan

猜你喜欢

转载自my.oschina.net/yolks/blog/1823617