前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >shell编程——初识(linux基础命令)

shell编程——初识(linux基础命令)

原创
作者头像
奇零才子
发布2024-08-01 23:19:04
650
发布2024-08-01 23:19:04
举报
文章被收录于专栏:Linux运维之路

shell编程——初识(linux基础命令)

随着技术的不断进步,命令行工具在软件开发和系统管理中扮演着越来越重要的角色。在众多命令行工具中,Shell脚本无疑是提高命令行工作效率的强大武器。它不仅能够简化日常任务,还能进行复杂的任务自动化。本文将引导你从基础走向进阶,全面掌握Shell脚本的编写和应用。

Shell脚本基础

首先,让我们认识一下什么是Shell。简单来说,Shell是操作系统的命令行解释器,它可以接收用户的命令并传递给操作系统执行。而Shell脚本则是由一系列命令和声明组成的文件,它能批量执行命令,实现自动化。

下面就简单介绍一下shell中常见的一些命令:

一、文件和目录管理

  • ls: 列出当前目录下的内容。例如ls -l也可简写成ll显示详细列表。 [root@xiaoge ~]# ls -l total 131756 -rw-r--r-- 1 root root 0 May 19 11:12 = -rw-r--r-- 1 root root 480 May 19 11:21 check_web.sh -rw-r--r-- 1 root root 148 May 19 13:28 expr_test1.sh -rw-r--r-- 1 root root 123 May 19 13:33 expr_test2.sh
  • cd: 切换到目标目录。如cd /var/www/。 #/var/www就是目标目录 [root@xiaoge ~]# cd /var/www/ [root@xiaoge www]#
  • mkdir: 创建新目录。如mkdir new_folder。 [root@xiaoge tmp]# mkdir test1
  • rmdir: 删除空目录。如rmdir empty_folder。 [root@xiaoge tmp]# rmdir test2
  • touch: 创建空文件或更新文件时间戳。如touch new_file.txt。 [root@xiaoge tmp]# touch haha1.txt [root@xiaoge tmp]# ll total 20 -rw-r--r-- 1 root root 0 Jul 1 22:35 haha1.txt #再次创建同名文件即为更新文件时间戳 [root@xiaoge tmp]# touch haha1.txt [root@xiaoge tmp]# ll total 20 -rw-r--r-- 1 root root 0 Jul 1 22:36 haha1.txt

二、文件操作

  • cp: 复制文件或目录。如cp file1.txt file2.txt。 [root@xiaoge tmp]# cp haha1.txt haha2.txt [root@xiaoge tmp]# ll total 20 -rw-r--r-- 1 root root 0 Jul 1 22:36 haha1.txt -rw-r--r-- 1 root root 0 Jul 1 22:37 haha2.txt
  • mv: 移动或重命名文件或目录。如mv 源文件/目录 目标文件/目录。 [root@xiaoge tmp]# mv haha1.txt /opt/ [root@xiaoge tmp]# ll /opt/ total 8 drwx--x--x 4 root root 4096 Nov 6 2023 containerd -rw-r--r-- 1 root root 0 Jul 1 22:36 haha1.txt
  • rm: 删除文件或目录。如rm unwanted_file.txt。 [root@xiaoge tmp]# rm -rf haha2.txt #删库跑路命令,慎用
  • cat: 查看文件内容。如cat file.txt。 [root@xiaoge opt]# cat haha1.txt “123456” “5421398” “78514632”
  • chmod: 更改文件或目录的权限。如chmod 755 file.txt。 [root@xiaoge opt]# ll haha1.txt -rw-r--r-- 1 root root 42 Jul 1 22:41 haha1.txt #给文件添加x(可执行权限)——读r(4)写w(2)执行x(1) [root@xiaoge opt]# chmod 755 haha1.txt [root@xiaoge opt]# ll haha1.txt -rwxr-xr-x 1 root root 42 Jul 1 22:41 haha1.txt

三、文本处理

  • grep: 文本搜索工具。如grep 'keyword' file.txt。 [root@xiaoge opt]# grep "123" haha1.txt “123456”
  • sed: 流编辑器,用于文本替换等。如sed 's/old_word/new_word/g' file.txt。 [root@xiaoge opt]# cat haha1.txt “123456” “5421398” “78514632” [root@xiaoge opt]# sed 's/123/xiao/g' haha1.txt “xiao456” “5421398” “78514632”
  • awk: 文本处理工具,用于模式扫描和处理。 [root@xiaoge opt]# cat passwd.txt chrony:x:998:996::/var/lib/chrony:/sbin/nologin ntp:x:38:38::/etc/ntp:/sbin/nologin tcpdump:x:72:72::/:/sbin/nologin nscd:x:28:28:NSCD Daemon:/:/sbin/nologin admin:x:1000:1000::/home/admin:/bin/bash saslauth:x:997:76:Saslauthd user:/run/saslauthd:/sbin/nologin zabbix:x:996:993:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin apache:x:48:48:Apache:/opt/rh/httpd24/root/usr/share/httpd:/sbin/nologin mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin nginx:x:995:992:Nginx web server:/var/lib/nginx:/sbin/nologin #以:为分隔符打印第一列信息 [root@xiaoge opt]# awk -F ":" '{print $1}' passwd.txt chrony ntp tcpdump nscd admin saslauth zabbix apache mysql nginx
  • cut: 截取文件的部分内容。如cut -d ' ' -f 1 file.txt。 #以:为分隔符截取文件第6列内容 [root@xiaoge opt]# cut -d ':' -f 6 passwd.txt /var/lib/chrony /etc/ntp / / /home/admin /run/saslauthd /var/lib/zabbix /opt/rh/httpd24/root/usr/share/httpd /var/lib/mysql /var/lib/nginx

