BASH Shell 文件管理,文件管理在哪


BASH Shell 对文件进行管理

========================================================
创建、复制、删除、移动、查看、编辑、压缩、查找

内容提要:
Linux目录结构
文件定位 路径
文件管理

一、Linux目录结构

Windows: 以多根的方式组织文件 C:\ D:\ E:\
Linux: 以单根的方式组织文件 /

/目录结构: FSH (Filesystem Hierarchy Standard)
[root@CentOS ~]# ls /
bin dev lib media net root srv usr
boot etc lib64 misc opt sbin sys var
cgroup home lost+found mnt proc selinux tmp

注:下图为Centos7目录结构

bin 普通用户使用的命令 /bin/ls, /bin/date
sbin 管理员使用的命令 /sbin/service
dev 设备文件 /dev/sda,/dev/sda1,/dev/tty1,/dev/tty2,/dev/pts/1, /dev/zero, /dev/null, /dev/random
root root用户的HOME
home 存储普通用户家目录
lost+found fsck修复时,存储没有链接的文件或目录
proc 虚拟的文件系统,反映出来的是内核,进程信息或实时状态

usr 系统文件,相当于C:\Windows
/usr/local 软件安装的目录,相当于C:\Program
/usr/bin 普通用户使用的应用程序
/usr/sbin 管理员使用的应用程序
/usr/lib 库文件Glibc
/usr/lib64 库文件Glibc
boot 存放的系统启动相关的文件,例如kernel,grub(引导装载程序)
etc 配置文件(系统相关如网络/etc/sysconfig/network,应用相关配置文件如/etc/ssh/sshd_config...)
lib 库文件Glibc
lib64 库文件Glibc
tmp 临时文件(全局可写:进程产生的临时文件)
var 存放的是一些变化文件,比如数据库,日志,邮件....
mysql: /var/lib/mysql
vsftpd: /var/ftp
mail: /var/spool/mail
cron: /var/spool/cron
log: /var/log
临时文件: /var/tmp(进程产生的临时文件)

设备(主要指存储设备)挂载目录

media 移动设备默认的挂载点
mnt 手工挂载设备的挂载点
misc automount进程挂载
net automount进程挂载

命令存储位置

/bin 普通用户使用的命令 /bin/ls, /bin/date RHEL7淘汰
/sbin 管理员使用的命令 RHEL7淘汰
/usr/bin 普通用户使用的应用程序
/usr/sbin 管理员使用的应用程序

库文件存储位置

/lib 库文件Glibc RHEL7淘汰
/lib64 库文件Glibc RHEL7淘汰
/usr/lib 库文件Glibc
/usr/lib64 库文件Glibc

文件时间

ls -l 文件名 仅看的是文件的修改时间

Linux文件有四种时间:
# stat anaconda-ks.cfg //查看文件的详细属性(其中包括文件时间属性)
访问时间:atime,查看内容 //RHEL6会延后修改atime
修改时间:mtime,修改内容
改变时间:ctime,文件属性,比如权限
删除时间:dtime,文件被删除的时间

[root@CentOS ~]# ls -l install.log
-rw-r--r-- 1 root root 34793 10-23 13:49 install.log

[root@CentOS ~]# stat install.log
File: “install.log”
Size: 34793 Blocks: 80 IO Block: 4096 一般文件
Device: 802h/2050d Inode: 65282 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2012-10-23 16:53:20.000000000 +0800
Modify: 2012-10-23 13:49:46.000000000 +0800
Change: 2012-10-23 13:49:52.000000000 +0800

注:RHEL6开始relatime,atime延迟修改,必须满足其中一个条件:
1. 自上次atime修改后,已达到86400秒;
2. 发生写操作时;

文件类型

通过颜色判断文件的类型是不一定正确的!!!
Linux系统中文件是没有扩展名!!!

方法一:
ls -l 文件名 //看第一个字符
- 普通文件(文本文件,二进制文件,压缩文件,电影,图片。。。)
d 目录文件(蓝色)
b 设备文件(块设备)存储设备硬盘,U盘 /dev/sda, /dev/sda1
c 设备文件(字符设备)打印机,终端 /dev/tty1
s 套接字文件
p 管道文件
l 链接文件(淡蓝色)

