环境变量PATH,cp命令,mv命令,文件查看cat_more_less_head_tail

环境变量PATH

  • 如果命令在以下目录的时候,可以不敲绝对路径。就能生效。这就是:环境变量
[root@aminglinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
  • 直接输入命令的绝对路径命令就可以生效。拷贝到别的地方一样可以生效。
[root@aminglinux-01 ~]# which ls
alias ls='ls --color=auto'
	/usr/bin/ls
[root@aminglinux-01 ~]# ls /usr/bin/ls 
/usr/bin/ls
[root@aminglinux-01 ~]# cp /usr/bin/ls /tmp/ls2
[root@aminglinux-01 ~]# /tmp/ls2 
anaconda-ks.cfg
[root@aminglinux-01 ~]# 
  • 但是直接输入ls2会生效吗?
[root@aminglinux-01 ~]# ls2
-bash: ls2: 未找到命令
  • 结果是并没有生效,这是因为ls2所在的/tmp/目录并不在(PATH)环境变量里面,现在把目录加到环境变量里面。

PATH=$PATH:/tmp/ 这是个shell意思是把tmp加入到PATH

[root@aminglinux-01 ~]# PATH=$PATH:/tmp/
[root@aminglinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/
  • 这样在输入ls2,就可以生效了。
[root@aminglinux-01 ~]# ls2
anaconda-ks.cfg
[root@aminglinux-01 ~]# 
  • 这时候如果在打开新的终端连接系统,在输入ls2,就不再生效了。如果需要在任何时候都会生效,永久生效。需要加到系统配置/etc/profile文件里。如下:
[root@aminglinux-01 ~]# vi /etc/profile
...
 if [ "$HISTCONTROL" = "ignorespace" ] ; then
    export HISTCONTROL=ignoreboth
else
    export HISTCONTROL=ignoredups
fi

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL

# By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
    umask 002
else
    umask 022
fi

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi
done

unset i
unset -f pathmunge
PATH=$PATH:/tmp/

在最后加上:PATH=$PATH:/tmp/

  • 如果想恢复之前的PATH,就去掉/tmp/,复制之前的PATH。命令如下:
[root@aminglinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/tmp/
[root@aminglinux-01 ~]#PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@aminglinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@aminglinux-01 ~]# ls2
-bash: ls2: 未找到命令
  • 如果设置了永久保存,就在vi /etc/profile里面把PATH=$PATH:/tmp/删掉重启就可以了。

 

cp 命令

  • cp ,copy的意思,把源文件拷贝到目标文件

[root@aminglinux-01 ~]# cp /etc/passwd   /tmp/12.txt
[root@aminglinux-01 ~]# ls /tmp/
12.txt  aminglinux  ks-script-suCXKE  ls2  systemd-private-8e1d573b9569400787aa2f163a2ae6d9-vmtoolsd.service-P3MfYI  systemd-private-9d23ed7881b349edb8dfffa96c3c1a5a-vmtoolsd.service-AfykKQ  yum.log
[root@aminglinux-01 ~]# ls -l /tmp/
总用量 124
-rw-r--r--. 1 root root   1008 8月   9 08:55 12.txt
drwxr-xr-x. 3 root root     17 8月   7 22:26 aminglinux
-rwx------. 1 root root    836 7月  31 23:06 ks-script-suCXKE
-rwxr-xr-x. 1 root root 117656 8月   8 23:25 ls2
drwx------. 3 root root     17 8月   3 08:40 systemd-private-8e1d573b9569400787aa2f163a2ae6d9-vmtoolsd.service-P3MfYI
drwx------. 3 root root     17 8月   9 08:31 systemd-private-9d23ed7881b349edb8dfffa96c3c1a5a-vmtoolsd.service-AfykKQ
-rw-------. 1 root root      0 7月  31 22:54 yum.log
[root@aminglinux-01 ~]# 
  • cp 目录也是需要加上-r的。
[root@aminglinux-01 ~]# cp -r /tmp/aminglinux/   /tmp/aming/
[root@aminglinux-01 ~]# tree /tmp/aming 
/tmp/aming
└── 111
    └── 222
        └── 333
[root@aminglinux-01 ~]# tree /tmp/aminglinux/
/tmp/aminglinux/
└── 111
    └── 222
        └── 333
  • 约定:拷贝 目录的时候后面加上/ 比如/tmp/ ,/root/。要写完整。

当复制文件已经存在的时候会问是否覆盖

[root@aminglinux-01 ~]# ls /tmp/aminglinux/
111
[root@aminglinux-01 ~]# cp /tmp/ls2     /tmp/111
[root@aminglinux-01 ~]# 111
anaconda-ks.cfg
[root@aminglinux-01 ~]# cp /tmp/ls2     /tmp/111
cp:是否覆盖"/tmp/111"? 

那么当复制目录的时候会不会提示覆盖呢?

[root@aminglinux-01 ~]# cp -r /tmp/aming1/    /tmp/aming2/
[root@aminglinux-01 ~]# ls /tmp/aming2/
abc  aming1
[root@aminglinux-01 ~]# tree /tmp/aming2/
/tmp/aming2/
├── abc
└── aming1
    └── 111
        └── 222
            └── 333

5 directories, 0 files
[root@aminglinux-01 ~]# cp -r /tmp/aming1/    /tmp/aming2/
cp:是否覆盖"/tmp/aming2/aming1/111/1.txt"? 

经过测试是如果目录都是空的,就不会有提示,如果目录里有文件就会提示是否覆盖。

mv命令

  • mv ,move移动文件

同一个目录下移动文件会给文件更改名字

[root@aminglinux-01 ~]# ls
anaconda-ks.cfg
[root@aminglinux-01 ~]# mv anaconda-ks.cfg    anaconda-ks.cfg.1
[root@aminglinux-01 ~]# ls
anaconda-ks.cfg.1

还可以移动文件并且改名字。

[root@aminglinux-01 tmp]# mv 12.txt     /root/2.txt
[root@aminglinux-01 tmp]# ls /root/
2.txt  anaconda-ks.cfg.1
[root@aminglinux-01 tmp]# 

如果移动的位置已经有一个相同文件名的文件,也是会提示覆盖的。输入mv的绝对路径就不会提示了。

也可以更改目录的名字,如果目标目录不存在的话,就会更改源目录的名字。

[root@aminglinux-01 tmp]# ls
111  aming  aming1  aming2  aminglinux  ks-script-suCXKE  ls2  systemd-private-8e1d573b9569400787aa2f163a2ae6d9-vmtoolsd.service-P3MfYI  systemd-private-9d23ed7881b349edb8dfffa96c3c1a5a-vmtoolsd.service-AfykKQ  yum.log
[root@aminglinux-01 tmp]# mv aming/  aminglinux2/
[root@aminglinux-01 tmp]# ls
111  aming1  aming2  aminglinux  aminglinux2  ks-script-suCXKE  ls2  systemd-private-8e1d573b9569400787aa2f163a2ae6d9-vmtoolsd.service-P3MfYI  systemd-private-9d23ed7881b349edb8dfffa96c3c1a5a-vmtoolsd.service-AfykKQ  yum.log

文件查看cat_more_less_head_tail

  • cat

查看一个文件

[root@aminglinux-01 ~]# cat anaconda-ks.cfg.1 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom

tac 倒序查看一个文件

cat -n 显示行号

其他用法可以man cat一下。

  • more

也是查看文件内容的,但是不会像cat一样全部显示出来。

它会一屏一屏的显示。按空格键下翻页。

看完之后就自动退出来了。

按Ctrl+b键是向上翻页。

[root@aminglinux-01 ~]# more /etc/profile
# /etc/profile

# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}
--More--(66%)

  • less命令

  • less的命令的用法包含了more的用法,同时可以用方向键来一行一行的翻页。翻到尾部不会自动退出来。

  • 按q可以退出。

  • 还可以搜索内容

    输入:/或者?“单词” 比如/and或?and 搜索下面文件内容所有的and都会高亮显示。
    按“n”像下查询查找内容
    按“N”从下向上查询查找内容

  • 按“g”定位到行首

  • 按“G”定位到行尾

[root@aminglinux-01 ~]# less /etc/profi
# System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

pathmunge () {
    case ":${PATH}:" in
        *:"$1":*)
            ;;
        *)
            if [ "$2" = "after" ] ; then
                PATH=$PATH:$1
            else
                PATH=$1:$PATH
            fi
    esac
}


/and

  • head 命令

查看文件的头十行

[root@aminglinux-01 ~]# head anaconda-ks.cfg.1 
#version=DEVEL
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
graphical
# Run the Setup Agent on first boot
firstboot --enable
ignoredisk --only-use=sda
[root@aminglinux-01 ~]# 
  • tail 命令

查看文件的尾十行

[root@aminglinux-01 ~]# tail anaconda-ks.cfg.1 

%addon com_redhat_kdump --enable --reserve-mb='auto'

%end

%anaconda
pwpolicy root --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=50 --notstrict --nochanges --notempty
pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty
%end
[root@aminglinux-01 ~]# 

tail 和head都是可以输入-n 来指定看几行的。比如只看两行。

[root@aminglinux-01 ~]# tail -n 2 anaconda-ks.cfg.1 
pwpolicy luks --minlen=6 --minquality=50 --notstrict --nochanges --notempty
%end
[root@aminglinux-01 ~]# 

tail还有一种用法就是tail -f 动态查询文件。比如查看日志。

使用tail -f 
如果查看的文档有新加入的文档。tail -f是可以动态查看到的
一般查看日志用

验证方法:
  新建一个会话,向tail -f 查看的文档里追加新的内容。返回原会话看显示有没有变化

彩蛋

  • wc -l 命令,显示一个文件的行数

  • 追加重定向:cat /etc/passwd >> anaconda-ks.cfg.1

意思是把/etc/passwd 里面的内容追加到anaconda那个文件里面。

posted on 2018-03-27 22:18  xxxyyzz点xyz  阅读(388)  评论(0)    收藏  举报

导航