且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Linux Shell 使用技巧

更新时间:2022-08-13 07:39:47

一次创建多个目录

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,如需转载请自行联系原作者