Linux的基本命令笔记

一、Linux的基本命令

1.切换用户命令

关键词:su

例1:从注册用户tubin切换到管理员用户root下
[tubin@localhost ~]$ su
密码:
[root@localhost tubin]#

例2:从管理员用户root下切换到注册用户tubin

[root@localhost tubin]# su - tubin
上一次登录:二 9月 19 18:34:25 CST 2023pts/0 上

2.显示当前的路径的命令

关键词:pwd

[root@localhost tubin]# pwd
/home/tubin

[tubin@localhost ~]$ pwd
/home/tubin

从普通用户tubin切换到root用户下,主目录还是原来注册用户的主目录
[tubin@localhost ~]$ pwd
/home/tubin
[tubin@localhost ~]$ su
密码:
[root@localhost tubin]# pwd
/home/tubin

从管理员用户root下切换到注册用户tubin,主目录不是与原来root用户的主目录/root,而是
注册用户tubin的主目录,注册用户的权限不够。
[root@localhost ~]# pwd
/root
[root@localhost ~]# su - tubin
上一次登录:四 9月 21 08:45:09 CST 2023pts/1 上
[tubin@localhost ~]$ pwd
/home/tubin
[tubin@localhost ~]$ cd /root
-bash: cd: /root: 权限不够

3.切换文件路径命令

关键词:cd

[root@localhost tubin]# cd ~ --切换到当前用户的主目录
[root@localhost ~]# pwd
/root

绝对路径:对所有文件路径进行描述

[root@localhost ~]# pwd
/root
[root@localhost ~]# cd /home/tubin
[root@localhost tubin]# pwd

相对路径:参考当前的文件路径进行描述
[root@localhost mnt]# cd dir1 --切换当前目录的子目录
[root@localhost dir1]# pwd
/mnt/dir1

[root@localhost dir1]# cd … --切换相对当前目录的父目录
[root@localhost mnt]# pwd
/mnt

[root@localhost mnt]# cd ./dir2/subdir2 --切换当前目录的子目录的下级目录
[root@localhost subdir2]# pwd
/mnt/dir2/subdir2

4.显示文件或文件目录的命令

关键词:ls

[root@localhost mnt]# ls
cdrom dir1 dir2 file.txt

显示隐藏文件使用参数 -a
[root@localhost ~]# ls -a
. .bash_history .bashrc .cshrc .tcshrc
… .bash_logout .cache .dbus .xauthUpkPV7
anaconda-ks.cfg .bash_profile .config initial-setup-ks.cfg .xauthwqUHJp

以“.”开头的文件就是Linux中隐藏文件或者目录

显示隐藏文件下所有子目录及文件 -A
[root@localhost ~]# ls -A
anaconda-ks.cfg .bash_profile .config initial-setup-ks.cfg .xauthwqUHJp
.bash_history .bashrc .cshrc .tcshrc
.bash_logout .cache .dbus .xauthUpkPV7

显示文件的文件名和类型(属性) 参数-F
[root@localhost mnt]# ls -F
cdrom/ dir1/ dir2/ file.txt

以/结尾的表示是目录名称(目录)

root@localhost etc]# ls -F
mtab@
以@结尾的表示是符号链接(链接文件)

[root@localhost vmware-tools-distrib]# ls -F
bin/ doc/ FILES installer/ vgauth/
caf/ etc/ INSTALL lib/ vmware-install.pl*
以*结尾的表示可执行文件

显示文件名以最新修改日期的顺序列出来
[root@localhost ~]# ls -t
initial-setup-ks.cfg anaconda-ks.cfg
[root@localhost ~]# ll
总用量 8
-rw-------. 1 root root 1881 9月 12 19:37 anaconda-ks.cfg
-rw-r–r–. 1 root root 1974 9月 12 19:38 initial-setup-ks.cfg

显示文件的权限、所有者、所属组、大小、修改日期、文件名称信息
[root@localhost ~]# ls -l
总用量 8
-rw-------. 1 root root 1881 9月 12 19:37 anaconda-ks.cfg
-rw-r–r–. 1 root root 1974 9月 12 19:38 initial-setup-ks.cfg

