您的位置:首页 > 运维架构 > Linux

5.Linux文件和目录相关操作

2014-12-20 16:04 369 查看
一、目录管理:
cd, pwd, ls 都是来查看目录的

mkdir: make directory 创建目录
-p: 当指定的目标目录的父目录不存在时,则先创建之
-p, --parents
no error if existing, make parent directories as needed
-v, --verbose
print a message for each created directory 打印创建每个目录的信息
[root@linux_basic tmp]# mkdir hello
[root@linux_basic tmp]# ls
hello
[root@linux_basic tmp]# ls hello/
[root@linux_basic tmp]# mkdir how are
[root@linux_basic tmp]# ls
are hello how
rmdir: remove directory
rmdir - remove empty directories 删除空目录
-p: 删除单传目录路径中各空目录
-p, --parents
remove DIRECTORY and its ancestors; e.g., ‘rmdir -p a/b/c’ is similar to ‘rmdir a/b/c a/b a’
[root@linux_basic tmp]# ls
are hello how
[root@linux_basic tmp]# rmdir are
[root@linux_basic tmp]# ls
hello how
[root@linux_basic tmp]# rmdir how
[root@linux_basic tmp]# ls
hello
[root@linux_basic tmp]# mkdir -pv test/apache/you
mkdir: created directory `test'
mkdir: created directory `test/apache'
mkdir: created directory `test/apache/you'
[root@linux_basic tmp]# ls
hello test you
[root@linux_basic tmp]# ls test/
apache
[root@linux_basic tmp]# rmdir test/apache -p
[root@linux_basic tmp]# ls
hello you

bash的工作特点:没有返回信息通常最好的信息
每个命令执行结束后,会有一个“执行状态返回值”,有效范围0-255
0: 表示执行成功
1-255: 表示执行失败

使用特殊变量$?可以获取最近一条命令的状态返回值
# echo $?
[root@linux_basic tmp]# ls
hello
[root@linux_basic tmp]# echo $? 查看上一条命令的状态返回值
0
[root@linux_basic tmp]# ls
hello
[root@linux_basic tmp]# rmdir are 目录不存在,删除报错
rmdir: failed to remove `are': No such file or directory
[root@linux_basic tmp]# echo $?
1 转态返回值不为0

