0
点赞
收藏
分享

微信扫一扫

Linux文件管理命令

一、复制命令

1:​cp​:命令(copy)
2: cp        [option]...           source dest
    命令  【选项】 源文件...   目标路径
#默认,只能拷贝文件

#选项:
-t​:目标路径 源文件
-p​:保持文件属性
-r​:递归复制,复制目录及目录下的所有文件
-a​:相当于 ​-pr​ ,既可以拷贝目录,又可以保持文件属性
-i:提示
-v​:显示,拷贝过程
#用法:
cp​ (命令):​0 ? 20:57:33 root@jinnan,10.0.0.101:/home # cp /home/d /abc/

##复制并改名
0 ✓ 20:59:54 root@jinnan,10.0.0.101:/home # ​cp /home/d /abc/jin

jin​是否存在
- 存在
1)存在,并且jin是个目录:将d放到/abc/jin目录下,不改名
2)存在,并且jin是个普通文件:则提示是否要覆盖,输入y则覆盖,输入n则不覆盖
- 不存在:复制到/abc下,并且改名为jin

#举例:
##​-p​保持文件属性
0 ✓ 21:04:22 root@jinnan,10.0.0.101:/home # ​cp -p /home/d /abc/

#源文件:0 ✓ 21:04:06 root@jinnan,10.0.0.101:/abc # cd /home

0 ✓ 21:04:13 root@jinnan,10.0.0.101:/home # lltotal 0-rw-r--r--. 1 root root 0 Mar 21 23:28 d

#查看文件属性:0 ✓ 21:04:50 root@jinnan,10.0.0.101:/abc # ll
total 0
-rw-r--r--. 1 root root 0 Mar 21 23:28 d

##​-r​:递归拷贝
21:09:31 root@jinnan,10.0.0.101:/home # ​cp -r /home /abc

#查看:0 ✓ 21:11:25 root@jinnan,10.0.0.101:/abc/home # ll
total 0
-rw-r--r--. 1 root root 0 Mar 23 21:09 d
drwxr-xr-x. 3 root root 20 Mar 23 21:09 home
drwxr-xr-x. 2 root root 6 Mar 23 21:09 jinbo
drwxr-xr-x. 2 root root 6 Mar 23 21:09 jinli
drwxr-xr-x. 2 root root 32 Mar 23 21:09 jinnan

##​-a​拷贝目录,保持目录属性
0 ✓ 21:15:48 root@jinnan,10.0.0.101:/home # ​cp -a /home /abc

#查看:0 ✓ 21:16:24 root@jinnan,10.0.0.101:/abc/home # ll
total 0
-rw-r--r--. 1 root root 0 Mar 21 23:28 d
drwxr-xr-x. 3 root root 20 Mar 21 19:00 home
drwxr-xr-x. 2 root root 6 Mar 22 00:02 jinbo
drwxr-xr-x. 2 root root 6 Mar 21 23:15 jinli
drwxr-xr-x. 2 root root 32 Mar 21 23:54 jinnan

##​-r​提示
[root@localhost ~]# ​cp -i /abc/oldboy.txt /tmp/zhujiao

cp: overwrite ‘/tmp/zhujiao’​?

## 避免提示有两种方法
1.使用转义符:让转义符后面的内容,做它自己
[root@localhost ~]# ​\cp -a /etc /tmp/

2.使用命令的绝对路径
[root@localhost ~]# ​/bin/cp -a /etc /tmp/

## ​-v​显示拷贝的过程
0 ✓ 21:25:36 root@jinnan,10.0.0.101:~ # ​cp -av /home /abc

‘/home’ -> ‘/abc/home’
‘/home/home’ -> ‘/abc/home/home’
‘/home/home/oldboy’ -> ‘/abc/home/home/oldboy’
‘/home/home/oldboy/test1’ -> ‘/abc/home/home/oldboy/test1’
‘/home/jinnan’ -> ‘/abc/home/jinnan’
‘/home/jinnan/home’ -> ‘/abc/home/jinnan/home’
‘/home/jinnan/jinnan’ -> ‘/abc/home/jinnan/jinnan’
‘/home/jinbo’ -> ‘/abc/home/jinbo’
‘/home/jinli’ -> ‘/abc/home/jinli’
‘/home/d’ -> ‘/abc/home/d’

二、删除命令

