jck28-linux常用命令之文件处理

ssh

1、连接ssh

ssh -p username@host

ssh jck28表单填写手机号后4位@shell.ceshiren.com
密码:hogwarts

## 查看帮助
ls --help
man ls #一行一行翻页按回车、一屏一屏翻按空格,往前翻按b,退出按q

##文件管理
### 查看文件信息: ls
ls -a 查看全部文件(包含隐藏文件)
一个点代表当前目录,两个点代表上一级目录

ls -l /ll 显示详细信息

ls -al 显示文件和详细信息

### 切换工作目录: cd
绝对路径: cd /usr/bin
[jck287213@shell.ceshiren.com ~]$ cd /usr/bin
切换会家目录: cd 回车

相对路径:
[jck287213@shell.ceshiren.com home]$ cd ../bin
[jck287213@shell.ceshiren.com bin]$ 


### 显示当前目录路径: pwd
[jck287213@shell.ceshiren.com hogwarts]$ pwd
/home/jck287213/hogwarts

### 创建新目录:mkdir
创建单级目录`预先格式化的文本`
[jck287213@shell.ceshiren.com ~]$ mkdir hogwarts
[jck287213@shell.ceshiren.com ~]$ mkdir feier
[jck287213@shell.ceshiren.com ~]$ ls
feier  hogwarts  test.sh

创建多级目录
[jck287213@shell.ceshiren.com hogwarts]$ mkdir a/b/c
mkdir: cannot create directory ‘a/b/c’: No such file or directory

需加参数 -p
[jck287213@shell.ceshiren.com hogwarts]$ mkdir a/b/c -p
[jck287213@shell.ceshiren.com hogwarts]$ ls
a

### 创建空文件:touch
[jck287213@shell.ceshiren.com hogwarts]$ touch 1.txt
[jck287213@shell.ceshiren.com hogwarts]$ cat 1.txt 
[jck287213@shell.ceshiren.com hogwarts]$ 

### 删除文件或[jck287213@shell.ceshiren.com hogwarts]$ touch 1.txt
[jck287213@shell.ceshiren.com hogwarts]$ cat 1.txt 
[jck287213@shell.ceshiren.com hogwarts]$ ls
1.txt  a
[jck287213@shell.ceshiren.com hogwarts]$ rm 1.txt 
[jck287213@shell.ceshiren.com hogwarts]$ ls
a

### 删除文件或目录:rm
删除空目录:-r
[jck287213@shell.ceshiren.com b]$ rm c
rm: cannot remove ‘c’: Is a directory
[jck287213@shell.ceshiren.com b]$ rm -r c
[jck287213@shell.ceshiren.com b]$ ls

强制删除目录: -f
[jck287213@shell.ceshiren.com b]$ rm -rf c

询问删除:-i
[jck287213@shell.ceshiren.com a]$ rm -ri b
rm: remove directory ‘b’? y
[jck287213@shell.ceshiren.com a]$ 


### 拷贝:cp
拷贝文件:
[jck287213@shell.ceshiren.com ~]$ touch 1
[jck287213@shell.ceshiren.com ~]$ ls
1  feier  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ cp ./1 ./feier
[jck287213@shell.ceshiren.com ~]$ ls
1  feier  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ cd feier/
[jck287213@shell.ceshiren.com feier]$ ls
1
[jck287213@shell.ceshiren.com feier]$ 

拷贝目录:加-a
[jck287213@shell.ceshiren.com feier]$ ls
1  a
[jck287213@shell.ceshiren.com feier]$ cp ./a ../hogwarts/
cp: omitting directory ‘./a’
[jck287213@shell.ceshiren.com feier]$ cp -a ./a ../hogwarts/
[jck287213@shell.ceshiren.com feier]$ ls
1  a
[jck287213@shell.ceshiren.com feier]$ cd ..
[jck287213@shell.ceshiren.com ~]$ ls
1  feier  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ cd hogwarts/
[jck287213@shell.ceshiren.com hogwarts]$ ls
a
[jck287213@shell.ceshiren.com hogwarts]$ 


### 移动/重命名:mv
重命名:
[jck287213@shell.ceshiren.com hogwarts]$ cd 
[jck287213@shell.ceshiren.com ~]$ ls
1  feier  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ mv 1 f
[jck287213@shell.ceshiren.com ~]$ ls
f  feier  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ 

