Linux常用命令大全_第1页
Linux常用命令大全_第2页
Linux常用命令大全_第3页
Linux常用命令大全_第4页
Linux常用命令大全_第5页
已阅读5页,还剩58页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Linux常用命令大全系统信息arch 显示机器的处理器架构(1uname -m 显示机器的处理器架构(2uname -r 显示正在使用的内核版本dmidecode -q 显示硬件系统部件 - (SMBIOS / DMI hdparm -i /dev/hda 罗列一个磁盘的架构特性hdparm -tT /dev/sda 在磁盘上执行测试性读取操作cat /proc/cpuinfo 显示CPU info的信息cat /proc/interrupts 显示中断cat /proc/meminfo 校验内存使用cat /proc/swaps 显示哪些swap被使用cat /proc/version 显

2、示内核的版本cat /proc/net/dev 显示网络适配器及统计cat /proc/mounts 显示已加载的文件系统lspci -tv 罗列 PCI 设备lsusb -tv 显示 USB 设备date 显示系统日期cal 2007 显示2007年的日历表date 0412*.00 设置日期和时间 - 月日时分年.秒clock -w 将时间修改保存到 BIOS关机 (系统的关机、重启以及登出 shutdown -h now 关闭系统(1init 0 关闭系统(2telinit 0 关闭系统(3shutdown -h hours:minutes & 按预定时间关闭系统shutdown

3、 -c 取消按预定时间关闭系统shutdown -r now 重启(1reboot 重启(2logout 注销文件和目录cd /home 进入 '/ home' 目录'cd . 返回上一级目录cd ./. 返回上两级目录cd 进入个人的主目录cd user1 进入个人的主目录cd - 返回上次所在的目录pwd显示工作路径ls 查看目录中的文件ls -F 查看目录中的文件ls -l 显示文件和目录的详细资料ls -a 显示隐藏文件ls *0-9* 显示包含数字的文件名和目录名tree 显示文件和目录由根目录开始的树形结构(1lstree 显示文件和目录由根目录开始的树形结

4、构(2mkdir dir1 创建一个叫做 'dir1' 的目录'mkdir dir1 dir2 同时创建两个目录mkdir -p /tmp/dir1/dir2 创建一个目录树rm -f file1 删除一个叫做 'file1' 的文件'rmdir dir1 删除一个叫做 'dir1' 的目录'rm -rf dir1 删除一个叫做 'dir1' 的目录并同时删除其内容rm -rf dir1 dir2 同时删除两个目录及它们的内容mv dir1 new_dir 重命名/移动一个目录cp file1 file2

5、复制一个文件cp dir/* . 复制一个目录下的所有文件到当前工作目录cp -a /tmp/dir1 . 复制一个目录到当前工作目录cp -a dir1 dir2 复制一个目录ln -s file1 lnk1 创建一个指向文件或目录的软链接ln file1 lnk1 创建一个指向文件或目录的物理链接touch -t 0712250000 file1 修改一个文件或目录的时间戳 - (YYMMDDhhmmfile file1 outputs the mime type of the file as texticonv -l 列出已知的编码iconv -f fromEncoding -t toE

6、ncoding inputFile > outputFile creates a new from the given input file by assuming it is encoded in fromEncoding and converting it to toEncoding.find . -maxdepth 1 -name *.jpg -print -exec convert "" -resize 80x60 "thumbs/" ; batch resize files in the current directory and sen

7、d them to a thumbnails directory (requires convert from Imagemagick文件搜索find / -name file1 从 '/' 开始进入根文件系统搜索文件和目录find / -user user1 搜索属于用户 'user1' 的文件和目录find /home/user1 -name *.bin 在目录 '/ home/user1' 中搜索带有'.bin' 结尾的文件find /usr/bin -type f -atime +100 搜索在过去100天内未被使用过的执

8、行文件find /usr/bin -type f -mtime -10 搜索在10天内被创建或者修改过的文件find / -name *.rpm -exec chmod 755 '' ; 搜索以 '.rpm' 结尾的文件并定义其权限find / -xdev -name *.rpm 搜索以 '.rpm' 结尾的文件,忽略光驱、捷盘等可移动设备locate *.ps 寻找以 '.ps' 结尾的文件 - 先运行 'updatedb' 命令whereis halt 显示一个二进制文件、源码或man的位置which halt

9、 显示一个二进制文件或可执行文件的完整路径挂载一个文件系统mount /dev/hda2 /mnt/hda2 挂载一个叫做hda2的盘 - 确定目录 '/ mnt/hda2' 已经存在umount /dev/hda2 卸载一个叫做hda2的盘 - 先从挂载点 '/ mnt/hda2' 退出fuser -km /mnt/hda2 当设备繁忙时强制卸载umount -n /mnt/hda2 运行卸载操作而不写入 /etc/mtab 文件- 当文件为只读或当磁盘写满时非常有用mount /dev/fd0 /mnt/floppy 挂载一个软盘mount /dev/cdr

10、om /mnt/cdrom 挂载一个cdrom或dvdrommount /dev/hdc /mnt/cdrecorder 挂载一个cdrw或dvdrommount /dev/hdb /mnt/cdrecorder 挂载一个cdrw或dvdrommount -o loop file.iso /mnt/cdrom 挂载一个文件或ISO镜像文件mount -t vfat /dev/hda5 /mnt/hda5 挂载一个Windows FAT32文件系统mount /dev/sda1 /mnt/usbdisk 挂载一个usb 捷盘或闪存设备mount -t smbfs -o username=user

11、,password=pass /WinClient/share /mnt/share 挂载一个windows网络共享磁盘空间df -h 显示已经挂载的分区列表ls -lSr |more 以尺寸大小排列文件和目录du -sh dir1 估算目录 'dir1' 已经使用的磁盘空间'du -sk * | sort -rn 以容量大小为依据依次显示文件和目录的大小rpm -q -a -qf '%10SIZEt%NAMEn' | sort -k1,1n 以大小为依据依次显示已安装的rpm包所使用的空间 (fedora, redhat类系统dpkg-query -W

12、 -f='$Installed-Size;10t$Packagen' | sort -k1,1n 以大小为依据显示已安装的deb包所使用的空间 (ubuntu, debian类系统返回顶部索引 用户和群组groupadd group_name 创建一个新用户组groupdel group_name 删除一个用户组groupmod -n new_group_name old_group_name 重命名一个用户组useradd -c "Name Surname " -g admin -d /home/user1 -s /bin/bash user1 创建一个属

13、于 "admin" 用户组的用户useradd user1 创建一个新用户userdel -r user1 删除一个用户 ( '-r' 排除主目录usermod -c "User FTP" -g system -d /ftp/user1 -s /bin/nologin user1 修改用户属性passwd 修改口令passwd user1 修改一个用户的口令 (只允许root执行chage -E 2005-12-31 user1 设置用户口令的失效期限pwck 检查 '/etc/passwd' 的文件格式和语法修正以及存在

14、的用户grpck 检查 '/etc/passwd' 的文件格式和语法修正以及存在的群组newgrp group_name 登陆进一个新的群组以改变新创建文件的预设群组返回顶部索引 文件的权限 - 使用 "+" 设置权限,使用 "-" 用于取消ls -lh 显示权限ls /tmp | pr -T5 -W$COLUMNS 将终端划分成5栏显示chmod ugo+rwx directory1 设置目录的所有人(u、群组(g以及其他人(o以读(r 、写(w和执行(x的权限chmod go-rwx directory1 删除群组(g与其他人(o对目

15、录的读写执行权限chown user1 file1 改变一个文件的所有人属性chown -R user1 directory1 改变一个目录的所有人属性并同时改变改目录下所有文件的属性chgrp group1 file1 改变文件的群组chown user1:group1 file1 改变一个文件的所有人和群组属性find / -perm -u+s 罗列一个系统中所有使用了SUID控制的文件chmod u+s /bin/file1 设置一个二进制文件的 SUID 位 - 运行该文件的用户也被赋予和所有者同样的权限chmod u-s /bin/file1 禁用一个二进制文件的 SUID位chmo

16、d g+s /home/public 设置一个目录的SGID 位 - 类似SUID ,不过这是针对目录的chmod g-s /home/public 禁用一个目录的 SGID 位chmod o+t /home/public 设置一个文件的 STIKY 位 - 只允许合法所有人删除文件chmod o-t /home/public 禁用一个目录的 STIKY 位返回顶部索引 文件的特殊属性 - 使用 "+" 设置权限,使用 "-" 用于取消chattr +a file1 只允许以追加方式读写文件chattr +c file1 允许这个文件能被内核自动压缩/解

17、压chattr +d file1 在进行文件系统备份时,dump程序将忽略这个文件chattr +i file1 设置成不可变的文件,不能被删除、修改、重命名或者链接chattr +s file1 允许一个文件被安全地删除chattr +S file1 一旦应用程序对这个文件执行了写操作,使系统立刻把修改的结果写到磁盘chattr +u file1 若文件被删除,系统会允许你在以后恢复这个被删除的文件lsattr 显示特殊的属性返回顶部索引 打包和压缩文件bunzip2 file1.bz2 解压一个叫做 'file1.bz2'的文件bzip2 file1 压缩一个叫做 '

18、;file1' 的文件gunzip file1.gz 解压一个叫做 'file1.gz'的文件Linux压缩保留源文件的方法:gzip c filename > filename.gzLinux解压缩保留源文件的方法:gunzip c filename.gz > filenamegzip file1 压缩一个叫做 'file1'的文件gzip -9 file1 最大程度压缩zcat命令查看一个gzip格式的压缩文件zcat file.gz > test.txt 查看一个压缩文件的内容,并将内容輸入到文件test.txtzcat命令也可以

19、解压缩数据,并把解压后的文件内容显示在屏幕上,但是在运行zcat命令之后,压缩文件的内容并不发生变化,仍然以压缩方式存储在硬盘上。linux下解压命令大全.tar解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!.gz解压1:gunzip FileName.gz解压2:gzip -d FileName.gz压缩:gzip FileName.tar.gz 和 .tgz.bz2解压1:bzip2 -d FileName.bz2解压2:bunzip2 FileName.bz2压缩:bzip2 -z FileNam

20、e.tar.bz2.bz解压1:bzip2 -d FileName.bz解压2:bunzip2 FileName.bz压缩:未知.tar.bz压缩:未知.Z解压:uncompress FileName.Z压缩:compress FileName.tar.Z.zip解压:unzip FileName.zip压缩:zip FileName.zip DirName.rar解压:rar x FileName.rar压缩:rar a FileName.rar DirName.lha解压:lha -e FileName.lha压缩:lha -a FileName.lha FileName.rpm解包:rp

21、m2cpio FileName.rpm | cpio -div.deb.tar .tgz .tar.gz .tar.Z .tar.bz .tar.bz2 .zip .cpio .rpm .deb .slp .arj .rar .ace .lha .lzh .lzx .lzs .arc .sda .sfx .lnx .zoo .cab .kar .cpt .pit .sit .sea解压:sEx x FileName.*压缩:sEx a FileName.* FileNamesEx只是调用相关程序,本身并无压缩、解压功能,请注意!gzip 命令减少文件大小有两个明显的好处,一是可以减少存储空间,

22、二是通过网络传输文件时,可以减少传输的时间。gzip 是在Linux 系统中经常使用的一个对文件进行压缩和解压缩的命令,既方便又好用。语法:gzip 选项 压缩(解压缩的文件名该命令的各选项含义如下:-c 将输出写到标准输出上,并保留原有文件。-d 将压缩文件解压。-l 对每个压缩文件,显示下列字段:压缩文件的大小;未压缩文件的大小;压缩比;未压缩文件的名字-r 递归式地查找指定目录并压缩其中的所有文件或者是解压缩。-t 测试,检查压缩文件是否完整。-v 对每一个压缩和解压的文件,显示文件名和压缩比。-num 用指定的数字num 调整压缩的速度,-1 或-fast 表示最快压缩方法(低压缩比,

23、-9 或-best表示最慢压缩方法(高压缩比。系统缺省值为6。指令实例:rar a file1.rar test_file 创建一个叫做 'file1.rar' 的包rar a file1.rar file1 file2 dir1 同时压缩 'file1', 'file2' 以及目录 'dir1'rar x file1.rar 解压rar包unrar x file1.rar 解压rar包tar -cvf archive.tar file1 创建一个非压缩的 tarballtar -cvf archive.tar file1 fil

24、e2 dir1 创建一个包含了 'file1', 'file2' 以及 'dir1'的档案文件tar -tf archive.tar 显示一个包中的内容tar -xvf archive.tar 释放一个包tar -xvf archive.tar -C /tmp 将压缩包释放到 /tmp目录下zip file1.zip file1 创建一个zip格式的压缩包zip -r file1.zip file1 file2 dir1 将几个文件和目录同时压缩成一个zip格式的压缩包unzip file1.zip 解压一个zip格式压缩包返回顶部索引 RPM

25、包 - (Fedora, Redhat及类似系统rpm -ivh package.rpm 安装一个rpm包rpm -ivh -nodeeps package.rpm 安装一个rpm包而忽略依赖关系警告rpm -U package.rpm 更新一个rpm包但不改变其配置文件rpm -F package.rpm 更新一个确定已经安装的rpm包rpm -e package_name.rpm 删除一个rpm包rpm -qa 显示系统中所有已经安装的rpm包rpm -qa | grep httpd 显示所有名称中包含 "httpd" 字样的rpm包rpm -qi package_na

26、me 获取一个已安装包的特殊信息rpm -qg "System Environment/Daemons" 显示一个组件的rpm包rpm -ql package_name 显示一个已经安装的rpm包提供的文件列表rpm -qc package_name 显示一个已经安装的rpm包提供的配置文件列表rpm -q package_name -whatrequires 显示与一个rpm包存在依赖关系的列表rpm -q package_name -whatprovides 显示一个rpm包所占的体积rpm -q package_name -scripts 显示在安装/删除期间所执行的

27、脚本lrpm -q package_name -changelog 显示一个rpm包的修改历史rpm -qf /etc/httpd/conf/httpd.conf 确认所给的文件由哪个rpm包所提供rpm -qp package.rpm -l 显示由一个尚未安装的rpm包提供的文件列表rpm -import /media/cdrom/RPM-GPG-KEY 导入公钥数字证书rpm -checksig package.rpm 确认一个rpm包的完整性rpm -qa gpg-pubkey 确认已安装的所有rpm包的完整性rpm -V package_name 检查文件尺寸、许可、类型、所有者、群组

28、、MD5检查以及最后修改时间rpm -Va 检查系统中所有已安装的rpm包- 小心使用rpm -Vp package.rpm 确认一个rpm包还未安装rpm2cpio package.rpm | cpio -extract -make-directories *bin* 从一个rpm包运行可执行文件rpm -ivh /usr/src/redhat/RPMS/arch/package.rpm 从一个rpm源码安装一个构建好的包返回顶部索引 YUM 软件包升级器 - (Fedora, RedHat及类似系统yum install package_name 下载并安装一个rpm包yum locali

29、nstall package_name.rpm 将安装一个rpm包,使用你自己的软件仓库为你解决所有依赖关系yum update package_name.rpm 更新当前系统中所有安装的rpm包yum update package_name 更新一个rpm包yum remove package_name 删除一个rpm包yum list 列出当前系统中安装的所有包yum search package_name 在rpm仓库中搜寻软件包yum clean packages 清理rpm缓存删除下载的包yum clean headers 删除所有头文件yum clean all 删除所有缓存的包和

30、头文件返回顶部索引 DEB 包 (Debian, Ubuntu 以及类似系统dpkg -i package.deb 安装/更新一个 deb 包dpkg -r package_name 从系统删除一个 deb 包dpkg -l 显示系统中所有已经安装的 deb 包dpkg -l | grep httpd 显示所有名称中包含 "httpd" 字样的deb包dpkg -s package_name 获得已经安装在系统中一个特殊包的信息dpkg -L package_name 显示系统中已经安装的一个deb包所提供的文件列表dpkg -contents package.deb 显示

31、尚未安装的一个包所提供的文件列表dpkg -S /bin/ping 确认所给的文件由哪个deb包提供返回顶部索引 APT 软件工具 (Debian, Ubuntu 以及类似系统apt-get install package_name 安装/更新一个 deb 包apt-cdrom install package_name 从光盘安装/更新一个 deb 包apt-get update 升级列表中的软件包apt-get upgrade 升级所有已安装的软件apt-get remove package_name 从系统删除一个deb包apt-get check 确认依赖的软件仓库正确apt-get c

32、lean 从下载的软件包中清理缓存apt-cache search searched-package 返回包含所要搜索字符串的软件包名称返回顶部索引 查看文件内容cat file1 从第一个字节开始正向查看文件的内容Linux Cat命令主要有三大功能:1.Linux Cat命令一次显示整个文件。$ cat filename2.Linux Cat命令从键盘创建一个文件。$ cat > filename只能创建新文件,不能编辑已有文件.3.Linux Cat命令将几个文件合并为一个文件。$ cat file1 file2 >filetac file1 从最后一行开始反向查看一个文件的

33、内容more file1 查看一个长文件的内容less file1 类似于 'more' 命令,但是它允许在文件中和正向操作一样的反向操作head -2 file1 查看一个文件的前两行tail -2 file1 查看一个文件的最后两行tail -f /var/log/messages 实时查看被添加到一个文件中的内容返回顶部索引 文本处理cat file1 | command( sed, grep, awk, grep, etc. > result.txt 合并一个文件的详细说明文本,并将简介写入一个新文件中cat file1 | command( sed, grep,

34、 awk, grep, etc. >> result.txt 合并一个文件的详细说明文本,并将简介写入一个已有的文件中grep Aug /var/log/messages 在文件 '/var/log/messages'中查找关键词"Aug"grep Aug /var/log/messages 在文件 '/var/log/messages'中查找以"Aug"开始的词汇grep 0-9 /var/log/messages 选择 '/var/log/messages' 文件中所有包含数字的行grep

35、Aug -R /var/log/* 在目录 '/var/log' 及随后的目录中搜索字符串"Aug"grep的用法$ cat demo_fileTHIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.this line is the 1st lower case line in this file.This Line Has All Its First Character Of The Word With Upper Case.Two lines above this line is empty.And this

36、is the last line.1.从单个文件中搜索指定的字串grep的基础用法是如下例的从指定的文件中搜索特定的字串。语法:grep "literal_string" filename$ grep "this" demo_filethis line is the 1st lower case line in this file.Two lines above this line is empty.And this is the last line.2. 在多个文件中检索指定的字串语法:grep "string" FILE_PATT

37、ERN先拷贝demo_file为demo_file1。grep的结果在符合条件的行前将包括文件名。当文件名包含元字符时,linux shell会将匹配的所有文件作为输入到grep中去。$ demo_file demo_file1$ grep "this" demo_*demo_file:this line is the 1st lower case line in this file.demo_file:Two lines above this line is empty.demo_file:And this is the last line.demo_file1:this

38、 line is the 1st lower case line in this file.demo_file1:Two lines above this line is empty.demo_file1:And this is the last line.3. 用grep -i 进行大小写无关的搜索语法:grep -i "string" FILE也是一个基本用法,对搜索的字串忽略大小写,因此下例中匹配“the”, “THE” and “The”。$ grep -i "the" demo_fileTHIS LINE IS THE 1ST UPPER CA

39、SE LINE IN THIS FILE.this line is the 1st lower case line in this file.This Line Has All Its First Character Of The Word With Upper Case. And this is the last line.4. 使用用正则表达式语法:grep "REGEX" filename如果你能有效地利用正则表达式,这是个很有用的特点。在下面的例子中,搜索全部以“lines”开始以“empty”结束的字串,如搜索“lines之间任意字e mpty” ,并且忽略大小写

40、。$ grep -i "lines.*empty" demo_fileTwo lines above this line is empty.正则表达式遵循的几个重复的操作? 最多匹配一次* 匹配零次或者任意多次+ 匹配一次以上n 匹配n次n, 最少匹配n次,m 最多匹配m次n,m 匹配n到m次5. 用grep -w搜索整个词,而不是词中的部分字串使用-w选项搜索一个单词,并且避免搜索到词中的部分字串。下例搜索"is"。如果不加-w选项,将显示“is”, “his”, “this” 等所有包含“is”的行。$ grep -i "is"

41、demo_fileTHIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.this line is the 1st lower case line in this file.This Line Has All Its First Character Of The Word With Upper Case. Two lines above this line is empty.And this is the last line.下例使用了-w选项,请注意结果中不包含“This Line Has All Its First Character Of Th

42、e Word With Upper Case”, 虽然“This”中包含“is”。$ grep -iw "is" demo_fileTHIS LINE IS THE 1ST UPPER CASE LINE IN THIS FILE.this line is the 1st lower case line in this file.Two lines above this line is empty.And this is the last line.6. 使用grep -A, -B and -C显示之前、之后、前后的几行当使用grep搜索大文件时,显示匹配行附近的多行数据是

43、一个很有用的功能。创建如下文件$ cat demo_text4. Vim Word NavigationYou may want to do several navigation in relation to the words, such as:* e - go to the end of the current word.* E - go to the end of the current WORD.* b - go to the previous (before word.* B - go to the previous (before WORD.* w - go to the next

44、 word.* W - go to the next WORD.WORD - WORD consists of a sequence of non-blank characters, separated with white space.word - word consists of a sequence of letters, digits and underscores. Example to show the difference between WORD and word6.1 显示匹配行之后的N行-A语法:grep -A "string" FILENAME下例显示

45、匹配行和之后的3行数据$ grep -A 3 -i "example" demo_textExample to show the difference between WORD and word6.2显示匹配行之前的N行-B语法:grep -B "string" FILENAME下例显示匹配行和之前的2行数据$ grep -B 2 "single WORD" demo_textExample to show the difference between WORD and word6.3显示匹配行前后的N行-C 显示之前的n行,之后的n

46、行数据.$ grep -C 2 "Example" demo_textword - word consists of a sequence of letters, digits and underscores. Example to show the difference between WORD and word7.通过GREP_OPTIONS高亮显示搜索的字串如果你希望搜索的字串高亮显示在结果中,可以试用以下的办法。通过修改GREP_OPTIONS对搜索字串高亮显示。$ export GREP_OPTIONS='-color=auto' GREP_COL

47、OR='100;8'$ grep this demo_filethis line is the 1st lower case line in this file.Two lines above this line is empty.And this is the last line.8. 用grep -r递归搜索全部的文件如果想查找当前目前以及其子目录的全部文件时,可以使用-r 选项。如下例$ grep -r "ramesh" *9. 使用grep -v进行不匹配可以使用-v选项显示不匹配搜索字串的行。下例显示demo_text文件中不包含“go”的行$ g

48、rep -v "go" demo_text4. Vim Word NavigationYou may want to do several navigation in relation to the words, such as:WORD - WORD consists of a sequence of non-blank characters, separated with white space.word - word consists of a sequence of letters, digits and underscores. Example to show t

49、he difference between WORD and word10. 显示不匹配全部模式的行语法:grep -v -e "pattern" -e "pattern"创建如下例子文件$ cat test-file.txtabcd$ grep -v -e "a" -e "b" -e "c" test-file.txtd11.用grep -c 统计匹配的行数语法:grep -c "pattern" filename$ grep -c "go" demo_

50、text6统计不匹配的行数$ grep -v -c this demo_file412. 用grep -l 只显示文件名$ grep -l this demo_*demo_filedemo_file113. 只显示匹配的字串缺省显示匹配字串的所在行,可以使用-o选项只显示匹配的字串。这项功能当使用正则表达式时比较有用处。$ grep -o "is.*line" demo_fileis line is the 1st lower case lineis lineis is the last line14. 显示匹配的位置语法:grep -o -b "pattern&

51、quot; file$ cat temp-file.txt1234512345$ grep -o -b "3" temp-file.txt0:36:3注意:以上输出显示的不是行内的位置,而是整个文件中的字节byte位置15. 用grep -n 在输出时显示行号行号从1开始$ grep -n "go" demo_text5: * e - go to the end of the current word.6: * E - go to the end of the current WORD.7: * b - go to the previous (befor

52、e word.8: * B - go to the previous (before WORD.9: * w - go to the next word.10: * W - go to the next WORD.sed 's/stringa1/stringa2/g' example.txt 将example.txt文件中的 "string1" 替换成"string2"sed '/$/d' example.txt 从example.txt文件中删除所有空白行sed '/ *#/d; /$/d' exampl

53、e.txt 从example.txt文件中删除所有注释和空白行sed -e '1d' result.txt 从文件example.txt 中排除第一行sed -n '/stringa1/p' 查看只包含词汇 "string1"的行sed -e 's/ *$/' example.txt 删除每一行最后的空白字符sed -e 's/stringa1/g' example.txt 从文档中只删除词汇 "string1" 并保留剩余全部sed -n '1,5p;5q' example

54、.txt 查看从第一行到第5行内容sed -n '5p;5q' example.txt 查看第5行sed -e 's/00*/0/g' example.txt 用单个零替换多个零Sed是一个非交互性文本流编辑器。它编辑文件或标准输入导出的文本拷贝。vi中的正则表达式命令在sed中大多可以通用。sed常用选项-e script 指定sed编辑命令-f scriptfile 指定的文件中是sed编辑命令-n 寂静模式,抑制来自sed命令执行过程中的冗余输出信息,比如只显示那些被改变的行。-iSUFFIX, in-place=SUFFIX 替换和备份源文件edit f

55、iles in place (makes backup if extension supplied1.参数p: 打印匹配行$ sed -n 2p/etc/passwd打印出第2行$ sed -n 1,3p/etc/passwd打印出第1到第3行$ sed -n $p/etc/passwd打印出最后一行$ sed -n /user/p /etc/passwd 打印出含有user的行$ sed -n /$/p /etc/passwd 打印出含有$元字符的行,$意为最后一行$ sed -n $= ok.txt 打印总行数2.参数a和i: 插入文本和附加文本(插入新行$ sed -n /FTP/p/e

56、tc/passwd打印出有FTP的行$ sed /FTP/a 456 /etc/passwd 在含有FTP的行后面新插入一行,内容为456$ sed /FTP/i 123 /etc/passwd在含有FTP的行前面新插入一行,内容为123$ sed /FTP/i “123/etc/passwd在含有FTP的行前面新插入一行,内容为”123$ sed 5 a 123 /etc/passwd 在第5行后插入一新行,内容为123$ sed 5i “12345/etc/passwd 在第5行前插入一新行,内容为”123表达式45sed-搜索结果后面增加嵌入代码#!/bin/bash#export LA

57、NG=zh_CNfind ./ -name “*.html” -exec grep 10000008/100 ; -exec sed -i/10000008/a<!-# include file="/code.html"-> ;#3.参数d:删除文本删空格sed -i s/*/g /$cid.txt删除无内容空行sed -i /$/d /$cid.txt删第一行sed -i 1d /$cid.txt删前两行sed -i 1,2d /$cid.txtdel多行cat SCTP.log |sed 1d;2d;$d删除最后一行:sed -e $d file删除最后两行:sed N;$!P;D filesed N;$!P;$!D;$d file删除最后n行:$vi sed.sh$!/bin/bashA=$(sed -n $= filesed $($A-n+1,$Ad file$ sed /user/d/etc/passwd删除带有string的行(del include love row headsed -i /

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论