[root@CentOS ~]# ll -d /etc/hosts /bin/ls /home /dev/sda /dev/tty1 /etc/grub2.cfg /dev/log /run/dmeventd-client
-rwxr-xr-x. 1 root root 117616 Nov 20 2015 /bin/ls
srw-rw-rw- 1 root root 0 Mar 14 2017 /dev/log
brw-rw---- 1 root disk 8, 0 Mar 14 09:03 /dev/sda
crw--w---- 1 root tty 4, 1 Mar 14 09:03 /dev/tty1
lrwxrwxrwx. 1 root root 22 Dec 21 01:40 /etc/grub2.cfg -> ../boot/grub2/grub.cfg
-rw-r--r-- 1 root root 392 Feb 22 15:51 /etc/hosts
drwxr-xr-x. 10 root root 4096 Mar 14 11:00 /home


方法二:file
[yang@CentOS ~]$ file /etc/hosts
/etc/hosts: ASCII text

[yang@CentOS ~]$ file /bin/ls
/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=aa7ff68f13de25936a098016243ce57c3c982e06, stripped

[yang@CentOS ~]$ file /dev/sda
/dev/sda: block special

[yang@CentOS ~]$ file /dev/tty1
/dev/tty1: character special

[yang@CentOS ~]$ file /etc/grub2.cfg
/etc/grub2.cfg: broken symbolic link to `../boot/grub2/grub.cfg'

[yang@CentOS ~]$ file /home
/home: directory

[yang@CentOS ~]$ file /run/dmeventd-client
/run/dmeventd-client: fifo (named pipe)

二、路径 (定位文件)

你要在哪儿创建文件?
你要将什么文件复制到什么地方?
你要删除什么地方的什么文件?

/home/alice/file1 和 /home/bob/file1 不是同一文件!!!
/abc/file5 和 abc/file5 有可能是不一样的!!!

绝对路径: 从/开始的路径 /home/alice/file1
相对路径: 相对于当前目录开始 a.txt ./a.txt ../bob/b.txt [假如:此时在目录/home/alice]

# useradd alice
# touch /home/alice/file1
# touch ~/file2
# touch ~alice/file3

# pwd
# mkdir abc
# touch ../file3
# touch file4
# touch abc/file5

三、文件管理

1. 文件管理之:cd 改变目录
cd 绝对路径 cd /home/alice cd ~alice
cd 相对路径 cd Desktop/abc cd .. cd .
=======================================================
cd 专用:
cd - 返回上次目录
cd 直接回家
=======================================================
2. 文件管理之:创建/复制/移动/删除
==创建
文件 touch
# touch file1.txt //无则创建,有则修改时间
# touch file3 file4
# touch /home/file10.txt
# touch /home/file5 file6
# touch /home/{zhuzhu,gougou}
# touch file{1..20}
# touch file{a..c}
# touch yang{a,b,c} //{}集合,等价touch yanga yangb yangc

目录 mkdir
# mkdir dir1
# mkdir /home/dir2 /home/dir3
# mkdir /home/{dir4,dir5}
# mkdir -v /home/{dir6,dir7}
# mkdir -v /hoem/dir8/111/22
# mkdir -pv /hoem/dir8/111/222 //包括其父母的创建,不会有任何消息输出
# mkdir -pv /home/{yang/{dir1,111},CentOS}


==复制 cp
Usage: cp [OPTION]... [-T] SOURCE DEST
# cd
# mkdir /home/dir{1,2}
# cp -v install.log /home/dir1
# cp -v install.log /home/dir1/yang.txt
# cp -rv /etc /home/dir1
# cp -v install.log /home/dir90 //没有/home/dir90
# cp -v install.log /home/dir2
# cp -v anaconda-ks.cfg !$
# cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/passwd /etc/grub.conf /home/dir2 //将多个文件拷贝到同一个目录
# cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/passwd /etc/grub.conf /etc/hosts .
# cp -r /etc /tmp
# cp -rf /etc /tmp
# \cp -r /etc /tmp

[root@CentOS ~]# type -a cp
cp is aliased to `cp -i'
cp is /bin/cp

[root@CentOS ~]# cp -rv /etc/sysconfig/network-scripts/{ifcfg-eth0,ifcfg-eth0.bak}
[root@CentOS ~]# cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0{,-org}
[root@CentOS ~]# cp -rv /etc/sysconfig/network-scripts/{ifcfg-eth0,yang-eth0}


==移动 mv
Usage: mv [OPTION]... [-T] SOURCE DEST
# mv file1 /home/dir3 将file2移动到/home/dir3
# mv file2 /home/dir3/file20 将file2移动到/home/dir3,并改名为file20
# mv file4 file5 将file4重命名为file5,当前位置的移动就是重命名

==删除 rm
示例1:删除/home/dir1
# cd /home
# rm -rf dir1
-r 递归
-f force强制
-v 详细过程

