zoukankan      html  css  js  c++  java
  • Linux 学习笔记之超详细基础linux命令 Part 10

    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 的大小输出,

    不然目录大小中会有 KM 等字样,会造成排序不正确。

     

    文件归档与压缩

    归档与压缩文件的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

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


  • 相关阅读:
    Servlet Filter
    Analysis of Web.xml in Hello1 project
    Introduction of Annotation
    Analysis of container and Injection in Java, their history and future.
    Maven + Glassfish 实现hello
    java Building tool
    HTTP RFC(自学心得)
    URI和URL
    Git 版本控制
    Oracle RAC 全局等待事件 gc current block busy 和 gc cr multi block request 说明--转载(http://blog.csdn.net/tianlesoftware/article/details/7777511)
  • 原文地址:https://www.cnblogs.com/shouke/p/10158182.html
Copyright © 2011-2022 走看看