Linux实操篇(文件目录类:常用命令)

接上篇:Linux实操篇(查看命令帮助文档)

 

pwd:打印工作目录(print working directory)的绝对路径

[root@hadoop100 ~]# pwd
/root

-- 返回当前软链接所映射的真实目录,-P 选项也适用与 cd 命令,会直接进入到软链接映射的真实目录下
[root@hadoop100 ~]# pwd -P

cd:位置切换

-- 绝对路径以/(根目录)为基准,相对路径以当前目录为基准,cd命令用于位置切换,语法如下:
cd [绝对路径或者相对路径]

-- 以绝对路径进入到桌面下 [root@hadoop100 ~]# cd /root/桌面/ -- 以相对路径进入到视频目录下 -- ..(点点):表示返回上一级目录 [root@hadoop100 桌面]# cd ../视频/ -- cd - 表示退回到上一次的位置 [root@hadoop100 视频]# cd - /root/桌面 -- cd后面不写会进入到当前用户目录下,(cd ~ 也会进入到用户目录下) [root@hadoop100 桌面]# cd -- 返回上一级目录 [root@hadoop100 ~]# cd .. -- .(点):表示当前目录下 [root@hadoop100 /]# cd ./root/ [root@hadoop100 ~]#

ls(list):列出目录内容

-- 选项 -a:全部的文件,连同隐藏的文件或文件夹(开头为.(点)开头的文件或文件夹)都将被列出
-- 选项 -l:长数据串列出,包含文件的属性与权限等数据,相当于命令 “ll”
-- 选项 -i:显示出每个文件或目录的索引号
-- 选项 -h:之前文件大小是按字节来显示,现在是按kb,或者mb,Gb,这些按实际内存大小展示相应的单位
ls [选项] [目录或文件] [root@hadoop100 ~]# ls anaconda-ks.cfg initial-setup-ks.cfg 公共 模板 视频 图片 文档 下载 音乐 桌面 [root@hadoop100 ~]# ls -a . .bash_history .bashrc .cshrc .ICEauthority .mozilla 公共 图片 音乐 .. .bash_logout .cache .dbus initial-setup-ks.cfg .tcshrc 模板 文档 桌面 anaconda-ks.cfg .bash_profile .config .esd_auth .local .viminfo 视频 下载 [root@hadoop100 ~]# ls -l 总用量 8 -rw-------. 1 root root 1729 7月 25 16:25 anaconda-ks.cfg -rw-r--r--. 1 root root 1777 7月 25 16:28 initial-setup-ks.cfg drwxr-xr-x. 2 root root 6 7月 25 16:28 公共 drwxr-xr-x. 2 root root 6 7月 25 16:28 模板 drwxr-xr-x. 2 root root 6 7月 25 16:28 视频 drwxr-xr-x. 2 root root 6 7月 25 16:28 图片 drwxr-xr-x. 2 root root 6 7月 25 16:28 文档 drwxr-xr-x. 2 root root 6 7月 25 16:28 下载 drwxr-xr-x. 2 root root 6 7月 25 16:28 音乐 drwxr-xr-x. 2 root root 34 8月 12 09:11 桌面 -- 查看/(根目录)下的内容 [root@hadoop100 ~]# ls / bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var

mkdir、rmdir:创建目录、删除目录

-- 创建文件夹
-- 选项 -p 创建多层文件夹
mkdir [选项] 目录

-- 在当前目录下创建文件夹 a
[root@hadoop100 ~]# mkdir a
[root@hadoop100 ~]# ls
a  anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 在当前目录下创建文件夹 b c
[root@hadoop100 ~]# mkdir b c
-- 在当前目录下创建文件夹 d,并在文件夹d下创建e,并在文件夹e下创建文件夹f,但创建失败,必须创建上一级
[root@hadoop100 ~]# mkdir d/e/f
mkdir: 无法创建目录"d/e/f": 没有那个文件或目录
-- 此写法可以正常创建,但写法复杂
[root@hadoop100 ~]# mkdir d d/e d/e/f
[root@hadoop100 ~]# ls
a  anaconda-ks.cfg  b  c  d  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@hadoop100 ~]# ls d/
e
[root@hadoop100 ~]# ls d/e/
f
-- 可以使用选项 -p 进行多层文件夹创建
[root@hadoop100 ~]# mkdir -p g/h/i
[root@hadoop100 ~]# ls
a  anaconda-ks.cfg  b  c  d  g  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@hadoop100 ~]# ls g/
h
[root@hadoop100 ~]# ls g/h/
i

