首页 > 系统相关 >linux的重定向、管道与环境变量PATH

linux的重定向、管道与环境变量PATH

时间:2022-10-09 14:35:15浏览次数:45  
标签:centos VM see file linux PATH txt root 环境变量

(文章目录)

一、重定向

1.输出重定向:>

1.写入指定文件

[root@VM-8-8-centos lesson5]#  cat file.txt
[root@VM-8-8-centos lesson5]# echo "hello world" > file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
hello world
[root@VM-8-8-centos lesson5]# cat file.txt > test.c
[root@VM-8-8-centos lesson5]#  cat test.c
hello world

将 cat file.txt默认到显示器上的内容 显示到了 test.c文件中

2. 覆盖写

[root@VM-8-8-centos lesson5]# cat file.txt
hello world
[root@VM-8-8-centos lesson5]#  echo "you can see you" > file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
you can see you

file.txt文件的原来内容是 hello world,被变成了 you can see me 将原来的文件内容清空,再重新写

2.追加重定向 :>>

[root@VM-8-8-centos lesson5]#  echo "you can see you" > file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
you can see you
[root@VM-8-8-centos lesson5]# echo "you can see me" >> file.txt
[root@VM-8-8-centos lesson5]#  cat file.txt
you can see you
you can see me
[root@VM-8-8-centos lesson5]#  echo "you can see me" >> file.txt
[root@VM-8-8-centos lesson5]# cat file.txt
you can see you
you can see me
you can see me

把file.txt文件的内容 you can see me 打印后, 使用 >> 发现会在文件结尾 追加内容

3.输出重定向:<

1.键盘显示

[root@VM-8-8-centos lesson5]# cat
abcdefhgjkl
abcdefhgjkl

cat 不跟文件,默认从键盘读到什么就显示什么。

2.文件显示

使用 < 变为 从 指定文件中读取数据



