打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
诚之和:RHEL6中文件基本操作命令有哪些

这篇文章主要介绍了RHEL6中文件基本操作命令有哪些,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

文件(夹)查看类命令

ls--显示指定目录下内容

说明:ls 显示结果以不同的颜色来区分文件类别。蓝色代表目录,灰色代表普通文件,绿色代表可执行文件,红色代表压缩文件,浅蓝色代表链接文件。

-a---显示所有内容,包括隐藏文件

说明:在Linux系统中,以“.”开头的就是隐藏文件或隐藏目录。

-l---以长格式(内容更详细)显示文件或目录的详细信息。

说明:ls -l命令可以简写成ll,

输出的信息共分为7组:
文件类别和文件权限、链接数或子目录个数、文件所有者、文件所属组、文件大小(单位为字节B)、文件创建或修改时间、文件名。

文件类别:第一组前1位表示文件类别,“-”代表普通文件,“d”代表目录,“l”代表符号链接,“c”代表字符设备,“b”代表块设备
文件权限:第一组后9位表示文件权限,前3位为user、中间3位为group、后三位为other的权限

-d---显示目录本身的属性而不是目录中的内容。

[root@localhost ~]# ls -ld /homedrwxr-xr-x. 4 root root 4096 9月  22 10:41 /home
[root@localhost ~]# ls -d /home/home
[root@localhost ~]#

-h---以K、M、G等单位显示文件大小(默认为字节)

[root@localhost ~]# ls -h /homejustin  lost+found[root@localhost ~]# ls -lh /home总用量 20Kdrwx------. 27 justin justin 4.0K 9月  22 13:19 justindrwx------.  2 root   root    16K 9月  18 15:30 lost+found[root@localhost ~]#

-R---若目录下有档案,也将档案依序列出

[root@localhost ~]# ls -lR /home/home:总用量 20drwx------. 27 justin justin  4096 9月  22 13:19 justindrwx------.  2 root   root   16384 9月  18 15:30 lost+found/home/justin:总用量 32drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 公共的drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 模板drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 视频drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 图片drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 文档drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 下载drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 音乐drwxr-xr-x. 2 justin justin 4096 9月  22 10:49 桌面/home/justin/公共的:总用量 0/home/justin/模板:总用量 0/home/justin/视频:总用量 0/home/justin/图片:总用量 0/home/justin/文档:总用量 0/home/justin/下载:总用量 0/home/justin/音乐:总用量 0/home/justin/桌面:总用量 0/home/lost+found:总用量 0[root@localhost ~]#

-t---将档案按照建立时间的先后次序列出

[root@localhost ~]# ls -l /home总用量 20drwx------. 27 justin justin  4096 9月  22 13:19 justindrwx------.  2 root   root   16384 9月  18 15:30 lost+found-rw-r--r--.  1 root   root       0 9月  22 15:21 t[root@localhost ~]# ls -lt /home总用量 20-rw-r--r--.  1 root   root       0 9月  22 15:21 tdrwx------. 27 justin justin  4096 9月  22 13:19 justindrwx------.  2 root   root   16384 9月  18 15:30 lost+found[root@localhost ~]#

说明:ls命令还可以结合通配符“?”或“*”一起使用,问号“?”可以匹配文件名中的一个任意字符,而“*”可以匹配文件名中的任意多个字符。这两个通配符同样也适用于Shell环境中的其他大多数命令。

gssapi_mech.conf         popt.d                        xml
gtk-2.0                  portreserve                   yp.conf
hal                      postfix                       yum
host.conf                ppp                           yum.conf
hosts                    prelink.cache                 yum.repos.d
hosts.allow              prelink.conf
hosts.deny               prelink.conf.d
[root@localhost etc]# ll -d /etc/po*.ddrwxr-xr-x. 2 root root 4096 1月  11 2010 /etc/popt.d
[root@localhost etc]# ll -d /etc/po?.dls: 无法访问/etc/po?.d: 没有那个文件或目录
[root@localhost etc]#

du---显示文件或目录大小

-h或--human-readable---以K,M,G为单位,提高信息的可读性

[root@localhost src]# du -h nagios-3.5.0.tar.gz1.8M    nagios-3.5.0.tar.gz[root@localhost src]# du nagios-3.5.0.tar.gz1748    nagios-3.5.0.tar.gz[root@localhost src]#

-a---显示全部目录和其次目录下的每个档案所占的磁盘空间