移动文件:
[jck287213@shell.ceshiren.com ~]$ ls
f  feier  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ mv f ./hogwarts/
[jck287213@shell.ceshiren.com ~]$ ls
feier  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ cd hogwarts/
[jck287213@shell.ceshiren.com hogwarts]$ ls
a  f
[jck287213@shell.ceshiren.com hogwarts]$ 

移动并且重命名文件:
[jck287213@shell.ceshiren.com hogwarts]$ ls
a  f
[jck287213@shell.ceshiren.com hogwarts]$ mv f ../feier/m
[jck287213@shell.ceshiren.com hogwarts]$ ls
a
[jck287213@shell.ceshiren.com hogwarts]$ cd ../feier/
[jck287213@shell.ceshiren.com feier]$ ls
1  a  m


### 建立文件链接:ln
软连接:+s(不占用磁盘空间,删除源文件,目标文件失效,不能用了)
[jck287213@shell.ceshiren.com ~]$ ls
1  feier  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ ll
total 12
lrwxrwxrwx 1 jck287213 jck287213    9 Oct  8 20:14 1 -> ./feier/1
drwxrwxr-x 3 jck287213 jck287213 4096 Oct  8 20:11 feier
drwxrwxr-x 3 jck287213 jck287213 4096 Oct  8 20:11 hogwarts
-rwxrwxr-x 1 jck287213 jck287213   26 Oct  7 22:18 test.sh
[jck287213@shell.ceshiren.com ~]$ cd feier/
[jck287213@shell.ceshiren.com feier]$ ls
1  a  m
[jck287213@shell.ceshiren.com feier]$ rm 1
[jck287213@shell.ceshiren.com feier]$ cd ..
[jck287213@shell.ceshiren.com ~]$ ls
1  feier  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ ll
total 12
lrwxrwxrwx 1 jck287213 jck287213    9 Oct  8 20:14 1 -> ./feier/1
drwxrwxr-x 3 jck287213 jck287213 4096 Oct  8 20:14 feier
drwxrwxr-x 3 jck287213 jck287213 4096 Oct  8 20:11 hogwarts
-rwxrwxr-x 1 jck287213 jck287213   26 Oct  7 22:18 test.sh
[jck287213@shell.ceshiren.com ~]$ cat 1
cat: 1: No such file or directory