-- 删除文件夹
-- 选项 -p 进行多层文件夹删除
rmdir [选项] 目录

 -- 删除文件夹 a
[root@hadoop100 ~]# rmdir a
[root@hadoop100 ~]# ls
anaconda-ks.cfg  b  c  d  g  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 删除文件夹 b c
[root@hadoop100 ~]# rmdir b c
-- 删除多层文件夹 d,但失败,因为文件夹d是一个非空文件夹
[root@hadoop100 ~]# rmdir d
rmdir: 删除 "d" 失败: 目录非空
-- 此写法可以正常删除,但写法复杂
[root@hadoop100 ~]# rmdir d/e/f d/e/ d/
[root@hadoop100 ~]# ls
anaconda-ks.cfg  g  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 使用选项 -p 进行删除多层文件夹 g,但删除失败,因为文件夹 g非空
[root@hadoop100 ~]# rmdir -p g/
rmdir: 删除 "g/" 失败: 目录非空
-- 使用选项 -p 进行删除多层文件夹时,有如下写法,可以删除成功
[root@hadoop100 ~]# rmdir -p g/h/i
[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@hadoop100 ~]#

 touch:创建一个空文件(也可以使用 VIM命令 :wq来创建文件)

-- 创建一个空文件
-- fileName:文件名称,linux中空文件可以不带后缀名,默认是文本文件;
-- fileName:可以指定在任意目录下创建空文件,但目录必须存在;若不指定,则表示当前所在的目录下;
touch fileName

[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 在当前目录下创建一个空文件,文件名为:hello1
[root@hadoop100 ~]# touch hello1
[root@hadoop100 ~]# ls
anaconda-ks.cfg  hello1  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@hadoop100 ~]# touch 桌面/hello2
[root@hadoop100 ~]# ls 桌面/
hello2
[root@hadoop100 ~]# touch 桌面/a/hello3
touch: 无法创建"桌面/a/hello3": 没有那个文件或目录
[root@hadoop100 ~]# touch /root/桌面/hello4
[root@hadoop100 ~]# ls /root/桌面/
hello2  hello4

cp:复制文件或目录

-- 复制source文件到dest
-- 选项 -r:递归复制整个文件夹
-- source:源文件
-- dest:目标文件
-- 强制覆盖不提示的方法:\cp
cp [选项] source dest

-- 将当前目录下的文件复制到其他目录下
[root@hadoop100 ~]# ls
anaconda-ks.cfg  hello1  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@hadoop100 ~]# ls 桌面/
hello2  hello4
-- 将当前目录下的文件复制到当前目录下的子目录桌面下
[root@hadoop100 ~]# cp initial-setup-ks.cfg 桌面/
-- 复制成功
[root@hadoop100 ~]# ls 桌面/
hello2  hello4  initial-setup-ks.cfg
-- 若目标参数是一个文件,则会覆盖内容,由于是非安全操作,会给出提示
[root@hadoop100 ~]# cp initial-setup-ks.cfg 桌面/hello2
-- 输入 y表示确认,输入 n表示取消
cp:是否覆盖"桌面/hello2"? y
-- 若目标目录下已存在当前同名文件,也会覆盖内容,由于是非安全操作,会给出提示
[root@hadoop100 ~]# cp initial-setup-ks.cfg  桌面/
cp:是否覆盖"桌面/initial-setup-ks.cfg"? y
-- 使用原生命令 \cp 可以强制操作,没有提示
[root@hadoop100 ~]# \cp initial-setup-ks.cfg 桌面/
[root@hadoop100 ~]# ls 桌面/
hello2  hello4  initial-setup-ks.cfg

