Linux命令1 管道,输出输入,文件,目录

1.创建一个文件a.txt,要求输入
name age year
jack 23 2021
mon 28 2019
tac 32 2009
tiny 22 2003

[root@centos6c yum.repos.d]# touch a.txt
[root@centos6c yum.repos.d]# vi a.txt

3.在/tmp目录下创建一个test目录,将当前的a.txt文件移动到/tmp目录下,然后查看这个目录下的文件是否含有此文件

mkdir 创建目录

touch 创建文件

删除 rm -r

mv 文件 文件(只能改名)

mv aa bb (把aa改成bb)

mv 文件 目录

mv 目录 目录

mv cc dd(把cc放入dd中)

4.将a.txt文件重命名为my.txt

[root@centos6c tmp]# ls
a.txt test yum.log
[root@centos6c tmp]# mv a.txt my.txt
[root@centos6c tmp]# ls
my.txt test yum.log

6.将my.txt文本内容倒序输出

tac 倒序输出

cat 查看文件

[root@centos6c tmp]# tac my.txt
tac 32 2009
mon 28 2019
jack 23 2021
name age year
[root@centos6c tmp]#

7.获取当前目录,并加目录加入文本my.txt中第一行

pwd 获取当前目录

vi 进去 然后改

[root@centos6c tmp]# pwd
/tmp

8.在/tmp目录下新创建一个mytest目录,复制my.txt内容到mytest目录,文件名为new.txt,同时输出文件内容及详细信息

[root@centos6c tmp]# cat my.txt | tee mytest/new.txt
name age year
jack 23 2021
mon 28 2019
tac 32 2009
[root@centos6c tmp]#

|tee 命令在数据流的处理过程中将某段信息保存下来,使其既能输出屏幕又能保存到某一个文件中**

1.使用alias实现输入love然后输出I love you(提示:可以使用echo命令)

alias name=’'

alias love='echo i love you'
[root@localhost ~]# love
i love you

等于号前后不能有空格

[root@localhost ~]# unalias love

[root@localhost ~]# love

撤销操作

2.编辑一个新文件mylinux.txt,使用**输出重定向向文本中输入如下内容
12123
This is a test
Hello World
hello girls and boys
end
然后使用管道命令过滤输出hello的行(不限制大小写)
|grep -i 不限制大小写

[root@localhost ~]# echo '12123

This is a test
Hello World
hello girls and boys
end' >mylinux.txt
[root@localhost ~]# cat mylinux.txt
12123
This is a test
Hello World
hello girls and boys
end

注意单引号的使用

[root@localhost ~]# cat mylinux.txt|grep -i 'hello'
Hello World
hello girls and boys

3.使用你学过的命令让mylinux.txt的内容同时输出到终端和新文件mytest.txt中

|tee 命令在数据流的处理过程中将某段信息保存下来,使其既能输出屏幕又能保存到某一个文件中

[root@localhost ~]# cat mylinux.txt|tee mytest.txt
12123
This is a test
Hello World
hello girls and boys
end
[root@localhost ~]# cat mytest.txt
12123
This is a test
Hello World
hello girls and boys
end
[root@localhost ~]#

4.向mylinux.txt文件中追加一行内容为Good I did it

1> 覆盖

1>> 追加

1可省略

[root@localhost ~]# echo 'Good I did it'>>mylinux.txt
[root@localhost ~]# cat mylinux.txt
12123
This is a test
Hello World
hello girls and boys
end
Good I did it
[root@localhost ~]#

5.使命令查看abc目录内容(这个目录不需要创建),将报错输出到文件中

2> 错误输出

[root@localhost ~]# ls /abc 2>a.txt
[root@localhost ~]# cat a.txt
ls: cannot access /abc: No such file or directory
[root@localhost ~]#

6.使用一个命令列出/tmp目录和/abc目录文件的列表,将标准输出和错误输出到同一个文件中,文件名自定义,然后查看下此文件内容。

正确输出和错误混合输出:&>

正确和错误都输入到相同位置:2>&1

[root@localhost ~]# ls /tmp /abc &>a.txt
[root@localhost ~]# cat a.txt
ls: cannot access /abc: No such file or directory
/tmp:
ks-script-CG9xfN
mytest
my.txt
test
vmware-root_6081-1983851914
vmware-root_6085-1992043535

vmware-root_6357-1957962399
vmware-root_6371-1991648432
vmware-root_6405-1983653167
yum.log
[root@localhost ~]#

2>&1

[root@localhost ~]# ls /tmp /abc >a.txt 2>&1
[root@localhost ~]# cat a.txt
ls: cannot access /abc: No such file or directory
/tmp:
ks-script-CG9xfN
mytest
my.txt
test
vmware-root_6081-1983851914
vmware-root_6085-1992043535
vmware-root_6096-1002483780
vmware-root_6405-1983653167
yum.log
[root@localhost ~]#

7.使用dd命令构造一个1G大小的文件,然后使用学过的命令查看下这个文件的大小

dd 命令创建任意大小的文件 例如:

dd if=/dev/zero of=文件名 bs=大小 count=数量

/dev/zero :初始化文件,任意大小

/dev/null :空设备 位筒

[root@localhost ~]# dd if=/dev/zero of=b bs=1M count=10
10+0 records in
10+0 records out
10485760 bytes (10 MB) copied, 0.0340043 s, 308 MB/s
[root@localhost ~]# ls
anaconda-ks.cfg a.txt b data.txt mylinux.txt mytest.txt
[root@localhost ~]# ls -lh b
-rw-r--r--. 1 root root 10M Jul 20 10:31 b
[root@localhost ~]#

**ls -lh 文件名 查看文件 **
在这里插入图片描述

bs 后面加单位

bs:块设备大小

count :块设备数量

8.使用课堂上老师讲过的命令,在mylinux.txt文件中过滤下关键字love,测试下是否能成功执行,要求不输出结果,仅验证是否能过滤到(提示:$?返回上一个命令执行结果)

[root@localhost ~]# cat mylinux.txt |grep 'love'
[root@localhost ~]# echo $?
1
[root@localhost ~]#

echo $? :返回上一条命令是否正确 0正确 其他任意数字错误

例如:上道题过滤出来0 但并没有love关键字 所以过滤不出来所以错误

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值