【日常积累】Linux基本命令(一)

今天分享一下Linux中常用的一些基础命令

目录及文件的基本操作

cd

描述:cd命令的作用是切换当前工作目录

#切换到某个指定的工作目录
[root@VM-4-6-centos ~]# cd /usr/lib/systemd/system
#切换至当前目录的上一层目录
[root@VM-4-6-centos system]# cd ..
#切换至上一次的工作目录
[root@VM-4-6-centos systemd]# cd -
/usr/lib/systemd/system
# 切换到当前用户的家目录
[root@VM-4-6-centos system]# cd 
[root@VM-4-6-centos ~]# 

pwd

描述:pwd命令的作用是显示当前工作目录的名称
用法::pwd [选项]…
选项::-P显示链接的真实路径

[root@VM-4-6-centos ~]# cd /tmp/
[root@VM-4-6-centos tmp]# mkdir -p a/b/c/d
[root@VM-4-6-centos tmp]# cd a/b/c/d/
#创建一个软连接
[root@VM-4-6-centos tmp]# ln -s  /tmp/ a/b/c/d/abcd
[root@VM-4-6-centos d]# ls
abcd
[root@VM-4-6-centos d]# cd abcd/
[root@VM-4-6-centos abcd]# pwd
/tmp/a/b/c/d/abcd
#注意和上面的区别,加了-P选项显示了真实的路径
[root@VM-4-6-centos abcd]# pwd -P
/tmp

ls

描述:命令的作用是显示目录与文件信息
用法::ls [选项]… [文件/目录]…
选项::-a 显示所有的信息,包括隐藏文件与目录。

  • -d 显示目录本身的信息,而非目录下的文件文件夹的信息。
  • -h 人性化显示容量信息。
  • -l 长格式显示详细信息。
  • -c 显示文件或目录属性最后修改的时间。.
  • -u 显示文件或目录最后被访问的时间。
  • -t 以修改时间排序,默认按文件名称排序。
[root@VM-4-6-centos ~]# cd /tmp/
#显示当前目录下的文件和文件夹
[root@VM-4-6-centos tmp]# ls
stargate.lock  tat_agent  test.sh
#指定显示某个目录下的子文件和目录
[root@VM-4-6-centos tmp]# ls /etc/ssh/
moduli  ssh_config  sshd_config  ssh_host_dsa_key  ssh_host_dsa_key.pub  ssh_host_ecdsa_key  ssh_host_ecdsa_key.pub  ssh_host_ed25519_key  ssh_host_ed25519_key.pub  ssh_host_rsa_key  ssh_host_rsa_key.pub
#查看包括以. 开头的隐藏文件和目录
[root@VM-4-6-centos tmp]# ls -a
.  ..   .font-unix  .ICE-unix  tat_agent  test.sh  .Test-unix  .X11-unix  .XIM-unix
#查看文件和目录的详细信息
[root@VM-4-6-centos tmp]# ls -l
total 12
drwxr-xr-x 4 root root 4096 Aug 14 16:25 tat_agent
-rw-r--r-- 1 root root  369 Aug 14 20:37 test.sh
#查看指定目录自身的详细信息
[root@VM-4-6-centos tmp]# ls -ld /root/
dr-xr-x---. 8 root root 4096 Aug 15 18:17 /root/
#人性化显示容量占用信息
[root@VM-4-6-centos tmp]# ls -lh
total 12K
drwxr-xr-x 4 root root 4.0K Aug 14 16:25 tat_agent
-rw-r--r-- 1 root root  369 Aug 14 20:37 test.sh
# 查看指定文件的属性变更时间
[root@VM-4-6-centos tmp]# ls -lc test.sh 
-rw-r--r-- 1 root root 369 Aug 14 20:37 test.sh
#查看指定文件最后访问时间
[root@VM-4-6-centos tmp]# ls -lu test.sh 
-rw-r--r-- 1 root root 369 Aug 16 10:33 test.sh
#查看文件的信息并以修改时间进行排序,默认倒序
[root@VM-4-6-centos tmp]# ls -lt
total 12
-rw-r--r-- 1 root root  369 Aug 14 20:37 test.sh
drwxr-xr-x 4 root root 4096 Aug 14 16:25 tat_agent

mkdir

描述:创建目录。
选项:-p 创建多级目录。

[root@VM-4-6-centos tmp]# mkdir a
[root@VM-4-6-centos tmp]# mkdir -p a/b/c/d

touch

描述:创建或修改文件时间。