-b或-bytes---显示目录或文件大小时,以byte为单位

[root@localhost local]# du -b src/nagios-3.5.0.tar.gz1789376 src/nagios-3.5.0.tar.gz
[root@localhost local]#

-c或--total---显示每个目录或文件的大小外,同时也显示所有目录或文件的总和

-m或--megabytes---以1MB为单位
-s---只显示各档案大小的总合

[root@localhost local]# du -sh src/41M src/
[root@localhost local]#

-x---只计算同属同一个档案系统的档案

-L---计算所有的档案大小

df---显示档案系统的状况;主要用来了解系统中已经挂载的各个文件系统的磁盘使用情况

-h 显示更易读的容量单位
-T 显示文件系统的类型

[root@localhost ~]# df -Th文件系统    类型      容量  已用  可用 已用%% 挂载点/dev/sda2ext4    9.9G  2.6G  6.9G  28% /tmpfs        tmpfs    504M  112K  504M   1% /dev/shm/dev/sda1ext4    194M   27M  158M  15% /boot/dev/sda5ext4    7.7G  147M  7.2G   2% /home/dev/sr0iso9660    2.9G  2.9G     0 100% /media/RHEL_6.3 i386 Disc 1[root@localhost ~]#

file---查看文件类型

[root@localhost home]# file justin/justin/: directory
[root@localhost home]# file justin1justin1: empty
[root@localhost ~]# file install.loginstall.log: UTF-8 Unicode text

说明:file命令用于查看文件的类型,可以根据文件的内部存储结构来进行判别,而不根据文件的扩展名来进行判别。在Linux系统中,文件扩展名与文件类型没有绝对的关系。

创建文件名中可以带有空格,但需要用引号引起来,不建议使用空格

文件内容查看

cat---显示文件内容

[root@localhost log]# cat /var/log/messages

说明:cat在显示文本文件的内容时不进行停顿,对于内容较长的文件,在快速滚屏显示之后,只有最后一页的文件内容保留在屏幕中显示,因此cat不适合查看长文件。

more---分页显示文件内容

-num--- 一次显示的行数
-s ---当遇到有连续两行以上的空白行,就代换为一行的空白行
+num ---从第 num 行开始显示

[root@localhost log]# more -5s /var/log/messagesSep 22 10:36:06 localhost kernel: imklog 5.8.10, log source = /proc/kmsg started.Sep 22 10:36:06 localhost rsyslogd: [origin software="rsyslogd" swVersion="5.8.10" x-pid="1323" x-info="http://www.rsyslog.com"] startSep 22 10:36:06 localhost kernel: Initializing cgroup subsys cpusetSep 22 10:36:06 localhost kernel: Initializing cgroup subsys cpu--More--(0%)

说明:类似 cat ,不过会以一页一页的显示方便使用者逐页阅读,空白键(space):显示下一页,b 键:显示上一页,q 键 :退出

less----分页显示文件内容

说明:less命令的用法与more命令类似,它们之间的区别是当文件内容显示到文件尾时,more命令会自动退出阅读环境,而less命令不自动退出,用户仍然可以利用上下键来卷动文件,这样更加有利于对文件内容的反复阅读。当要结束浏览时,要在less命令的提示符“:”后按Q键退出

head---查看文件开头部分的内容

-n 指定显示的具体行数。

[root@localhost ~]# head -5 /etc/passwdroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinlp:x:4:7:lp:/var/spool/lpd:/sbin/nologin[root@localhost ~]#

说明:默认情况下,head显示前10行内容

tail---查看文件末尾部分的内容

[root@localhost ~]# tail -5 /etc/passwdnfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologinabrt:x:173:173::/etc/abrt:/sbin/nologinsshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologintcpdump:x:72:72::/:/sbin/nologinjustin:x:500:500:justin_peng:/home/justin:/bin/bash[root@localhost ~]#

说明:tail命令用法与head命令类似

paste---用于合并文件的列,将多行合并为一行

-d<间隔字符>或--delimiters=<间隔字符>  用指定的间隔字符取代跳格字符。

-s或--serial  将一个文件中的多行数据合并为一行进行显示。只是将文件的内容调整显示方式,并不会改变原文件的内容格式。