[root@localhost ~]# ll
总用量 8
-rw-------. 1 root root 1881 9月 12 19:37 anaconda-ks.cfg
-rw-r–r–. 1 root root 1974 9月 12 19:38 initial-setup-ks.cfg

[root@localhost mnt]# ls -l
总用量 12
drwxr-xr-x. 2 root root 4096 9月 15 00:34 cdrom
-rw-r–r–. 1 root root 0 9月 18 22:54 file.txt

以-开头为普通文件,以d开头目录文件

brw-rw----. 1 root disk 8, 0 9月 21 22:05 sda
brw-rw----+ 1 root cdrom 11, 0 9月 21 22:05 sr0
以b开头设备文件(块设备包括光驱、硬盘等)

crw-rw----. 1 root lp 6, 0 9月 21 22:05 lp0
crw-------. 1 root root 249, 0 9月 21 22:05 usbmon0
以c开头的设备文件(字符设备包括鼠标、键盘、打印机等)

lrwxrwxrwx. 1 root root 3 9月 21 22:05 cdrom -> sr0
以l开头的为链接文件

注:在Linux中设备都是以文件形式出现,设备都是用文件来表示,目录文件表示目录,
链接文件表示快捷方式

5.创建目录命令
关键词:mkdir

mkdir [参数] 目录名称

-p 创建目录同时,如果父目标不存在,那么会同时创建该目录及其该目录子目录

[root@localhost mnt]# ls
cdrom dir1 dir2 file.txt
[root@localhost mnt]# mkdir dir3
[root@localhost mnt]# ls
cdrom dir1 dir2 dir3 file.txt

在/mnt目录下创建dir4同时在dir4下级目录创建subdir4
/mnt/dir4/subdir4

[root@localhost mnt]# mkdir -p dir4/subdir4
[root@localhost mnt]# ls
cdrom dir1 dir2 dir3 dir4 file.txt
[root@localhost mnt]# cd dir4
[root@localhost dir4]# ls
subdir4

6.创建文件命令
关键词:touch 创建空文件

[root@localhost dir4]# touch file.txt ·
[root@localhost dir4]# ls
file.txt subdir4

file查看文件中内容
[root@localhost dir4]# file file.txt
file.txt: empty --说明文件是空文件

[root@localhost ~]# file initial-setup-ks.cfg
initial-setup-ks.cfg: ASCII text --ASCII文本文件

[root@localhost vmware-tools-distrib]# file vmware-install.pl
vmware-install.pl: Perl script, UTF-8 Unicode text executable --Perl可以执行脚本文件

7.删除目录命令
关键词:rmdir

-p 在删除目录时候,一同删除父目录的同时删除包括的子目录

删除目录dir1
[root@localhost mnt]# ls
cdrom dir1 dir2 dir3 dir4 file.txt
[root@localhost mnt]# rmdir dir1
[root@localhost mnt]# ls

删除目录dir2,提示dir2是非空目录,删除子目录subdir2才能删除dir2
[root@localhost mnt]# cd dir2
[root@localhost dir2]# ls
subdir2
[root@localhost dir2]# cd …
[root@localhost mnt]# rmdir dir2
rmdir: 删除 “dir2” 失败: 目录非空

[root@localhost mnt]# rmdir -p dir2/subdir2 --递归删除
[root@localhost mnt]# ls
cdrom dir3 dir4 file.txt

8.复制命令
关键词:cp

同一用户下,复制文件,文件的创建日期就是当前的日期(最新修改日期)
cp [参数] 源文件 目录文件

-a : 尽可能将文件所有属性和权限、状体原封不动的进行复制

-f:如果目标文件或目录存在,先删除后进行复制(覆盖),并且不进行提示

-i :如果目标文件或目录存在,提示是否覆盖已有的文件

-R:递归复制目录,即将包括目录下各级子目录进行复制

