Linux的shell命令 - 详解

1.用户管理

        Linux中采用shell命令对用户进行管理是比较重要的,下面我们来看看与用户管理相关的shell命令介绍:

linux@ubuntu:~$ id linux
uid=1000(linux) gid=1000(linux) 组=1000(linux),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),122(lpadmin),135(lxd),136(sambashare)
linux@ubuntu:~$ id root
uid=0(root) gid=0(root) 组=0(root)
linux@ubuntu:~$ sudo adduser ysy
正在添加用户"ysy"...
正在添加新组"ysy" (1001)...
正在添加新用户"ysy" (1001) 到组"ysy"...
创建主目录"/home/ysy"...
正在从"/etc/skel"复制文件...
新的密码:
无效的密码: 密码是一个回文
重新输入新的密码:
passwd:已成功更新密码
正在改变 ysy 的用户信息
请输入新值,或直接敲回车键以使用默认值
	全名 []:
	房间号码 []:
	工作电话 []:
	家庭电话 []:
	其它 []:
这些信息是否正确? [Y/n]
linux@ubuntu:~$ ls /home/
linux  ysy
linux@ubuntu:~$ id ysy
uid=1001(ysy) gid=1001(ysy) 组=1001(ysy)
linux@ubuntu:~$ su ysy
密码:
ysy@ubuntu:/home/linux$ ls

id:用来显示用户的详细信息。adduser:用来创建一个新的用户,useradd也可以用来创建一个新的用户,但是useradd命令创建的用户是一个三无用户。su:进行用户间的切换。

linux@ubuntu:~$ ls /home/
linux  ysy
linux@ubuntu:~$ userdel ysy
userdel: Permission denied.
userdel:无法锁定 /etc/passwd,请稍后再试。
linux@ubuntu:~$ sudo userdel ysy
[sudo] linux 的密码:
linux@ubuntu:~$ ls /home/
linux  ysy
linux@ubuntu:~$ id ysy
id: "ysy": 无此用户
linux@ubuntu:~$ cd /home/
linux@ubuntu:/home$ ls
linux  ysy
linux@ubuntu:/home$ rm -rf ysy
rm: 无法删除 'ysy': 权限不够
linux@ubuntu:/home$ sudo rm -rf ysy
linux@ubuntu:/home$ ls
linux
linux@ubuntu:/home$

sudo:给与普通用户更高的权限。userdel:删除一个用户,用户被删除但是文件夹下还有。rm -rf:强制删除文件或目录,且不显示错误信息。

ysy@ubuntu:/home/linux$ passwd
更改 ysy 的密码。
当前的密码:
新的密码:
重新输入新的密码:
passwd:已成功更新密码
ysy@ubuntu:/home/linux$ exit
exit

passwd:修改用户密码。exit:退出当前用户。

2.文件管理

        对于linux的文件管理,我们先来了解一下linux中比较重要的一些文件或目录。如下:

linux@ubuntu:~$ ls /
bin  boot  cdrom  dev  etc  home  lib  lib32  lib64  libx32  lost+found  media  mnt  opt  proc  root  run  sbin  snap  srv  swapfile  sys  tmp  usr  var
linux@ubuntu:~$ ls /bin/
'['                                   gedit                              nautilus                    spd-say
 7z                                   gem                                nautilus-autorun-software   spdsend
 7za                                  gem3.0                             nautilus-sendto             speaker-test
 7zr                                  gencat                             nawk                        speech-dispatcher
 aa-enabled                           genisoimage                        nc                          spice-vdagent
 aa-exec                              geqn                               nc.openbsd                  splain
 aa-features-abi                      GET                                neqn                        split
 aconnect                             getconf                            net                         splitfont
 acpidbg                              geteltorito                        netcat
linux@ubuntu:~$ ls /sbin/
aa-remove-unknown      delgroup               grub-mkdevicemap             lsmod                  plymouthd                swapon
aa-status              deluser                grub-probe                   lspcmcia               poweroff                 switch_root
aa-teardown            depmod                 grub-reboot                  make-ssl-cert          pppd                     sysctl
accessdb               devlink                grub-set-default             mii-tool               pppdump                  tarcat
acpid                  dhclient               halt                         mkdosfs                pppoe-discovery          tc

/:可见根目录下有着很多的文件和目录。bin:其中bin里面存放的大多是二进制可执行文件,或者一些可执行文件的链接,比如我们的ls,mkdir等就在这里。sbin:sbin是系统的二进制可执行文件,要使用里面的一些命令来配置系统时就要使用sudo来获取权限,其中有ifconfig等。