[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 使用 mkdir命令创建多层文件夹
[root@hadoop100 ~]# mkdir -p hello1/hello2/hello3/
[root@hadoop100 ~]# ls
anaconda-ks.cfg  hello1  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 将当前文件复制到多层文件夹中
[root@hadoop100 ~]# cp initial-setup-ks.cfg hello1/hello2/hello3/
[root@hadoop100 ~]# ls hello1/hello2/hello3/
initial-setup-ks.cfg
-- 使用选项 -r 将整个文件夹复制到桌面
[root@hadoop100 ~]# cp -r hello1/ 桌面/
[root@hadoop100 ~]# ls 桌面/
hello1
[root@hadoop100 ~]# ls 桌面/hello1/hello2/hello3/
initial-setup-ks.cfg

[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 将当前文件夹中的文件复制到当前目录下,并且重命名(备份文件)
[root@hadoop100 ~]# cp initial-setup-ks.cfg initial-setup-ks.cfg.bak
[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  initial-setup-ks.cfg.bak  公共  模板  视频  图片  文档  下载  音乐  桌面

mv:移动文件或目录

-- 重命名:将当前目录下的文件移动到当前目录下并且重命名
-- oldNameFile:源文件
-- newNameFile:新文件
mv oldNameFile newNameFile

-- 移动文件:将目录下的文件移动到另一个目录下
-- /temp/fileName:根目录下的temp目录下的源文件
-- /targetFolder:移动到根目录下的targetFolder目录下
mv /temp/fileName /targetFolder

[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  initial-setup-ks.cfg.bak  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 将当前目录下的文件进行重命名操作
[root@hadoop100 ~]# mv initial-setup-ks.cfg.bak initial-setup-ks.cfg.bak100
[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  initial-setup-ks.cfg.bak100  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@hadoop100 ~]# ls 桌面/
-- 将当前目录下的文件移动到 桌面下
[root@hadoop100 ~]# mv initial-setup-ks.cfg.bak100 桌面/
[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@hadoop100 ~]# ls 桌面/
initial-setup-ks.cfg.bak100

[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  initial-setup-ks.cfg.bak  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 将文件移动到其他目录,并且重命名
[root@hadoop100 ~]# mv initial-setup-ks.cfg.bak 桌面/1.cfg
[root@hadoop100 ~]# ls 桌面/
1.cfg

[root@hadoop100 ~]# mkdir -p a/b/c
[root@hadoop100 ~]# cp initial-setup-ks.cfg a/b/c/1.cfg
[root@hadoop100 ~]# ls a/b/c/
1.cfg
[root@hadoop100 ~]# ls
a  anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@hadoop100 ~]# ls 桌面/
-- 移动文件夹到其他目录
[root@hadoop100 ~]# mv a 桌面/
[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@hadoop100 ~]# ls 桌面/
a
[root@hadoop100 ~]# ls 桌面/a/b/c/
1.cfg

rm:删除文件或者目录

-- 删除文件或者目录
-- 选项 -r:递归删掉目录中的内容
-- 选项 -f:强制删除,不需要提示
-- 选项 -v:显示指令的详细执行步骤
-- dataFile:要删除的文件或者目录
rm [选项] dataFile

[root@hadoop100 ~]# ls
a  anaconda-ks.cfg  b  hello1  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 删除当前目录下的文件 a
[root@hadoop100 ~]# rm a
-- 删除是非安全操作,会有提示
rm:是否删除普通空文件 "a"?y
-- 使用选项 -f 强制删除文件b,不会有提示
[root@hadoop100 ~]# rm -f b
-- 删除文件夹时,必须使用 选项 -r,否则提示是一个目录无法删除。 -f 表示强制删除不会提示,
[root@hadoop100 ~]# rm -rf hello1/
[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@hadoop100 ~]# ls 桌面/
hello1  hello.txt
[root@hadoop100 ~]# rm 桌面/hello.txt
rm:是否删除普通空文件 "桌面/hello.txt"?y
-- 提示是一个目录无法删除,必须使用 选项 -r
[root@hadoop100 ~]# rm 桌面/hello1/
rm: 无法删除"桌面/hello1/": 是一个目录
-- 使用选项 -r递归删除一个目录,使用 选项 -rf 则可以直接删除文件夹,并且不会提示,但操作不安全,慎用!
[root@hadoop100 ~]# rm -r 桌面/hello1/
rm:是否进入目录"桌面/hello1/"? y
rm:是否进入目录"桌面/hello1/hello2"? y
rm:是否进入目录"桌面/hello1/hello2/hello3"? y
rm:是否删除普通文件 "桌面/hello1/hello2/hello3/initial-setup-ks.cfg"?y
rm:是否删除目录 "桌面/hello1/hello2/hello3"?y
rm:是否删除目录 "桌面/hello1/hello2"?y
rm:是否删除目录 "桌面/hello1/"?y
[root@hadoop100 ~]# ls 桌面/
-- 创建一个文件 a
[root@hadoop100 ~]# touch a
[root@hadoop100 ~]# ls
a  anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 选项 -v 显示操作指令
[root@hadoop100 ~]# rm -v a
rm:是否删除普通空文件 "a"?y
-- 显示的操作指令
已删除"a"
[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面

cat:查看文件内容

-- 查看文件内容
-- 选项 -n:显示所有行的行号,包括空行
-- fileName:要查看的文件名
cat [选项] fileName

[root@hadoop100 ~]# ls
anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 查看文件
[root@hadoop100 ~]# cat initial-setup-ks.cfg
#version=DEVEL
# X Window System configuration information
xconfig  --startxonboot
# License agreement
eula --agreed
...
...
...
-- 查看文件,并显示行号
[root@hadoop100 ~]# cat -n initial-setup-ks.cfg
     1  #version=DEVEL
     2  # X Window System configuration information
     3  xconfig  --startxonboot
     4  # License agreement
     5  eula --agreed
     6  ...
     7  ...
     8  ...

more:文件内容分屏查看器 

操作说明:
空格建(space):向下翻一页
F键:向下翻一页
回车键(enter):向下翻一行
q:立刻离开不在显示该文件内容
ctrl+f:向下滚动一屏
crtl+b:返回上一屏
=:输出当前行的行号
:f:输出文件名和当前行的行号

less:分屏显示文件内容

-- 查看文件内容
-- fileName:要查看的文件名
less fileName

操作说明:
空格建(space):向下翻一页
F键:向下翻一页
回车键(enter):向下翻一行
q:立刻离开不在显示该文件内容
ctrl+f:向下滚动一屏
crtl+b:返回上一屏
=:输出当前行的行号
:f:输出文件名和当前行的行号

[pageUp]:向上翻动一页
[pageDown]:向下翻动一页

shift + G:返回第一页
g键:返回第一页
G键:返回最后一页

/关键字:搜索指定字符,按 n:向下查找,N:向上查找
?关键字:搜索指定字符,按 n:向上查找,N:向下查找

head:显示文件头部内容

-- 查看文件头部内容
-- 选项 -n:查看文件头部 n 行内容
-- fileName:文件名称
head [选项] fileName

-- 查看文件头10行内容(默认不写是10行)
head fileName

-- 查看文件头5行内容(5可以是任意行数)
head -n 5 fileName

[root@hadoop100 ~]# head initial-setup-ks.cfg
#version=DEVEL
# X Window System configuration information
xconfig  --startxonboot
# License agreement
eula --agreed
# System authorization information
auth --enableshadow --passalgo=sha512
# Use CDROM installation media
cdrom
# Use graphical install
[root@hadoop100 ~]# head -n 5 initial-setup-ks.cfg
#version=DEVEL
# X Window System configuration information
xconfig  --startxonboot
# License agreement
eula --agreed

tail:显示文件尾部内容

-- 显示文件尾部内容
-- 选项 -n:显示文件尾部 n 行内容
-- 选项 -f:实时追踪文件的所有更新
-- fileName:文件名称
tail [选项] fileName

-- 显示文件尾部10行内容(默认10行)
tail fileName

-- 显示文件尾部5行内容(5可以是任意行数)
tail -n 5 fileName

-- 实时追踪该文件的所有更新(追加内容 >>)
-- 处于监控状态时,按键 ctrl + s:暂停监控(不会实时显示文件追加的内容,但依旧会记录文件变化),按键 ctrl + q:继续监控,此时会实时显示文件的追加内容,若使用 > 进行文件内容覆盖,则会显示 “文件已截断”
-- 若使用vim命令进行更新文件,此操作不会被监控到,后续使用 >> 进行文件的追加依旧不会被监控,需要关闭监控,再重新打开监控;
-- 使用vim修改文件不会被监控的原因是:Linux使用tail -f进行文件监控时,监控的是文件的索引号,每个文件都会有一个索引号(使用 ls -i fileName命令查看文件索引号),在使用vim命令进行文件修改之后,当前文件的索引号会改变,因此不会被监控到;
tail -f fileName

[root@hadoop100 ~]# tail initial-setup-ks.cfg
%end

%addon com_redhat_kdump --enable --reserve-mb=auto
%end

%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@hadoop100 ~]# tail -n 5 initial-setup-ks.cfg
%anaconda
pwpolicy root --minlen=6 --minquality=1 --notstrict --nochanges --notempty
pwpolicy user --minlen=6 --minquality=1 --notstrict --nochanges --emptyok
pwpolicy luks --minlen=6 --minquality=1 --notstrict --nochanges --notempty
%end
[root@hadoop100 ~]# ls
anaconda-ks.cfg  info  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
-- 查看info文件的索引号
[root@hadoop100 ~]# ls -i info
67365482 info
-- 使用vim命令对info文件进行修改
[root@hadoop100 ~]# vim info
-- info文件索引号发生了改变,与之前不一致
[root@hadoop100 ~]# ls -i info
67365445 info

echo:输出内容到控制台

-- 输出内容到控制台
-- 选项 -e:支持\(反斜杠)控制的字符转换
-- 例如 \\:输出\(反斜杠)本身
-- 例如 \n:换行
-- 例如 \t:制表符,也就是tab键
-- content:需要输出的内容
echo [选项] content

[root@hadoop100 ~]# echo hello,world
hello,world
[root@hadoop100 ~]# echo hello world
hello world
[root@hadoop100 ~]# echo "hello world"
hello world
[root@hadoop100 ~]# echo "hello \ world"
hello \ world
[root@hadoop100 ~]# echo "hello \n world"
hello \n world
[root@hadoop100 ~]# echo -e "hello\nworld"
hello
world
[root@hadoop100 ~]# echo -e "hello\\world"
hello\world
[root@hadoop100 ~]# echo -e "hello\tworld"
hello   world
-- 也可以用来查看系统环境变量,若不知道哪些环境变量,可通过 $+tab按键(cmd无效,使用Linux终端有效)
[root@hadoop100 ~]# echo $
-- 输入y
Display all 135 possibilities? (y or n)
$_                                   $ID
$ABRT_DEBUG_LOG                      $IFS
$_backup_glob                        $IMSETTINGS_INTEGRATE_DESKTOP
$BASH                                $IMSETTINGS_MODULE
$BASH_ALIASES                        $inx
...
...
...
[root@hadoop100 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@hadoop100 ~]# echo $HOSTNAME
hadoop100

>:输出重定向,>>:追加内容

-- 将列表内容写进文件中(覆盖)
ls > 文件
-- 将列表内容追加到文件末尾
ls >> 文件
-- 将文件1的内容写到文件2中(覆盖)
cat 文件1 > 文件2
-- 将内容追加到 文件末尾
echo "内容" >> 文件

ln:软链接

-- 给原文件创建一个软链接;
-- 删除软链接 使用命令:rm -rf [软链接名],后面不能带有 /(斜杠),若带有斜杠,则删除的是软链接的真实目录下的内容;
-- 查看:通过 ll命令可以查看,列表属性第一位是 l,尾部会有位置的指向;
ln -s [原文件或目录] [软链接名]

-- 可以看到 lib、lib64、sbin 都是软链接,后面指向的是真实目录
[root@hadoop100 ~]# ll /
总用量 28
lrwxrwxrwx.   1 root root    7 7月  25 16:09 bin -> usr/bin
dr-xr-xr-x.   5 root root 4096 7月  26 09:34 boot
drwxr-xr-x.  19 root root 3260 8月  16 13:52 dev
drwxr-xr-x. 144 root root 8192 8月  16 13:52 etc
drwxr-xr-x.   3 root root   22 7月  25 16:23 home
lrwxrwxrwx.   1 root root    7 7月  25 16:09 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 7月  25 16:09 lib64 -> usr/lib64
drwxr-xr-x.   2 root root    6 4月  11 2018 media
drwxr-xr-x.   2 root root    6 4月  11 2018 mnt
drwxr-xr-x.   3 root root   16 7月  25 16:16 opt
dr-xr-xr-x. 215 root root    0 8月  16 13:52 proc
dr-xr-x---.  15 root root 4096 8月  16 15:11 root
drwxr-xr-x.  43 root root 1260 8月  16 14:15 run
lrwxrwxrwx.   1 root root    8 7月  25 16:09 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 4月  11 2018 srv
dr-xr-xr-x.  13 root root    0 8月  16 13:52 sys
drwxrwxrwt.  23 root root 4096 8月  16 15:11 tmp
drwxr-xr-x.  13 root root  155 7月  25 16:09 usr
drwxr-xr-x.  21 root root 4096 7月  25 16:26 var

-- 创建一个软链接,将根目录下的root目录下的initial-setup-ks.cfg文件链接到 桌面下的myLink文件上
[root@hadoop100 ~]# ln -s /root/initial-setup-ks.cfg 桌面/myLink
[root@hadoop100 ~]# ll 桌面/
总用量 0
lrwxrwxrwx. 1 root root 26 8月  16 15:14 myLink -> /root/initial-setup-ks.cfg
-- 创建一个硬链接
ln [原文件] [硬链接名]

Linux 链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link)。默认情况下,ln 命令产生硬链接。

硬连接

硬连接指通过索引节点来进行连接。在 Linux 的文件系统中,保存在磁盘分区中的文件不管是什么类型都给它分配一个编号,称为索引节点号(Inode Index)。在 Linux 中,多个文件名指向同一索引节点是存在的。比如:A 是 B 的硬链接(A 和 B 都是文件名),则 A 的目录项中的 inode 节点号与 B 的目录项中的 inode 节点号相同,即一个 inode 节点对应两个不同的文件名,两个文件名指向同一个文件,A 和 B 对文件系统来说是完全平等的。删除其中任何一个都不会影响另外一个的访问。

硬连接的作用是允许一个文件拥有多个有效路径名,这样用户就可以建立硬连接到重要文件,以防止“误删”的功能。其原因如上所述,因为对应该目录的索引节点有一个以上的连接。只删除一个连接并不影响索引节点本身和其它的连接,只有当最后一个连接被删除后,文件的数据块及目录的连接才会被释放。也就是说,文件真正删除的条件是与之相关的所有硬连接文件均被删除。

软连接

另外一种连接称之为符号连接(Symbolic Link),也叫软连接。软链接文件有类似于 Windows 的快捷方式。它实际上是一个特殊的文件。在符号连接中,文件实际上是一个文本文件,其中包含的有另一文件的位置信息。比如:A 是 B 的软链接(A 和 B 都是文件名),A 的目录项中的 inode 节点号与 B 的目录项中的 inode 节点号不相同,A 和 B 指向的是两个不同的 inode,继而指向两块不同的数据块。但是 A 的数据块中存放的只是 B 的路径名(可以根据这个找到 B 的目录项)。A 和 B 之间是“主从”关系,如果 B 被删除了,A 仍然存在(因为两个是不同的文件),但指向的是一个无效的链接。

测试

-- 创建一个测试文件f1
[root@hadoop100 桌面]# touch f1
-- 创建f1的一个硬连接文件f2
[root@hadoop100 桌面]# ln f1 f2
-- 创建f1的一个符号连接文件f3
[root@hadoop100 桌面]# ln -s f1 f3
-- 选项-i:显示文件的inode节点信息
[root@hadoop100 桌面]# ls -li
total 0
9797648 -rw-r--r--  2 oracle oinstall 0 Apr 21 08:11 f1
9797648 -rw-r--r--  2 oracle oinstall 0 Apr 21 08:11 f2
9797649 lrwxrwxrwx  1 oracle oinstall 2 Apr 21 08:11 f3 -> f1

从上面的结果中可以看出,硬连接文件 f2 与原文件 f1 的 inode 节点相同,均为 9797648,然而符号连接文件的 inode 节点不同。

总结

  • 1).删除符号连接f3,对f1,f2无影响;
  • 2).删除硬连接f2,对f1,f3也无影响;
  • 3).删除原文件f1,对硬连接f2没有影响,导致符号连接f3失效;
  • 4).同时删除原文件f1,硬连接f2,整个文件会真正的被删除。

