Linux学习笔记之超详细基础linux命令

by:授客 QQ1033553122

---------------------------------接Part 9------------------------------

find命令

方法:find
[路径]
[选项]

表达式

说明:从指定路径开始向下搜素满足表达式的文件或目录,不指定目录路径时查找当前目录。当查找到用户不具有执行权限的目录时,屏幕将显示“权限不够”等提示信息。

主要表达式:

-name
文件名

按文件名查找,可使用通配符

-group
组群名

查找文件的所属组群为指定组群的文件

-user
用户名

查找文件所有者为指定用户的文件

-type
文件类型

按照文件类型查找,其中d为目录文件,l为符号链接文件

-size
[+][-]文件大小

查找指定大小的文件

例子:查找/etc目录中以“fs”开头的文件和目录[root权限]

[laiyu@localhost ~]$ find
/etc -name fs*

find: `/etc/dhcp':
Permission denied

find: `/etc/sudoers.d':
Permission denied

find: `/etc/sssd':
Permission denied

...

[laiyu@localhost ~]$
su

Password:

[root@localhost laiyu]#
find /etc -name fs*

/etc/fstab

例子:查找当前目录中的所有符号链接文件

[laiyu@localhost ~]$ find
-type l

./.xinputrc

./.kde/cache-localhost.localdomain

./.kde/tmp-localhost.localdomain

./.kde/socket-localhost.localdomain

./.pulse/8811a723d421b24ed1412d7e00000033-runtime

./file.lnk

注意:find命令将显示满足条件的所有文件,包括隐藏文件和隐藏目录。

例子:查找当前目录中所有大于10kb的文件和目录[反之:-10k则表示小于10kb]

[laiyu@localhost ~]$ find
-size +10k

./.mozilla/firefox/scrsbbvc.default/cesessions/1351781697741.js

./.mozilla/firefox/scrsbbvc.default/cesessions/1352521543586.js

...

#Debain下测试

例子:在当前目录下查找名为fil2的文件,排除对当前目录下的fil目录的搜索

builder:~# ls

fil  myfile

builder:~# ls -R
myfile/   
#说明,-R必须大写

fil2

builder:~# find . -path
./fil -prune -o -name file2 -print

builder:~# find . -path
./fil -prune -o -name fil2 -print

./myfile/fil2

builder:~# find . -path
./fil -prune -o -name fil2 -print

例子:在当前目录下查找名为tes的文件或目录,排除对Picutre目录的搜索

[root@localhost ~]# find .
-path ./Pictures -prune -o -name tes -print

./tes

./tes/tes

例子:在当前目录下查找名为tes的文件或目录,排除对tes目录下的tes目录的搜索

[root@localhost ~]# find .
-path ./tes/tes -prune -o -name tes -print

./tes/tes

例子:在当前目录下查找名为fil2的文件,排除对当前目录下的fil以及file目录的搜索

builder:~# ls

fil  file  myfile

builder:~# find . \(-path
./fil -o -path ./file \) -prune -o -name fil2 -print

find: invalid ex pression;
you have used a binary operator '-o' with nothing before
it.

builder:~# find . \( -path
./fil -o -path ./file \) -prune -o -name fil2 -print

./myfile/fil2

说明:\(  \)
->(),,,注意括号和-path有空格分开

grep命令

方法:grep
[选项]

字符串
文件列表

功能:从指定的文本文件或标准输出中查找符合条件的字符串,默认显示其所在行的内容

主要选项:

-n(number)

显示行号

-v(invert)

显示不包含指定字符串的行

-i(ignore)

查找时不区分大小写

例子:查找/etc/passwd文件中包含“laiyu”的行,并显示其行号

[laiyu@localhost ~]$ grep
-n laiyu /etc/passwd

37:laiyu:x:500:500:laiyu:/home/laiyu:/bin/bash

du命令

方法:du
[选项]
[目录|文件]

功能:显示目录或文件大小,默认以KB为单位,参数为目录,默认递归显示指定目录及其所有子目录的大小

主要选项:

-a(all)

显示指定目录及其所有子目录和文件的大小,默认只显示目录的大小

-h(human)

以易读方式显示目录或文件的大小

-s(summarize)

只显示指定目录的大小,而不显示其子目录的大小

-b

以字节为单位列出磁盘空间使用情况

-k
以1024字节为单位列出磁盘空间使用情况。

-c
最后再加上一个总计(系统缺省设置)。

-l
计算所有的文件大小,对硬链接文件,则计算多次。

-x
跳过在不同文件系统上的目录不予统计。

[laiyu@localhost ~]$ du -sh
/home/laiyu

53M   
/home/laiyu

小知识: du
/etc | sort -nr | more

sort
的参数 -nr
表示要以数字排序法进行反向排序,因为我们要对目录大小做排序,所以不可以使用 human-readable

的大小输出,

不然目录大小中会有 K、M

等字样,会造成排序不正确。

文件归档与压缩

归档与压缩文件的Shell命令

1.tar命令

格式:tar
[选项]

归档/压缩文件
[目录或文件列表]

功能:将多个文件或目录归档为tar文件,如果使用相关选项还可压缩归档文件。

备注:建议使用tar归档时,让归档文件中包含一个子目录,解压归档文件时,子目录会被产生,所有文件都会放在这个目录里。也就是说把

所有文件都放到一个子目录下,然后归档该子目录

备注:tar会把文件的拥有者和权限存在备份文件中,并且保留完整的目录结构,符号链接,物理链接,所以使用tar可以说是在同一操作系统

上拷贝或者搬移整个树状目录的最好方法。

主要选项说明:

-c(create) 
创建归档压缩文件

-r 
向归档/压缩文件追加文件和目录

-t(list) 
显示归档/压缩文件的内容

-u(update)

更新归档或压缩文件

-x(extract)

还原归档或压缩文件中的文件和目录

-v(verbose)

显示命令的执行过程(可以同时用两个v选项,以显示更多的信息)

-z(gzip)

采用gzip方式压缩/解压缩归档文件

-j
采用bzip2方式压缩/解压缩归档文件

-f tar命令的必需选项

例:将/ecc目录下的所有conf文件归档为etc.tar文件

[laiyu@localhost ~]$ tar
-cf etc.tar /etc/*.conf

tar: Removing leading `/'
from member names