[root@VM-4-6-centos tmp]# touch  a.txt

如果a.txt不存在,则创建,如果已存在,则更新文件所有的时间为当前系统时间。

cp

描述:复制文件与目录
用法:cp[选项]源目标
选项: -r递归,复制子文件与子目录,一般复制目录时使用。

#复制文件/etc/ssh/sshd_config至/ tmp目录下
[root@VM-4-6-centos tmp]# cp /etc/ssh/sshd_config /tmp/
#复制文件/etc/ssh/sshd_config至/目录下并改名为sshd.config
[root@VM-4-6-centos tmp]# cp /etc/ssh/sshd_config /tmp/sshd.config
#复制目录/etc/ssh/至/tmp/目录下
[root@VM-4-6-centos tmp]# cp  -r /etc/ssh/ /tmp/

rm

描述:删除文件或目录
用法: rm [选项]…文件…
选项:

  • -f 不提示,强制删除。
  • -i 删除前,提示是否删除。不指定默认就会提示
  • -r 删除目录以及目录下的所有内容。
# 强制删除不提示
[root@VM-4-6-centos tmp]# rm -f sshd_config 

#删除默认提示
[root@VM-4-6-centos tmp]# rm  sshd.config 
rm: remove regular file ‘sshd.config’? 

#删除目录且不提示
[root@VM-4-6-centos tmp]# rm -rf  ssh

mv

描述:移动(重命名)文件或目录。

#将test.sh 改名为 test.txt
[root-4-6-centos tmp]# mv test.sh  test.txt
#将test.txt移至/root目录下
[root-4-6-centos tmp]# mv test.txt /root/

find

描述:搜索文件或目录。
用法:find [命令选项] [路径] [表达式选项]
选项:

  • -empty 查找空白文件或目录。
  • -group 按组查找。
  • -name 按名称查找。
  • -iname按名称查找,不区分大小写。
  • -mtime 按修改时间查找。
  • -size 按容量大小查找。
  • -type 按档案类型查找, 文件(f)、 目录(d)、 设备(b,c)、 链接(I) 等。
  • -user 按用户查找。
  • -exec 对找到的文件执行命令。
  • -a 并且。
  • -o 或者。
#查找当前目录下名为test.txt的文件
[root@VM-4-6-centos tmp]# find . -name test.txt 
./test.txt
#当前目录下忽略大小写查找文件
[root@VM-4-6-centos tmp]# find . -iname TEST.TXT
./test.txt
# 在/var/log目录下查找以log结尾的文件
[root@VM-4-6-centos tmp]# find  /var/log/ -name *.log
/var/log/audit/audit.log
/var/log/boot.log
/var/log/qcloud_action.log
/var/log/cloud-init-output.log
/var/log/cloud-init.log
/var/log/yum.log
/var/log/tuned/tuned.log

#查找所有内容为空的文件,包括空文件夹和内容为空的文件
[root@VM-4-6-centos tmp]# find  / -empty 
#只查找文件内容为空的所有文件
[root@VM-4-6-centos tmp]# find  / -empty -type f
#查找所有属于nginx组的文件
[root@VM-4-6-centos tmp]# find / -group nginx

#当前目录下查找3天前被修改过的文件
[root@VM-4-6-centos tmp]# find . -mtime +3
./.XIM-unix
./.font-unix
./.X11-unix
./.ICE-unix
./.Test-unix
#当前目录下查找2天内被修改过的文件
[root@VM-4-6-centos tmp]# find . -mtime -2
./tat_agent
./tat_agent/commands
./tat_agent/commands/202308
./tat_agent/logs
./tat_agent/logs/invt-7k2m4fa1b6.log
./test.txt
#当前目录下查找2天前的当天被修改过的文件
[root@VM-4-6-centos tmp]# find . -mtime 2
./stargate.lock
#当前目录下查找大小大于10M的文件
[root@VM-4-6-centos tmp]# find . -size +10M
#当前目录下查找普通文件
[root@VM-4-6-centos tmp]# find . -type f
./tat_agent/logs/invt-7k2m4fa1b6.log
./test.txt
./stargate.lock
#当前目录下查找普通文件,并执行后面的命令。也可以是其他合适的命令。注意格式正确
[root@VM-4-6-centos tmp]# find . -type f -exec ls -l {} \;
-rw-r----- 1 root root 0 Aug 14 16:25 ./tat_agent/logs/invt-7k2m4fa1b6.log
-rw-r--r-- 1 root root 369 Aug 14 20:37 ./test.txt
-rw-r--r-- 1 root root 0 Aug 13 15:12 ./stargate.lock

