Linux学习笔记4

Linux学习笔记4

绝对路径与相对路径

绝对路径是基于根目录的,通过根目录来定位文件,以“\”开头。

例:

[root@localhost ~]# cd /home/switch/

相对路径是基于当前目录的,通过当前目录来定位文件,可以以“.”开头。

例:

[root@localhost ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
[root@localhost ~]# cd Desktop/

查看当前目录

pwd

查看家目录

echo $HOME

进入家目录

cd,cd ~

当前目录和上一级目录

.,..

例:

[root@localhost ~]# pwd
/root
[root@localhost ~]# cd .
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd ..
[root@localhost /]# pwd
/
[root@localhost /]#

创建目录

mkdir

例:创建目录

[root@localhost ~]# mkdir 123
[root@localhost ~]# ls
123 Desktop Downloads Music Public Videos
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
[root@localhost ~]#

例:级联创建目录

[root@localhost ~]# mkdir -p 1/2/3
[root@localhost ~]# ls
1 anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
123 Desktop Downloads Music Public Videos
[root@localhost ~]# cd 1/
[root@localhost 1]# ls
2
[root@localhost 1]# cd 2/
[root@localhost 2]# ls
3
[root@localhost 2]#

删除目录

rmdir

例:删除目录

[root@localhost ~]# rmdir 123/
[root@localhost ~]# ls
1 Desktop Downloads Music Public Videos
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
[root@localhost ~]#

例:级联删除目录

[root@localhost ~]# ls -d 1/2/3/
1/2/3/
[root@localhost ~]# rmdir -p 1/2/3/
[root@localhost ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
[root@localhost ~]#

创建一个文件

touch

文件不存在创建文件,文件存在刷新atime(文件访问时间),ctime(文件改变时间),mtime(文件修改时间)。

Linux之atime,mtime,ctime

例:创建同一个文件两次

[root@localhost ~]# touch 123.txt
[root@localhost ~]# ls -l 123.txt
-rw-r--r-- 1 root root 0 3月 29 12:32 123.txt
[root@localhost ~]# touch 123.txt
[root@localhost ~]# ls -l 123.txt
-rw-r--r-- 1 root root 0 3月 29 12:33 123.txt
[root@localhost ~]#

打印文字

echo

重定向和追加重定向

>,>>

例:将要打印的文字重定向到文件中

[root@localhost ~]# echo "123" > 123.txt
[root@localhost ~]# cat 123.txt
123
[root@localhost ~]#

PS:如果两次重定向到同一个文件中,则后一个重定向会覆盖前一个。

例:将要打印的文字追加重定向到文件中

[root@localhost ~]# echo 321 >> 123.txt
[root@localhost ~]# cat 123.txt
123
321
[root@localhost ~]#

删除文件

rm

例:删除一个文件

[root@localhost ~]# ls
123.txt Desktop Downloads Music Public Videos
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
[root@localhost ~]# rm 123.txt
rm:是否删除普通文件 "123.txt"?y
[root@localhost ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
[root@localhost ~]#

例:强制删除一个文件

[root@localhost ~]# touch 123.txt
[root@localhost ~]# ls
123.txt Desktop Downloads Music Public Videos
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
[root@localhost ~]# rm -f 123.txt
[root@localhost ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
[root@localhost ~]#

例:删除目录及目录下的文件

[root@localhost ~]# mkdir 123
[root@localhost ~]# touch 123/123.txt
[root@localhost ~]# ls 123
123.txt
[root@localhost ~]# rm -rf 123
[root@localhost ~]# ls 123
ls: 无法访问123: 没有那个文件或目录
[root@localhost ~]#

给命令起别名

alias

例:给一个命令起一个别名

[root@localhost ~]# alias abc="123456789"
[root@localhost ~]# abc
bash: 123456789: 未找到命令...
[root@localhost ~]#

例:查看所有已有的别名

[root@localhost ~]# alias
alias abc='123456789'
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]#

例:解除别名

[root@localhost ~]# unalias abc
[root@localhost ~]# abc
bash: abc: 未找到命令...
[root@localhost ~]#

查看命令在哪个目录下,被取了什么别名

which

例:查看命令ls在哪个目录下,有什么别名

[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@localhost ~]#

环境变量

echo $PATH

例:查看当前的环境变量

[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]#

PS:如果不想使用命令的路径全称,则可以将命令加入到环境变量中的其中一个,则可以通过命令名直接使用命令。

例:加入一个路径到环境变量中

[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost ~]# PATH=$PATH:/root
[root@localhost ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/root
[root@localhost ~]#

PS:环境变量可以添加路径,但不要随意更改,否则可能造成某些命令,某些服务使用不了。

PS:环境变量的更改只在当前终端生效,如果想让它永久生效,vim /etc/profile然后将PATH=$PATH:新增目录加到最底部一行。

复制文件或目录

cp

例:复制一个文件到另一个文件

[root@localhost ~]# ls
anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
Desktop Downloads Music Public Videos
[root@localhost ~]# touch 123.txt
[root@localhost ~]# cp 123.txt 234.txt
[root@localhost ~]# ls
123.txt anaconda-ks.cfg Documents initial-setup-ks.cfg Pictures Templates
234.txt Desktop Downloads Music Public Videos
[root@localhost ~]#

例:复制一个文件夹到另一个文件夹

[root@localhost ~]# mkdir 1
[root@localhost ~]# cp -r 1 2
[root@localhost ~]# ls
1 234.txt Documents Music Templates
123.txt anaconda-ks.cfg Downloads Pictures Videos
2 Desktop initial-setup-ks.cfg Public
[root@localhost ~]#

PS:如果目标目录不存在,那么就复制成相应目录,如果存在,则源目录成为目标目录的子目录。

移动文件或目录

mv

例:更改文件名

[root@localhost ~]# ls
1 234.txt Documents Music Templates
123.txt anaconda-ks.cfg Downloads Pictures Videos
2 Desktop initial-setup-ks.cfg Public
[root@localhost ~]# mv 123.txt 321.txt
[root@localhost ~]# ls
1 321.txt Documents Music Templates
2 anaconda-ks.cfg Downloads Pictures Videos
234.txt Desktop initial-setup-ks.cfg Public
[root@localhost ~]#

例:更改目录名

[root@localhost ~]# ls
1 234.txt Documents Music Templates
123.txt anaconda-ks.cfg Downloads Pictures Videos
2 Desktop initial-setup-ks.cfg Public
[root@localhost ~]# touch 1/1.txt
[root@localhost ~]# mv 1 3
[root@localhost ~]# ls
2 321.txt Documents Music Templates
234.txt anaconda-ks.cfg Downloads Pictures Videos
3 Desktop initial-setup-ks.cfg Public
[root@localhost ~]# mv 3 2
[root@localhost ~]# ls
2 anaconda-ks.cfg Downloads Pictures Videos
234.txt Desktop initial-setup-ks.cfg Public
321.txt Documents Music Templates
[root@localhost ~]# ls 2/3/1.txt
2/3/1.txt
[root@localhost ~]#

PS:如果目标目录不存在,那么就重命名成相应目录,如果存在,则源目录成为目标目录的子目录。

与文档相关命令

cat,tac,more,less,head,tail

例:正序逆序查看文档

[root@localhost ~]# echo 12321312312 > 321.txt
[root@localhost ~]# echo 32132132132 >> 321.txt
[root@localhost ~]# cat 123.txt
12321312312
32132132132
[root@localhost ~]# tac 123.txt
32132132132
12321312312
[root@localhost ~]#

例:显示行号

[root@localhost ~]# cat -n 123.txt
1 12321312312
2 32132132132
[root@localhost ~]#

例:显示所有字符,包括特殊字符

[root@localhost ~]# cat -A 123.txt
12321312312$
32132132132$
[root@localhost ~]#

例:将要显示的文件内容重定向到文件中

[root@localhost ~]# cat 234.txt
[root@localhost ~]# cat 123.txt >> 234.txt
[root@localhost ~]# cat 234.txt
12321312312
32132132132
[root@localhost ~]#

PS:通过less查看文件内容时可以上下翻页,也可以通过”/字符串”,”?字符串”来搜索字符串,区别是”/字符串”是在当前行向下搜索,”?字符串”是在当前行向上搜索。可以按”n”显示下一个匹配字符串。

PS:快捷键:j向下,k向上,g首部,G尾部

例:查看文件前5行内容

[root@localhost ~]# head -n3 123.txt
12321312312
32132132132
21321321312
[root@localhost ~]# head -n 3 123.txt
12321312312
32132132132
21321321312
[root@localhost ~]# head -3 123.txt
12321312312
32132132132
21321321312
[root@localhost ~]#

PS:tail也可以这么用,默认显示10行。

例:动态显示文件最后10行内容

[root@localhost ~]# tail -f 123.txt
21321321312
12321312312
32132132132
21321321312
12321312312
32132132132
21321321312
12321312312
32132132132
21321321312

PS:一直等待更新,如果有更新,则显示更新后该文件最后10行内容。

通配符的使用

*,?

例:使用通配符匹配文件

[root@localhost ~]# ls -l *.txt
-rw-r--r-- 1 root root 180 3月 29 15:03 123.txt
-rw-r--r-- 1 root root 24 3月 29 14:49 234.txt
[root@localhost ~]# ls -l ???.txt
-rw-r--r-- 1 root root 180 3月 29 15:03 123.txt
-rw-r--r-- 1 root root 24 3月 29 14:49 234.txt
[root@localhost ~]#

PS:*匹配任意个字符,?匹配一个字符。

文件类型

- 普通文件

d 目录文件

b 块设备文件(磁盘)

s 套接字文件(通信文件)

l 软链接文件

c 串口设备文件(键盘、鼠标等)

文件权限

rwx(读写执行)

r:4,w:2,x:1

文件所属主,所属组和其他人

[2-4]文件所属主权限

[5-7]文件所属组权限

[8-10]其他人权限
[root@localhost ~]# ls -l 123.txt
-rw-r--r-- 1 root root 180 3月 29 15:03 123.txt

更改文件权限

chmod

例:修改文件的权限1

[root@localhost ~]# ls -l 123.txt
-rw-r--r-- 1 root root 180 3月 29 15:03 123.txt
[root@localhost ~]# chmod 700 123.txt
[root@localhost ~]# ls -l 123.txt
-rwx------ 1 root root 180 3月 29 15:03 123.txt
[root@localhost ~]#

例:修改文件的权限2

[root@localhost ~]# ls -l 123.txt
-rwx------ 1 root root 180 3月 29 15:03 123.txt
[root@localhost ~]# chmod u-x,go+r 123.txt
[root@localhost ~]# ls -l 123.txt
-rw-r--r-- 1 root root 180 3月 29 15:03 123.txt
[root@localhost ~]#

PS:u代表文件所属主,g代表文件所属组,o代表其他。

inode解析

inode表示链接占用的节点,与硬链接有关。

例:查看inode信息

[root@localhost ~]# ls -a 123.txt
123.txt
[root@localhost ~]# ls -i 123.txt
137915477 123.txt

参考《跟阿铭学Linux》


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值