tar:
/etc/autofs_ldap_auth.conf: Cannot open: Permission
denied

tar: /etc/libaudit.conf:
Cannot open: Permission denied

tar: /etc/sudo-ldap.conf:
Cannot open: Permission denied

tar: Exiting with failure
status due to previous errors

[laiyu@localhost ~]$ ls |
grep etc

etc

etc.tar

例子:etc.tar中添加文件

[laiyu@localhost ~]$ tar
-rf etc.tar testfile

例子:将/etc目录下的所有conf文件归档为etc.tar.gz文件

[laiyu@localhost ~]$ tar
-cf etc.tar.gz /etc/*.conf

tar: Removing leading `/'
from member names

tar:
/etc/autofs_ldap_auth.conf: Cannot open: Permission
denied

tar: /etc/libaudit.conf:
Cannot open: Permission denied

tar: /etc/sudo-ldap.conf:
Cannot open: Permission denied

tar: Exiting with failure
status due to previous errors

[laiyu@localhost ~]$ ls |
grep etc

etc

etc.tar

etc.tar.gz

例子:将文件file归档压缩为file.tgz文件

[laiyu@localhost ~]$ tar
-czvf file.tgz file

例子:查看etc/tar.gz文件的内容

[laiyu@localhost ~]$ tar
-tf etc.tar.gz   
#注意这里的f选项不可少

etc/asound.conf

etc/cas.conf

etc/cgconfig.conf

etc/cgrules.conf

etc/cgsnapshot_blacklist.conf

etc/dnsmasq.conf

...

etc/yum.conf

...

注:归档/压缩操作时,系统会保留文件和目录的路径,并将绝对路径变为相对路径

例:将etc.tar文件中的yum.conf文件还原到当前目录

[laiyu@localhost ~]$ tar
-xf etc.tar etc/yum.conf

tar: etc/yum.conf: Cannot
open: File exists

tar: Exiting with failure
status due to previous errors

[laiyu@localhost ~]$ tar
-xf etc.tar test/yum.conf

tar: test/yum.conf: Not
found in archive

tar: Exiting with failure
status due to previous errors

[laiyu@localhost ~]$ cd
test

[laiyu@localhost test]$
mkdir etc

[laiyu@localhost test]$
ls

etc  file  file1  linux

[laiyu@localhost test]$ tar
-xf etc.tar etc/yum.conf

tar: etc.tar: Cannot open:
No such file or directory

tar: Error is not
recoverable: exiting now

[laiyu@localhost test]$ mv
../etc.tar .

[laiyu@localhost test]$
ls

etc  etc.tar  file  file1  linux

[laiyu@localhost test]$ tar
-xf etc.tar etc/yum.conf

[laiyu@localhost test]$ ls
| grep etc

etc

etc.tar

[laiyu@localhost test]$ ls
etc/

yum.conf

说明:由于进行归档/压缩操作采用的是相对路径,所以还原某个文件时必须使用相对路径。

1.tar: etc/yum.conf: Cannot
open: File exists出现该错误的原因是源目录的权限问题,比如源目录是root创建的,其它用户没写的权限,而当前操作者是普通用户laiyu,因为其他用户没写的权限,这样一来,写操作就没权限执行了。

2.如果你想把压缩文件解压到某个目录下,则先进入该目录,然后tar
[选项]

压缩/归档文件

或tar
[选项]

压缩/归档文件

压缩/归档文件中的首目录

(注:这里的首目录形如上面例子中的etc,可以是已经存在的或不存在的)或者
tar
[选项]

压缩/归档文件

压缩/归档文件中的首目录/文件名,总之,形式要和压缩包对应

也就是说,你创建时文件名采用了路径的形式,如tar -cf etc.tar
/etc/*,那么当你解压时,可以这样:tar
-xf etc.tar
或 tar
-xf etc.tar etc
或 tar
-xf etc.tar etc/yum.conf

注意:etc前不能加/,,加了则变成根目录下的etc目录了,为绝对路径

例子:将etc.tar.gz文件的所有文件还原到/tmp目录

[laiyu@localhost ~]$ tar
-xzf etc.tar.gz

gzip: stdin: not in gzip
format 
说明这貌似因为创建时使用tar -cf
,没用tar
-czf,,,所以解压时也不能用

tar: Child returned status
1

tar: Error is not
recoverable: exiting now

[laiyu@localhost ~]$ cd
/tmp

[laiyu@localhost tmp]$
pwd

/tmp

[laiyu@localhost tmp]$ tar
-xvf ../home/laiyu/etc.tar.gz

etc/asound.conf

etc/cas.conf

...

[laiyu@localhost ~]$ cd
/tmp/

[laiyu@localhost tmp]$ ls |
grep etc

etc

[laiyu@localhost tmp]$ ls
etc/

asound.conf            
   krb5.conf                    
prelink.conf

cas.conf                  
latrace.conf                 
readahead.conf

cgconfig.conf             
ld.so.conf                   
reader.conf

cgrules.conf              
libuser.conf                 
request-key.conf

cgsnapshot_blacklist.conf 
logrotate.conf               
resolv.conf

dnsmasq.conf              
ltrace.conf                  
rsyslog.conf

dracut.conf               
mke2fs.conf                  
sestatus.conf

elinks.conf               
mtools.conf  
                smartd.conf

fprintd.conf              
nfsmount.conf                
sos.conf

gai.conf                  
nsswitch.conf                
sysctl.conf

grub.conf                 
ntp.conf                     
Trolltech.conf

gssapi_mech.conf    
      numad.conf                   
updatedb.conf

host.conf                 
oddjobd.conf                 
warnquota.conf

idmapd.conf               
openct.conf                  
yp.conf

kdump.conf                
pm-utils-hd-apm-restore.conf  yum.conf

说明:也就是说要解压到哪个目录就先进入到那个目录,然后解压

Linux 学习笔记之超详细基础linux命令 Part 10的更多相关文章

  1. Linux 学习笔记之超详细基础linux命令(the end)

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 14---------------- ...

  2. Linux 学习笔记之超详细基础linux命令 Part 14

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 13---------------- ...

  3. Linux 学习笔记之超详细基础linux命令 Part 13

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 12---------------- ...

  4. Linux 学习笔记之超详细基础linux命令 Part 12

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 11---------------- ...

  5. Linux 学习笔记之超详细基础linux命令 Part 11

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 10---------------- ...

  6. Linux 学习笔记之超详细基础linux命令 Part 9

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 8----------------- ...

  7. Linux 学习笔记之超详细基础linux命令 Part 8

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 7----------------- ...

  8. Linux 学习笔记之超详细基础linux命令 Part 7

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 6----------------- ...

  9. Linux 学习笔记之超详细基础linux命令 Part 6

    Linux学习笔记之超详细基础linux命令 by:授客 QQ:1033553122 ---------------------------------接Part 5----------------- ...

随机推荐

  1. js中的柯里化

    概述 今天查询事件绑定资料的时候偶然遇到了柯里化这个词,很感兴趣,于是记录下来供以后开发时参考,相信对其他人也有用. 定义 柯里化是函数式编程里面的术语,它是把接受多个参数的函数变换成接受一个单一参数 ...

  2. redis 实现发布订阅的功能

    redis 除了作为缓存的功能外还可以用作消息中间件的功能,这片博客主要是介绍一下 redis 整合spring 实现消息的发布和订阅功能: 1:redis依赖,依赖两个包,redis 包, spri ...

  3. 从github clone文件: Failed to receive SOCKS4 connect request ack.

    安装了代理,能上网,也能从github上下载文件,就是无法从github上clone文件, 查了很久资料后,终于发现使用sudo可以解决问题.不过,不知道原因是什么? 比如:git clone htt ...

  4. Apple Watch笔记-应用内导航模式

    最近苹果婊上市,水果也发布了Xcode 6.2正式版,WatchKit也可以正常使用了.水果很及时地提供了Apple Watch的开发文档,我也及时地尝试着边学习边开发Watch App. 今天主要想 ...

  5. Intellij-忽略其他编译错误,运行当前文件

    在使用Intellij IDEA的时候,有时候想在项目中写个main()方法或者单元测试测试个功能,但是有其他文件编译出错IDEA就提示我说不能运行,很是烦恼. 能不能像Eclipse一样呢,其他编译 ...

  6. mysql 开发进阶篇系列 27 数据库字符集设置

    在安装完数据库后,使用汉字插入到表中,会报错,需要修改字符集类型,如下图所示: -- 插入汉字时报错 INSERT INTO User2 VALUES('张三') -- 查看字符集 SHOW VARI ...

  7. CSRF理解和实战

    目录 啥是CSRF攻击 写一个CSRF攻击 如何避免CSRF攻击 啥是CSRF攻击 CSRF(Cross-site request forgery)跨站请求伪造,CSRF通过伪装来自受信任用户的请求来 ...

  8. js禁止鼠标右键功能

    1.禁止指定元素 document.getElementById("active-intro").oncontextmenu = function () { event.retur ...

  9. C#对象内部属性排序测试

    构建对象: class SortGrid { int indexI; int indexJ; public SortGrid(int x, int y) { indexI = x; indexJ = ...

  10. Jenkins CLI 命令详解

    笔者在前文<通过 CLI 管理 Jenkins Server>中介绍了如何通过 SSH 或客户端命令行的方式管理 Jenkins Server,限于篇幅,前文主要的目的是介绍连接 Jenk ...