Linux Shell 使用技巧

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
云数据库 RDS MySQL Serverless,价值2615元额度,1个月
简介:

一次创建多个目录

1
2
3
[root@localhost tmp] # mkdir -p /user/{folder1,folder2,folder3}    
[root@localhost tmp] # ls /user/     
folder1  folder2  folder3

找出根目录下最大的10个目录,并按使用空间从大到小排序

1
2
3
4
5
6
7
8
9
10
11
[root@localhost ~] # du -a ./ | sort -nr | head -n 10    
132380    ./     
132316    . /source     
69916    . /source/ZendGuard-5_5_0 . tar .gz     
18720    . /source/xunzai .com_mysql-5.0.18. tar .gz     
13732    . /source/php-5 .4.11. tar .gz     
6144    . /source/phpMyAdmin-3 .5.6-all-languages. tar .gz     
5996    . /source/httpd-2 .4.3. tar .gz     
5044    . /source/libxml2-2 .9.0. tar .gz     
1984    . /source/pcre-8 .32.zip     
1960    . /source/freetype-2 .4.10. tar .gz

查看根目录下所有以“.”开头的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost ~] # find ./ -name ".[^.]*"    
./.bash_logout     
./.bash_profile     
./.bashrc     
./.cshrc     
./.tcshrc     
./.cache     
./.config     
./.bash_history     
./.xauth96WqtE     
./.mysql_history     
./.mysql_history.TMP     
./.viminfo

修改文件或目录的时间戳

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@localhost ~] # stat person.txt    
   File: ?.erson.txt?     
   Size: 74            Blocks: 8          IO Block: 4096   regular  file     
Device: 803h /2051d     Inode: 145535279   Links: 1     
Access: (0644 /-rw-r--r-- )  Uid: (    0/    root)   Gid: (    0/    root)     
Context: unconfined_u:object_r:admin_home_t:s0     
Access: 2016-04-02 05:05:10.370059171 -0700     
Modify: 2016-04-02 05:04:40.854898705 -0700     
Change: 2016-04-02 05:04:40.913901033 -0700     
Birth: -     
[root@localhost ~] # touch -t 201604052135 person.txt #格式为YYMMDDhhmm     
[root@localhost ~] # stat person.txt     
   File: ?.erson.txt?     
   Size: 74            Blocks: 8          IO Block: 4096   regular  file     
Device: 803h /2051d     Inode: 145535279   Links: 1     
Access: (0644 /-rw-r--r-- )  Uid: (    0/    root)   Gid: (    0/    root)     
Context: unconfined_u:object_r:admin_home_t:s0     
Access: 2016-04-05 21:35:00.000000000 -0700     
Modify: 2016-04-05 21:35:00.000000000 -0700     
Change: 2016-04-05 06:36:16.304945163 -0700     
Birth: -

快速备份一个文件:cp filename{,.bak}

1
2
3
4
5
[root@localhost ~] # ls    
anaconda-ks.cfg  person.txt   source     
[root@localhost ~] # cp person.txt{,.bak}     
[root@localhost ~] # ls     
anaconda-ks.cfg  person.txt  person.txt.bak   source

进程运行到后台

1
[root@localhost ~] # Ctrl + z

进程运行到前台

1
[root@localhost ~] # fg

随机产生10位字符数的十六进制数

1
2
[root@localhost ~] # openssl rand -hex 10    
c3e805e84074211cc698

将文件解压到新的目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[root@localhost src] # ls    
apr-1.4.6. tar .gz        libmcrypt-2.5.8. tar .gz     
apr-util-1.5.1. tar .gz   libpng-1.5.14. tar .gz     
autoconf-2.69. tar .gz    libxml2-2.9.0. tar .gz     
debug                   pcre-8.32.zip     
freetype-2.4.10. tar .gz  php-5.4.11. tar .gz     
gd-2.0.35. tar .gz        phpMyAdmin-3.5.6-all-languages. tar .gz     
httpd-2.4.3             xunzai.com_mysql-5.0.18. tar .gz     
httpd-2.4.3. tar .gz      ZendGuard-5_5_0. tar .gz     
jpegsrc.v8b. tar .gz      zlib-1.2.7. tar .gz     
kernels     
[root@localhost src] # tar zxvf apr-1.4.6.tar.gz -C /tmp/tmp/     
apr-1.4.6/     
apr-1.4.6 /shmem/     
apr-1.4.6 /shmem/win32/     
…………     
[root@localhost src] # ls /tmp/tmp/     
apr-1.4.6

将所有文件名中含有”txt”的文件移入“/tmp/tmp”目录

1
2
3
4
5
[root@localhost ~] # find -iname "*txt*" -exec mv -v {} /tmp/tmp/ \;    
?. /person .txt?.-> ?.tmp /tmp/person .txt?     
?. /person .txt.bak?.-> ?.tmp /tmp/person .txt.bak?     
[root@localhost ~] # ls /tmp/tmp/     
apr-1.4.6  person.txt  person.txt.bak

将任意一行开头为“#”的去除掉

1
2
3
4
5
6
7
8
[root@localhost ~] # cat a.txt    
This is the  file     
#This is another file     
#This is the final file     
[root@localhost ~] # sed '2s/^#//' a.txt     
This is the  file     
This is another  file     
#This is the final file










本文转自 Nico_Lv 51CTO博客,原文链接:http://blog.51cto.com/nearlv/1771641,如需转载请自行联系原作者
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
3天前
|
网络协议 Shell Linux
LabVIEW 在NI Linux实时设备上访问Shell
LabVIEW 在NI Linux实时设备上访问Shell
|
4天前
|
Shell Linux
【Linux】进程实践项目(更新中) — 自主shell编写
前几篇文章,我们学习进程的相关知识:进程概念,进程替换,进程控制。熟悉了进程到底是个什么事情,接下来我们来做一个实践,来运用我们所学的相关知识。这个项目就是手搓一个shell模块,模拟实现Xshell中的命令行输入。
10 1
|
5天前
|
Shell Linux 信息无障碍
5 个有用的 Linux Shell 转义序列
5 个有用的 Linux Shell 转义序列
|
6天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
24 5
|
7天前
|
Linux Shell 程序员
【Linux】权限(shell运行原理、概念,Linux权限)
【Linux】权限(shell运行原理、概念,Linux权限)
14 2
|
7天前
|
存储 运维 Java
Linux笔记02 —— Shell补充
Linux笔记02 —— Shell补充
31 2
|
7天前
|
安全 Linux Shell
Linux笔记01 —— Linux初识与Shell汇总(请配合另一篇《Linux笔记02》一起使用)
Linux笔记01 —— Linux初识与Shell汇总(请配合另一篇《Linux笔记02》一起使用)
19 1
|
7天前
|
Shell Linux 编译器
C语言,Linux,静态库编写方法,makefile与shell脚本的关系。
总结:C语言在Linux上编写静态库时,通常会使用Makefile来管理编译和链接过程,以及Shell脚本来自动化构建任务。Makefile包含了编译规则和链接信息,而Shell脚本可以调用Makefile以及其他构建工具来构建项目。这种组合可以大大简化编译和构建过程,使代码更易于维护和分发。
15 3
|
19天前
|
监控 网络协议 数据可视化
Shell脚本查看linux系统性能瓶颈
Shell脚本查看linux系统性能瓶颈
|
28天前
|
Shell Linux 网络安全
[01 Linux&Shell ] 清华大学电子系科协软件部2023暑期培训
[01 Linux&Shell ] 清华大学电子系科协软件部2023暑期培训
49 0