[root@localhost ~]# cp anaconda-ks.cfg /mnt
[root@localhost ~]# cd /mnt
[root@localhost mnt]# ls
anaconda-ks.cfg cdrom dir3 dir4 file.txt

用root用户将/root目录下.bashrc 复制到/tmp下并进行更改名称为bashrc
[root@localhost ~]# cp .bashrc /tmp/bashrc
[root@localhost ~]# cd /tmp
[root@localhost tmp]# ls
anaconda.log
bashrc

[root@localhost ~]# cp -i .bashrc /tmp/bashrc
cp:是否覆盖"/tmp/bashrc"? y

-rw-rw-r–. 1 root utmp 12288 9月 21 22:07 wtmp

10.用于文件或目录的移动或改名:mv命令

mv [参数] 源文件或目录 目标文件或目录
同一个用户下,移动的文件,文件的属性不改变(包括创建日期、文件权限等)

[root@localhost mnt]# mv initial-setup-ks.cfg /home/tubin
[root@localhost mnt]# cd /home/tubin
[root@localhost tubin]# ls
bb.cfg finder subdir2 公共 视频 文档 音乐
dir2 initial-setup-ks.cfg

参数-i如果文件或目录存在,提示是否覆盖文件或目录
[root@localhost mnt]# mv -i initial-setup-ks.cfg /home/tubin
mv:是否覆盖"/home/tubin/initial-setup-ks.cfg"? y

参数-f无论文件或目录是否存在,直接覆盖目标文件或目录,不提示
[root@localhost tubin]# mv -f test.txt /mnt
[root@localhost mnt]# ll
-rw-r–r–. 1 root root 0 9月 28 08:40 test.txt

11.用于文件或目录的删除:rm命令

rm [参数] 文件名或目录名

不能用于删除空目录

[root@localhost mnt]# rm dir3
rm: 无法删除"dir3": 是一个目录
[root@localhost mnt]# rmdir dir3
[root@localhost mnt]# ls
anaconda-ks.cfg dir2 file.txt
cdrom dir4 test.txt
dir1 file2.txt??exit?touch file2.txt??quit??

删除普通文件,提示是否删除该文件
[root@localhost mnt]# rm file.txt
rm:是否删除普通空文件 “file.txt”?y

删除dir2/subdir2/file.tx目录和文件

参数-r用于递归删除,逐层进入依次删除
[root@localhost mnt]# rm dir2/ -r
rm:是否进入目录"dir2/“? y
rm:是否进入目录"dir2/subdir2”? y
rm:是否删除普通空文件 “dir2/subdir2/file.tx”?y
rm:是否删除目录 “dir2/subdir2”?y
rm:是否删除目录 “dir2/”?y

删除dir2/subdir2/file.txt目录和文件
参数-f 直接删除文件或目录文件不进行提示
[root@localhost mnt]# ls
anaconda-ks.cfg dir2 test.txt
cdrom dir4
dir1 file2.txt??exit?touch file2.txt??quit??
[root@localhost mnt]# rm -f dir2/ -r
[root@localhost mnt]# ls
anaconda-ks.cfg dir1

删除该目录下的所有文件(包括所有文件以及下面目录文件,隐藏文件)
rm *
删除dir4当前目录的所有文件,提示用户是否删除
[root@localhost mnt]# cd dir4
[root@localhost dir4]# ls
file.txt
[root@localhost dir4]# rm *
rm:是否删除普通空文件 “file.txt”?y

  1. 查找命令:find

1)按文件的名称去查找
文件的后缀名称去查找:
[root@localhost ~]# find -name “*.cfg”
./aa.cfg
./initial-setup-ks.cfg
./bb.cfg
./anaconda-ks.cfg

[root@localhost ~]# find /root -name “*.cfg”
/root/aa.cfg
/root/initial-setup-ks.cfg
/root/bb.cfg
/root/anaconda-ks.cfg

文件的名称去模糊查找:
[root@localhost ~]# find -name “initial*”
./initial-setup-ks.cfg

2)按照所有者(创建者)去查找

[root@localhost tubin]# find -user tubin
.
./.bashrc
./.cache
./.cache/ims

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值