Linux-莫烦python学习笔记(linux)

Linux 系统的初尝试。学习自莫烦python。

官网

Linux 简易教学 | 莫烦 Python (yulizi123.github.io)

【莫烦 Python】Linux 简易教学

课程

在 Ubuntu 中打开 Terminal 窗口

Ctrl+Shift+T

png

和文件打交道

在 Terminal 中, 默认打开文件管理器中的 Home 文件夹, 可以使用 pwd 命令查看当前文件路径

shell
gz@ubuntu:~$ pwd
/home/gz
png

cd 指令

使用 cd 指令, 我们能在 Terminal 中轻松切换到不同的文件夹

shell
gz@ubuntu:~$ cd Documents
gz@ubuntu:~/Documents$ 

接着你会看到它在下一行跳出了这个东西, 在 $ 前面的 ~/Documents 就说明你现在已经在 Documents 这个文件夹里了. 你现在要执行的命令将会在这个目录下生效.

cd.. 返回上一级目录

shell
gz@ubuntu:~/Documents$ cd ..
gz@ubuntu:~$ 

cd 去往子文件夹

shell
gz@ubuntu:~$ cd Documents/folder1
gz@ubuntu:~/Documents/folder1$ 
png

cd - 返回刚刚所在的目录

shell
gz@ubuntu:~/Documents/folder1$ cd -
/home/gz
gz@ubuntu:~$ 

cd ../.. 向上返回两次

shell
gz@ubuntu:~$ cd Documents/folder1
gz@ubuntu:~/Documents/folder1$ cd ../..
gz@ubuntu:~$ 

cd~ 去往 Home 文件夹

shell
gz@ubuntu:~/Documents/folder1$ cd ~
gz@ubuntu:~$

cd 绝对路径

shell
gz@ubuntu:~/Documents/folder1$ cd ~
gz@ubuntu:~$

ls 指令

查看文件夹里的文件内容

png
shell
gz@ubuntu:~/Documents$ ls
file1  folder1

ls -l (long 的简写)输出详细信息

shell
gz@ubuntu:~/Documents$ ls -l
total 4
-rw-rw-r-- 1gz gz    0 Aug 11 21:32 file1
drwxrwxr-x 2gz gz 4096 Aug 11 21:12 folder1

ls -a (all 的简写)显示所有文件, 还会显示隐藏的文件(以.开头)

shell
gz@ubuntu:~/Documents$ ls -a
.  ..  file1  folder1

ls -lh (human 的简写) 输出的字符串方便人看

shell
gz@ubuntu:~/Documents$ ls -lh
total 4.0K
-rw-rw-r-- 1gz gz    0 Aug 11 21:32 file1
drwxrwxr-x 2gz gz 4.0K Aug 11 21:12 folder1

ls --help 查看帮助文档

shell
gz@ubuntu:~/Documents$ ls --help

touch 新建文件

新建一个空白文件

shell
gz@ubuntu:~/Documents$ touch file2

新建多个空白文件 以空格分开

shell
gz@ubuntu:~/Documents$ touch file3 file4 file5
png

cp 复制

cp(copy)是复制文件或者文件夹的命令, 常用的方式是复制"老文件"到"新文件"

shell
gz@ubuntu:~/Documents$ cp file1 file1copy
png

cp -i (interactive, 交互式)

注意: 如果 file1copy 已经存在, 它将会直接覆盖已经存在的 file1copy, 如果要直接覆盖, 我们在 cp 后面加一个选项

shell
gz@ubuntu:~/Documents$ cp -i file1 file1copy
cp: overwrite 'file1copy'? n
gz@ubuntu:~/Documents$ cp -i file1 file1copy
cp: overwrite 'file1copy'? y

将文件复制进文件夹

shell
gz@ubuntu:~/Documents$ cp file1 folder1/
png

复制文件夹, 需要加上 -R (recursive, 递归)

错误示范:

shell
gz@ubuntu:~/Documents$ cp folder1/ folder2/
cp: -r not specified; omitting directory 'folder1/'

正确示范:

shell
gz@ubuntu:~/Documents$ cp -R folder1/ folder2/
png

* 复制多个文件

shell
gz@ubuntu:~/Documents$ cp file* folder2/
png

单独选定多个文件复制到某个文件夹

shell
gz@ubuntu:~/Documents$ cp file1copy file2 folder1/
png

mv 剪切

移动去文件夹

shell
gz@ubuntu:~/Documents$ mv file1 folder1/

重命名文件

将文件移动到原始的地点, 但以不同的文件名命名

shell
gz@ubuntu:~/Documents$ mv file1 file1rename

mkdir 建立文件夹

shell
gz@ubuntu:~/Documents$ mkdir folder2

在文件夹里再创建文件夹

shell
gz@ubuntu:~/Documents$ mkdir folder2/f2
png

rmdir 移除文件夹

将要移除的文件夹必须是空的, 不然会失败

尝试移除非空文件夹:

shell
gz@ubuntu:~/Documents$ rmdir folder2
rmdir: failed to remove 'folder2': Directory not empty

移出空文件夹:

shell
gz@ubuntu:~/Documents$ rmdir folder2/f2

rm 移除文件

移除单个文件

shell
gz@ubuntu:~/Documents$ rm file1

-i-I有提示地移除文件

shell
gz@ubuntu:~/Documents$ rm -i file2 file3 file4
rm: remove regular empty file 'file2'? y
rm: remove regular empty file 'file3'? 
rm: remove regular empty file 'file4'? y

-I要超过 3 个文件才进行提示

shell
gz@ubuntu:~/Documents$ rm -I *
rm: remove 6 arguments? y

-r 或 -R 删除文件夹

shell
gz@ubuntu:~/Documents$ rm -r folder1

删库跑路

shell
rm -rf /*

rm: 移除命令

r: 递归文件删除

f: 强制删除

/:根目录

*: 所有文件

nano 文字编辑

shell
gz@ubuntu:~/Documents$ nano t.py
png png

cat (cantenate, 链接)

查看文件内容

shell
gz@ubuntu:~/Documents$ cat t.py
print("Hello world!")
print("This is a Python script!")

> 将文件的内容放到另一个文件里

shell
gz@ubuntu:~/Documents$ cat t.py > t1.py
png

> 将多个文件的内容打包一起放入另一个文件

shell
gz@ubuntu:~/Documents$ cat t.py t1.py > t2.py
png

>> 将内容添加在一个文件末尾

shell
gz@ubuntu:~/Documents$ cat t3.py >> t2.py
png

文件权限

ls -l 查看文件权限

shell
gz@ubuntu:~/Documents$ ls -l
total 16
-rw-rw-r-- 1gz gz  56 Aug 11 22:17 t1.py
-rw-rw-r-- 1gz gz 132 Aug 11 22:22 t2.py
-rw-rw-r-- 1gz gz  20 Aug 11 22:21 t3.py
-rw-rw-r-- 1gz gz  56 Aug 11 22:15 t.py
png
  • Type: 很多种 (最常见的是 - 为文件, d 为文件夹, 其他的还有l, n … 这种东西, 真正自己遇到了, 网上再搜就好, 一次性说太多记不住的).

  • User: 后面跟着的三个空是使用 User 的身份能对这个做什么处理 (r 能读; w 能写; x 能执行; - 不能完成某个操作).

  • Group: 一个 Group 里可能有一个或多个 user, 这些权限的样式和 User 一样.

  • Others: 除了 User 和 Group 以外人的权限.

以下t1.py为例

shell
-rw-rw-r-- 1gz gz  56 Aug 11 22:17 t1.py

-文件; rw-用户可以读写, 不可执行; rw-组用户可以读写, 不可执行;r--其他用户可读, 不可执行

chmod 修改权限

尝试运行 t1.py

shell
gz@ubuntu:~/Documents$ python3 t1.py
Hello world!
This is a Python script!

但是该命令下运行 t1.py, 会因没有相应权限而被阻止

shell
gz@ubuntu:~/Documents$ ./t1.py
bash: ./t1.py: Permission denied

给 t1.py 增加执行权限

shell
gz@ubuntu:~/Documents$ chmod u+x t1.py

[谁]

  • u: 对于 User 修改
  • g: 对于 Group 修改
  • o: 对于 Others 修改
  • a: (all) 对于所有人修改

[怎么修改]

  • +, -, =: 作用的形式, 加上, 减掉, 等于某些权限
  • r, w, x 或者多个权限一起, 比如 rx

一个使用 Python 的技巧

在 t1.py 文件头添加

python
#!/usr/bin/python3

若有执行权限, 就可以这么执行:

shell
gz@ubuntu:~/Documents$ ./t1.py
Hello world!
This is a Python script!
png

否则执行失败:

shell
gz@ubuntu:~/Documents$ ./t1.py
./t1.py: line 1: syntax error near unexpected token `"Hello world!"'
./t1.py: line 1: `print("Hello world!")'

远程操控

怎么样从 MacOS/Windows 或 Linux 通过 SSH 远程 Linux

Windows 下:

shell
ssh [用户名]@[linux 系统的 ip 地址]

linux 查看本机 IP 地址:(ifconfig 命令, Windows 为 ipconfig 命令..我还以为我拼错了)

shell
gz@ubuntu:~$ ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.89.130  netmask 255.255.255.0  broadcast 192.168.89.255
        inet6 fe80::8565:1325:bab3:8017  prefixlen 64scopeid 0x20<link>
        ether 00:0c:29:95:09:31  txqueuelen 1000  (Ethernet)
        RX packets 27262  bytes 18561854 (18.5 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 15044  bytes 2133723 (2.1 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 4135  bytes 456719 (456.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 4135  bytes 456719 (456.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

(192.168.89.130)

png

怎么样从手机 (Android 安卓/IOS 苹果) 通过 SSH 远程 Linux

在 app store 里搜"JuiceSSH"

怎么样用 TeamViewer 和 VNC 从远程控制电脑

TeamViewer: 适合公网, 对网速要求高

VNC: 适合局域网

云端机器学习

算了, 以后有机会再折腾吧..