history:查看命令的历史记录

[root@hadoop100 ~]# history
    1  ll
    2  cls
    3  clear
    ...
    ...
    ...
-- 输出命令的历史记录编号为3的命令,并执行
[root@hadoop100 ~]# !3
-- 清空命令的历史记录
[root@hadoop100 ~]# history -c
[root@hadoop100 ~]# history
    1  history

date:显示时间

-- 显示当前时间
-- 选项 -d:显示非当前时间
-- 选项 -s:设置系统时间
date [选项] [时间字符串]

-- 同步系统时间
ntpdate [服务器名称]

-- 查看当前月份日历
cal
-- 将周日放到最后
cal -m
-- 查看前后三个月的日历
cal -3
-- 查看2022年的所有月份日历
cal 2022

-- 显示今天的时间
[root@hadoop100 ~]# date
2022年 08月 16日 星期二 15:55:53 CST
[root@hadoop100 ~]# date +%Y
2022
[root@hadoop100 ~]# date +%m
08
[root@hadoop100 ~]# date +%d
16
[root@hadoop100 ~]# date +%Y-%m-%d-%H:%M:%S
2022-08-16-15:57:36
[root@hadoop100 ~]# date "+%Y-%m-%d %H:%M:%S"
2022-08-16 15:58:31
-- 时间戳
[root@hadoop100 ~]# date +%s
1660636740