普通文件查看命令
1:​rm​ :remove(命令)
#默认,只能删除普通文件
##语法
     rm        [option]...     file...
    命令         [选项]...      文件...
##选项
-i​:提示
-r​:递归删除
-f​: force强制
-----------------

-d​:只能删除空目录

##举例
0 ✓ 21:34:47 root@jinnan,10.0.0.101:/abc/home # ​rm jin.txt

rm: remove regular empty file ​‘jin.txt’?​ y

#​-r​递归删除
0 ✓ 21:35:57 root@jinnan,10.0.0.101:~ #​ rm -r /abc/home

rm: descend into directory ‘/abc/home’? y
rm: descend into directory ‘/abc/home/home’? y
rm: descend into directory ‘/abc/home/home/oldboy’? y
rm: remove directory ‘/abc/home/home/oldboy/test1’? y
rm: remove directory ‘/abc/home/home/oldboy’? y
rm: remove directory ‘/abc/home/home’? y
rm: descend into directory ‘/abc/home/jinnan’? y
rm: remove regular empty file ‘/abc/home/jinnan/home’? y
rm: remove regular empty file ‘/abc/home/jinnan/jinnan’? y
rm: remove directory ‘/abc/home/jinnan’?

##​-f​强制删除

0 ✓ 21:37:54 root@jinnan,10.0.0.101:~ # ​rm -rf /abc/home

rm - r​ :删除目录
rm ​:删除文件

三、普通文件查看命令

cat​(命令):​只能查看文件内容

##语法
   cat     [option]...   [file]...
  命令       选项...    普通文件...
##选项
-n​(numder):打印行号,显示行号
-E​(end):常看文件每一行的结尾,以$符表示
-T​(tab):查看文件中每一行内容是否含有制表符,以^I表示一个制表符
-A​:即显示制表符,又显示文件的结尾
 

##举例:​e​cho​(所见即所得)
0 ✓ 21:38:38 root@jinnan,10.0.0.101:/ # ​echo www.baidi > 1.txt

0 ✓ 21:47:27 root@jinnan,10.0.0.101:/ # ​echo www.baidi > 2.txt

0 ✓ 21:47:40 root@jinnan,10.0.0.101:/ # ​echo www.baidi > 3.txt

0 ✓ 21:47:46 root@jinnan,10.0.0.101:/ # ​cat 3.txt  www.baidi

0 ✓ 21:47:56 root@jinnan,10.0.0.101:/ # ​cat 1.txt  www.baidi

#​-n​打印行号
0 ✓ 21:51:14 root@jinnan,10.0.0.101:/ # ​cat -n /etc/virc

1 if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
2 set fileencodings=ucs-bom,utf-8,latin1
3 endif
4
5 set nocompatible " Use Vim defaults (much better!)
6 set bs=indent,eol,start " allow backspacing over everything in insert mode
7 "set ai " always set autoindenting on
8 "set backup " keep a backup file
9 set viminfo='20,\"50 " read/write a .viminfo file, don't store more
10 " than 50 lines of registers
11 set history=50 " keep 50 lines of command line history
12 set ruler " show the cursor position all the time
13
14 " Only do this part when compiled with support for autocommands
15 if has("autocmd")

#​-E​查看文件结尾

0 ✓ 21:51:28 root@jinnan,10.0.0.101:/ # ​cat -E /etc/virc

if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"$
set fileencodings=ucs-bom,utf-8,latin1$
endif$
$
set nocompatible " Use Vim defaults (much better!)$
set bs=indent,eol,start " allow backspacing over everything in insert mode$
"set ai " always set autoindenting on$
"set backup " keep a backup file$
set viminfo='20,\"50 " read/write a .viminfo file, don't store more$
" than 50 lines of registers$
set history=50 " keep 50 lines of command line history$
set ruler " show the cursor position all the time$
$
" Only do this part when compiled with support for autocommands$
if has("autocmd")$
augroup redhat$
autocmd!$
" In text files, always limit the width of text to 78 characters$
" autocmd BufRead *.txt set tw=78$
" When editing a file, always jump to the last cursor position$
autocmd BufReadPost *$
\ if line("'\"") > 0 && line ("'\"") <= line("$") |$
\ exe "normal! g'\"" |$