![image|800x546](upload://8L6R9vhWkagFyoaSBspiPqoywob.png)


### 查找文件:find
[jck287213@shell.ceshiren.com ~]$ touch 1.txt
[jck287213@shell.ceshiren.com ~]$ touch 2.txt
[jck287213@shell.ceshiren.com ~]$ ls
1.txt  2.txt  feier  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ find ./ -name 1.txt
./1.txt
[jck287213@shell.ceshiren.com ~]$ find ./ -name '*.txt'
./1.txt
./2.txt


### 查看文件内容:cat、less、more、head、tail
[jck287213@shell.ceshiren.com hogwarts]$ less nginx.log 
[jck287213@shell.ceshiren.com hogwarts]$ cat nginx.log 
[jck287213@shell.ceshiren.com hogwarts]$ more nginx.log 
[jck287213@shell.ceshiren.com hogwarts]$ head nginx.log 
[jck287213@shell.ceshiren.com hogwarts]$ head -n 10 nginx.log 
[jck287213@shell.ceshiren.com hogwarts]$ tail -n 10 nginx.log  

### 打包压缩:tar
压缩:
[jck287213@shell.ceshiren.com ~]$ tar -zcvf f.tar.gz 1.txt 2.txt
1.txt
2.txt
[jck287213@shell.ceshiren.com ~]$ ls
1.txt  2.txt  feier  f.tar.gz  hogwarts  test.sh

解压缩到当前目录:
[jck287213@shell.ceshiren.com ~]$ rm 1.txt 2.txt 
[jck287213@shell.ceshiren.com ~]$ ls
feier  f.tar.gz  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ tar -xf f.tar.gz 
[jck287213@shell.ceshiren.com ~]$ ls
1.txt  2.txt  feier  f.tar.gz  hogwarts  test.sh

解压缩到指定目录:
[jck287213@shell.ceshiren.com ~]$ cd feier/
[jck287213@shell.ceshiren.com feier]$ ls
a  m
[jck287213@shell.ceshiren.com feier]$ cd ..
[jck287213@shell.ceshiren.com ~]$  tar -xf f.tar.gz -C ./feier/
[jck287213@shell.ceshiren.com ~]$ ls
1.txt  2.txt  feier  f.tar.gz  hogwarts  test.sh
[jck287213@shell.ceshiren.com ~]$ cd feier/
[jck287213@shell.ceshiren.com feier]$ ls
1.txt  2.txt  a  m
[jck287213@shell.ceshiren.com feier]$ 


## 文本处理
### 文本编辑:vi/vim
文本编辑界面:光标跳到开头按gg,光标跳到结尾按G
[Vim] 光标移动到行首、行尾
Shift + 6 : 行首
Shift + 4 : 行尾

### 屏幕输出:echo
[jck287213@shell.ceshiren.com feier]$ echo 123
123

### 输出重定向:>
[jck287213@shell.ceshiren.com feier]$ echo 123 > n.txt
[jck287213@shell.ceshiren.com feier]$ ls
1.txt  2.txt  a  hog  m  n.txt
[jck287213@shell.ceshiren.com feier]$ cat n.txt 
123

## 文件属性
![image|800x313](upload://ztpNRFvQ8Vm9DvLmpUsKIp6xVHM.jpeg)

## 修改文件权限
r:读权限 read — 4
w:写权限 write — 2
x:执行权限 execute — 1
-: 无权限 — 0
chmod 777 文件
[jck287213@shell.ceshiren.com feier]$ ll
total 12
-rw-rw-r-- 1 jck287213 jck287213    0 Oct  8 20:22 1.txt
-rw-rw-r-- 1 jck287213 jck287213    0 Oct  8 20:22 2.txt
drwxrwxr-x 2 jck287213 jck287213 4096 Oct  7 23:11 a
-rw-rw-r-- 1 jck287213 jck287213   33 Oct  8 20:46 hog
-rw-rw-r-- 1 jck287213 jck287213    0 Oct  7 23:08 m
-rw-rw-r-- 1 jck287213 jck287213    4 Oct  8 20:49 n.txt
[jck287213@shell.ceshiren.com feier]$ chmod 777 1.txt 
[jck287213@shell.ceshiren.com feier]$ ll
total 12
-rwxrwxrwx 1 jck287213 jck287213    0 Oct  8 20:22 1.txt
-rw-rw-r-- 1 jck287213 jck287213    0 Oct  8 20:22 2.txt
drwxrwxr-x 2 jck287213 jck287213 4096 Oct  7 23:11 a
-rw-rw-r-- 1 jck287213 jck287213   33 Oct  8 20:46 hog
-rw-rw-r-- 1 jck287213 jck287213    0 Oct  7 23:08 m
-rw-rw-r-- 1 jck287213 jck287213    4 Oct  8 20:49 n.txt

## 查看网卡信息
ifconfig
ip addr

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.19.147.8  netmask 255.255.240.0  broadcast 172.19.159.255
        ether 00:16:3e:04:1c:75  txqueuelen 1000  (Ethernet)
        RX packets 658592431  bytes 238581038818 (222.1 GiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 561733446  bytes 382054803847 (355.8 GiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

## 测试远程主机的连通性
ping
-c:ping 的次数
-i:每次 ping 的时间间隔

[jck287213@shell.ceshiren.com feier]$ ping -c 3 -i 3 www.baidu.com 
PING www.a.shifen.com (153.3.238.102) 56(84) bytes of data.
64 bytes from 153.3.238.102 (153.3.238.102): icmp_seq=1 ttl=49 time=11.4 ms
64 bytes from 153.3.238.102 (153.3.238.102): icmp_seq=2 ttl=49 time=11.2 ms
64 bytes from 153.3.238.102 (153.3.238.102): icmp_seq=3 ttl=49 time=11.2 ms

--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 6005ms
rtt min/avg/max/mdev = 11.209/11.297/11.474/0.174 ms


## 打印 Linux 网络系统的状态信息
netstat
-t:列出所有 tcp
-n:以数字形式显示地址和端口
-p:显示进程的 pid 和名字

[jck287213@shell.ceshiren.com feier]$ netstat -tnp
(No info could be read for "-p": geteuid()=10316 but you should be root.)
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0     52 172.19.147.8:22         211.161.248.214:52799   ESTABLISHED -                   
tcp        0      0 172.19.147.8:50562      140.205.135.3:443       ESTABLISHED -                   
tcp        0      0 172.19.147.8:47260      100.100.30.26:80        ESTABLISHED -                   
tcp6       0      0 172.19.147.8:9100       47.95.206.45:29654      ESTABLISHED -                   
tcp6       0      0 127.0.0.1:43748         127.0.0.1:25            TIME_WAIT   -                   
[jck287213@shell.ceshiren.com feier]$ 

## 退出 Linux 系统
exit