示例2:
[root@CentOS ~]# mkdir /home/dir10
[root@CentOS ~]# touch /home/dir10/{file2,file3,.file4}
[root@CentOS ~]# rm -rf /home/dir10/* //不包括隐藏文件
[root@CentOS ~]# ls /home/dir10/ -a
. .. .file4

示例3:
[root@CentOS ~]# rm -rf file*
[root@CentOS ~]# rm -rf *.pdf


3. 文件管理之:查看文件内容
==文本文件 (cat tac less more head tail tailf grep ...)
/bin/date
/etc/hosts
/etc/sysconfig/network
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/passwd
/etc/shadow
/etc/group
/etc/grub2.cfg
/etc/resolv.conf
/etc/profile //设置系统环境变量
/etc/bashrc //影响bash shell环境
/var/log/messages //系统主日志文件
/var/log/secure //跟安全相关的日志如ssh登录,本地登录...

cat
-n 显示行号
-A 包括控制字符(换行符/制表符)
linux $
Windows ^M$

less more head tail tailf
[root@CentOS ~]# head /etc/passwd
[root@CentOS ~]# head -2 /etc/passwd
[root@CentOS ~]# tail /etc/passwd
[root@CentOS ~]# tail -1 /etc/passwd
[root@CentOS ~]# tail /var/log/messages
[root@CentOS ~]# tail -20 /var/log/messages
[root@CentOS ~]# tail -f /var/log/secure //-f 动态查看文件的尾部
[root@CentOS ~]# tail -F /var/log/secure //-F 动态查看文件的尾部
注:vim, gedit编辑文件时,索引号会改变

grep 针对文件内容进行过滤
# grep 'root' /etc/passwd
# grep '^root' /etc/passwd
# grep 'bash$' /etc/passwd
# grep 'failure' /var/log/secure


4. 文件管理之:修改文件内容
[root@CentOS ~]# ll -a > list.txt
文件编辑器 gedit
文件编辑器 vi, vim, nano

 

 

命令模式:
a. 光标定位
hjkl
0 $
gg G
3G 进入第三行
/string (n N 可以循环的) 快速定位到某一行
/^d
/txt$

b. 文本编辑(少量)
y 复制 yy 3yy ygg yG (以行为单位)
d 删除 dd 3dd dgg dG (以行为单位)
p 粘贴
x 删除光标所在的字符
D 从光标处删除到行尾
u undo撤销
^r redo重做
r 可以用来修改一个字符

c. 进入其它模式
a 进入插入模式
i 进入插入模式
o 进入插入模式
A 进入插入模式

: 进入末行模式(扩展命令模式)
v 进入可视模式
^v 进入可视块模式
V 进入可视行模式
R 进入替换模式

插入模式:

可视块模式:
块插入(在指定块前加入字符): 选择块,I 在块前插入字符, ESC
块替换: 选择块,r 输入替换的字符
块删除: 选择块,d | x
块复制: 选择块,y

扩展命令模式:
a. 保存退出
:10 进入第10行
:w 保存
:q 退出
:wq 保存并退出
:w! 强制保存
:q! 不保存并退出
:wq! 强制保存退出
:x 保存并退出 ZZ


b. 查找替换
:范围 s/old/new/选项
:1,5 s/root/yang/ 从1-5行的root 替换为yang
:5,$ s/root/yang/ $表示最后一行
:1,$ s/root/yang/g = :% s/root/yang/g %表示全文 g表示全局
:% s#/dev/sda#/var/ccc#g
:,8 s/root/yang/ 从当前行到第8行
:4,9 s/^#// 4-9行的开头#替换为空
:5,10 s/.*/#&/ 5-10前加入#字符 (.*整行 &引用查找的内容)

c. 读入文件/写文件(另存为)
:w 存储到当前文件
:w /tmp/aaa.txt 另存为/tmp/aaa.txt
:1,3 w /tmp/2.txt
:r /etc/hosts 读入文件到当前行后
:5 r /etc/hosts 读入文件到第5行后

d. 设置环境
临时设置:
:set nu 设置行号
:set ic 不区分大小写
:set ai 自动缩进
:set list 显示控制字符
:set nonu 取消设置行号
:set noic

永久的环境:
/etc/vimrc 影响所有系统用户
~/.vimrc 影响某一个用户

# vim ~/.vimrc
set ic
set nu

$ sudo vim -O /etc/hosts /etc/grub2.cfg ^ww切换
$ cp /etc/hosts hosts1
$ vimdiff /etc/hosts hosts1



相关内容

    暂无相关文章