一,创建文件
touch file 建立空文件,或修改文件的时间戳
touch file1 file2 连续创建多个文件
2,建立目录
mkdir dir1 创建一个单层目录
mkdir dir2 dir3 dir3 同时创建多个单层目录
mkdir -p dir/dir1/dir2 创建一个多层目录
命令如下:
[kiosk@foundation42 Desktop]$ mkdir dir1
[kiosk@foundation42 Desktop]$ mkdir dir2 dir3
[kiosk@foundation42 Desktop]$ mkdir -p dir4/d/dd/ddd
3,文件及目录的删除
rm -f file 强行删除文件不提示
rm -fr dir 强行删除目录本身和里面的内容补提时
注意:去掉-f时,在超级用户模式删除操作下会有是否删除提示,普通模式加不加“-f”都一样
[kiosk@foundation42 Desktop]$ rm test1
[kiosk@foundation42 Desktop]$ rm test2
[kiosk@foundation42 Desktop]$ rm -f test3
[kiosk@foundation42 Desktop]$ rm -r dir1
[kiosk@foundation42 Desktop]$ rm -fr dir4
[kiosk@foundation42 Desktop]$ rm -r dir2
[root@foundation42 Desktop]# rm -r dir3
rm: remove directory ‘dir3’? y
[root@foundation42 Desktop]# rm -fr dir5
[root@foundation42 Desktop]#
四,文件的编辑
(1)vim file 进入编辑模式
进入文件后是浏览模式,不能编辑
[zhou@localhost Desktop]$ vim test1
按下<I> 或<Inset>键进入插入模式写字符,左下角会显示一个"INSETT"
编写完成后按<ESC>退出插入模式
输入“:”+wq 完成文件编辑并退出。w表示写入,q退出
退出后返回到shell命令界面
(2)vim下的使用技巧
*1,字符的处理方法:
yy 复制一整行
yl 复制一个字母
yw 复制一个单词
p 粘贴
dd 删除一整行
dl 删除一个字母
dw 删除一个单词
cc 剪掉整行
cl 剪掉一个字母
cw 剪掉一个单词
注意:
当剪切后vim会进入插入模式,
如果要粘贴需要按<esc>退出插入模式然后粘贴
u 撤销
快捷键:ctrl+r 恢复
*2.字符的批量处理
快捷键:ctrl + v 进入字符批量处理模式,化该模式中可以批量选择字符并操作
如何批量添加字符
1)把光标移动到要加入字符所在列
2)ctrl + v 进入可视化模式
3)选中要家字符所在列
4)按<I>进入插入模式并写入要加入的字符
5)按<ESC>批量添加结束
如何批量修改
:%s/原有字符/修改后字符/g
:1,5s/原有字符/修改后字符/g (将1到5行字符进行替换)
*3.如何同时编辑多个文件
vim file
:sp file1
注意:
光标默认在那个文件中操作的就是那个文件
ctrl+w 按完放开在按上或下可以移动光标所在窗口
*4,vim的退出
正常退出方法
:q ##当文件没有做任何修改是可用
:q! ##当文件修改但不行保存修改时可用
:wq ##退出保存
:wq! ##当文件属于自己或用户为root时可用
异常退出
当vim非正常退出会产生.filename.swap文件
当此文件出现,vim工作异常,如下:
E325: ATTENTION
Found a swap file by the name ".test2.swp"
owned by: zhou dated: Sat Aug 4 05:22:59 2018
file name: ~zhou/Desktop/test2
modified: YES
user name: zhou host name: localhost.localdomain
process ID: 3714 (still running)
While opening file "test2"
dated: Sat Aug 4 05:14:05 2018
(1) Another program may be editing the same file. If this is the case,
be careful not to end up with two different instances of the same
file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r test2"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".test2.swp"
to avoid this message.
Swap file ".test2.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:
Swap file ".test2.swp" already exists!
Interrupt: Press ENTER or type command to continue
五,文件的查卡
cat file 输出文件的所有内容
[zhou@localhost Desktop]$ cat test1
ijasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkdf
jasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkdf
jasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkdf
jasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkidn
zhe shi wo bian ji de yi duan hua ..
[zhou@localhost Desktop]$
cat -b file 输出文件所有内容并加入行号
[zhou@localhost Desktop]$ cat -b test1
1 ijasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
2 asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
3 cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkdf
4 jasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
5 asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
6 cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkdf
7 jasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
8 asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
9 cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkdf
10 jasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
11 asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
12 cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkidn
13 zhe shi wo bian ji de yi duan hua ..
[zhou@localhost Desktop]$
less 分页浏览
ijasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkdf
jasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkdf
jasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkdf
jasjdjskdlajdlsajdlasjdlsakjdlasjkldajsdlkjsaljjdgshgsjd
asjhdjashdkjfgygwquyeuwqyequweyiqdhjashdsgfhvcqwpeopo[qw
cnxzbcnbvjdksajdlakdlaskdwqoehfushdcbzjkakasjknzcjhdnkidn
zhe shi wo bian ji de yi duan hua ..
~
~
~
~
~
~
~
~
~
(END)
上|下 向上|向下 移动行
<pgup>|<pgdn> ##向上|向下移动一页
/关键字 高亮显示关键字,n向下匹配N向上匹配
q 退出
tail -n 显示文件的后多少行
head -n 显示文件的前多少行
六,文件的复制
cp sourcefile destfile ##文件复制
[zhou@localhost Desktop]$ cp test1 copy1
cp sourcefile1 sourcefile2 destdir/ ##复制多个文件到目录中
[zhou@localhost Desktop]$ cp test1 copy1 dirtest1
cp -r sourcedir destdir/ ##复制目录
[zhou@localhost Desktop]$ cp -r dirtest1 dietest2
七,文件的移动
mv sourcefile destfile 重名命
将桌面的copy1文件重命名为renametext1
[zhou@localhost Desktop]$ mv copy1 renametext1
mv sourcefile destdir 移动
将桌面的renametext1文件移动到桌面的dirtest1目录中
[zhou@localhost Desktop]$ mv renametext1 dirtest1
mv sourcedir destdir 移动目录
将目录dirtest1移动到目录mulu中
[zhou@localhost Desktop]$ mv dirtest1 mulu
mv test/* . "."代表当前目录
将目录mulu中的文件全部移动到当前目录(当前目录为桌面)
[zhou@localhost Desktop]$ mv mulu/* .
八,文件路径
1.相对路径
文件相对当前系统位置的一个名称简写
文件名称省略了当前路径的值
只有当前在此目录中时可以使用
文件名称不以/开头
文件名称会自动在字符前加入'pwd'显示的路径
2.绝对路径
是文件在系统中真实的位置
任何时间都可以精确表示一个文件的名称
文件名称以/开头
九,关于路径的命令
pwd ##显示当前工作目录
[zhou@localhost Desktop]$ pwd
/home/zhou/Desktop
cd ##切换当前工作目录
[zhou@localhost ~]$ cd /home/zhou/Desktop
[zhou@localhost Desktop]$ touch test1
cd /mnt ##切换到/mnt目录中
切换到桌面的mulu目录中(“/”代表根目录,这里有的相对路径,不加“/”)
[zhou@localhost Desktop]$ cd mulu
[zhou@localhost mulu]$
cd - ##进入当前目录之前所在目录中
返回之前的目录
[zhou@localhost mulu]$ cd -
/home/zhou/Desktop
[zhou@localhost Desktop]$
cd ~student ##进入到student用户家目录中
注意用户名字是不同的
[zhou@localhost Desktop]$ cd ~zhou
[zhou@localhost ~]$
十,显示文件信息
ls dirname 显示文件信息
显示文件信息
[root@localhost Desktop]#ls krb5.conf
krb5.conf
[root@localhost Desktop]#ls te/we/ke
test1 test2 test3
[root@localhost Desktop]#
ls -l dirname 长列表显示文件信息
[root@localhost Desktop]#ls -l krb5.conf
-rw-r--r-- 1 root root 495 Aug 8 07:15 krb5.conf
[root@localhost Desktop]#ls -l te
total 0
drwxr-xr 3 root root 15 Aug 8 05:43 we
ls -a dirname 显示所有文件包括以“.”开头的隐藏文件
首先创建一个隐藏文件
[root@localhost Desktop]#tuoch te/.free
编辑隐藏文件
[root@localhost Desktop]#vim .free
[root@localhost Desktop]#ls -a te
. .. .free we
ls -s dirname 显示文件大小
[root@localhost Desktop]#ls -s krb5.conf
4 krb5.conf
[root@localhost Desktop]#ls -s te
total 0
0 we
ls -S dirname 文件大小排序
给te目录下创建一些文件
[root@localhost Desktop]#touch te/file1
[root@localhost Desktop]#mkdir te/dir
查看并排序
[root@localhost Desktop]#ls -S te
we dir1 file1
ls -d dirname 显示目录
[root@localhost Desktop]#ls -d te
te
[root@localhost Desktop]#ls -d krb5.conf
krd5.conf
ls -R dirname 第归显示
[root@localhost Desktop]#ls -R te
te:
dir1 file1 we
te/dir1:
te/we:
ke
te/we/ke:
test1 test2 test3
十一,通配符
根目录下etc目录中有很多文件
* 匹配0到任意字符
? 匹配单个字符
复制etc里的4个字符的文件到桌面
删除桌面上所有文件
[[:alpha:]] 单个字母
[[:upper:]] 单个大写字母
[[:lower:]] 单个小写字母
[[:digit:]] 单个数字
[[:alnum:]] 单个数字或字母
[[:space:]] 单个空格
[[:pubct:]] 单个符号
先创建多个文件:
选择删除中间字符为字母的文件
{1..3} 精确指定1到3
创建文件结尾1到3的文件
{a..c} 精确指定a到c
[1-3] 模糊指定1到3
[ac] a和c
选择删除中间为@和2的文件
[^ac] 除了a或c
[!ac] 除了a或c