du

描述:计算文件或目录的容量。
用法: du [选项… [文件或目录]…
选项:

  • -h人性化显示容量信息。
  • -s仅显示总容量。
#查看/root目录总的容量占用信息
[root-4-6-centos tmp]# du -sh /root/
224K    /root/

查看文件内容

cat

描述:查看文件内容。
用法:cat [选项]… [文件]…
选项:

  • -b 显示行号,空白行不显示行号。
  • -n 显示行号,包括空白行。
[root-4-6-centos ~]# cat -b /etc/ssh/sshd_config
[root-4-6-centos ~]# cat -n /etc/ssh/sshd_config 

more

描述:分页查看文件内容,通过空格键查看下一-页,q键则退出查看。

[root@VM-4-6-centos ~]# more /var/log/messages 

less

描述:分页查看文件内容,空格(下一页)、方向键(上下回翻)、q键(退出查看)。

[root@VM-4-6-centos ~]# less /var/log/messages 

head

描述:查看文件头部内容,默认显示前10行。
用法:head [选项… [文件…
选项::

  • -c nK 显示文件前nKB的内容。
  • -n 显示文件前n行的内容。
[root@VM-4-6-centos ~]# head -c 1k /var/log/messages 
[root@VM-4-6-centos ~]# head -20 /var/log/messages 

tail

描述:查看文件的尾部内容,默认显示末尾10行。
用法:tail [选项]… [文件]
选项:

  • -cnK显示文件末尾 nKB的内容。
  • -n 显示文件末尾n行的内容。
  • -f 动态显示文件内容,按Ctrl+C组合键退出。
[root@VM-4-6-centos ~]# tail -c 1k /var/log/messages 
[root@VM-4-6-centos ~]# tail -n 20 /var/log/messages 
[root@VM-4-6-centos ~]# tail -f /var/log/messages 
[root@VM-4-6-centos ~]# tailf  /var/log/messages 

wc

描述:显示文件的行、单词与字节统计信息。
用法::we [选项]… [文件]…
选项:

  • -c 显示文件字节统计信息。
  • -l 显示文件行数统计信息。
  • -w 显示文件单词统计信息。
#默认统计行数、单词数、字节数
[root@VM-4-6-centos ~]# wc -l  /var/log/messages 
98534 /var/log/messages
[root@VM-4-6-centos ~]# wc -w  /var/log/messages 
1082239 /var/log/messages
[root@VM-4-6-centos ~]# wc -c  /var/log/messages 
7410879 /var/log/messages
[root@VM-4-6-centos ~]# wc /var/log/messages 
  98534 1082239 7410879 /var/log/messages

grep

描述:查找关键词并打印匹配的行。
用法:grep [选项]匹配模式[文件]…
选项:

  • -i忽略大小写。
  • -v取反匹配。
  • -w匹配单词,如果是单词的一部分也不会过滤出来
  • -color 显示颜色,默认就会显示颜色
[root@VM-4-6-centos ~]# grep error /var/log/messages 
[root@VM-4-6-centos ~]# grep --color error /var/log/messages 
[root@VM-4-6-centos ~]# grep -i error /var/log/messages 
[root@VM-4-6-centos ~]# grep -w hell /var/log/messages  #技术有hello的单词也不会显示出来
[root@VM-4-6-centos ~]# grep -v error /var/log/messages 

echo

描述:显示一行指定的文本。
用法:echo [选项]… [字符串]…
选项:

  • -n 不输出换行,默认echo输出内容后会换行。
  • -e 支持反斜线开始的转义字符,屏蔽反斜线后面字符的原本含义。如果使用-e选项,则可以识别如下字符序列的特殊含义。
    • \ 反斜线。
    • \a 报警器。
    • \b 退格键。
    • \c 不生成格外输出,默认echo会自动添加换行。等同于-n选项
    • \f 输入表单格式,换行后保留光标位置。
    • \n 换行。
    • \t 生成水平Tab。.
    • \v 生成垂直Tab。
[root@VM-4-6-centos ~]# echo -e "\\"
\
[root@VM-4-6-centos ~]# echo -e "\a"
[root@VM-4-6-centos ~]# echo -e "11\b22"
122

更多关 于Linux的知识请前往博客主页查看,编写过程中可能由于能力有限难免出现问题,敬请指出,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

margu_168

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值