-- 选项 -d,显示非当前时间
-- 显示一天前(昨天)
[root@hadoop100 ~]# date -d "1 day ago"
2022年 08月 15日 星期一 16:00:15 CST
-- 显示一个小时前
[root@hadoop100 ~]# date -d "1 hours ago"
2022年 08月 16日 星期二 15:01:41 CST
-- 显示一天后(明天)
[root@hadoop100 ~]# date -d "-1 day ago"
2022年 08月 17日 星期三 16:00:33 CST

-- 查看当前月份日历
[root@hadoop100 ~]# cal
      八月 2022
日 一 二 三 四 五 六
    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
28 29 30 31

[root@hadoop100 ~]# cal -m
      八月 2022
一 二 三 四 五 六 日
 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 28
29 30 31

[root@hadoop100 ~]# cal -3
      七月 2022             八月 2022             九月 2022
日 一 二 三 四 五 六  日 一 二 三 四 五 六  日 一 二 三 四 五 六
                1  2      1  2  3  4  5  6               1  2  3
 3  4  5  6  7  8  9   7  8  9 10 11 12 13   4  5  6  7  8  9 10
10 11 12 13 14 15 16  14 15 16 17 18 19 20  11 12 13 14 15 16 17
17 18 19 20 21 22 23  21 22 23 24 25 26 27  18 19 20 21 22 23 24
24 25 26 27 28 29 30  28 29 30 31           25 26 27 28 29 30
31