```c

[root@VM-8-8-centos lesson5]# cat < file.txt
you can see you
you can see me
you can see me
[root@VM-8-8-centos lesson5]#  cat file.txt
you can see you
you can see me
you can see me

cat < file.txt 与 cat file.txt等价 cat < file.txt :从fille.txt文件中读取数据

4.重定向的一些认知误区

1. test.c只显示错误的

find /home -name test.c > msg.c

寻找 主目录中的 test.c文件 并重定向到 msg .c文件中

在这里插入图片描述

发现只能显示出权限不够而不能访问的 即错误的

2. msg.c只显示正确的

打印 cat msg.c文件 只显示正确的

在这里插入图片描述

结论:显示器输出的信息中,有正确的,也有错误的, 只把正确的进行了重定向

3.分析

在这里插入图片描述

标准输出 和 标准错误输出 都是在显示器上打印,是两个不同的文件, 所以 >只重定向 标准输出

find /home -name test.c > msg.c 默认重定向 是 find /home -name test.c 1> msg.c 只不过把代码是1省略了 ,而代码1对应标准输出

4.显示出正确的

find /home -name test.c 2> msg.c 这里就代表将代码为2重定向到 msg.c文件,代码2代表标准输出 此时 test.c只显示正确的 在这里插入图片描述

二、管道

last指令

系统,历史上以时间为单位,登录服务器用户的个数

1.前五行数据的查询

取last数据的前五行

[root@VM-8-8-centos ~]# last | head -5
root     pts/1        103.57.12.38     Mon Oct  3 16:16   still logged in   
root     pts/0        106.41.249.118   Mon Oct  3 15:19   still logged in   
root     pts/0        103.57.12.38     Sun Oct  2 18:13 - 19:42  (01:29)    
root     pts/1        106.41.249.15    Sat Oct  1 14:09 - 15:59  (01:50)    
root     pts/0        103.57.12.38     Sat Oct  1 13:24 - 14:22  (00:58) 

last:登录的历史信息 | :被称为管道,用来数据传导 head:将文本行的前5行内容进行显示

1.用户登录次数

[root@VM-8-8-centos ~]#  last | grep root | wc -l
45

grep :按 行 为单位,进行关键字过滤 wc :统计行数 将 历史登录的信息(last)中的root用户的信息( grep root)统计行数(wc)

三、环境变量PATH

1.判断自己写的与系统的命令是否一致

在这里插入图片描述 使用 which 查询 ls 命令,发现是一个 64为可执行 程序,可以动态链接。

在这里插入图片描述

创建一个 mycmd.c文件,再通过vim编辑器,编辑一个c 通过 mycmd.c源文件 生成一个 可执行程序 mycmd

在这里插入图片描述

此时若直接输入 mycmd 则会报错,不带./ command命令找不到

在这里插入图片描述

此时我们会发现 为什么 我们自己写的就需要 ./ ,而系统命令就不需要。

结论:说明 系统自带的命令 不需要,而自己写的需要带上./

2.环境变量 PATH

1.环境变量 PATH概念

在系统中,可执行程序的搜索路径是保存在一个"全局的变量"PATH中,给系统提供命令的搜索路径,是环境变量的一种。

2.查看环境变量

在这里插入图片描述

PATH中保存了多条路径,路径之间用":"隔开 PATH: 从左到右依次进行程序搜索,找不到就就继续下一条路径,找到了就停下了,若所有路径都没找到,则爆出:command not found!

3.修改自己写的拷贝到 PATH中

在这里插入图片描述 使用 sudo是为了提高权限 将 mycmd拷贝到 /usr/bin目录中 此时 直接输入 mycmd 就可以显示出来它的内容

2.当前路径添加到环境变量中

在这里插入图片描述

此时就把 当前路径 lesson5导入PATH中 以此直接使用 mycmd

标签:centos,VM,see,file,linux,PATH,txt,root,环境变量
From: https://blog.51cto.com/u_15787387/5739545

相关文章

  • Linux文件操作命令touch
    touchtouch[filename]创建文件,参数可以跟多个如果要创建50个有规律的文件,例如text1-text50利用参数扩展touchtest{1..50}touchtest{a..e}touchtest{a..e}_{......
  • node_exporter到prometheus到grafana,监控linux机器,监控搭建
    搭建架构:Linux被监控机:node_exporterLinux监控机:prometheus&grafanamacos图形化展示监控结果:远程访问 prometheus&grafana 一、Linux被监控机的配置下载node_e......
  • linux磁盘已满,查看哪个文件占用多
    1.使用df-h查看磁盘空间占用情况2.使用sudodu-s-h/*|sort-nr命令查看那个目录占用空间大3.然后那个目录占用多再通过sudodu-s-h/var/*|sort-nr一层层......
  • LINUX系统同步网络时间
    安装了一个ntp缩减版的工具,然后同步时间的master是一个所有人都可以用的master安装工具yum -yinstallntpntpdate同步网络时间ntpdatecn.pool.ntp.org......
  • Linux中安装使用rsync
    获取 rsync-3.1.0 我的网盘里放了一个。地址:​​http://pan.baidu.com/s/1dDs4lSt​​安装rsync-3.1.0.tar.gz #tarzxvfrsync-3.1.0.tar.gz#cdrsync-3.1.0#./config......
  • 记录Linux下启动docker中Mysql,并进入mysql。
    1.启动dockersystemctlstartdocker  2.查看docker容器启动信息,并找到mysql容器  3.使用进程名启动mysql:dockerstartmysql-test;也可以使用进程id启动:docker......
  • linux 定时任务 加锁
    ​​*/3****flock-xn/home/work/fupeng/oem_apk_new.lock-c'sh/home/work/fupeng/oem_apk_new.sh>/dev/null2>&1'>/dev/null2>&1......
  • Linux系统睡眠
    参考Linuxkernel文档Documentation\admin-guide\pm\sleep-states.rstSystemSleepStates—TheLinuxKerneldocumentation系统睡眠状态名称ACPIState说明唤......
  • maven篇1: 安装、设置和环境变量配置
    一、为什么需要maven①一个项目就是一个工程如果项目非常庞大,就不适合使用package来划分模块,最好是每一个模块对应一个工程,利于分工协作。借助于maven就可以将一个项目拆分......
  • linux下解压zip 命令
    ## 基础命令是 unzip 解压在当前目录下,我以解压一个 logstash 的压缩包为例  unziplogstash-7.5.1.zip  ## 解压到指定目录下   这个是常用的,可以看到下......