Linux基础(二)

 1. mkdir,rmdir

mkdir创建目录
farsight@ubuntu:~/test$ mkdir d1 d2 d3        # 同时创建 3 个目录 d1 d2 d3
farsight@ubuntu:~/test$ ls
d1  d2  d3
farsight@ubuntu:~/test$ mkdir d1/d11 d2/d21   # 创建子目录
farsight@ubuntu:~/test$ ls d1 d2
d1:
d11

d2:
d21
farsight@ubuntu:~/test$ mkdir d3/d31/d311     # 要创建 d311 子目录,但它的父目录 d31 不存在,因此报错
mkdir: cannot create directory ‘d3/d31/d311’: No such file or directory
farsight@ubuntu:~/test$ mkdir -p d3/d31/d311  # 使用 -p 选项可以创建路径中不存在的中间目录
farsight@ubuntu:~/test$ ls d3 -R
d3:
d31

d3/d31:
d311

d3/d31/d311:
 rmdir 删除空目录
farsight@ubuntu:~/test$ rmdir d1/d11 d2/d21   # 删除两个子目录

farsight@ubuntu:~/test$ rmdir d3              # 非空目录不能删除
rmdir: failed to remove 'd3': Directory not empty
farsight@ubuntu:~/test$ rmdir -p d3/d31/d311  # -p 选项删除路径中所有空目录
farsight@ubuntu:~/test$ ls
1  11.txt  1.txt  2.txt  3.txt  d1  d2

farsight@ubuntu:~/test$ rmdir d*              # 使用通配符删除以 d 开头的空目录
farsight@ubuntu:~/test$ ls
1  11.txt  1.txt  2.txt  3.txt

2. cp 复制文件目录

farsight@ubuntu:~/test$ cp /usr/include/stdio.h .    # 复制文件到当前目录(.)
farsight@ubuntu:~/test$ ls
1  11.txt  1.txt  2.txt  3.txt  stdio.h

farsight@ubuntu:~/test$ cp *.txt stdio.h d1   # 复制多个文件到 d1 目录,使用通配符 *
farsight@ubuntu:~/test$ ls d1
11.txt  1.txt  2.txt  3.txt  stdio.h

-t,选项反转源和目标的顺序,即将右边的文件复制到左边的目录 d2 中。

farsight@ubuntu:~/test$ cp -t d2 1.txt 2.txt 3.txt    
farsight@ubuntu:~/test$ ls d2
1.txt  2.txt  3.txt

复制目录

farsight@ubuntu:~/test$ cp d1 d3              # 复制目录,没有使用 -r 选项会报错
cp: -r not specified; omitting directory 'd1'
farsight@ubuntu:~/test$ cp -r d1 d3           # 复制目录,要使用 -r 选项。注意目标目录 d3 此时不存在,则 d3 与 d1 的内容相同
farsight@ubuntu:~/test$ ls d3
11.txt  1.txt  2.txt  3.txt  stdio.h
farsight@ubuntu:~/test$ cp -r d1 d3           # 复制目录,此时 d3 已存在,则将 d1 复制到 d3 下面成为其子目录
farsight@ubuntu:~/test$ ls d3
11.txt  1.txt  2.txt  3.txt  d1  stdio.h

注意:-r 与 -R 相同。

3. rm 删除文件目录

删除文件

farsight@ubuntu:~/test$ rm 1 11.txt           # 删除多个文件
farsight@ubuntu:~/test$ ls
1.txt  2.txt  3.txt  d1  d2  d3  stdio.h

farsight@ubuntu:~/test$ rm *.txt              # 使用通配符
farsight@ubuntu:~/test$ ls
d1  d2  d3  stdio.h

 删除目录

farsight@ubuntu:~/test$ rm d1                 # 删除目录报错
rm: cannot remove 'd1': Is a directory
farsight@ubuntu:~/test$ rm -r d1              # 使用 -r 选项可以删除目录,递归进入子目录进行删除,删除目录时还是调用 rmdir 命令
farsight@ubuntu:~/test$ ls
d2  d3  stdio.h

 -i 选项在每删除一个文件时都要询问,回答 y 删除,n 不删除

farsight@ubuntu:~/test$ rm -i stdio.h         
rm: remove regular file 'stdio.h'? n          # 回答 n 表示不删除
farsight@ubuntu:~/test$ ls
d2  d3  stdio.h
farsight@ubuntu:~/test$ rm -i stdio.h 
rm: remove regular file 'stdio.h'? y          # 回答 y 表示删除
farsight@ubuntu:~/test$ ls
d2  d3
farsight@ubuntu:~/test$ rm -rf d2             # -f 选项表示不询问,直接删除
farsight@ubuntu:~/test$ ls
d3

4. mv 移动或改名文件

farsight@ubuntu:~/test$ mv d3 d1     # 将目录 d3 改名为 d1,因为 d1 不存在
farsight@ubuntu:~/test$ ls
d1

farsight@ubuntu:~/test$ mv d1 d2     # d2 是已存在的,则将 d1 移动到 d2 下面
farsight@ubuntu:~/test$ ls
d2
farsight@ubuntu:~/test$ ls d2
d1

farsight@ubuntu:~/test$ mv *.txt d2  # 移动多个文件到目录

5. sudo 以管理员身份运行

farsight@ubuntu:~/test$ fdisk -l     # 普通用户不能使用 fdisk 命令,否则报错无权操作
fdisk: cannot open /dev/loop0: Permission denied

farsight@ubuntu:~/test$ sudo fdisk -l   # 使用 sudo 以管理员身份运行 fdisk 命令,要求输入当前用户的密码
[sudo] password for farsight: 

6. echo 显示变量的值或文本

farsight@ubuntu:~/test$ echo $PATH   # 显示环境变量 PATH 的值,注意引用变量时要在前面使用 $ 符号,但对变量赋值时不能使用 $
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

farsight@ubuntu:~/test$ echo PATH=$PATH   # 显示字符串 PATH= 及变量 PATH 的值
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

# echo 命令显示提示文本,read 命令读取输入并保存到 MYNUM 自定义变量中

farsight@ubuntu:~/test$ echo 请输入一个整数:; read MYNUM   
请输入一个整数:
12
farsight@ubuntu:~/test$ echo MYNUM=$MYNUM
MYNUM=12

farsight@ubuntu:~/test$ echo -n 请输入一个整数:; read MYNUM  # 默认输出字符串后换行,-n 选项不换行
请输入一个整数:34
farsight@ubuntu:~/test$ echo "Hello\nworld"      # 转义字符默认不生效
Hello\nworld
farsight@ubuntu:~/test$ echo -e "Hello\nworld"   # 使用 -e 选项启用转义
Hello
world

使用 unset 命令(bash 的 built-in 内置命令)可以删除变量和函数

7. touch 创建一个空文件或修改文件的最后修改时间

farsight@ubuntu:~/test$ touch 1.txt 2.txt 3.txt    # 创建三个空文件,它们原来不存在
farsight@ubuntu:~/test$ ls -l
total 36
-rw-rw-r-- 1 farsight farsight     0 7月  13 11:17 1.txt
-rw-rw-r-- 1 farsight farsight     0 7月  13 11:17 2.txt
-rw-rw-r-- 1 farsight farsight     0 7月  13 11:17 3.txt
-rw-r--r-- 1 farsight farsight 29665 7月  13 11:17 stdio.h

farsight@ubuntu:~/test$ touch stdio.h              # 因为 stdio.h 已存在,修改它的最后修改时间为当前时间
farsight@ubuntu:~/test$ ls -l stdio.h 
-rw-r--r-- 1 farsight farsight 29665 7月  13 11:18 stdio.h

       在编程中,由于两个系统的时间存在时间差,比如在两个时区的系统之间可能存在时间差,从东部时区打包的文件到西部时区,就有可能存在这种情况,即西部时区解包的文件,是在未来的某个时间创建的,这显然不符合逻辑。要想纠正这种“时间错”,可以使用 touch * 来修改所有文件的最后修改时间。

8. cat,less,more

cat

一般用于显示行数不多的文件内容。显示完毕返回命令行,不会停留在文件中。

farsight@ubuntu:~/test$ cat /etc/passwd    # 显示密码文件的内容

farsight@ubuntu:~/test$ cat -n /etc/passwd   # -n 显示行号

farsight@ubuntu:~/test$ cat -ns stdio.h    # -s 压缩连续的多行为一行
less

可以上下翻页,上下逐行滚动,查找。不想以编辑的方式查看文件内容时,可以使用 less 命令

farsight@ubuntu:~/test$ less stdio.h    # 查看 stdio.h 的内容

上下箭头可以上下滚动一行,回车向下滚动一行,PageUp 向上翻页,PageDown 向下翻页,
/word 查找 word,并高亮显示所有的 word,输入 n 向下查找下一个 word,输出 N 向上查找
?word 查找 word,并高亮显示所有的 word,输入 N 向下查找下一个 word,输出 n 向上查找
q 退出

more

回车向下翻一行,空格向下翻一页,q 退出,显示当前所处位置的百分比

farsight@ubuntu:~/test$ more stdio.h 

9.head,tail

head

显示文件的前 n 行

farsight@ubuntu:~/test$ head stdio.h        # 默认显示文件的前 10 行

farsight@ubuntu:~/test$ head -20 stdio.h    # -20 指定显示文件的前 20 行

farsight@ubuntu:~/test$ head -c 20 stdio.h  # -c 20 显示前 20 个字节
tail

显示文件的末 n 行

farsight@ubuntu:~/test$ tail stdio.h        # 默认显示文件的末 10 行

farsight@ubuntu:~/test$ tail -20 stdio.h    # -20 指定显示文件的末 20 行

farsight@ubuntu:~/test$ tail -c 20 stdio.h  # -c 20 显示末 20 个字节

10.ln 链接

硬链接:可以理解为一个文件取了多个名称。
              如果删除硬链接,只有当硬链接数为 0 时才从磁盘删除这个文件。
              限制:不能跨设备(文件系统)创建硬链接,不能对目录创建硬链接。
符号链接:链接文件中保存了目标文件的路径。可以通过链接文件找到目标文件。
              删除链接文件,则仅将其自动删除,不会删除它所链接的目标文件。
              不存在硬链接的两个限制。

farsight@ubuntu:~/test$ ls -i           # 查看文件的 inode 号
135190 1.txt  135129 2.txt  135134 3.txt  134035 d2  135113 stdio.h
farsight@ubuntu:~/test$ ln stdio.h stdio-ln.h   # 创建硬链接
farsight@ubuntu:~/test$ ls -i
135190 1.txt  135129 2.txt  135134 3.txt  134035 d2  135113 stdio.h  135113 stdio-ln.h   # stdio.h 与 srdio-ln.h 的 inode 相同
farsight@ubuntu:~/test$ ls -l stdio*    # 硬链接数为 2
-rw-r--r-- 2 farsight farsight 29665 7月  13 11:18 stdio.h
-rw-r--r-- 2 farsight farsight 29665 7月  13 11:18 stdio-ln.h
farsight@ubuntu:~/test$ rm stdio-ln.h   # 删除其中一个硬链接,数目变为 1
farsight@ubuntu:~/test$ ls -l stdio*
-rw-r--r-- 1 farsight farsight 29675 7月  13 11:56 stdio.h

farsight@ubuntu:~/test$ ln d2 d2-ln     # 不能对目录创建硬链接
ln: d2: hard link not allowed for directory

11.ifconfig,ping

ifconfig
farsight@ubuntu:~/test$ ifconfig    # 查看活动网卡信息
farsight@ubuntu:~/test$ ifconfig ens33   # 查看 ens33 网卡信息

farsight@ubuntu:~/test$ ifconfig -a      # 查看所有网卡信息,包含不活动的网卡

farsight@ubuntu:~/test$ ifconfig ens33 down   # 普通用户不能关闭网卡
SIOCSIFFLAGS: Operation not permitted

farsight@ubuntu:~/test$ sudo ifconfig ens33 down   # 管理员身份关闭网卡
[sudo] password for farsight: 

farsight@ubuntu:~$ sudo ifconfig ens33 up     # 打开网卡
[sudo] password for farsight: 

farsight@ubuntu:~$ sudo ifconfig ens33 192.168.90.215   # 修改 IP 地址,这是临时修改,下次重启恢复

ens33 是网卡名
lo 是本地环回网卡,发送到这个网卡上的信息还可以读取回来

ping

测试与远程主机的网络是否连通

farsight@ubuntu:~$ ping 192.168.90.166 -c 4     # 指定 IP,-c 4 测试 4 个包,不带 -c 则一直测试下去,可以用 CTL+C 停止
PING 192.168.90.166 (192.168.90.166) 56(84) bytes of data.
64 bytes from 192.168.90.166: icmp_seq=1 ttl=128 time=0.155 ms    # 发送的是 ICMP 包
64 bytes from 192.168.90.166: icmp_seq=2 ttl=128 time=0.178 ms
^C
--- 192.168.90.166 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1016ms
rtt min/avg/max/mdev = 0.155/0.166/0.178/0.017 ms
farsight@ubuntu:~$ ping baidu.com -c 4          # 指定域名

12.man, info

info 比 man 更细致,但一般还是用 man 更多些

farsight@ubuntu:~$ man 3 printf    # 查看第 3 章中的 printf() 函数
farsight@ubuntu:~$ man 1 printf    # 查看第 1 章中的 printf 命令

13.vim 文本编程器

三种模式:命令模式,插入模式,末行模式,它们之间必须通过 命令模式 进行切换:
插入模式 <--> 命令模式 <--> 末行模式
打开一个文件就进入 命令模式。

farsight@ubuntu:~$ vim     # 不带文件名,匿名文件,需要在保存时提供名称
farsight@ubuntu:~/test$ vim 2.txt    # 带文件名,则会以此名称保存。如果文件不存在就新建,存在就打开编辑多个文件:	
farsight@ubuntu:~/test$ vim 1.txt 2.txt -O    # -O 从左到右排列文件
2 files to edit
farsight@ubuntu:~/test$ vim 1.txt 2.txt -o    # -o 从上到下排列文件
2 files to edit
farsight@ubuntu:~/test$ vim 1.txt 2.txt -p    # -p 标签方式排列文件

farsight@ubuntu:~/test$ vim +200 stdio.h      # 打开文件并定位到 200 行

在 VIM 的末行模式下:    
:split 1.txt  以从上到下方式排列文件
:vsplit 1.txt 以从左到右方式排列文件
:tabe 1.txt   以标签方式排列文件
:set cuc      设置光标所在列标记
:set cul      设置光标所在行下划线
:set cc=80    设置列标
:set co=90    设置屏幕宽度

14.chmod 修改文件权限

 两种模式:
    . 字符模式,[ugoa][+-=][rwx],u 所有者,g 所属组,o 其他用户,a 所有用户;+ 增加,- 去掉,= 设置
    . 数字模式,用 "八进制数" 表示权限,如:
rw-rw-r--  --> 二进制:110110100  --> 八进制:664

farsight@ubuntu:~/test$ chmod g+w,o+w stdio.h    # 所属组和其他用户都增加写权限,多个模式用逗号分隔
farsight@ubuntu:~/test$ chmod go-w stdio.h       # 所属组和其他用户都去掉写权限
farsight@ubuntu:~/test$ chmod g=rw stdio.h       # 所属组设置读写权限
farsight@ubuntu:~/test$ chmod 644 stdio.h        # 设置权限为 rw-r--r--
farsight@ubuntu:~/test$ chmod 777 stdio.h        # 设置权限为 rwxrwxrwx

farsight@ubuntu:~/test$ sudo chmod -R 777 d2     # -R 递归设置目录级其子目录的权限

farsight@ubuntu:~/test$ chmod --reference=stdio.h 1.txt   # 参考 stdio.h 的权限来设置 1.txt 的权限

15.chown, chgrp,passwd,su

chown, chgrp 更改所有组和所属组
farsight@ubuntu:~/test$ sudo chown root:root 1.txt    # 设置为 root 用户 root 组

farsight@ubuntu:~/test$ sudo chown farsight 1.txt     # 只改所有者而不改所属组

farsight@ubuntu:~/test$ sudo chown :farsight 1.txt    # 只改所属组而不改所有者

farsight@ubuntu:~/test$ sudo chgrp root 1.txt         # 改所属组

farsight@ubuntu:~/test$ sudo chown -R :root d2        # -R 递归更改
  
farsight@ubuntu:~/test$ sudo chown --reference=1.txt 2.txt   # 参考 1.txt 来更改 2.txt 的所属组
passwd 更改密码

超级用户可以更改所有人的密码。
普通用户只能更改自身密码,并且对密码有限制,太简单不行,太相似也不行。

farsight@ubuntu:~/test$ passwd 
Changing password for farsight.
(current) UNIX password:             # 输入原密码 123456
Enter new UNIX password:             # 输入新密码 654321
Retype new UNIX password:            # 确认新密码 654321
Bad: new and old password are too similar    # 报告太相似,不通过
Enter new UNIX password:             # 输入新密码 abcdef
Retype new UNIX password:            # 确认新密码 abcdef
Bad: new password is too simple      # 报告太简单,不通过
Enter new UNIX password:             # 输入复杂密码 1q2w3e4r
Retype new UNIX password:            # 确认复杂密码 1q2w3e4r
passwd: password updated successfully   # 通过

farsight@ubuntu:~/test$ sudo passwd farsight   # 以管理员身份更改 farsight 的密码
Enter new UNIX password:             # 输入新密码 123456,没有限制
Retype new UNIX password: 
passwd: password updated successfully   # 通过
su

更改用户 ID 或切换为超级用户

farsight@ubuntu:~/test$ sudo passwd root   # 更改超级用户密码
Enter new UNIX password:                   # 新密码为 1
Retype new UNIX password: 
passwd: password updated successfully
farsight@ubuntu:~/test$ su                 # 切换为超级用户,输入超级用户的密码
Password: 

root@ubuntu:/home/farsight/test# su farsight   # 切换为 farsight 用户,由于是超级用户切换的,因此不用输入密码,否则需要输入密码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值