四、系统信息和管理

  • ps: 显示当前进程状态。如ps aux。 [root@xiaoge opt]# ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.1 125596 3284 ? Ss 2023 68:45 /usr/lib/systemd/systemd --switched-root -- root 2 0.0 0.0 0 0 ? S 2023 0:01 [kthreadd] root 3 0.0 0.0 0 0 ? S 2023 0:54 [ksoftirqd/0] root 5 0.0 0.0 0 0 ? S< 2023 0:00 [kworker/0:0H] root 7 0.0 0.0 0 0 ? S 2023 0:10 [migration/0] root 8 0.0 0.0 0 0 ? S 2023 0:00 [rcu_bh]
  • top: 实时显示系统中各个进程的资源占用状况。 top - 22:54:46 up 238 days, 2:55, 1 user, load average: 0.02, 0.05, 0.05 Tasks: 156 total, 1 running, 155 sleeping, 0 stopped, 0 zombie %Cpu(s): 1.0 us, 0.8 sy, 0.0 ni, 98.1 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1882004 total, 79804 free, 672940 used, 1129260 buff/cache KiB Swap: 0 total, 0 free, 0 used. 1008532 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 18672 root 10 -10 147032 13200 6100 S 3.3 0.7 288:57.87 AliYunDunMonito 18661 root 20 0 115224 7212 5104 S 1.0 0.4 57:14.18 AliYunDun 1750 root 20 0 1037128 23556 2956 S 0.3 1.3 884:49.49 containerd 2662 mysql 20 0 1634992 231496 4512 S 0.3 12.3 1339:36 mysqld 16153 zabbix 20 0 233620 2552 576 S 0.3 0.1 29:55.67 zabbix_server 16156 zabbix 20 0 251096 5528 1396 S 0.3 0.3 64:15.21 zabbix_server 18554 root 20 0 51856 4432 2956 S 0.3 0.2 8:40.80 AliYunDunUpdate 1 root 20 0 125596 3284 1876 S 0.0 0.2 68:45.58 systemd 2 root 20 0 0 0 0 S 0.0 0.0 0:01.07 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:54.28 ksoftirqd/0 5 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
  • free: 查看内存使用情况。如free -m。 [root@xiaoge opt]# free -m total used free shared buff/cache available Mem: 1837 656 78 2 1102 985 Swap: 0 0 0
  • df: 查看磁盘使用情况。如df -h。 [root@xiaoge opt]# df -h Filesystem Size Used Avail Use% Mounted on /dev/vda1 50G 9.0G 38G 20% / devtmpfs 909M 0 909M 0% /dev tmpfs 919M 0 919M 0% /dev/shm tmpfs 919M 548K 919M 1% /run tmpfs 919M 0 919M 0% /sys/fs/cgroup tmpfs 184M 0 184M 0% /run/user/0
  • du: 查看目录或文件的磁盘使用空间。如du -sh directory_name。 [root@xiaoge opt]# du -sh /root/ 130M /root/

五、网络工具

  • ping: 测试网络连接。如ping www.google.com。 #linux中ping会一直执行,可以按ctrl+C中断 [root@xiaoge opt]# ping www.baidu.com PING www.a.shifen.com (180.101.50.188) 56(84) bytes of data. 64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=1 ttl=50 time=9.54 ms 64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=2 ttl=50 time=9.59 ms 64 bytes from 180.101.50.188 (180.101.50.188): icmp_seq=3 ttl=50 time=9.61 ms
  • ifconfig: 配置和显示网络接口信息。 [root@xiaoge opt]# ifconfig docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:81:0d:00:60 txqueuelen 0 (Ethernet) RX packets 1923 bytes 136466 (133.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1831 bytes 4502816 (4.2 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  • netstat: 显示网络状态信息。如netstat -tuln。 [root@xiaoge opt]# netstat -tunlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 16110/php-fpm: mast tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2662/mysqld tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3945/httpd tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 999/sshd tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 16100/zabbix_agentd
  • ssh: 安全地访问远程 shell。如ssh username@hostname(或IP)
  • wget: 从网络上自动下载文件。如wget http://example.com/file.txt

六、权限和所有权

  • chown: 改变文件或目录的所有者和群组。如chown user:group file.txt
  • chgrp: 改变文件或目录的群组。如chgrp group file.txt
  • sudo: 以其他用户身份执行命令。如sudo command
  • su: 切换用户。如su - user

**小结:**以上是一些linux shell中常见命令基础用法,如需深入了解可自行资料检索或后台私信,共同学习,共同进步

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • shell编程——初识(linux基础命令)
    • Shell脚本基础
      • 一、文件和目录管理
      • 二、文件操作
      • 三、文本处理
      • 四、系统信息和管理
      • 五、网络工具
      • 六、权限和所有权
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档