1、Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。
1、目录管理类的命令:
mkdir:创建目录
常用参数:
-p: 自动按需创建父目录
-v: 显示详细过程
-m MODE:直接给定权限
[[email protected] ~]# mkdir -pv -m 600 /tmp/a/b mkdir: created directory ‘/tmp/a’ mkdir: created directory ‘/tmp/a/b’ [[email protected] ~]# ls -ld /tmp/a/b drw-------. 2 root root 6 May 7 21:14 /tmp/a/b
rmdir:删除空目录
常用参数:
-p:删除某目录后,如果其父目录为空,则一并删除
-v: 显示详细过程
[[email protected] ~]# rmdir -pv /tmp/a/b rmdir: removing directory, ‘/tmp/a/b’ rmdir: removing directory, ‘/tmp/a’ rmdir: removing directory, ‘/tmp’ rmdir: failed to remove directory ‘/tmp’: Device or resource busy
2、文件管理类命令:
cp:复制文件
单源复制:cp [OPTION]... [-T] SOURCE DEST
如果DEST不存在:则事先创建此文件,并复制源文件的数据流至DEST中
如果DEST存在:如果DEST是非目录文件,则覆盖目标文件;如果DEST是目录文件,则先在DEST目录下创建一个与源文件同名的文件,并复制其数据流
[[email protected] ~]# mkdir /tmp/src /tmp/dst [[email protected] ~]# touch /tmp/src/file{1,2,3} [[email protected] ~]# ls /tmp/src/ /tmp/dst /tmp/dst: /tmp/src/: file1 file2 file3 [[email protected] ~]# cp /tmp/src/file1 /tmp/dst/ [[email protected] ~]# ls /tmp/dst/ file1 [[email protected] ~]# cp /tmp/src/file2 /tmp/dst/file1 cp: overwrite ‘/tmp/dst/file1’? y [[email protected] ~]# ls /tmp/dst/ file1 [[email protected] ~]# cp /tmp/src/file2 /tmp/dst/ [[email protected] ~]# ls /tmp/dst/ file1 file2
多源复制:cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...
如果DEST不存在:错误
如果DEST存在:如果DEST是非目录文件,错误;如果DEST是目录文件,分别复制每个文件至目标目录中,并保持原名
注:多源复制仅支持DEST为目录文件
[[email protected] ~]# cp /tmp/src/* /tmp/dst1/ cp: target ‘/tmp/dst1/’ is not a directory [[email protected] ~]# cp /tmp/src/* /tmp/dst/file cp: target ‘/tmp/dst/file’ is not a directory [[email protected] ~]# cp /tmp/src/* /tmp/dst/ cp: overwrite ‘/tmp/dst/file1’? y cp: overwrite ‘/tmp/dst/file2’? y [[email protected] ~]# ls /tmp/dst file1 file2 file3
常用参数:
-i:交互式,即覆盖之前提醒用户确认
注:root用户默认使用添加-i参数的别名命令,普通用户默认使用命令本身,root用户也想使用命令本身需在命令前增加"\"
[[email protected] ~]# alias alias cp=‘cp -i‘
-f:强制覆盖目标文件
[[email protected] ~]# echo "123" > /tmp/src/file1 [[email protected] ~]# cp -f /tmp/src/file1 /tmp/dst/file1 cp: overwrite ‘/tmp/dst/file1’? y [[email protected] ~]# \cp -f /tmp/src/file1 /tmp/dst/file1 [[email protected] ~]# cat /tmp/dst/file1 123
-r, -R:递归复制目录
-d:复制符号链接文件本身,而非其指向的源文件
[[email protected] ~]# ln -s /tmp/src/file1 /tmp/src/filelink [[email protected] ~]# ls -l /tmp/src/ total 4 -rw-r--r--. 1 root root 4 May 7 23:55 file1 -rw-r--r--. 1 root root 0 May 7 23:46 file2 -rw-r--r--. 1 root root 0 May 7 23:46 file3 lrwxrwxrwx. 1 root root 14 May 8 00:08 filelink -> /tmp/src/file1 [[email protected] ~]# cp -d /tmp/src/filelink /tmp/dst/ [[email protected] ~]# ls -l /tmp/dst/ total 4 -rw-r--r--. 1 root root 4 May 8 00:02 file1 -rw-r--r--. 1 root root 0 May 7 23:57 file2 -rw-r--r--. 1 root root 0 May 7 23:57 file3 lrwxrwxrwx. 1 root root 14 May 8 00:09 filelink -> /tmp/src/file1 [[email protected] ~]# cp /tmp/src/filelink /tmp/dst/filelink1 [[email protected] ~]# ls -l /tmp/dst/ total 8 -rw-r--r--. 1 root root 4 May 8 00:02 file1 -rw-r--r--. 1 root root 0 May 7 23:57 file2 -rw-r--r--. 1 root root 0 May 7 23:57 file3 lrwxrwxrwx. 1 root root 14 May 8 00:09 filelink -> /tmp/src/file1 -rw-r--r--. 1 root root 4 May 8 00:12 filelink1
-a:-dR --preserve=all, archive,用于实现归档
--preserv=mode:权限
ownership:属主和属组
timestamps: 时间戳
context:安全标签(selinux安全上下文)
xattr:扩展属性
links:符号链接
all:上述所有属性
mv:移动文件或重命名
mv [OPTION]... [-T] SOURCE DEST
mv [OPTION]... SOURCE... DIRECTORY
mv [OPTION]... -t DIRECTORY SOURCE..
常用参数:
-i:交互式,即移动之前提醒用户确认
-f:强制移动
[[email protected] ~]# mv /tmp/dst/file1 /tmp/dst/file4 [[email protected] ~]# ls -l /tmp/dst/ total 8 -rw-r--r--. 1 root root 0 May 7 23:57 file2 -rw-r--r--. 1 root root 0 May 7 23:57 file3 -rw-r--r--. 1 root root 4 May 8 00:02 file4 lrwxrwxrwx. 1 root root 14 May 8 00:09 filelink -> /tmp/src/file1 -rw-r--r--. 1 root root 4 May 8 00:12 filelink1
rm:删除文件
rm [OPTION]... FILE...
常用参数:
-i:交互式,即删除之前提醒用户确认
-f:强制删除
-r: 递归删除
[[email protected] ~]# rm -rf /tmp/dst/ [[email protected] ~]# ls /tmp/dst ls: cannot access /tmp/dst: No such file or directory
3、文件查看类命令:
ls:列出指定目录下的内容
常用参数:
-a: 显示所有文件,包括隐藏文件
[[email protected] ~]# ls -a / . bin dev home lib64 mnt proc run srv tmp var .. boot etc lib media opt root sbin sys usr
-A:显示除.和..之外的所有文件
[[email protected] ~]# ls -A / bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr
-l:长格式列表,即显示文件的详细属性信息
-h:以人类可读的方式对文件大小单位换算;换算后结果可能会是非精确值
[[email protected] ~]# ls -lh / total 32K lrwxrwxrwx. 1 root root 7 Aug 3 2016 bin -> usr/bin dr-xr-xr-x. 3 root root 4.0K Aug 2 2016 boot drwxr-xr-x. 19 root root 3.1K Aug 2 2016 dev drwxr-xr-x. 132 root root 8.0K Aug 2 2016 etc drwxr-xr-x. 4 root root 90 Aug 2 2016 home lrwxrwxrwx. 1 root root 7 Aug 3 2016 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Aug 3 2016 lib64 -> usr/lib64 drwxr-xr-x. 3 root root 18 Aug 2 2016 media drwxr-xr-x. 3 root root 17 Aug 2 2016 mnt drwxr-xr-x. 3 root root 15 Aug 3 2016 opt dr-xr-xr-x. 505 root root 0 Aug 3 2016 proc dr-xr-x---. 14 root root 4.0K Aug 2 2016 root drwxr-xr-x. 35 root root 1.2K May 7 21:26 run lrwxrwxrwx. 1 root root 8 Aug 3 2016 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Mar 13 2014 srv dr-xr-xr-x. 13 root root 0 Aug 3 2016 sys drwxrwxrwt. 17 root root 4.0K May 7 21:54 tmp drwxr-xr-x. 13 root root 4.0K Aug 3 2016 usr drwxr-xr-x. 22 root root 4.0K Aug 3 2016 var
-d:查看目录自身而非其内部的文件列表
[[email protected] ~]# ls -ld /etc drwxr-xr-x. 132 root root 8192 Aug 2 2016 /etc
-r:逆序显示
[[email protected] ~]# ls -r / var tmp srv run proc mnt lib64 home dev bin usr sys sbin root opt media lib etc boot
-R:递归显示
注:Linux系统上的文件类型有:
-:file,常规文件
d:directory,目录文件
b:block device,块设备文件,支持以“block”为单位进行随机访问
c:character device,字符设备文件,支持以“character”为单位进行线性访问
major number:主设备号,用于标识设备类型,进而确定要加载的驱动程序
minor number:次设备号,用于标识同一类型中的不同的设备
l:symbolic link,符号链接文件
p:pipe,命名管道
s:socket,套接字文件
file:查看文件内容类型
[[email protected] ~]# file /sbin/ss /sbin/ss: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=0x14230a7d5743155147230c263cb721757a49db89, stripped
cat:连接并显示文件
常用参数:
-n:给显示的文本行编号
[[email protected] ~]# cat -n /etc/fstab 1 2# 3# /etc/fstab 4# Created by anaconda on Tue Aug 2 21:31:19 2016 5# 6# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘ 7# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info 8# 9/dev/mapper/rhel-root / xfs defaults 1 1 10UUID=ce40e87b-854d-42dc-ac50-e1309101c43d /boot xfs defaults 1 2 11/dev/mapper/rhel-swap swap swap defaults 0 0 12/dev/cdrom/media/cdromiso9660defaults0 0
-E: 显示行结束符$
[[email protected] ~]# cat -E /etc/issue \S$ Kernel \r on an \m$ $
tac:连接并显示文件,是cat的反向显示
常用参数:
-n:给显示的文本行编号
-E: 显示行结束符$
head:查看文件的前n行
常用参数:
-n #(-#):查看文件的前#行
[[email protected] ~]# head -n 10 /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin
tail:查看文件的后n行
常用参数:
-n #(-#):查看文件的后#行
[[email protected] ~]# tail -5 /etc/passwd gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin postfix:x:89:89::/var/spool/postfix:/sbin/nologin sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash
-f:查看文件尾部内容结束后不退出,跟随显示新增的行(通常用于查看系统日志信息)
[[email protected] ~]# tail -f /var/log/messages May 7 22:18:40 localhost dbus[1088]: [system] Activating via systemd: service name=‘net.reactivated.Fprint‘ unit=‘fprintd.service‘ May 7 22:18:40 localhost systemd: Starting Fingerprint Authentication Daemon... May 7 22:18:41 localhost dbus-daemon: dbus[1088]: [system] Successfully activated service ‘net.reactivated.Fprint‘ May 7 22:18:41 localhost dbus[1088]: [system] Successfully activated service ‘net.reactivated.Fprint‘ May 7 22:18:41 localhost systemd: Started Fingerprint Authentication Daemon. May 7 22:18:41 localhost fprintd: ** (fprintd:4822): WARNING **: fprint init failed with error -99 May 7 22:18:41 localhost systemd: fprintd.service: main process exited, code=exited, status=157/n/a May 7 22:18:41 localhost systemd: Unit fprintd.service entered failed state. May 7 22:20:01 localhost systemd: Starting Session 9 of user root. May 7 22:20:01 localhost systemd: Started Session 9 of user root.
more:分屏查看文件,翻屏至文件尾部后自动退出
less:分屏查看文件,支持上下翻页、查询操作,按q退出
2、bash的工作特性之命令执行状态返回值和命令行展开所涉及的内容及其示例演示。
1、bash通过状态返回值来输出命令执行的结果:
成功:0
失败:1-255之间的随机数,根据命令及其功能不同,结果各不相同
命令执行完成之后,其状态返回值保存于bash的特殊变量$?中,可以通过echo $?查看
[[email protected] ~]# ls /boot/ config-3.10.0-123.el7.x86_64 grub2 initramfs-0-rescue-49da9e087d6e4f9dab50f2af4a9a5921.img initramfs-3.10.0-123.el7.x86_64.img initramfs-3.10.0-123.el7.x86_64kdump.img initrd-plymouth.img symvers-3.10.0-123.el7.x86_64.gz System.map-3.10.0-123.el7.x86_64 vmlinuz-0-rescue-49da9e087d6e4f9dab50f2af4a9a5921 vmlinuz-3.10.0-123.el7.x86_64 [[email protected] ~]# echo $? 0 [[email protected] ~]# ls /boot/error ls: cannot access /boot/error: No such file or directory [[email protected] ~]# echo $? 2
2、命令行展开所涉及的内容
~:自动展开为用户的家目录,或指定的用户的家目录
[[email protected] ~]# cp /etc/issue ~/ [[email protected] ~]# ls ~/ 2017-05-07 anaconda-ks.cfg Downloads Music Templates 22:50 Desktop initial-setup-ks.cfg Pictures Videos 22:50:00 Documents issue Public
{}:可承载一个以逗号分隔的路径列表,并能够将其展开为多个路径
[[email protected] ~]# touch /tmp/file{1..10} [[email protected] ~]# ls /tmp/file* /tmp/file1 /tmp/file2 /tmp/file4 /tmp/file6 /tmp/file8 /tmp/file10 /tmp/file3 /tmp/file5 /tmp/file7 /tmp/file9
3、请使用命令行展开功能来完成以下练习:
(1)、创建/tmp目录下的:a_c, a_d, b_c, b_d
[[email protected] ~]# mkdir -pv /tmp/{a,b}_{c,d} mkdir: created directory ‘/tmp/a_c’ mkdir: created directory ‘/tmp/a_d’ mkdir: created directory ‘/tmp/b_c’ mkdir: created directory ‘/tmp/b_d’ [[email protected] ~]# tree -L 1 /tmp/ /tmp/ ├── a_c ├── a_d ├── b_c ├── b_d
(2)、创建/tmp/mylinux目录下的:
mylinux/
├── bin
├── boot
│ └── grub
├── dev
├── etc
│ ├── rc.d
│ │ └── init.d
│ └── sysconfig
│ └── network-scripts
├── lib
│ └── modules
├── lib64
├── proc
├── sbin
├── sys
├── tmp
├── usr
│ └── local
│ ├── bin
│ └── sbin
└── var
├── lock
├── log
└── run
[[email protected] ~]# mkdir -pv /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network- scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}} mkdir: created directory ‘/tmp/mylinux’ mkdir: created directory ‘/tmp/mylinux/bin’ mkdir: created directory ‘/tmp/mylinux/boot’ mkdir: created directory ‘/tmp/mylinux/boot/grub’ mkdir: created directory ‘/tmp/mylinux/dev’ mkdir: created directory ‘/tmp/mylinux/etc’ mkdir: created directory ‘/tmp/mylinux/etc/rc.d’ mkdir: created directory ‘/tmp/mylinux/etc/rc.d/init.d’ mkdir: created directory ‘/tmp/mylinux/etc/sysconfig’ mkdir: created directory ‘/tmp/mylinux/etc/sysconfig/network-scripts’ mkdir: created directory ‘/tmp/mylinux/lib’ mkdir: created directory ‘/tmp/mylinux/lib/modules’ mkdir: created directory ‘/tmp/mylinux/lib64’ mkdir: created directory ‘/tmp/mylinux/proc’ mkdir: created directory ‘/tmp/mylinux/sbin’ mkdir: created directory ‘/tmp/mylinux/sys’ mkdir: created directory ‘/tmp/mylinux/tmp’ mkdir: created directory ‘/tmp/mylinux/usr’ mkdir: created directory ‘/tmp/mylinux/usr/local’ mkdir: created directory ‘/tmp/mylinux/usr/local/bin’ mkdir: created directory ‘/tmp/mylinux/usr/local/sbin’ mkdir: created directory ‘/tmp/mylinux/var’ mkdir: created directory ‘/tmp/mylinux/var/lock’ mkdir: created directory ‘/tmp/mylinux/var/log’ mkdir: created directory ‘/tmp/mylinux/var/run’ [[email protected] ~]# tree /tmp/mylinux/ /tmp/mylinux/ ├── bin ├── boot │ └── grub ├── dev ├── etc │ ├── rc.d │ │ └── init.d │ └── sysconfig │ └── network-scripts ├── lib │ └── modules ├── lib64 ├── proc ├── sbin ├── sys ├── tmp ├── usr │ └── local │ ├── bin │ └── sbin └── var ├── lock ├── log └── run 24 directories, 0 files
4、文件的元数据信息有哪些,分别表示什么含义,如何查看?如何修改文件的时间戳信息。
1、文件的数据分为元数据(metadata)和数据(data)。其中元数据是对数据本身特征的描述,包括:文件路径、文件大小、数据块信息,文件类型、文件权限、属主属组、安全信息、时间戳信息(访问时间、修改时间、改变时间)等内容。
2、查看文件元数据信息
stat:显示文件或文件系统状态
[[email protected] ~]# echo 123 > /tmp/testfile [[email protected] ~]# stat /tmp/testfile File: ‘/tmp/testfile’ Size: 4 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768dInode: 102531229 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:user_tmp_t:s0 Access: 2017-05-08 01:10:05.142639974 +0800 Modify: 2017-05-08 01:10:05.142639974 +0800 Change: 2017-05-08 01:10:05.142639974 +0800 Birth: -
3、修改文件的时间戳信息
touch:修改文件时间戳或创建文件
常用参数:
-c: 指定的文件路径不存在时不予创建
-a: 仅修改access time
[[email protected] ~]# touch -a -t 05101223 /tmp/testfile [[email protected] ~]# stat /tmp/testfile File: ‘/tmp/testfile’ Size: 4 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768dInode: 102531229 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:user_tmp_t:s0 Access: 2017-05-10 12:23:00.000000000 +0800 Modify: 2017-05-08 01:13:30.103620135 +0800 Change: 2017-05-08 01:14:37.774613584 +0800 Birth: -
-m:仅修改modify time
[[email protected] ~]# touch -m -t 05111122 /tmp/testfile [[email protected] ~]# stat /tmp/testfile File: ‘/tmp/testfile’ Size: 4 Blocks: 8 IO Block: 4096 regular file Device: fd00h/64768dInode: 102531229 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root) Context: unconfined_u:object_r:user_tmp_t:s0 Access: 2017-05-10 12:23:00.000000000 +0800 Modify: 2017-05-11 11:22:00.000000000 +0800 Change: 2017-05-08 01:15:16.110609873 +0800 Birth: -
-t STAMP [[CC]YY]MMDDhhmm[.ss]:修改时间戳
5、如何定义一个命令的别名,如何在命令中引用另一个命令的执行结果?
1、定义别名及撤销别名
alias NAME=‘COMMAND‘
注:仅对当前shell进程有效
unalias NAME
[[email protected] ~]# alias lsver=‘cat /etc/redhat-release‘ [[email protected] ~]# lsver Red Hat Enterprise Linux Server release 7.0 (Maipo) [[email protected] ~]# alias alias cp=‘cp -i‘ alias egrep=‘egrep --color=auto‘ alias fgrep=‘fgrep --color=auto‘ alias grep=‘grep --color=auto‘ alias l.=‘ls -d .* --color=auto‘ alias ll=‘ls -l --color=auto‘ alias ls=‘ls --color=auto‘ alias lsver=‘cat /etc/redhat-release‘ alias mv=‘mv -i‘ alias rm=‘rm -i‘ alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘ [[email protected] ~]# unalias lsver [[email protected] ~]# alias alias cp=‘cp -i‘ alias egrep=‘egrep --color=auto‘ alias fgrep=‘fgrep --color=auto‘ alias grep=‘grep --color=auto‘ alias l.=‘ls -d .* --color=auto‘ alias ll=‘ls -l --color=auto‘ alias ls=‘ls --color=auto‘ alias mv=‘mv -i‘ alias rm=‘rm -i‘ alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
2、引用另一个命令的执行结果
$(COMMAND)或`COMMAND`
[[email protected] ~]# touch /tmp/file$(date +%F) [[email protected] ~]# touch /tmp/file`date +%T` [[email protected] ~]# ls /tmp/file* /tmp/file01:31:33 /tmp/file2017-05