码迷,mamicode.com
首页 > 系统相关 > 详细

linux文件管理之bash shell

时间:2019-03-26 01:21:01      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:设置   tor   ls -l   移动   rip   默认   rar   lib64   tailf   

 

BASH Shell 对文件进行管理


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

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


一、Linux目录结构
Windows: 以多根的方式组织文件 C:\ D:\ E:\
Linux: 以单根的方式组织文件 /

/目录结构: FSH (Filesystem Hierarchy Standard)
[root@localhost ~]# 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@localhost ~]# ls -l install.log
-rw-r--r-- 1 root root 34793 10-23 13:49 install.log

[root@localhost ~]# 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@localhost ~]# 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@localhost ~]$ file /etc/hosts
/etc/hosts: ASCII text

[yang@localhost ~]$ 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@localhost ~]$ file /dev/sda
/dev/sda: block special

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

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

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

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


二、路径 (定位文件)
你要在哪儿创建文件?
你要将什么文件复制到什么地方?
你要删除什么地方的什么文件?

技术图片


/home/alice/file1 和 /home/bob/file1 不是同一文件!!!
/abc/file5 和 abc/file5 有可能是不一样的!!!
命名规则:
1、长度不能超过255个字符;
2、不能使用/当文件名;
3、严格区分大小写;

绝对路径: 从/开始的路径 /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},localhost}


==复制 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@localhost ~]# type -a cp
cp is aliased to `cp -i‘
cp is /bin/cp

[root@localhost ~]# cp -rv /etc/sysconfig/network-scripts/{ifcfg-eth0,ifcfg-eth0.bak}
[root@localhost ~]# cp -rv /etc/sysconfig/network-scripts/ifcfg-eth0{,-org}
[root@localhost ~]# 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@localhost ~]# mkdir /home/dir10
[root@localhost ~]# touch /home/dir10/{file2,file3,.file4}
[root@localhost ~]# rm -rf /home/dir10/* //不包括隐藏文件
[root@localhost ~]# ls /home/dir10/ -a
. .. .file4

示例3:
[root@localhost ~]# rm -rf file*
[root@localhost ~]# 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@localhost ~]# head /etc/passwd
[root@localhost ~]# head -2 /etc/passwd
[root@localhost ~]# tail /etc/passwd
[root@localhost ~]# tail -1 /etc/passwd
[root@localhost ~]# tail /var/log/messages
[root@localhost ~]# tail -20 /var/log/messages
[root@localhost ~]# tail -f /var/log/secure //-f 动态查看文件的尾部
[root@localhost ~]# 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@localhost ~]# ll -a > list.txt
文件编辑器 gedit
文件编辑器 vi, vim, nano



本节作业:

 

1. 完成课堂中学习的所有内容

 

2. type file stat 3个命令的区别是什么


3. 文件管理命令练习:
在student用户主目录下创建一个临时目录tmp;
在临时目录下创建一个文件,文件名为a.txt;
将a.txt复制成b.txt;将b.txt改名成c.txt;
为c.txt创建符号链接,链接文件名为linkc.txt;
使用gzip压缩创建/etc的归档,将文件另存为/home/student/tmp/etc.tar.gz;
将压缩的归档文件解压到/backups目录;
删除tmp目录




linux文件管理之bash shell

标签:设置   tor   ls -l   移动   rip   默认   rar   lib64   tailf   

原文地址:https://www.cnblogs.com/anttech/p/10597660.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!