[root@hadoop100 ~]# cal 2022
                               2022

        一月                   二月                   三月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                   1          1  2  3  4  5          1  2  3  4  5
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 31
        四月                   五月                   六月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30

        七月                   八月                   九月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
        十月                  十一月                 十二月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                   1          1  2  3  4  5                1  2  3
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31

[root@hadoop100 ~]# cal -y
                               2022

        一月                   二月                   三月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                   1          1  2  3  4  5          1  2  3  4  5
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    6  7  8  9 10 11 12
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   13 14 15 16 17 18 19
16 17 18 19 20 21 22   20 21 22 23 24 25 26   20 21 22 23 24 25 26
23 24 25 26 27 28 29   27 28                  27 28 29 30 31
30 31
        四月                   五月                   六月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                1  2    1  2  3  4  5  6  7             1  2  3  4
 3  4  5  6  7  8  9    8  9 10 11 12 13 14    5  6  7  8  9 10 11
10 11 12 13 14 15 16   15 16 17 18 19 20 21   12 13 14 15 16 17 18
17 18 19 20 21 22 23   22 23 24 25 26 27 28   19 20 21 22 23 24 25
24 25 26 27 28 29 30   29 30 31               26 27 28 29 30

        七月                   八月                   九月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                1  2       1  2  3  4  5  6                1  2  3
 3  4  5  6  7  8  9    7  8  9 10 11 12 13    4  5  6  7  8  9 10
10 11 12 13 14 15 16   14 15 16 17 18 19 20   11 12 13 14 15 16 17
17 18 19 20 21 22 23   21 22 23 24 25 26 27   18 19 20 21 22 23 24
24 25 26 27 28 29 30   28 29 30 31            25 26 27 28 29 30
31
        十月                  十一月                 十二月
日 一 二 三 四 五 六   日 一 二 三 四 五 六   日 一 二 三 四 五 六
                   1          1  2  3  4  5                1  2  3
 2  3  4  5  6  7  8    6  7  8  9 10 11 12    4  5  6  7  8  9 10
 9 10 11 12 13 14 15   13 14 15 16 17 18 19   11 12 13 14 15 16 17
16 17 18 19 20 21 22   20 21 22 23 24 25 26   18 19 20 21 22 23 24
23 24 25 26 27 28 29   27 28 29 30            25 26 27 28 29 30 31
30 31
View Code

 

接下篇:Linux实操篇(用户权限类:常用命令)

posted @ 2022-08-12 16:04  DHaiLin  阅读(108)  评论(0编辑  收藏  举报