linux@ubuntu:~$ ls /boot
config-6.8.0-40-generic  initrd.img                   memtest86+.bin               System.map-6.8.0-87-generic  vmlinuz.old
config-6.8.0-87-generic  initrd.img-6.8.0-40-generic  memtest86+.elf               vmlinuz
efi                      initrd.img-6.8.0-87-generic  memtest86+_multiboot.bin     vmlinuz-6.8.0-40-generic
grub                     initrd.img.old               System.map-6.8.0-40-generic  vmlinuz-6.8.0-87-generic
linux@ubuntu:~$ ls /dev
autofs           dma_heap   input   loop6         port    sg0       tty11  tty23  tty35  tty47  tty59      ttyS11  ttyS23  ttyS7        vcs4   vcsu3
block            dmmidi     kmsg    loop7         ppp     sg1       tty12  tty24  tty36  tty48  tty6       ttyS12  ttyS24  ttyS8        vcs5   vcsu4
bsg              dri        log     loop8         psaux   shm       tty13  tty25  tty37  tty49  tty60      ttyS13  ttyS25  ttyS9        vcs6   vcsu5
btrfs-control    ecryptfs   loop0   loop9         ptmx    snapshot  tty14  tty26  tty38  tty5   tty61      ttyS14  ttyS26  udmabuf      vcsa   vcsu6
bus              fb0        loop1   loop-control  pts     snd       tty15  tty27  tty39  tty50  tty62      ttyS15  ttyS27  uhid         vcsa1  vfio
linux@ubuntu:~$ ls /etc
acpi                           crontab              group-           libao.conf           networks       rc6.d              sysctl.d
adduser.conf                   cron.weekly          grub.d           libaudit.conf        newt           rcS.d              systemd
adjtime                        cups                 gshadow          libblockdev          nfs.conf       request-key.conf   terminfo
alsa                           cupshelpers          gshadow-         libibverbs.d         nfs.conf.d     request-key.d      thermald
linux@ubuntu:~$ ls /lib
apg                emacsen-common             init                                  modules-load.d       python3                    thunderbird-addons
apparmor           environment.d              initramfs-tools                       netplan              python3.10                 tmpfiles.d
apt                evolution-data-server      ispell                                networkd-dispatcher  python3.11                 ubiquity
aspell             file                       kernel                                NetworkManager       recovery-mode              ubuntu-advantag

boot:里面是一些系统启动文件,其中的grub是开机引导程序。dev:是一些设备文件,在linux中任何东西都是文件包括硬件。etc:里面是一些系统的配置文件。lib:是一些可执行文件的依赖。

linux@ubuntu:~$ ls /media
floppy  floppy0  linux
linux@ubuntu:~$ ls /mnt
hgfs  linux  nfs  roots
linux@ubuntu:~$ ls /opt
linux@ubuntu:~$ ls /proc
1     1118  1226  1274  1487  16    1676  189   20    213   2323  244  28   413  53   69   82   92   98          diskstats      kpagecount     self
100   112   1227  1285  1493  1602  1682  19    200   214   233   245  284  42   54   7    823  93   981         dma            kpageflags     slabinfo
1003  114   1228  1287  1495  1603  1687  190   201   215   235   246  29   43   55   70   825  94   982         driver         latency_stats  softirqs
101   1154  1229  1294  15    1606  1690  1904  202   217   236   247  3    436  56   71   827  95   987         dynamic_debug  loadavg        stat
102   1167  123   13    1505  1608  1691  191   2029  2173  237   248  30   437  57   72   828  953  989         execdomains    locks          swaps
linux@ubuntu:~$ sudo ls /root
snap
linux@ubuntu:~$ ls /run
acpid.pid     cloud-init     dbus       irqbalance      openvpn         rpcbind.sock     sm-notify.pid      sshd        udisks2       xinetd.pid
acpid.socket  console-setup  fsck       lock            openvpn-client  rpc_pipefs       snapd              sshd.pid    user
alsa          credentials    gdm3       log             openvpn-server  rpc.statd.pid    snapd-snap.socket  sudo        utmp
avahi-daemon  crond.pid      gdm3.pid   mount           plymouth        samba            snapd.socket       systemd     uuidd
blkid         crond.reboot   initctl    netns           rpcbind         sendsigs.omit.d  speech-dispatcher  tmpfiles.d  vmblock-fuse
blkmapd.pid   cups           initramfs  NetworkManager  rpcbind.lock    shm              spice-vdagentd     udev        vmware
linux@ubuntu:~$ ls /sys
block  bus  class  dev  devices  firmware  fs  hypervisor  kernel  module  power