[root@linux_basic tmp]# ls
hello
[root@linux_basic tmp]# mkdir you/are
mkdir: cannot create directory `you/are': No such file or directory
[root@linux_basic tmp]# mkdir -p you/are 创建两层的目录
[root@linux_basic tmp]# ls
hello you
[root@linux_basic tmp]# ls you/
are

bash特性之一:命令行展开
~: 用户家目录
~USERNAME: 指定用户的家目录
[root@linux_basic tmp]# echo ~
/root
[root@linux_basic tmp]# ls /home/
cactiuser
[root@linux_basic tmp]# echo ~cactiuser
/home/cactiuser
{}: 有多个的话,每个都会展开到对应项
/tmp/{x,y}
/tmp/x, /tmp/y

/tmp/{x,y}/z
/tmp/x/z, /tmp/y/z

创建/tmp/x/z, /tmp/y/z, /tmp/x/m, /tmp/y/m
mkdir /tmp/{x,y}/{z,m}
练习1:创建/tmp/
a_b, c_b, a_d, c_d
[root@linux_basic tmp]# mkdir -pv {a,c}_{b,d}
mkdir: created directory `a_b'
mkdir: created directory `a_d'
mkdir: created directory `c_b'
mkdir: created directory `c_d'

练习2:创建/tmp/mylinux/
boot
grub
bin
sbin
etc
rc.d
init.d
sysconfig
network-scripts
lib
modules
lib64
usr
local
bin
sbin
lib
lib64
bin
sbin
lib
lib64
proc
sys
dev
var
log
run
lock
tmp

# mkdir -pv /tmp/mylinux/{boot/grub,bin,sbin,etc/{rc.d/init.d,sysconfig/network-scripts},lib/modules,lib64,usr/{bin,sbin,lib,lib64,local/{bin,sbin,lib,lib64}},proc,sys,dev,var/{log,run,lock},tmp}
用tree可以查看目录的结构,安装tree,yum install tree -y ;此时是需要有网络的

二、文件查看和文件属性信息
ls命令:
list简写
NAME
ls - list directory contents

SYNOPSIS
ls [OPTION]... [FILE]...
ls [option] [file]

常用选项:
-l: long,长格式显示文件的详细属性信息
drwxr-xr-x. 2 root root 4096 Feb 12 09:55 account
左起第一位:文件类型
后面的9位:权限,常称为mode
r: 可读,Read
w: 可写, Write
x:可执行, eXcute
.: 表示文件有隐藏属性
lsattr命令可以查看隐藏属性
数字:此文件被硬链接的次数,目录一般都是2
属主:owner, 文件的拥有者
属组:group, 文件所属的组
4096: 文件大小,单位是字节
-h: human-readable,自动做单位换算,打印大小
-h, --human-readable
with -l, print sizes in human readable format (e.g., 1K 234M 2G)
[root@linux_basic tmp]# ls -lh /tmp/
total 8.0K
drwxr-xr-x. 2 root root 4.0K Dec 20 14:01 hello
drwxr-xr-x. 3 root root 4.0K Dec 20 14:08 you
文件最近一次被修改的时间
文件名
-a: 显示所有文件
-a, --all
do not ignore entries starting with .
[root@linux_basic tmp]# ls -a
. .. hello .ICE-unix you
-d: 通常和-l一起使用,用于仅显示目录自身属性
-d, --directory
list directory entries instead of contents, and do not dereference symbolic links
[root@linux_basic tmp]# ls -ld /tmp
drwxrwxrwt. 5 root root 4096 Dec 20 14:40 /tmp
-r: reverse, 逆序显示 默认是升序显示的
[root@linux_basic tmp]# ls
hello you
[root@linux_basic tmp]# ls -r
you hello
-R: recursive, 递归显示,显示子目录中的内容
[root@linux_basic tmp]# ls -R
.:
hello you

./hello:

./you:
are

./you/are:

文件管理类的命令:
查看:cat, tac, head, tail, less, more
时间戳管理:touch
复制:cp
移动:mv
查看元数据属性:stat
文本编辑器:nano, vi

stat: 显示文件的元数据
时间戳:每个文件都有三个时间戳
atime 访问时间 最近一次被访问的时间
mtime 修改时间 最近一次被修改的时间 文件内容的改变
ctime 改变时间 最近一次改变的时间 文件元数据的改变
[root@linux_basic tmp]# stat hello/
File: `hello/'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: fd00h/64768d Inode: 524365 Links: 2 被硬连接的次数
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-12-20 14:01:23.980000639 +0800 最近一次被访问的时间
Modify: 2014-12-20 14:01:19.755999191 +0800 最近一次被修改的时间 文件内容的改变
Change: 2014-12-20 14:01:19.755999191 +0800 最近一次改变的时间 文件元数据的改变
inode(索引节点号,每个文件都有索引节点,也叫元数据元数据条目的编号)
[root@linux_basic tmp]# stat you/ 注意时间戳的改变
File: `you/'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: fd00h/64768d Inode: 524384 Links: 3
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-12-20 14:08:57.103000297 +0800
Modify: 2014-12-20 14:08:52.309009173 +0800
Change: 2014-12-20 14:08:52.309009173 +0800
[root@linux_basic tmp]# touch you/
[root@linux_basic tmp]# stat you/
File: `you/'
Size: 4096 Blocks: 8 IO Block: 4096 directory
Device: fd00h/64768d Inode: 524384 Links: 3
Access: (0755/drwxr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-12-20 14:55:18.363998749 +0800
Modify: 2014-12-20 14:55:18.363998749 +0800
Change: 2014-12-20 14:55:18.363998749 +0800

touch:改变文件的atime和mtime
NAME
touch - change file timestamps

SYNOPSIS
touch [OPTION]... FILE...
DESCRIPTION
Update the access and modification times of each FILE to the current time.
如果FILE不存在,默认会创建一个空文件

-a: 仅改变atime
-a change only the access time
-m: 仅改变mtime
-m change only the modification time
-c: 不创建空文件
-c, --no-create
do not create any files
-t [[CC]YY]MMDDhhmm[.ss] 指定那个时间修改和改变的
-t STAMP
use [[CC]YY]MMDDhhmm[.ss] instead of current time
[root@linux_basic tmp]# stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 524405 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-12-20 14:57:35.737999993 +0800
Modify: 2014-12-20 14:57:35.737999993 +0800
Change: 2014-12-20 14:57:35.737999993 +0800
[root@linux_basic tmp]# date
Sat Dec 20 15:15:42 CST 2014
[root@linux_basic tmp]# touch -a test.txt
[root@linux_basic tmp]# stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 524405 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-12-20 15:15:56.177992895 +0800
Modify: 2014-12-20 14:57:35.737999993 +0800
Change: 2014-12-20 15:15:56.177992895 +0800
[root@linux_basic tmp]# touch -m test.txt
[root@linux_basic tmp]# stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 524405 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2014-12-20 15:15:56.177992895 +0800
Modify: 2014-12-20 15:16:53.171995492 +0800
Change: 2014-12-20 15:16:53.171995492 +0800
[root@linux_basic tmp]# touch -a -t 201211091251.36 test.txt
[root@linux_basic tmp]# stat test.txt
File: `test.txt'
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd00h/64768d Inode: 524405 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2012-11-09 12:51:36.000000000 +0800
Modify: 2014-12-20 15:20:31.025993115 +0800
Change: 2014-12-20 15:22:44.780992659 +0800

文件查看类命令:
cat: 连接并显示文本文件内容
NAME
cat - concatenate files and print on the standard output 连接文件和打印到标准输出

SYNOPSIS
cat [OPTION]... [FILE]...

DESCRIPTION
Concatenate FILE(s), or standard input, to standard output.
数据流:stream

-E:显示行结束符
-E, --show-ends
display $ at end of each line
-n: 显示行号
-n, --number
number all output lines
[root@linux_basic tmp]# cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m

[root@linux_basic tmp]# cat /etc/issue -n
1 CentOS release 6.6 (Final)
2 Kernel \r on an \m
3
[root@linux_basic tmp]# cat /etc/issue -E
CentOS release 6.6 (Final)$
Kernel \r on an \m$
$
Linux的换行符是$,windows下的换行符是$和回车符

tac: 逆序显示文件内容

Shift+PageUp/PageDown: 翻屏,在虚拟终端上翻屏

分屏显示:
more 和 less
more到文件尾部时,会自动退出
less到文件尾部时,不会自动退出,用q退出,和man的选项相似

查看首部或尾部的部分内容:
head 默认显示头10行
NAME
head - output the first part of files

SYNOPSIS
head [OPTION]... [FILE]...

tail 默认显示尾部10行
-n #: 指定的行数,head和tail都支持此选项
[root@linux_basic tmp]# tail -5 /etc/inittab
# 5 - X11
# 6 - reboot (Do NOT set initdefault to this)
#
id:3:initdefault:
S0:12345:respawn:/sbin/agetty ttyS0 115200

tail -f
-f, --follow[={name|descriptor}]
output appended data as the file grows; -f, --follow, and --follow=descriptor are equivalent
监控一个尾部不断变化的文件,对于日志文件的查看很方便,可以实时查看新增的信息

本文出自 “快乐就好” 博客,请务必保留此出处http://wdllife.blog.51cto.com/6615958/1591982
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: