Ubuntu学习笔记1#

[TOC]

这几天试用了一个服务器,寻思着就把Linux学了,服务器系统装了一个ubuntu我使用了ubuntu,所以找了个学习教程来看,至于为什么不看文档,因为这个教程就几个小时,并且只是个使用工具而已,最重要的是看视频不用动脑子,哈哈哈~~

小Tips#

  1. 在输入命令和路径的时候,按tab键可以自动补全
1
ls /ho  -> ls /home
  1. 输入历史可以翻阅

文件系统#

Linux使用统一的目录树结构

一级目录:

/ /root /mnt /etc
/home/username /bin /usr

用户目录#

支持多用户,每个用户一个目录

比如 /home/echih就代表有一个echin的用户,如果/home/hyc就代表有一个hyc的用户

/root这个不需要创建,为超级用户

linux的每个目录下面都有它对应的权限,权限机制非常严格,这个与windows不同,需要区分

创建用户#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
root@xx:~# adduser echin
info: Adding user `echin' ...
info: Selecting UID/GID from range 1000 to 59999 ...
info: Adding new group `echin' (1000) ...
info: Adding new user `echin' (1000) with group `echin (1000)' ...
info: Creating home directory `/home/echin' ...
info: Copying files from `/etc/skel' ...
New password: echin
Retype new password: echin
Sorry, passwords do not match.
passwd: Authentication token manipulation error
passwd: password unchanged
Try again? [y/N] y
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for echin
Enter the new value, or press ENTER for the default
Full Name []: echin
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
info: Adding new user `echin' to supplemental / extra groups `users' ...
info: Adding user `echin' to group `users' ...

切换到普通用户#

可以看见前面的root变成了echin

1
2
root@xxxx:~# su echin
echin@xxxx:/root$

同时切换到这个用户的目录上面

1
2
3
echin@iZbp19bvr4g3vqr24ft07rZ:~$ cd /home/echin
echin@iZbp19bvr4g3vqr24ft07rZ:~$ pwd
/home/echin

创建目录和文件#

目录:

1
2
3
4
5
6
7
8
# 创建一个目录
mkdir example1

# 创建多个目录
mkdir example2 example3

# 创建嵌套的目录
mkdir -p abc/ad/ddd

文件:

1
touch file.txt

删除目录和文件#

目录

1
2
3
4
5
6
7
8
9
10
11
# 删除一个空目录
rmdir example

# 删除一个目录以及所有内容 -r表示删除选中目录以及所有内容
rm -r example

# 强制删除一个目录以及所有内容
rm -rf example

# 只删除目录中的所有内容(保留目录)
rm -rf example/*

查看目录和文件#

1
2
3
4
5
6
# 展示当前文件夹下的所有文件和目录
ls
# 查看具体目录下的所有文件和目录
ls /xx/example
# 展现当前路径
pwd
命令 说明
ls -l 显示更加详细的信息
ls -a 显示所有文件(包括隐藏文件)
ls -R 递归显示所有子目录的内容

切换目录#

要求你已经知道绝对目录相对目录的知识

一般来说使用的命令都很简单,复杂的命令都一般存在于脚本形式中

1
2
3
4
5
6
7
8
9
10
11
# 切换到具体目录
cd /home/echin/xx
# 切换到上一个目录
cd ..
# 切换到下一个目录
cd ./home
# 切换到主目录
cd ~
# 随意发挥的时间
cd ../example #切换到上一个目录的子目录
cd ~/example #切换到主目录的子目录

复制文件和目录#

1
2
# 把example 复制为example1,可以添加路径
cp -rf example example1

移动目录和文件#

1
2
# 注意 第二个参数一定要是路径,并且不是当前路径
mv example example1/

重命名#

1
2
# 把example 重命名为 example1 ,注意第二个参数为当前路径下的目录
mv example example1

归档压缩#

合并文件tar 可以将多个文件和目录合并成一个归档文件,这使得管理和传输大量文件变得更加方便。

压缩:虽然 tar 本身不提供压缩功能,但它经常与压缩工具(如 gzip、bzip2、xz 等)一起使用来创建压缩归档。例如,tar.gz.tgz 文件是使用 gzip 压缩的 tar 归档,而 .tar.xz 是使用 xz 压缩的。

tar就是 tape archive 档案打包

归档 就是备份的意思,可以长期存储

创建档案包#

  • c 表示 创建档案
  • v 表示 显示详情
  • f表示 文件
1
2
3
4
5
# 就是将 file1,file2,file3等文件打包成xxx.tar形式的档案
# 一个目录打包
tar -cvf example.tar example
# 多个目录打包
tar -cvf xxx.tar file1 file2 file3

例子:

1
2
3
4
5
6
7
8
9
10
echin@ip:~$ tar -cvf  xxx.tar hello world
hello/
hello/example/
hello/example/example4/
hello/example/example4/tt.txt
hello/example/tt.txt
hello/tt.txt
world/
echin@iZbp19bvr4g3vqr24ft07rZ:~$ ls
hello world xxx.tar

释放档案包#

  • x 表示 解压档案
  • v 表示 显示详情
  • f表示 文件
1
2
3
4
# 解压到当前目录中
tar -xvf xxx.tar
# 解压到指定目录下 注意这个`C`必须要为大写
tar -xvf xxx.tar -C outDir

例子1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
echin@ip:~$ ls
hello world xxx.tar
echin@iZbp19bvr4g3vqr24ft07rZ:~$ rm -rf hello
echin@iZbp19bvr4g3vqr24ft07rZ:~$ rm -rf world
echin@iZbp19bvr4g3vqr24ft07rZ:~$ ls
xxx.tar
echin@ip:~$ tar -xvf xxx.tar
hello/
hello/example/
hello/example/example4/
hello/example/example4/tt.txt
hello/example/tt.txt
hello/tt.txt
world/
echin@iZbp19bvr4g3vqr24ft07rZ:~$ ls
hello world xxx.tar

例子2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
echin@ip:~$ tar -xvf xxx.tar -c world/ 
tar: You may not specify more than one '-Acdtrux', '--delete' or '--test-label' option
Try 'tar --help' or 'tar --usage' for more information.
echin@ip:~$ tar -xvf xxx.tar -C world/
hello/
hello/example/
hello/example/example4/
hello/example/example4/tt.txt
hello/example/tt.txt
hello/tt.txt
world/
echin@ip:~$ cd world/
echin@ip:~/world$ ls
hello world

归档并压缩#

先前创建的tar格式只是归档了,但是并没有压缩,所以体积较大

  • z 压缩的意思
  • 归档压缩后的文件为 *.tar.gz格式

压缩

1
tar -czvf xxx.tar.gz example

解压缩

1
2
3
4
# 解压缩到当前目录
tar -xzvf xxx.tar.gz
# 解压缩到指定目录中
tar -xzvf xxx.tar -C outDir

例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
echin@i:~$ ls
hello world xxx.tar
echin@r:~$ tar -czvf aaa.tar.gz world
world/
world/hello/
world/hello/example/
world/hello/example/example4/
world/hello/example/example4/tt.txt
world/hello/example/tt.txt
world/hello/tt.txt
world/world/
echin@i:~$ ls
aaa.tar.gz hello world xxx.tar
echin@i:~$ rm -rf hello world
echin@i:~$ ls
aaa.tar.gz xxx.tar
echin@i:~$ tar -xzvf aaa.tar.gz
world/
world/hello/
world/hello/example/
world/hello/example/example4/
world/hello/example/example4/tt.txt
world/hello/example/tt.txt
world/hello/tt.txt
world/world/
echin@i:~$ ls
aaa.tar.gz world xxx.tar

软链接#

软链接就是windows下面的快捷方式

软链接的特点:

  1. 删除软链接,对源文件没有任何影响
  2. 删除原文件,则软链接失效

创建软链接\#

  • s表示软链接
1
ln -s source linkname

例子:

1
2
3
4
5
6
7
8
9
10
11
echin@i:/root$ cd /home/echin
echin@i:~$ ls
aaa.tar.gz world xxx.tar
echin@i:~$ ln -s world link
echin@i:~$ ls
aaa.tar.gz link world xxx.tar #在服务器的显示中link的颜色是与其他有区别的--蓝色
echin@iZbp19bvr4g3vqr24ft07rZ:~$ ls
aaa.tar.gz link world xxx.tar
echin@i:~$ cd link
echin@i:~/link$ ls
hello world

查看软链接指向#

1
ls -l

例子

1
2
3
4
5
6
echin@i:~$ ls -l
total 20
-rw-rw-r-- 1 echin echin 264 May 30 13:09 aaa.tar.gz
lrwxrwxrwx 1 echin echin 5 May 30 17:17 link -> world
drwxrwxr-x 4 echin echin 4096 May 30 13:03 world
-rw-rw-r-- 1 echin echin 10240 May 30 12:56 xxx.tar