media:这是系统用来自动挂载设备的,比如U盘等被识别后就回被挂载到该目录下。mnt:这也是用来挂载文件的,但是一般都是需要手动进行挂载。opt:该目录下可以用来存放一些下载安装包等。proc:里面记录的是一些正在运行的程序。root:这是root管理员的家目录。run:用来存放一些程序运行时的信息。sys:用来存放一些系统信息。

linux@ubuntu:~$ ls /srv
linux@ubuntu:~$ ls /tmp
snap-private-tmp
systemd-private-2fc4a91a81bd4907a61136a017215810-bluetooth.service-jE9ODe
systemd-private-2fc4a91a81bd4907a61136a017215810-colord.service-Ne80E2
systemd-private-2fc4a91a81bd4907a61136a017215810-ModemManager.service-IaC4Nh
systemd-private-2fc4a91a81bd4907a61136a017215810-power-profiles-daemon.service-GVlnuR
systemd-private-2fc4a91a81bd4907a61136a017215810-switcheroo-control.service-goLD9X
systemd-private-2fc4a91a81bd4907a61136a017215810-systemd-logind.service-iBcIOy
systemd-private-2fc4a91a81bd4907a61136a017215810-systemd-oomd.service-oXo8UJ
systemd-private-2fc4a91a81bd4907a61136a017215810-systemd-resolved.service-hsqQ3u
systemd-private-2fc4a91a81bd4907a61136a017215810-systemd-timesyncd.service-a07y7o
systemd-private-2fc4a91a81bd4907a61136a017215810-upower.service-lc7Li4
VMwareDnD
vmware-root_835-3988097475
linux@ubuntu:~$ ls /usr
bin  games  include  lib  lib32  lib64  libexec  libx32  local  sbin  share  src
linux@ubuntu:~$ ls /var
backups  cache  crash  lib  local  lock  log  mail  metrics  opt  run  snap  spool  tftpboot  tmp
linux@ubuntu:~$ ls /home
linux
linux@ubuntu:~$ ls /cdrom
linux@ubuntu:~$ ls /snap
bare  bin  core22  firefox  gnome-42-2204  gtk-common-themes  README  snapd  snapd-desktop-integration  snap-store
linux@ubuntu:~$ ls /swapfile
/swapfile

srv:用来存放一些服务器数据。tmp:用来存放一些临时文件。usr:存放的是一些系统非必须的文件,如用户安装的一些软件等。var:里面存放的一些日志。home:是用户的家目录。cdrom:这是一个光盘驱动器,现在里面一般为空。snap:存放一些sanp软件。swapfile:交换文件当系统的内存用完了之后就会使用一般分磁盘空间用来存放东西。

        在Linux中有着许多不同类型的文件,文件的类型是非常重要的我们来看看其中的文件类型。首先是普通文件:常见的文本文件,二进制文件等都市普通文件。目录文件:就是指的文件夹。字符设备文件:数据以字符流形式传输每次读写操作直接与设备通信。块设备文件:数据通过内核缓冲区缓存支持任意位置的数据读写,以固定大小的数据块进行传输。符号链接文件:分为软链接和硬链接,软连接类似于windows中的快捷方式。硬链接:同一个​inode的多个目录条目。管道文件:管道分为有名管道和无名管道两种管道,主要用于进程间通信,可以同时被两个进程同时访问的文件。套接字文件,也是进程间通信的一种方式,可以在不同主机的进程间进行通信,是网络通信。

        文件的权限,是对该文件是否拥有可读,可写,可执行的权限。对于文件来说可读表示的是可以使用cat命令,可写表示的是可以使用vim命令,可执行表示可以使用./。对于目录来说可读表示可以使用ls命令,可写表示可以使用touch命令,可执行cd命令。

linux@ubuntu:~/work/01-linux/02-makefile/01-Makefile$ ls -l main.c
-rw-rw-r-- 1 linux linux 265 12月 17 12:05 main.c

通过这段显示信息,我们要知道前面的字符段表示的就是文件的信息。第一个字符是文件的类型,第二到第四个字符表示文件所有者的权限,5到7表示文件所属组的权限,8到10其他用户权限。