#-T查看文件中的制表符
0 ✓ 21:54:39 root@jinnan,10.0.0.101:~ # ​cat -T 1.txt

1^I2
2^I3^I^I4
^I3
^I^I4
^I5
^I^I6
^I7
8
#​-A​即查看制表符,又查看文件结尾
0 ✓ 21:55:06 root@jinnan,10.0.0.101:~ # ​cat -A 1.txt

1^I2$
2^I3^I^I4$
^I3$
^I^I4$
^I5$
^I^I6$
^I7$
8$

## 高难度用法​​(在文件不存在的情况下,创建文件并写入内容)

# 1.直接​vi​一个不存在的文件
0 ✓ 21:57:47 root@jinnan,10.0.0.101:~ # ​vi 2.txt

0 ✓ 21:58:22 root@jinnan,10.0.0.101:~ # ll
total 12
-rw-r--r--. 1 root root 30 Mar 23 21:54 1.txt
-rw-r--r--. 1 root root 334 Mar 23 21:58 2.txt

# 2.​echo​内容重定向到一个文件中
0 ✓ 21:58:24 root@jinnan,10.0.0.101:~ # ​echo 123 > 5.txt

0 ✓ 21:59:22 root@jinnan,10.0.0.101:~ # ll
total 16
-rw-r--r--. 1 root root 30 Mar 23 21:54 1.txt
-rw-r--r--. 1 root root 334 Mar 23 21:58 2.txt
-rw-r--r--. 1 root root 4 Mar 23 21:59 5.txt

# 3.使用​cat​创建文件并写入内容
0 ✓ 21:59:23 root@jinnan,10.0.0.101:~ # ​​cat >> 3.txt <<eof 

>123

>eof

0 ✓ 22:01:02 root@jinnan,10.0.0.101:~ # ll
total 20
-rw-r--r--. 1 root root 30 Mar 23 21:54 1.txt
-rw-r--r--. 1 root root 334 Mar 23 21:58 2.txt
-rw-r--r--. 1 root root 4 Mar 23 22:01 3.txt

#eof:end of 以...结尾,不是固定用法,规范用法

​#注意:cat查看文件时,会将文件所有内容全部加载到内存里

四、普通文件查看命令 扩展

1#:​more​(命令)
#语法: more(命令) 文件名
按回车,一行一行向下显示
按空格,一页一页向下显示
Ctrl + b:向上查看
Ctrl + f:向下查看

 2#:​less​(命令)
#:语法:less(命令) 文件名
Ctrl + b:向上查看
Ctrl + f:向下查看
按回车,一行一行向下显示
按空格,一页一页向下显示
输入/关键字,高亮显示,所有匹配到的内容
按n,查找​​下一个​​关键字
Shift + n 查找​​上一个​​关键字
--------------------
3#:​head

#语法:head(命令) 文件名
默认显示一个文件的前10行内容
#选项
​​-n​​:指定查看文件开头的前N行,N可以省略
0 ✓ 22:25:50 root@jinnan,10.0.0.101:~ # ​head -16 /etc/virc

if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
set fileencodings=ucs-bom,utf-8,latin1
endif
set nocompatible " Use Vim defaults (much better!)
set bs=indent,eol,start " allow backspacing over everything in insert mode
"set ai " always set autoindenting on
"set backup " keep a backup file
set viminfo='20,\"50 " read/write a .viminfo file, don't store more
" than 50 lines of registers
set history=50 " keep 50 lines of command line history
set ruler " show the cursor position all the time
" Only do this part when compiled with support for autocommands
if has("autocmd")
augroup redhat

4#:​tail

tail(命令) 文件名
默认显示一个文件的后10行内容
0 ✓ 22:26:29 root@jinnan,10.0.0.101:~ #​ tail /etc/virc

if &term=="xterm"
set t_Co=8
set t_Sb=m
set t_Sf=m
endif
" Don't wake up system with blinking cursor:
" http://www.linuxpowertop.org/known.php
let &guicursor = &guicursor . ",a:blinkon0"
#选项
​​-n​​:指定查看文件末尾的后N行,n可以省略
​​-f​​:follow,跟踪,动态跟踪一个文件的末尾
0 ✓ 22:32:07 root@jinnan,10.0.0.101:~ # ​tail -f 1.txt

1 2
2 3 4
3
4
5
6
7

举报

相关推荐

0 条评论