Linux 文件管理命令
1、文件建立
touch filename
注意:
touch不但可以建立文件也可以修改文件的时间戳
时间戳分为:
atime:文件内容被访问的时间标识
mtime:文件内容被修改的时间标识
ctime:文件属性或文件内容被修改的时间标识
2、目录建立
1) 建立一个目录
mkdir directory ##建立一个目录directory
2)建立递归目录
mkdir -p dir1/dir2/dir3 ##建立递归目录
注意:建立递归目录时,上层目录不存在则自动建立。
3、删除
1)文件删除
rm file ##删除file文件
rm -f file ## -f=force 标示强制删除不提示
2)目录删除
rm -r directory ## -r标示递归删除,也就是删除目录本身和里面的所有内容
rm -fr directory ## -fr=-f -r=-rf, 强制删除目录本身和里面的所有内容
4、编辑文件
1)gedit
gedit file ##必须有图形
2)vim
vim file ##进入file 文件
按[i]进入insert模式–>书写内容–>按[esc]退出insert模式–>:wq退出保存
注意:当vim异常退出时会生成.file.swp文件
当再次打开此文件时会出现以下情况:
[0] 只读打开 [E]继续编辑 [R]恢复数据 [D]删除swap文件 [Q]退出 [A]退出
无论按[O] (E) ® (Q) (A) 任何一个都不会删除.swp文件
那么再次打开hello文件是还会出现以上界面直到按(D),.swp文件被删除
vim恢复正常工作
6.文件的复制
cp file1 file2 ##文件file1内的内容复制到file2中
cp -r text1 text2 ##目录text1复制到text2中
cp 源文件1 源文件2 目的地目录
cp -r 源目录1 源目录2 目的地目录
这两种情况:目的地目录必须存在
7、文件的移动
mv 源文件 目的地文件
mv 源目录 目的地文件
mv 源目录 目的地目录
mv 源文件 目的地目录
注意:
相同磁盘的文件移动只是一个重命名过程
mv file file1 ##文件或目录名字 由file改为file1
不同磁盘的文件移动是复制删除过程
8、如何查看文件的内容
1)cat命令
[kiosk@foundation72 Desktop]$ cat 666 ##查看文件的全部内容
## Linux(小白)
cat -b
[kiosk@foundation72 Desktop]$ cat -b 666 ##-b显示行号
1 ## Linux(小白)
例:
[kiosk@foundation72 Desktop]$ vim 666
[kiosk@foundation72 Desktop]$ cat -b 666
1 ## Linux(小白)
2 hahaha
3 q
4 w
5 e
6 r
7 t
8 y
9 u
10 i
11 o
12 p
13 a
14 s
2)less命令
less file ##分页浏览
[kiosk@foundation72 Desktop]$ less 666 ##分页浏览
## Linux(小白)
hahaha
q
w
e
r
t
y
u
i
o
p
a
s
~
~
~
~
~
~
~
~
(END)
用法:
上|下 ##逐行移动
pageup|pagedown ##逐页移动
/关键字 ##高亮显示关键字,n向下匹配,N向上匹配
v ##进入vim模式 --->在vim模式中按":wq "退回到less模式
q ##退出
3)head 命令
head file ##查看文件前10行
head -n 3 file ##查看文件前3行
例:
[kiosk@foundation72 Desktop]$ head 666 ##查看文件前10行
## Linux(小白)
hahaha
q
w
e
r
t
y
u
i
[kiosk@foundation72 Desktop]$ head -n 4 666 ##查看文件前4行
## Linux(小白)
hahaha
q
w
[kiosk@foundation72 Desktop]$
4)tail命令
tail file ##查看文件后10行
tail -n 3 file ##查看文件后3行
[kiosk@foundation72 Desktop]$ tail 666 ##查看文件后10行
r
t
y
u
i
o
p
a
s
[kiosk@foundation72 Desktop]$ tail -n 4 666 ##查看文件后4行
p
a
s
[kiosk@foundation72 Desktop]$
9、文件的寻址
1)相对路径:
相对与当前系统所在目录的一个文件名称的简写,
此名称省略了系统当前所在目录的名称
此名称不以“/”开头
此名称在命令执行时会自动在操作对象前加入“PWD”所显示的值
[kiosk@foundation72 Desktop]$ pwd
/home/kiosk/Desktop
[kiosk@foundation72 Desktop]$ cp -r 777 /home/kiosk/Desktop/444 ##绝对路径
[kiosk@foundation72 Desktop]$ cp -r 777 444 ##相对路径
两种路径运行结果相同
2)绝对路径:
绝对路径是文件在系统的真实位置
此命令是以“/”开头的
此命令在命执行时系统不会考虑现在所在位置的信息
**注意**:
当操作对象是 对象1 空格 对象2 时,
这两个对象之间没有任何关系
豆角 茄子 -----------这是2个对象
豆角茄子 ------------这是1个对象
10、自动补齐
系统中的键可以实现命令的自动补齐
可以补齐系统中存在的命令,文件名称,和部分命令的参数
当一次<TAB>补齐不了时代表以此关键字开头的内容不唯一
可以用<TAB>X2来列出所有以此关键字开头的内容
11、关于路径的命令
1)显示路径
pwd
[kiosk@foundation72 Destop]$ pwd
/home/kiosk/Desktop
2)切换路径
cd 目录名称 -------进入到指定目录中
cd - ------------------当前目录和当前目录值前所在目录之间的切换
cd ~ ------------------进入当前用户家目录
cd ~user ------------进入user家目录
cd … ----------------进入当前目录的上级目录
[kiosk@foundation72 Desktop]$ cd 第二天 ##进入到指定目录中
[kiosk@foundation72 第二天]$ cd - ##当前目录(第二天)和当前目录值前所在目录(Desktop)之间的切换
/home/kiosk/Desktop
[kiosk@foundation72 Desktop]$ cd ~ ##进入当前用户家目录
[kiosk@foundation72 ~]$ cd Desktop
[kiosk@foundation72 Desktop]$ cd 第二天
[kiosk@foundation72 第二天]$ cd .. ##进入当前目录的上级目录
[kiosk@foundation72 Desktop]$ cd ~kiosk ##-进入user(kiosk)家目录
[kiosk@foundation72 ~]$
3)查看命令
ls
[kiosk@foundation72 Desktop]$ ls 666
666
ls -l file----文件属性
[kiosk@foundation72 Desktop]$ ls -l 666
-rw-rw-r--. 1 kiosk kiosk 52 Dec 28 19:53 666
ls dir------目录中包含的内容
[kiosk@foundation72 Desktop]$ ls -s 444
total 12
4 1 0 555 0 777 8 unit2
ls -a dir-----所有文件包含隐藏的
[kiosk@foundation72 Desktop]$ ls -a 444
. .. 1 555 777 unit2
ls -s dir------查看文件包含及其大小
[kiosk@foundation72 Desktop]$ ls -s 444
total 12
4 1 0 555 0 777 8 unit2
ls -S dir------查看并且按照大小排序
[kiosk@foundation72 Desktop]$ ls -S 444
unit2 1 777 555
ls -d dir-----目录本身
[kiosk@foundation72 Desktop]$ ls -d 444
444
4)文件大小统计
wc
[kiosk@foundation72 Desktop]$ wc 666
16 18 64 666
wc -l ---------行数
[kiosk@foundation72 Desktop]$ wc -l 666
16 666
wc -w ---------单词数
[kiosk@foundation72 Desktop]$ wc -w 666
18 666
wc -m ----------字符数
[kiosk@foundation72 Desktop]$ wc -m 666
58 666
wc -c -----------字节数
[kiosk@foundation72 Desktop]$ wc -c 666
64 666
12、系统中历史的调用
history ---------查看历史
history -c ------清空当前环境中的历史
!数字 -----------调用此行历史比如"!5"时执行历史中地5条命令
!test ------------执行以test关键字开头的最近一条历史
ctrl +r +关键字 ----调用历史中最近一条含有此关键字的历史
[kiosk@foundation72 Desktop]$ history ##查看历史
1 wc 666
2 wc -l 666
3 wc -w 666
4 wc -m 666
5 wc -c 666
6 history
[kiosk@foundation72 Desktop]$ !2 ##调用第二行历史执行历史中第二条命令
wc -l 666
16 666
[kiosk@foundation72 Desktop]$ !h ##-执行以h开头的最近一条历史
history
1 wc 666
2 wc -l 666
3 wc -w 666
4 wc -m 666
5 wc -c 666
6 history
7 wc -l 666
8 history
13、在系统1.查看命令大用途
1)whatis 命令 -------查看命令的用途
例:
[kiosk@foundation72 Desktop]$ whatis cp
cp (1) - copy files and directories
cp (1p) - copy files
2)–help-----查看命令的用法
[ ] --------标示选项可以加也可以不加
… -------标示选项加入的个数任意
<> -------标示选项是必须要加入的
Userage: ----标示命令用法
Opetions: ----对参数的作用做说明
例:
[kiosk@foundation72 Desktop]$ touch --help
Usage: touch [OPTION]... FILE...
Update the access and modification times of each FILE to the current time.
A FILE argument that does not exist is created empty, unless -c or -h
is supplied.
A FILE argument string of - is handled specially and causes touch to
change the times of the file associated with standard output.
Mandatory arguments to long options are mandatory for short options too.
-a change only the access time
-c, --no-create do not create any files
-d, --date=STRING parse STRING and use it instead of current time
-f (ignored)
-h, --no-dereference affect each symbolic link instead of any referenced
file (useful only on systems that can change the
timestamps of a symlink)
-m change only the modification time
-r, --reference=FILE use this file's times instead of current time
-t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time
--time=WORD change the specified time:
WORD is access, atime, or use: equivalent to -a
WORD is modify or mtime: equivalent to -m
--help display this help and exit
--version output version information and exit
Note that the -d and -t options accept different time-date formats.
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'touch invocation'
3)man-----查看命令或文件的说明手册
man 命令
man 5 文件
man的级别
1 #命令
2 #系统调用接口
3 #函数库调用接口
4 #特殊设备文件
5 #系统配置文件
6 #游戏规则说明
7 #包说明
8 #系统管理命令
9 #内核调用规则
man的基本用法
上|下 ##逐行查看
pgup|pgdn ##分页查看
/关键字 ##高亮显示关键字,n向下匹配,N上向匹配
:q ##退出
man -k 字符串 ##下看当前字符串在系统中拥有的man的级别
/usr/share/doc ##系统中所有安装过的软件的帮助文档
14、系统中的通用配置符号
* ##匹配0到任意字符
? ##匹配单个字符
[[:alpha:]] ##单个字母
[[:lower:]] ##单个小写字母
[[:upper:]] ##单个大写字母
[[:digit:]] ##单个数字
[[:punct:]] ##单个符号
[[:space:]] ##单个空格
[[:alnum:]] ##单个数字或字母
[1-9] #1位,这位是1-9的任意数字
[!3-5] #1位,不是3-5的数字
[^3-5] #1位,不是3-5的数字
[a-z] #1位,a-z的任意字母
{1..3} #3个,1 2 3
{1,4,7} #3个1,4,7这三个数
~ #当前用户家目录
~username #指定用户的家目录
~+ #当前目录
~- #进入当前目录之前所在目录(olddir)