[root@localhost ~]# cat > 1.txt << EOF> 1> 11> 111> EOF[root@localhost ~]# cat > 2.txt << eof> 2> 22> 222> eof[root@localhost ~]# cat > 3.txt << eof> 3> 33> 333> eof[root@localhost ~]# paste 1.txt 2.txt 3.txt 123112233111222333[root@localhost ~]#
[root@localhost ~]# cat > test.txt << EOF> 001> 002> 003> 004> 005> 006> 007> EOF[root@localhost ~]# paste -d " " -s < test.txt 001 002 003 004 005 006 007[root@localhost ~]# cat test.txt 001002003004005006007[root@localhost ~]# cat test.txt | paste -d " " -s >> test1.txt[root@localhost ~]# cat test1.txt 001 002 003 004 005 006 007[root@localhost ~]#

创建文件(夹)命令

touch---改变档案的时间记录,档案不存在则创建一个空文件,通常用来创建一个文件

[root@localhost home]# touch file1 file2[root@localhost home]# lsfile1  file2  justin  lost+found  t
[root@localhost home]#
[root@justin touch]# touch file{1..5}[root@justin touch]# lsfile1  file2  file3  file4  file5
[root@justin touch]# touch file{6...9}[root@justin touch]# lsfile1  file2  file3  file4  file5  file{6...9}
[root@Zabbix zabbix]# mkdir dir{1..5}[root@Zabbix zabbix]# lsdir1  dir3  dir5   file10  file12  file14  file16  file18  file2   file3  file5  file7  file9
dir2  dir4  file1  file11  file13  file15  file17  file19  file20  file4  file6  file8
[root@Zabbix zabbix]# touch dir{1..5}/file{1..20}[root@Zabbix zabbix]# lsdir1  dir3  dir5   file10  file12  file14  file16  file18  file2   file3  file5  file7  file9
dir2  dir4  file1  file11  file13  file15  file17  file19  file20  file4  file6  file8
[root@Zabbix zabbix]#

说明:在实际使用中经常用于创建新的测试文件。使用文件名作为参数,可以同时创建多个文件,切中间只能是两个点号。当目标文件已经存在时,将更新该文件的时间标记,否则将创建指定名称的空文件。

-a   或--time=atime或--time=access或--time=use  只更改存取时间。

-m   或--time=mtime或--time=modify  只更改变动时间。

-c   或--no-create  假如目的档案不存在,不会建立新的档

-r  把指定文档或目录的日期时间,统统设成和参考文档或目录的日期时间相同。

-t  使用指定的日期时间,而非现在的时间。    

     [[CC]YY]MMDDhhmm[.SS]     

      这里,CC为年数中的前两位,即”世纪数”;YY为年数的后两位,即某世纪中的年数.如果不给出CC的值,则touch   将把年数CCYY限定在1969--2068之内.MM为月数,DD为天将把年数CCYY限定在1969--2068之内.MM为月数,DD为天数,hh 为小时数(几点),mm为分钟数,SS为秒数.此处秒的设定范围是0--61,这样可以处理闰秒.这些数字组成的时间是环境变量TZ指定的时区中的一个时 间.由于系统的限制,早于1970年1月1日的时间是错误的。

[root@localhost touch]# dateSat Jan 14 10:54:16 CST 2017[root@localhost touch]# touch test1.txt[root@localhost touch]# touch -t 201610141053 test2.txt [root@localhost touch]# lltotal 0-rw-r--r-- 1 root root 0 Jan 14 10:54 test1.txt-rw-r--r-- 1 root root 0 Oct 14 10:53 test2.txt[root@localhost touch]# touch -r test1.txt test2.txt [root@localhost touch]# lltotal 0-rw-r--r-- 1 root root 0 Jan 14 10:54 test1.txt-rw-r--r-- 1 root root 0 Jan 14 10:54 test2.txt[root@localhost touch]#

mkdir---Make Directory---创建新的目录

-p 确保目录名称存在,不存在的就建一个。

[root@localhost home]# lsjustin  lost+found  t
[root@localhost home]# mkdir dir1[root@localhost home]# mkdir dir2/dirmkdir: 无法创建目录"dir2/dir": 没有那个文件或目录
[root@localhost home]# mkdir -p dir2/dir[root@localhost home]#

同时创建多级目录

[root@localhost home]# lsjustin  lost+found  t
[root@localhost home]# mkdir -p {dir1,dir2/{dir3,dir4}}[root@localhost home]# lsdir1  dir2  justin  lost+found  t
[root@localhost home]# ls dir2dir3  dir4
[root@localhost home]#

删除文件(夹)命令

rmdir---删除空目录(若目录非空删除中会报错)

-p 当子目录被删除后也成为空目录的话,则顺便一并删除

