linux入门常用命令(一)

linux(一)

提示:在开始本教程前我们需要先知道什么是绝对路径与相对路径。
绝对路径
路径的写法,由根目录 / 写起,例如: /usr/share/doc 这个目录。

相对路径
路径的写法,不是由 / 写起,例如由 /usr/share/doc 要到 /usr/share/man 底下时,
可以写成: cd …/man 这就是相对路径的写法 (两个点,显示问题)


一、常用命令

1.cd命令 – 切换目录

切换当前工作目录至/etc:

[root@linuxcool ~]# cd /etc
[root@linuxcool etc]#

2.pwd命令 – 显示当前工作目录的路径

查看当前工作目录路径:

[root@linuxcool ~]# pwd
/root

3.ls命令 – 显示指定工作目录下的文件及属性信息

(1)输出当前目录中的文件(默认不含隐藏文件):

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

(2)ls -l(ll) 输出文件的长格式,包含属性详情信息(权限,大小,创建时间…):

[root@linuxcool ~]# ls -l
total 8
-rw-------. 1 root root 1430 Dec 14 08:05 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Dec 14 08:37 Desktop
drwxr-xr-x. 2 root root    6 Dec 14 08:37 Documents
drwxr-xr-x. 2 root root    6 Dec 14 08:37 Downloads

(3)ls -a 输出当前目录中的文件(含隐藏文件以.开头):

[root@linuxcool ~]# ls -a
.                .bashrc  Documents             Music      Videos
..               .cache   Downloads             Pictures   .viminfo
anaconda-ks.cfg  .config  .esd_auth             .pki
.bash_history    .cshrc   .ICEauthority         Public
.bash_logout     .dbus    initial-setup-ks.cfg  .tcshrc
.bash_profile    Desktop  .local                Templates

(4)ls -d 查看目录本身的信息

4.touch命令 – 创建空文件

创建出一个指定名称的空文件:

[root@linuxcool ~]# touch File.txt

5.mkdir命令 – 创建目录文件

(1)在当前工作目录中,建立一个目录文件:

[root@linuxcool ~]# mkdir dir1

(2)mkdir -p 递归的创建(多级目录创建):
注意:mkdir -p a/b/c 当前目录创建a的子目录下b的子目录c
mkdir -p /a/b/c 在根目录下创建

6.rmdir命令 – 删除空目录文件

删除指定的空目录(只能删除空目录):

[root@linuxcool ~]# rmdir Documents
//redir-p 删除指定的空目录,及其内的子空目录
[root@linuxcool ~]# rmdir -p Documents

7.rm命令 – 删除文件或目录

(1)删除某个文件,默认会进行二次确认,敲击y进行确认:

[root@linuxcool ~]# rm anaconda-ks.cfg 
rm: remove regular file 'anaconda-ks.cfg'? y

(2)rm -rf 删除某个目录及其内的子文件或子目录,一并都强制删除:

[root@linuxcool ~]# rm -rf Documents

(3)强制删除当前工作目录内的所有以.txt为后缀的文件(不能删除目录):

[root@linuxcool ~]# rm -f *.txt

(4)【离职小妙招,谨慎!!!】强制清空服务器系统内的所有文件:

[root@linuxcool ~]# rm -rf /*
//连同根目录一起删

8.cp命令 – 复制文件或目录

(1)cp -r 递归将目录dir1复制到目录dir2下面(此时dir2不存在):

[root@linuxcool ~]# cp dir1 dir2
cp: omitting directory ‘dir1’
[root@linuxcool ~]# cp -r dir1 dir2

(2)cp -a 复制文件所有(属性)

9.mv命令 – 移动或改名文件

(1)
修改文件为文件名2 :

//mv 文件名 文件名2
root@linuxcool ~]# mv txt1 txt2

将文件移动到目标目录:

//mv 文件名 目录名
root@linuxcool ~]# mv txt dir

10.cat – 查看文件内容

查看某个文件的内容:

[root@linuxcool ~]# cat anaconda-ks.cfg
#version=RHEL8
ignoredisk --only-use=sda
autopart --type=lvm
# Partition clearing information

11.more命令 – 分页显示文本文件内容

分页显示指定的文本文件内容:

[root@linuxcool ~]# more anaconda-ks.cfg 
#version=RHEL8
ignoredisk --only-use=sda
autopart --type=lvm
# Partition clearing information
clearpart --none --initlabel
# Use graphical install
graphical
# Use CDROM installation media
cdrom
//Ctrl + c 结束
………………省略部分输出信息………………

12.head命令 – 显示文件开头的内容

(1)head -n num 指定显示文件的前5行内容:

[root@linuxcool ~]# head -n 5 anaconda-ks.cfg 
#version=RHEL8
ignoredisk --only-use=sda
autopart --type=lvm
# Partition clearing information
clearpart --none --initlabel

(2)head -c num 指定显示文件的前20个字符:

[root@linuxcool ~]# head -c 20 anaconda-ks.cfg 
#version=RHEL8

13.tail命令 – 查看文件尾部内容

(1)指定显示某个文件尾部的后5行内容:

[root@linuxcool ~]# 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

(2)指定显示某个文件尾部的后15个字节:

[root@linuxcool ~]# tail -c 30 initial-setup-ks.cfg 
t --nochanges --notempty
%end

14.du命令 – 查看文件或目录的大小

(1)du -h 以易读的容量格式显示指定目录内各个文件的大小信息:

[root@linuxcool ~]# du -h /etc
28K	/etc/dnf/modules.d
20K	/etc/dnf/plugins
12K	/etc/dnf/protected.d
64K	/etc/dnf
16K	/etc/fonts/conf.d
20K	/etc/fonts
………………省略部分输出信息………………

(2)du -sh 以易读的容量格式显示指定目录内总文件的大小信息:

[root@linuxcool ~]# du -sh /etc 
29M	/etc

15.grep命令 – 强大的文本搜索工具

(1)搜索某个文件中,包含某个关键词的内容:

[root@linuxcool ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

(2)-i 忽略大小写
(3)-v 显示不匹配
(4)-n 并显示行号

16.find命令 –在目录里面找符合条件的文件和目录

代码如下(示例):
(1)全盘搜索系统中所有以.conf结尾的文件:

[root@linuxcool ~]# find / -name *.conf
/run/tmpfiles.d/kmod.conf
/etc/resolv.conf
/etc/dnf/dnf.conf
/etc/dnf/plugins/copr.conf
/etc/dnf/plugins/debuginfo-install.conf
/etc/dnf/plugins/product-id.conf
/etc/dnf/plugins/subscription-manager.conf
………………省略部分输出信息………………

(2)find type d 查找符合条件的目录
(3)find type f 查找符合条件的文件

总结

提示:linux基本的命令:

以上就是linux基本命令要讲的内容,本文仅仅简单介绍了linux基本命令的使用,参考文献https://www.linuxcool.com/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值