linux@ubuntu:~/work/01-linux/02-makefile/01-Makefile$ ls -l main.c
-rw-rw-r-- 1 linux linux 265 12月 17 12:05 main.c
linux@ubuntu:~/work/01-linux/02-makefile/01-Makefile$ chmod 0777 main.c
linux@ubuntu:~/work/01-linux/02-makefile/01-Makefile$ ls -l main.c
-rwxrwxrwx 1 linux linux 265 12月 17 12:05 main.c
linux@ubuntu:~/work/01-linux/02-makefile/01-Makefile$ sudo chown root:root main.c
[sudo] linux 的密码:
linux@ubuntu:~/work/01-linux/02-makefile/01-Makefile$ ls -l main.c
-rwxrwxrwx 1 root root 265 12月 17 12:05 main.c

chmod:更改文件的权限。chown:更改文件的所有者。

linux@ubuntu:~/work$ ls
01-linux  test.c
linux@ubuntu:~/work$ cp test.c test1.c
linux@ubuntu:~/work$ ls
01-linux  test1.c  test.c
linux@ubuntu:~/work$ mkdir -p dir1/dir2
linux@ubuntu:~/work$ ls
01-linux  dir1  test1.c  test.c
linux@ubuntu:~/work$ cp dir1 hello
cp: 未指定 -r;略过目录 'dir1'
linux@ubuntu:~/work$ cp -rf dir1 hello
linux@ubuntu:~/work$ ls
01-linux  dir1  hello  test1.c  test.c
linux@ubuntu:~/work$ ls /hello
ls: 无法访问 '/hello': 没有那个文件或目录
linux@ubuntu:~/work$ ls hello
dir2
linux@ubuntu:~/work$ mv hello/dir2 .
linux@ubuntu:~/work$ ls
01-linux  dir1  dir2  hello  test1.c  test.c

cp:复制文件。cp -rf:复制目录。mv:剪切和重命名文件。

linux@ubuntu:~/work$ ls
01-linux
linux@ubuntu:~/work$ mkdir -p dir1/dir2
linux@ubuntu:~/work$ ls
01-linux  dir1
linux@ubuntu:~/work$ zip -r dir1.zip dir/
	zip warning: name not matched: dir/
zip error: Nothing to do! (try: zip -r dir1.zip . -i dir/)
linux@ubuntu:~/work$ zip -r dir1.zip dir1/
  adding: dir1/ (stored 0%)
  adding: dir1/dir2/ (stored 0%)
linux@ubuntu:~/work$ ls
01-linux  dir1  dir1.zip
linux@ubuntu:~/work$ rm -rf dir1
linux@ubuntu:~/work$ ls
01-linux  dir1.zip
linux@ubuntu:~/work$ unzip dir1.zip
Archive:  dir1.zip
   creating: dir1/
   creating: dir1/dir2/
linux@ubuntu:~/work$ ls
01-linux  dir1  dir1.zip
linux@ubuntu:~/work$ tar cvJf dir1.xz dir1
dir1/
dir1/dir2/
linux@ubuntu:~/work$ ls
01-linux  dir1  dir1.xz  dir1.zip
linux@ubuntu:~/work$ rm -rf dir1
linux@ubuntu:~/work$ ls
01-linux  dir1.xz  dir1.zip
linux@ubuntu:~/work$ tar xvf dir1.xz
dir1/
dir1/dir2/
linux@ubuntu:~/work$ ls
01-linux  dir1  dir1.xz  dir1.zip
linux@ubuntu:~/work$ 7z a dir1.7z dir1
7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=zh_CN.UTF-8,Utf16=on,HugeFiles=on,64 bits,4 CPUs 13th Gen Intel(R) Core(TM) i9-13900H (B06A2),ASM,AES-NI)
Scanning the drive:
2 folders, 0 files, 0 bytes
Creating archive: dir1.7z
Items to compress: 2
Files read from disk: 0
Archive size: 127 bytes (1 KiB)
Everything is Ok
linux@ubuntu:~/work$ ls
01-linux  dir1  dir1.7z  dir1.xz  dir1.zip
linux@ubuntu:~/work$ rm -rf dir1
linux@ubuntu:~/work$ ls
01-linux  dir1.7z  dir1.xz  dir1.zip
linux@ubuntu:~/work$ 7z x dir1.7z
7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=zh_CN.UTF-8,Utf16=on,HugeFiles=on,64 bits,4 CPUs 13th Gen Intel(R) Core(TM) i9-13900H (B06A2),ASM,AES-NI)
Scanning the drive for archives:
1 file, 127 bytes (1 KiB)
Extracting archive: dir1.7z
--
Path = dir1.7z
Type = 7z
Physical Size = 127
Headers Size = 127
Solid = -
Blocks = 0
Everything is Ok
Folders: 2
Files: 0
Size:       0
Compressed: 127
linux@ubuntu:~/work$ ls
01-linux  dir1  dir1.7z  dir1.xz  dir1.zip