[root@localhost home]# mkdir -p {dir1,dir2/dir3}[root@localhost home]# lsdir1  dir2  justin  lost+found  t
[root@localhost home]# rmdir dir1[root@localhost home]# rmdir dir2rmdir: 删除 "dir2" 失败: 目录非空
[root@localhost home]# rmdir -p dir2/dir3/[root@localhost home]# lsjustin  lost+found  t
[root@localhost home]#

rm---删除档案或目录

文件的删除权限是由上级目录决定的,即使一个用户有该文件的删除权限,没有上次目录的写权限也是无法删除该文件

-i 删除前逐一询问确认。

-v 显示删除过程
-f 即使原档案属性设为唯读,亦直接删除,无需逐一确认。
-r 将目录及以下档案也逐一删除。

[root@localhost home]# mkdir -p dir1/dir2[root@localhost home]# rm -r dir1/rm:是否进入目录"dir1"? y
rm:是否删除目录 "dir1/dir2"?y
rm:是否删除目录 "dir1"?y
[root@localhost home]#

Linux 删除除某个文件之外的所有文件

[root@APP rm]# lssda  SDa  sdaa  SDaa  sdaaa  sdaaaa  SDaaaa  sdaab  sdaz
[root@APP rm]# ls |grep -v SD|xargs rm -f[root@APP rm]# lsSDa  SDaa  SDaaaa
[root@APP rm]#

还有一种更为方便的方法,需要开启BashShell的extglob选项,

[root@APP rm]# touch sda  SDa  sdaa  SDaa  sdaaa  sdaaaa  SDaaaa  sdaab  sdaz[root@APP rm]# rm -f !(SD*)-bash: !: event not found
[root@APP rm]# shopt -s extglob[root@APP rm]# rm -rf !(SD)[root@APP rm]# lsSDa  SDaa  SDaaaa
[root@APP rm]# rm -f !(a|b)            #保留a和b

如果出现-bash: !: event not found表示当前系统未开启,

                  列出所有选项:shopt -p 

                  列出所有打开的选项:shopt -s 

                  开启extglob命令:shopt -s extglob 

                  列出所有关闭的选项:shopt -u 

                  关闭extglob命令:shopt -u extglob

开启之后,以下5个模式匹配操作符将被识别:

?(pattern-list) - 所给模式匹配0次或1次; 

*(pattern-list) - 所给模式匹配0次以上包括0次;

+(pattern-list) - 所给模式匹配1次以上包括1次; 

@(pattern-list) - 所给模式仅仅匹配1次;

!(pattern-list) - 不匹配括号内的所给模式。 !():与@()相反

-bash: /bin/rm: Argument list too long

[root@mail cur]# rm -rf *-bash: /bin/rm: Argument list too long
[root@mail new]# ll |wc -l221993[root@mail cur]# ls | xargs -n 10 rm -fr ls[root@mail new]# ll |wc -l1[root@mail new]#

文件数达到21万多,rm -f的参数过多,输出所有的文件名(用空格分割) xargs就是将ls的输出,每10个为一组(以空格为分隔符),作为rm -rf的参数也就是说将所有文件名10个为一组,由rm -rf删除

linux中删除文件名带横杠的文件的方法

rm  -filename (其中“-filename”为文件名)这时系统会报错,提示:无效的选项。这是因为shell把“-”后的文件名当做前面rm命令的参数选项了。

解决方法:

方法一:在文件名之前加上“--”,

如:rm -- -filename

这是因为shell把--之后的参数当做文件名来对待,禁止把-filename当做参数选项来解释,所以能够正确的执行shell命令。 

注:可以在shell命令行中输入man sh来参考shell对“--”的说明。

方法二:按照i节点号删除

root@localhost node_api]# ls -i 403627443 config   16073267 -g
[root@localhost node_api]# find ./ -inum 16073267 -exec rm {} \;[root@localhost node_api]# lsconfig
[root@localhost node_api]#

删除大量文件的方法

删除数万或者数十万甚至数百万的文件时使用rm -rf *要等待很长一段时间通过rsync的是替换原理,处理数十万个文件也是秒删。

rsync提供了一些跟删除有关的参数:
rsync --help | grep delete
    --del                                an alias for --delete-during
    --delete                          delete extraneous files from destination dirs
    --delete-before            receiver deletes before transfer, not during   接收者在传输之前进行删除操作
    --delete-during            receiver deletes during transfer (default)
    --delete-delay              find deletions during, delete after
    --delete-after                receiver deletes after transfer, not during
    --delete-excluded        also delete excluded files from destination dirs
    --ignore-errors            delete even if there are I/O errors
    --max-delete=NUM    don't delete more than NUM files

