shell输入输出

举报
yd_221104950 发表于 2020/12/02 23:51:40 2020/12/02
【摘要】 重定向字符>和>> >符号可以将前面命令的结果重定向到符号后面指定的地方,如: ~/Desktop$ ls > hello.txt ~/Desktop$ cat hello.txt a AIDLEXample Book c EMAS EnglishHouse GithubSource git常用命令.txt GTK+学习.txt hello.txt ... 1234...

重定向字符>和>>

  • >符号可以将前面命令的结果重定向到符号后面指定的地方,如:
~/Desktop$ ls > hello.txt
~/Desktop$ cat hello.txt
a
AIDLEXample
Book
c
EMAS
EnglishHouse
GithubSource
git常用命令.txt
GTK+学习.txt
hello.txt
...

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

如上面将ls的结果重定向输出到一个文件hello.txt上,如果hello.txt一开始不存在,就会创建一个新的,如果存在,就会将hello.txt的内容覆盖掉,如果不想被覆盖掉,可以使用>>重定向符号。它会将内容追加到文件末尾,如:

~/Desktop$ ls >> hello.txt

  
 
  • 1

管道(|)

管道可将一个命令的执行结果输出到另一个命令,让另一个命令来处理结果,如:

~/Desktop$ head /proc/cpuinfo | tr a-z A-Z
PROCESSOR	: 0
VENDOR_ID	: GENUINEINTEL
CPU FAMILY	: 6
MODEL		: 78
MODEL NAME	: INTEL(R) CORE(TM) I5-6200U CPU @ 2.30GHZ
STEPPING	: 3
MICROCODE	: 0XD6
CPU MHZ		: 624.617
CACHE SIZE	: 3072 KB
PHYSICAL ID	: 0

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

首先使用head命令显示/proc/cpuinfo文件的前10行(默认是10行),然后这10行数据通过管道(|)传递到tr命令来处理,tr命令将完成小写转大写的功能,最后显示到终端上,默认是显示到终端上,我们也可以将结果重定向到某个文件,如:

~/Desktop$ head /proc/cpuinfo | tr a-z A-Z > hell.txt

  
 
  • 1

head与tail命令

head显示文件的前n行,显示是前10行:

~/Desktop$ head /proc/cpuinfo
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 78
model name	: Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
stepping	: 3
microcode	: 0xd6
cpu MHz		: 616.761
cache size	: 3072 KB
physical id	: 0

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

tail默认显示最后10行:

~/Desktop$ tail /proc/cpuinfo
cpuid level	: 22
wp		: yes
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp md_clear flush_l1d
bugs		: cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit
bogomips	: 4800.00
clflush size	: 64
cache_alignment	: 64
address sizes	: 39 bits physical, 48 bits virtual
power management:


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

sort排序

按字母顺序进行快速排序。

~/Desktop$ sort hello.txt
a
AIDLEXample
Book
c
EMAS
EnglishHouse
GithubSource
git常用命令.txt
GTK+学习.txt
hello.txt
MOK.der
MOK.priv
...

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

file显示文件格式信息

~/Desktop$ file hello.txt
hello.txt: UTF-8 Unicode text

  
 
  • 1
  • 2

find与locate查找命令

locate在系统创建的文件索引中查找文件,这个索引由操作系统周期性更新,查找速度比find要快,如:

~/Desktop$ locate stdio.h
...
/usr/include/stdio.h
/usr/include/c++/7/tr1/stdio.h
/usr/include/glib-2.0/glib/gstdio.h
/usr/include/unicode/ustdio.h
/usr/include/x86_64-linux-gnu/bits/stdio.h
/usr/lib/x86_64-linux-gnu/perl/5.26.1/CORE/nostdio.h


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

但是如果用locate来查找新创建的文件,可能会无能为力,因为它可能还没有被加到索引中,如:

~/Desktop$ touch wongkyunban.txt
~/Desktop$ locate wongkyunban.txt
~/Desktop$ 


  
 
  • 1
  • 2
  • 3
  • 4

find默认只在当前目录下查找文件,如:

~/Desktop$ ls
a git常用命令.txt  Studio.desktop   周报.xlsx
AIDLEXample   GTK+学习.txt SVN常用命令 数据结构
Book hello.txt TestAIDL 数据结构.xmind
c hell.txt TestOkHttp 破解apk的步骤.txt
EMAS h.txt Weex 超级简历.doc
EnglishHouse  MOK.der WindowsShared 面试的回答.txt
GithubSource  MOK.priv wongkyunban.txt
~/Desktop$ find hello.txt
hello.txt
~/Desktop$ find stdio.h
find: ‘stdio.h’: No such file or directory

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

find命令很灵活,如可以改变路径进行查找,如:

~/Desktop$ find /usr/include -name "stdio.h"
/usr/include/x86_64-linux-gnu/bits/stdio.h
/usr/include/c++/7/tr1/stdio.h
/usr/include/stdio.h


  
 
  • 1
  • 2
  • 3
  • 4
  • 5

谢谢阅读

文章来源: blog.csdn.net,作者:WongKyunban,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_40763897/article/details/103874919

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。