zip -r:压缩目录和子目录。unzip:解压zip压缩包。tar cvJf:linux常用的一个压缩方法。tar xvf解压tar压缩包。7z -a:压缩。7z -x :解压7z压缩包。

linux@ubuntu:~/work$ ls
01-linux  hello.c
linux@ubuntu:~/work$ cp hello.c hello1.c
linux@ubuntu:~/work$ ls
01-linux  hello1.c  hello.c
linux@ubuntu:~/work$ diff hello.c hello1.c
linux@ubuntu:~/work$ vi hello1.c
linux@ubuntu:~/work$ diff hello.c hello1.c
4c4
<     printf("hello");
---
>     printf("hello world");
linux@ubuntu:~/work$ diff -uNr hello.c hello1.c > 1.patch
linux@ubuntu:~/work$ diff -uNr hello.c hello1.c > 1.patch
linux@ubuntu:~/work$ cat 1.patch
--- hello.c	2025-12-17 20:41:45.714867917 +0800
+++ hello1.c	2025-12-17 20:43:20.982784838 +0800
@@ -1,6 +1,6 @@
 #include 
 int main(int argc, const char *argv[])
 {
-    printf("hello");
+    printf("hello world");
     return 0;
 }
linux@ubuntu:~/work$ patch -p0 < 1.patch
patching file hello.c
linux@ubuntu:~/work$ diff hello.c hello1.c

diff:用于比较两个文件的不同,不同则将不同的地方输出,相同则不输出任何东西。diff -nNr:生成补丁。patch:对文件打补丁,后可跟参数 -p0表示需要打补丁的两个参数在同一个目录下,-p1表示在子一级目录下进行,后面的参数一次类推。

3.磁盘管理

        在Linux系统中,⽬录、字符设备、套接字、硬盘、光驱、打印机等都被抽象成⽂件形式,⼀直强调的“Linux系统中⼀切都是⽂件。既然平时我们打交道的都是⽂件,那么⼜应该如何找到它们呢?在Windows操作系统中,想要找到⼀个⽂件,要依次进⼊该⽂件所在的磁盘分区,然后再进⼊该分区下的具体⽬录,最终找到这个⽂件。在Linux系统中并不存在CDEF等盘符,Linux系统中的⼀切⽂件都是从⽬录(/)开始的,并按照⽂件系统层次标准(FHS) 采⽤倒树状结构来存放⽂件,以及定义了常⻅⽬录的⽤途。 另外,Linux系统中的⽂件和⽬录名称是严格区分⼤⼩写的。

        在磁盘管理中fdisk是用来创建和维护分区表的程序,mkfs是用来进行磁盘格式化的。mount和unmount分别是用来挂载设备和卸载设备的。e2label 命令⽤来设置ext2ext3ext4⽂件系统的卷标, 卷标就是盘符。 fatlabel 命令⽤来设置fat⽂件系统的卷标。 ntfslabel 命令⽤来设置ntfs⽂件系统的卷标。

linux@ubuntu:~/work$ df
文件系统          1K的块     已用      可用 已用% 挂载点
tmpfs             396132     3556    392576    1% /run
/dev/sda3       50770432 14762056  33396972   31% /
tmpfs            1980644        0   1980644    0% /dev/shm
tmpfs               5120        4      5116    1% /run/lock
vmhgfs-fuse    209714172 79031784 130682388   38% /mnt/hgfs
/dev/sda2         524252     6232    518020    2% /boot/efi
tmpfs             396128      100    396028    1% /run/user/1000
/dev/sr0          160192   160192         0  100% /media/linux/CDROM
linux@ubuntu:~/work$ ls
01-linux
linux@ubuntu:~/work$ du -h 01-linux/
72K	01-linux/01-shell
44K	01-linux/02-makefile/04-exam
44K	01-linux/02-makefile/02-exam
44K	01-linux/02-makefile/03-exam
44K	01-linux/02-makefile/01-Makefile
180K	01-linux/02-makefile
linux@ubuntu:~/work/01-linux/01-shell$ du -h 01-hello.sh
4.0K	01-hello.sh

df:用来查看磁盘空间使用情况。du -h:用来查看目录或者文件的内存大小。

posted @ 2026-01-15 09:38  yangykaifa  阅读(2)  评论(0)    收藏  举报