例如:/opt/data目录下有好几个目录,目录里总共有几千万个大小几kb的小文件,需要删除

1、创建一个空的目录:mkdir /app/tmp

2、用rsync删除目标目录:rsync --delete-before -d -a -H -v --progress --stats /app/tmp/ /opt/data/

–delete-before 接收者在传输之前进行删除操作
–progress          在传输时显示传输过程
-a                      归档模式,表示以递归方式传输文件,并保持所有文件属性
-H                      保持硬连接的文件
-v                      详细输出模式
–stats                给出某些文件的传输状态
-d                      transfer directories without recursing

例如:日志文件/app/logs/log.txt有300多G,现在要删除

1、创建一个空文件:touch /app/log
2、用rsync清空/root/data文件: rsync --delete-before -d --progess --stats /app/log /app/logs/log.txt
注意:
当SRC和DEST文件性质不一致时将会报错
当SRC和DEST性质都为文件【f】时,意思是清空文件内容而不是删除文件
当SRC和DEST性质都为目录【d】时,意思是删除该目录下的所有文件,使其变为空目录

修改文件(夹)名:
        rename
            格式:rename [options] expression replacement file...
                  rename [options] 被替换字符串 被替换字符串 被替换字符串
            参数:
                -v, --verbose    详细模式:打印成功更改的文件名列表
                -n, --no-act    测试模式:并不真正的执行命令,而只是显示哪些文件名应该怎么进行更改,用于测试模式。
                -f, --force     强制模式:在更改文件名,如果更改后的文件已经存在时覆盖已经存在的文件
            实例:

[root@localhost rename]# ls111222  111.sh  111.txt  222.txt  333.txt
[root@localhost rename]# rename 111 aaa *     将所有的文件(夹)中的111替换成aaa[root@localhost rename]# ls222.txt  333.txt  aaa222  aaa.sh  aaa.txt
[root@localhost rename]# rename '.txt' '.sh' *   将所有的以.txt的后缀替换成.sh[root@localhost rename]# ls222.sh  333.sh  aaa222  aaa.sh
[root@localhost rename]# rename \.sh '' *         去掉所有的.sh后缀[root@localhost rename]# ls222  333  aaa  aaa222
[root@localhost rename]#

清空或删除大文件内容的方法

1、使用重定向的方法

[root@localhost ~]# dd if=/dev/zero of=test bs=1M count=1024[root@localhost ~]# du -sh test 1.1Gtest
[root@localhost ~]# > test [root@localhost ~]# du -sh test 0test
[root@localhost ~]#

2、使用true命令重定向清空文件

[root@localhost ~]# dd if=/dev/zero of=test bs=1M count=10241024+0 records in1024+0 records out1073741824 bytes (1.1 GB) copied, 19.9467 s, 53.8 MB/s
[root@localhost ~]# du -sh test 1.1Gtest
[root@localhost ~]# true > test [root@localhost ~]# du -sh test 0test
[root@localhost ~]#

3、使用echo命令清空文件

[root@localhost ~]# dd if=/dev/zero of=test bs=1M count=10241024+0 records in1024+0 records out1073741824 bytes (1.1 GB) copied, 22.563 s, 47.6 MB/s
[root@localhost ~]# echo "" > test [root@localhost ~]# du -sh test 4.0Ktest
[root@localhost ~]# echo -n "" > test #要加上"-n"参数,默认情况下会"\n",也就是回车符[root@localhost ~]# du -sh test 0test
[root@localhost ~]#

4、使用truncate命令清空文件

[root@localhost ~]# dd if=/dev/zero of=test bs=1M count=10241024+0 records in1024+0 records out1073741824 bytes (1.1 GB) copied, 22.0746 s, 48.6 MB/s
[root@localhost ~]# du -sh test 1.1Gtest
[root@localhost ~]# truncate -s 0 test #-s参数用来设定文件的大小,清空文件,就设定为0;[root@localhost ~]# du -sh test 0test
[root@localhost ~]#
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
linux下文件夹操作命令
云计算学习路线课程大纲资料:参数传递 Xargs
linux常用命令
Linux基础之—实例解说目录/文件命令
Linux 文件和目录管理之列出、删除、复制、移动及改名
linux下文件的特殊权限s和t
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服