小编还为您整理了以下内容,可能对您也有帮助:
linux系统中find命令之exec使用介绍
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了。
exec解释:
-exec 参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。
{} 花括号代表前面find查找出来的文件名。
使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个/,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。
实例1:ls -l命令放在find命令的-exec选项中
命令:
find . -type f -exec ls -l {} /;
输出:
复制代码
代码如下:
[root@localhost test]# find . -type f -exec ls -l {} /;
-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log
-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log
-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log
-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log
-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log
[root@localhost test]#
说明:
上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。
实例2:在目录中查找更改时间在n日以前的文件并删除它们
命令:
find . -type f -mtime +14 -exec rm {} /;
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 328
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 33 10-28 16:54 log2013.log
-rw-r--r-- 1 root root 127 10-28 16:51 log2014.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log - log.log
-rw-r--r-- 1 root root 25 10-28 17:02 log.log
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
[root@localhost test]# find . -type f -mtime +14 -exec rm {} /;
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log - log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
说明:
在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。
实例3:在目录中查找更改时间在n日以前的文件并删除它们,在删除之前先给出提示
命令:
find . -name "*.log" -mtime +5 -ok rm {} /;
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log - log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} /;
rm ... ./log_link.log ? y
rm ... ./log2012.log ? n
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
说明:
在上面的例子中, find命令在当前目录中查找所有文件名以.log结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。 按y键删除文件,按n键不删除。
实例4:-exec中使用grep命令
命令:
find /etc -name "passwd*" -exec grep "root" {} /;
输出:
复制代码
代码如下:
[root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} /;
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
[root@localhost test]#
说明:
任何形式的命令都可以在-exec选项中使用。 在上面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个root用户。
实例5:查找文件移动到指定目录
命令:
find . -name "*.log" -exec mv {} .. /;
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:49 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# cd test3/
[root@localhost test3]# ll
总计 304
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
[root@localhost test3]# find . -name "*.log" -exec mv {} .. /;
[root@localhost test3]# ll
总计 0[root@localhost test3]# cd ..
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
实例6:用exec选项执行cp命令
命令:
find . -name "*.log" -exec cp {} test3 /;
输出:
复制代码
代码如下:
[root@localhost test3]# ll
总计 0[root@localhost test3]# cd ..
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -name "*.log" -exec cp {} test3 /;
cp: “./test3/log2014.log” 及 “test3/log2014.log” 为同一文件
cp: “./test3/log2013.log” 及 “test3/log2013.log” 为同一文件
cp: “./test3/log2012.log” 及 “test3/log2012.log” 为同一文件
[root@localhost test]# cd test3
[root@localhost test3]# ll
总计 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test3]#
linux系统中find命令之exec使用介绍
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了。
exec解释:
-exec 参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。
{} 花括号代表前面find查找出来的文件名。
使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个/,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。
实例1:ls -l命令放在find命令的-exec选项中
命令:
find . -type f -exec ls -l {} /;
输出:
复制代码
代码如下:
[root@localhost test]# find . -type f -exec ls -l {} /;
-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log
-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log
-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log
-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log
-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log
[root@localhost test]#
说明:
上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。
实例2:在目录中查找更改时间在n日以前的文件并删除它们
命令:
find . -type f -mtime +14 -exec rm {} /;
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 328
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 33 10-28 16:54 log2013.log
-rw-r--r-- 1 root root 127 10-28 16:51 log2014.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log - log.log
-rw-r--r-- 1 root root 25 10-28 17:02 log.log
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
[root@localhost test]# find . -type f -mtime +14 -exec rm {} /;
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log - log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
说明:
在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。
实例3:在目录中查找更改时间在n日以前的文件并删除它们,在删除之前先给出提示
命令:
find . -name "*.log" -mtime +5 -ok rm {} /;
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log - log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} /;
rm ... ./log_link.log ? y
rm ... ./log2012.log ? n
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
说明:
在上面的例子中, find命令在当前目录中查找所有文件名以.log结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。 按y键删除文件,按n键不删除。
实例4:-exec中使用grep命令
命令:
find /etc -name "passwd*" -exec grep "root" {} /;
输出:
复制代码
代码如下:
[root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} /;
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
[root@localhost test]#
说明:
任何形式的命令都可以在-exec选项中使用。 在上面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个root用户。
实例5:查找文件移动到指定目录
命令:
find . -name "*.log" -exec mv {} .. /;
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:49 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# cd test3/
[root@localhost test3]# ll
总计 304
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
[root@localhost test3]# find . -name "*.log" -exec mv {} .. /;
[root@localhost test3]# ll
总计 0[root@localhost test3]# cd ..
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
实例6:用exec选项执行cp命令
命令:
find . -name "*.log" -exec cp {} test3 /;
输出:
复制代码
代码如下:
[root@localhost test3]# ll
总计 0[root@localhost test3]# cd ..
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -name "*.log" -exec cp {} test3 /;
cp: “./test3/log2014.log” 及 “test3/log2014.log” 为同一文件
cp: “./test3/log2013.log” 及 “test3/log2013.log” 为同一文件
cp: “./test3/log2012.log” 及 “test3/log2012.log” 为同一文件
[root@localhost test]# cd test3
[root@localhost test3]# ll
总计 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test3]#
linux系统中find命令之exec使用介绍
find是我们很常用的一个Linux命令,但是我们一般查找出来的并不仅仅是看看而已,还会有进一步的操作,这个时候exec的作用就显现出来了。
exec解释:
-exec 参数后面跟的是command命令,它的终止是以;为结束标志的,所以这句命令后面的分号是不可缺少的,考虑到各个系统中分号会有不同的意义,所以前面加反斜杠。
{} 花括号代表前面find查找出来的文件名。
使用find时,只要把想要的操作写在一个文件里,就可以用exec来配合find查找,很方便的。在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前,最好先用ls命令看一下,确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本,然后是一对儿{ },一个空格和一个/,最后是一个分号。为了使用exec选项,必须要同时使用print选项。如果验证一下find命令,会发现该命令只输出从当前路径起的相对路径及文件名。
实例1:ls -l命令放在find命令的-exec选项中
命令:
find . -type f -exec ls -l {} /;
输出:
复制代码
代码如下:
[root@localhost test]# find . -type f -exec ls -l {} /;
-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log
-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log
-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log
-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log
-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log
-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log
[root@localhost test]#
说明:
上面的例子中,find命令匹配到了当前目录下的所有普通文件,并在-exec选项中使用ls -l命令将它们列出。
实例2:在目录中查找更改时间在n日以前的文件并删除它们
命令:
find . -type f -mtime +14 -exec rm {} /;
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 328
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 33 10-28 16:54 log2013.log
-rw-r--r-- 1 root root 127 10-28 16:51 log2014.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log - log.log
-rw-r--r-- 1 root root 25 10-28 17:02 log.log
-rw-r--r-- 1 root root 37 10-28 17:07 log.txt
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 10-28 14:47 test3
drwxrwxrwx 2 root root 4096 10-28 14:47 test4
[root@localhost test]# find . -type f -mtime +14 -exec rm {} /;
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log - log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
说明:
在shell中用任何方式删除文件之前,应当先查看相应的文件,一定要小心!当使用诸如mv或rm命令时,可以使用-exec选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。
实例3:在目录中查找更改时间在n日以前的文件并删除它们,在删除之前先给出提示
命令:
find . -name "*.log" -mtime +5 -ok rm {} /;
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
lrwxrwxrwx 1 root root 7 10-28 15:18 log_link.log - log.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -name "*.log" -mtime +5 -ok rm {} /;
rm ... ./log_link.log ? y
rm ... ./log2012.log ? n
[root@localhost test]# ll
总计 312
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxrwx 2 root root 4096 11-12 19:32 test3
drwxrwxrwx 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
说明:
在上面的例子中, find命令在当前目录中查找所有文件名以.log结尾、更改时间在5日以上的文件,并删除它们,只不过在删除之前先给出提示。 按y键删除文件,按n键不删除。
实例4:-exec中使用grep命令
命令:
find /etc -name "passwd*" -exec grep "root" {} /;
输出:
复制代码
代码如下:
[root@localhost test]# find /etc -name "passwd*" -exec grep "root" {} /;
root:x:0:0:root:/root:/bin/bash
root:x:0:0:root:/root:/bin/bash
[root@localhost test]#
说明:
任何形式的命令都可以在-exec选项中使用。 在上面的例子中我们使用grep命令。find命令首先匹配所有文件名为“ passwd*”的文件,例如passwd、passwd.old、passwd.bak,然后执行grep命令看看在这些文件中是否存在一个root用户。
实例5:查找文件移动到指定目录
命令:
find . -name "*.log" -exec mv {} .. /;
输出:
复制代码
代码如下:
[root@localhost test]# ll
总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:49 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# cd test3/
[root@localhost test3]# ll
总计 304
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
[root@localhost test3]# find . -name "*.log" -exec mv {} .. /;
[root@localhost test3]# ll
总计 0[root@localhost test3]# cd ..
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]#
实例6:用exec选项执行cp命令
命令:
find . -name "*.log" -exec cp {} test3 /;
输出:
复制代码
代码如下:
[root@localhost test3]# ll
总计 0[root@localhost test3]# cd ..
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:44 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:25 log2014.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-12 22:50 test3
drwxrwxr-x 2 root root 4096 11-12 19:32 test4
[root@localhost test]# find . -name "*.log" -exec cp {} test3 /;
cp: “./test3/log2014.log” 及 “test3/log2014.log” 为同一文件
cp: “./test3/log2013.log” 及 “test3/log2013.log” 为同一文件
cp: “./test3/log2012.log” 及 “test3/log2012.log” 为同一文件
[root@localhost test]# cd test3
[root@localhost test3]# ll
总计 304
-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log
-rw-r--r-- 1 root root 61 11-12 22:54 log2013.log
-rw-r--r-- 1 root root 0 11-12 22:54 log2014.log
[root@localhost test3]#
linux下exec 函数第一个参数和第二个参数的区别22
哪里写的这些...好乱阿..
先解释下基本的:
int main(argc,char * argv[])
main的参数,就是命令行参数.
比如你的可执行文件是test,你希望在程序执行时传入IP地址,那么可以这样:
./test 127.0.0.1
此时,argc =1,argv[1]是就是指向"127.0.0.1"指针(命令参数全部当作字符串来处理的)。
而 argv[0]就代表第一个参数,这里对应的就是"./test"。
argc和argv在mian里面都是可以使用的,出了main的范围就不能使用了。
再来说你提出的第一个地方,exec的问题。
exec实际上包含了一组函数,execl, execlp, execle, execv, execvp, execvpe
具体使用方法,你man execv就可以得到这些函数的使用方法。
exec函数的作用是,产生一个新进程,结束当前进程(具体执行的操作是复制当前进程的一部分数据和权限,然后根据参数启动一个新的进程)。
exec这组函数执行时候,需要提供的参数包括:一个可执行程序的路径,传递给可执行程序的参数。(这里的参数,与刚才说到的main的参数含义相同。)
说到这里应该明白了吧...就一个。
我不知道你要hello world干什么...照你意思给写了个.
第一个,就是你贴出来的代码改动一点点(我这边运行有点问题):
#include <stdio.h>
main(int argc,char* argv[])
{
int i=0
while(i<=argc)
{
printf("arguement %d : %s ",i,argv[i]);
printf("\n");
i++;
}
}
运行程序:
$gcc test.c -o test
$./test hello world
输出结果:
[ksl@myhost WGX]$ ./test hello world
Arguement 0:./test
Arguement 1:hello
Arguement 2:world
Arguement 3:(null)
然后第二个,使用exec的例子,我用execl吧..
文件名是test1.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
void main(int argc,char *argv[])
{
printf("This is not exec...");
execl("./test","hello","world",NULL);
//如果exec执行正常,下面的printf将不会被执行
//因为当前进程已经结束,./test将被执行
printf("exec error");
}
输出结果:
[ksl@myhost WGX]$ ./test1
Arguement 0:hello
Arguement 1:world
Arguement 2:(null)
后者并没输出"./test"....就是exec启动的程序,其命令行参数中只有参数.(我也不晓得原因...=.=||)
linux下exec 函数第一个参数和第二个参数的区别22
哪里写的这些...好乱阿..
先解释下基本的:
int main(argc,char * argv[])
main的参数,就是命令行参数.
比如你的可执行文件是test,你希望在程序执行时传入IP地址,那么可以这样:
./test 127.0.0.1
此时,argc =1,argv[1]是就是指向"127.0.0.1"指针(命令参数全部当作字符串来处理的)。
而 argv[0]就代表第一个参数,这里对应的就是"./test"。
argc和argv在mian里面都是可以使用的,出了main的范围就不能使用了。
再来说你提出的第一个地方,exec的问题。
exec实际上包含了一组函数,execl, execlp, execle, execv, execvp, execvpe
具体使用方法,你man execv就可以得到这些函数的使用方法。
exec函数的作用是,产生一个新进程,结束当前进程(具体执行的操作是复制当前进程的一部分数据和权限,然后根据参数启动一个新的进程)。
exec这组函数执行时候,需要提供的参数包括:一个可执行程序的路径,传递给可执行程序的参数。(这里的参数,与刚才说到的main的参数含义相同。)
说到这里应该明白了吧...就一个。
我不知道你要hello world干什么...照你意思给写了个.
第一个,就是你贴出来的代码改动一点点(我这边运行有点问题):
#include <stdio.h>
main(int argc,char* argv[])
{
int i=0
while(i<=argc)
{
printf("arguement %d : %s ",i,argv[i]);
printf("\n");
i++;
}
}
运行程序:
$gcc test.c -o test
$./test hello world
输出结果:
[ksl@myhost WGX]$ ./test hello world
Arguement 0:./test
Arguement 1:hello
Arguement 2:world
Arguement 3:(null)
然后第二个,使用exec的例子,我用execl吧..
文件名是test1.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
void main(int argc,char *argv[])
{
printf("This is not exec...");
execl("./test","hello","world",NULL);
//如果exec执行正常,下面的printf将不会被执行
//因为当前进程已经结束,./test将被执行
printf("exec error");
}
输出结果:
[ksl@myhost WGX]$ ./test1
Arguement 0:hello
Arguement 1:world
Arguement 2:(null)
后者并没输出"./test"....就是exec启动的程序,其命令行参数中只有参数.(我也不晓得原因...=.=||)
linux下exec 函数第一个参数和第二个参数的区别22
哪里写的这些...好乱阿..
先解释下基本的:
int main(argc,char * argv[])
main的参数,就是命令行参数.
比如你的可执行文件是test,你希望在程序执行时传入IP地址,那么可以这样:
./test 127.0.0.1
此时,argc =1,argv[1]是就是指向"127.0.0.1"指针(命令参数全部当作字符串来处理的)。
而 argv[0]就代表第一个参数,这里对应的就是"./test"。
argc和argv在mian里面都是可以使用的,出了main的范围就不能使用了。
再来说你提出的第一个地方,exec的问题。
exec实际上包含了一组函数,execl, execlp, execle, execv, execvp, execvpe
具体使用方法,你man execv就可以得到这些函数的使用方法。
exec函数的作用是,产生一个新进程,结束当前进程(具体执行的操作是复制当前进程的一部分数据和权限,然后根据参数启动一个新的进程)。
exec这组函数执行时候,需要提供的参数包括:一个可执行程序的路径,传递给可执行程序的参数。(这里的参数,与刚才说到的main的参数含义相同。)
说到这里应该明白了吧...就一个。
我不知道你要hello world干什么...照你意思给写了个.
第一个,就是你贴出来的代码改动一点点(我这边运行有点问题):
#include <stdio.h>
main(int argc,char* argv[])
{
int i=0
while(i<=argc)
{
printf("arguement %d : %s ",i,argv[i]);
printf("\n");
i++;
}
}
运行程序:
$gcc test.c -o test
$./test hello world
输出结果:
[ksl@myhost WGX]$ ./test hello world
Arguement 0:./test
Arguement 1:hello
Arguement 2:world
Arguement 3:(null)
然后第二个,使用exec的例子,我用execl吧..
文件名是test1.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
void main(int argc,char *argv[])
{
printf("This is not exec...");
execl("./test","hello","world",NULL);
//如果exec执行正常,下面的printf将不会被执行
//因为当前进程已经结束,./test将被执行
printf("exec error");
}
输出结果:
[ksl@myhost WGX]$ ./test1
Arguement 0:hello
Arguement 1:world
Arguement 2:(null)
后者并没输出"./test"....就是exec启动的程序,其命令行参数中只有参数.(我也不晓得原因...=.=||)
linux系统-execcmd{}什么意思
linux系统-execcmd括号意思是对查找出的文件执行cmd命令。根据查询相关资料信息显示,括号表示找到的文件,命令要以分号结尾,linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。
linux系统-execcmd{}什么意思
linux系统-execcmd括号意思是对查找出的文件执行cmd命令。根据查询相关资料信息显示,括号表示找到的文件,命令要以分号结尾,linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。
linux系统-execcmd{}什么意思
linux系统-execcmd括号意思是对查找出的文件执行cmd命令。根据查询相关资料信息显示,括号表示找到的文件,命令要以分号结尾,linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户、多任务、支持多线程和多CPU的操作系统。
linux的find -exec命令的问题3
find . -size +200000000c -exec cp /dev/null {} \;
这条指令是将找到的清除
linux的find -exec命令的问题3
find . -size +200000000c -exec cp /dev/null {} \;
这条指令是将找到的清除
Linux shell常用命令
Linux shell常用命令汇总
对于经常使用linux系统的博主来说,基本常用的shell命令是少不了的,下面为大家总结了Linux shell的常用命令,希望对大家有所帮助!
1.检查远程端口是否对bash开放:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
2.让进程转入后台:
Ctrl + z
3、将进程转到前台:
fg
4.产生随机的十六进制数,其中n是字符数:
openssl rand -hex n
5.在当前shell里执行一个文件里的命令:
source /home/user/file.name
6.截取前5个字符:
${variable:0:5}
7.SSH debug 模式:
ssh -vvv user@ip_address
8.SSH with pem key:
ssh user@ip_address -i key.pem
9.用wget抓取完整的网站目录结构,存放到本地目录中:
wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs
10.一次创建多个目录:
mkdir -p /home/user/{test,test1,test2}
11.列出包括子进程的进程树:
ps axwef
12.创建 war 文件:
jar -cvf name.war file
13.测试硬盘写入速度:
dd if=/dev/zero of=/tmp/output.img bs=8k count=256k; rm -rf /tmp/output.img
14.测试硬盘读取速度:
hdparm -Tt /dev/sda
15.获取文本的md5 hash:
echo -n "text" | md5sum
16.检查xml格式:
xmllint --noout file.xml
17.将tar.gz提取到新目录里:
tar zxvf package.tar.gz -C new_dir
18.使用curl获取HTTP头信息:
curl -I http://www.example.com
19.修改文件或目录的时间戳(YYMMDDhhmm):
touch -t 0712250000 file
20.用wget命令执行ftp下载:
wget -m ftp://username:password@hostname
21.生成随机密码(例子里是16个字符长):
LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;
22.快速备份一个文件:
cp some_file_name{,.bkp}
23.访问Windows共享目录:
smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir
24.执行历史记录里的命令(这里是第100行):
!100
25.解压:
unzip package_name.zip -d dir_name
26.输入多行文字(CTRL + d 退出):
cat > test.txt
27.创建空文件或清空一个现有文件:
\> test.txt
28.与Ubuntu NTP server同步时间:
ntpdate ntp.ubuntu.com
29.用netstat显示所有tcp4监听端口:
netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'
30.qcow2镜像文件转换:
qemu-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img \precise-server-cloudimg-amd64-disk1.raw
31.重复运行文件,显示其输出(缺省是2秒一次):
watch ps -ef
32.所有用户列表:
getent passwd
33.Mount root in read/write mode:
mount -o remount,rw /
34.挂载一个目录(这是不能使用链接的情况):
mount --bind /source /destination
35.动态更新DNS server:
nsupdate < <eof p=""> </eof>
update add $HOST 86400 A $IP
send
EOF
36.递归grep所有目录:
grep -r "some_text" /path/to/dir
37.列出前10个最大的文件:
lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n -u | tail
39.打开Vim并跳到文件末:
vim + some_file_name
40.Git 克隆指定分支(master):
git clone git@github.com:name/app.git -b master
41.Git 切换到其它分支(develop):
git checkout develop
42.Git 删除分支(myfeature):
git branch -d myfeature
43.Git 删除远程分支
git push origin :branchName
44.Git 将新分支推送到远程服务器:
git push -u origin mynewfeature
45.打印历史记录中最后一次cat命令:
!cat:p
46.运行历史记录里最后一次cat命令:
!cat
47.找出/home/user下所有空子目录:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
1.检查远程端口是否对bash开放:
find /home/user -maxdepth 1 -type d -empty
48.获取test.txt文件中第50-60行内容:
< test.txt sed -n '50,60p'
49.运行最后一个命令(如果最后一个命令是mkdir /root/test, 下面将会运行: sudo mkdir /root/test):
sudo !!
50.创建临时RAM文件系统 – ramdisk (先创建/tmpram目录):
mount -t tmpfs tmpfs /tmpram -o size=512m
51.Grep whole words:
grep -w "name" test.txt
52.在需要提升权限的.情况下往一个文件里追加文本:
echo "some text" | sudo tee -a /path/file
53.列出所有kill signal参数:
kill -l
54.在bash历史记录里禁止记录最后一次会话:
kill -9 $$
55.扫描网络寻找开放的端口:
nmap -p 8081 172.20.0.0/16
56.设置git email:
git config --global user.email "me@example.com"
57.To sync with master if you have unpublished commits:
git pull --rebase origin master
58.将所有文件名中含有”txt”的文件移入/home/user目录:
find -iname "*txt*" -exec mv -v {} /home/user \;
59.将文件按行并列显示:
paste test.txt test1.txt
60.shell里的进度条:
pv data.log
61.使用netcat将数据发送到Graphite server:
echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000
62.将tabs转换成空格:
expand test.txt > test1.txt
63.Skip bash history:
< space >cmd
64.去之前的工作目录:
cd -
65.拆分大体积的tar.gz文件(每个100MB),然后合并回去:
split –b 100m /path/to/large/archive /path/to/output/files
cat files* > archive
66.使用curl获取HTTP status code:
curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null
67.设置root密码,强化MySQL安全安装:
/usr/bin/mysql_secure_installation
68.当Ctrl + c不好使时:
Ctrl + \
69.获取文件owner:
stat -c %U file.txt
70.block设备列表:
lsblk -f
71.找出文件名结尾有空格的文件:
find . -type f -exec egrep -l " +$" {} \;
72.找出文件名有tab缩进符的文件
find . -type f -exec egrep -l $'\t' {} \;
73.用”=”打印出横线:全选复制放进笔记
printf '%100s\n' | tr ' ' = ;
Linux shell常用命令
Linux shell常用命令汇总
对于经常使用linux系统的博主来说,基本常用的shell命令是少不了的,下面为大家总结了Linux shell的常用命令,希望对大家有所帮助!
1.检查远程端口是否对bash开放:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
2.让进程转入后台:
Ctrl + z
3、将进程转到前台:
fg
4.产生随机的十六进制数,其中n是字符数:
openssl rand -hex n
5.在当前shell里执行一个文件里的命令:
source /home/user/file.name
6.截取前5个字符:
${variable:0:5}
7.SSH debug 模式:
ssh -vvv user@ip_address
8.SSH with pem key:
ssh user@ip_address -i key.pem
9.用wget抓取完整的网站目录结构,存放到本地目录中:
wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs
10.一次创建多个目录:
mkdir -p /home/user/{test,test1,test2}
11.列出包括子进程的进程树:
ps axwef
12.创建 war 文件:
jar -cvf name.war file
13.测试硬盘写入速度:
dd if=/dev/zero of=/tmp/output.img bs=8k count=256k; rm -rf /tmp/output.img
14.测试硬盘读取速度:
hdparm -Tt /dev/sda
15.获取文本的md5 hash:
echo -n "text" | md5sum
16.检查xml格式:
xmllint --noout file.xml
17.将tar.gz提取到新目录里:
tar zxvf package.tar.gz -C new_dir
18.使用curl获取HTTP头信息:
curl -I http://www.example.com
19.修改文件或目录的时间戳(YYMMDDhhmm):
touch -t 0712250000 file
20.用wget命令执行ftp下载:
wget -m ftp://username:password@hostname
21.生成随机密码(例子里是16个字符长):
LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;
22.快速备份一个文件:
cp some_file_name{,.bkp}
23.访问Windows共享目录:
smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir
24.执行历史记录里的命令(这里是第100行):
!100
25.解压:
unzip package_name.zip -d dir_name
26.输入多行文字(CTRL + d 退出):
cat > test.txt
27.创建空文件或清空一个现有文件:
\> test.txt
28.与Ubuntu NTP server同步时间:
ntpdate ntp.ubuntu.com
29.用netstat显示所有tcp4监听端口:
netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'
30.qcow2镜像文件转换:
qemu-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img \precise-server-cloudimg-amd64-disk1.raw
31.重复运行文件,显示其输出(缺省是2秒一次):
watch ps -ef
32.所有用户列表:
getent passwd
33.Mount root in read/write mode:
mount -o remount,rw /
34.挂载一个目录(这是不能使用链接的情况):
mount --bind /source /destination
35.动态更新DNS server:
nsupdate < <eof p=""> </eof>
update add $HOST 86400 A $IP
send
EOF
36.递归grep所有目录:
grep -r "some_text" /path/to/dir
37.列出前10个最大的文件:
lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n -u | tail
39.打开Vim并跳到文件末:
vim + some_file_name
40.Git 克隆指定分支(master):
git clone git@github.com:name/app.git -b master
41.Git 切换到其它分支(develop):
git checkout develop
42.Git 删除分支(myfeature):
git branch -d myfeature
43.Git 删除远程分支
git push origin :branchName
44.Git 将新分支推送到远程服务器:
git push -u origin mynewfeature
45.打印历史记录中最后一次cat命令:
!cat:p
46.运行历史记录里最后一次cat命令:
!cat
47.找出/home/user下所有空子目录:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
1.检查远程端口是否对bash开放:
find /home/user -maxdepth 1 -type d -empty
48.获取test.txt文件中第50-60行内容:
< test.txt sed -n '50,60p'
49.运行最后一个命令(如果最后一个命令是mkdir /root/test, 下面将会运行: sudo mkdir /root/test):
sudo !!
50.创建临时RAM文件系统 – ramdisk (先创建/tmpram目录):
mount -t tmpfs tmpfs /tmpram -o size=512m
51.Grep whole words:
grep -w "name" test.txt
52.在需要提升权限的.情况下往一个文件里追加文本:
echo "some text" | sudo tee -a /path/file
53.列出所有kill signal参数:
kill -l
54.在bash历史记录里禁止记录最后一次会话:
kill -9 $$
55.扫描网络寻找开放的端口:
nmap -p 8081 172.20.0.0/16
56.设置git email:
git config --global user.email "me@example.com"
57.To sync with master if you have unpublished commits:
git pull --rebase origin master
58.将所有文件名中含有”txt”的文件移入/home/user目录:
find -iname "*txt*" -exec mv -v {} /home/user \;
59.将文件按行并列显示:
paste test.txt test1.txt
60.shell里的进度条:
pv data.log
61.使用netcat将数据发送到Graphite server:
echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000
62.将tabs转换成空格:
expand test.txt > test1.txt
63.Skip bash history:
< space >cmd
64.去之前的工作目录:
cd -
65.拆分大体积的tar.gz文件(每个100MB),然后合并回去:
split –b 100m /path/to/large/archive /path/to/output/files
cat files* > archive
66.使用curl获取HTTP status code:
curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null
67.设置root密码,强化MySQL安全安装:
/usr/bin/mysql_secure_installation
68.当Ctrl + c不好使时:
Ctrl + \
69.获取文件owner:
stat -c %U file.txt
70.block设备列表:
lsblk -f
71.找出文件名结尾有空格的文件:
find . -type f -exec egrep -l " +$" {} \;
72.找出文件名有tab缩进符的文件
find . -type f -exec egrep -l $'\t' {} \;
73.用”=”打印出横线:全选复制放进笔记
printf '%100s\n' | tr ' ' = ;
Linux shell常用命令
Linux shell常用命令汇总
对于经常使用linux系统的博主来说,基本常用的shell命令是少不了的,下面为大家总结了Linux shell的常用命令,希望对大家有所帮助!
1.检查远程端口是否对bash开放:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
2.让进程转入后台:
Ctrl + z
3、将进程转到前台:
fg
4.产生随机的十六进制数,其中n是字符数:
openssl rand -hex n
5.在当前shell里执行一个文件里的命令:
source /home/user/file.name
6.截取前5个字符:
${variable:0:5}
7.SSH debug 模式:
ssh -vvv user@ip_address
8.SSH with pem key:
ssh user@ip_address -i key.pem
9.用wget抓取完整的网站目录结构,存放到本地目录中:
wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs
10.一次创建多个目录:
mkdir -p /home/user/{test,test1,test2}
11.列出包括子进程的进程树:
ps axwef
12.创建 war 文件:
jar -cvf name.war file
13.测试硬盘写入速度:
dd if=/dev/zero of=/tmp/output.img bs=8k count=256k; rm -rf /tmp/output.img
14.测试硬盘读取速度:
hdparm -Tt /dev/sda
15.获取文本的md5 hash:
echo -n "text" | md5sum
16.检查xml格式:
xmllint --noout file.xml
17.将tar.gz提取到新目录里:
tar zxvf package.tar.gz -C new_dir
18.使用curl获取HTTP头信息:
curl -I http://www.example.com
19.修改文件或目录的时间戳(YYMMDDhhmm):
touch -t 0712250000 file
20.用wget命令执行ftp下载:
wget -m ftp://username:password@hostname
21.生成随机密码(例子里是16个字符长):
LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;
22.快速备份一个文件:
cp some_file_name{,.bkp}
23.访问Windows共享目录:
smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir
24.执行历史记录里的命令(这里是第100行):
!100
25.解压:
unzip package_name.zip -d dir_name
26.输入多行文字(CTRL + d 退出):
cat > test.txt
27.创建空文件或清空一个现有文件:
\> test.txt
28.与Ubuntu NTP server同步时间:
ntpdate ntp.ubuntu.com
29.用netstat显示所有tcp4监听端口:
netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'
30.qcow2镜像文件转换:
qemu-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img \precise-server-cloudimg-amd64-disk1.raw
31.重复运行文件,显示其输出(缺省是2秒一次):
watch ps -ef
32.所有用户列表:
getent passwd
33.Mount root in read/write mode:
mount -o remount,rw /
34.挂载一个目录(这是不能使用链接的情况):
mount --bind /source /destination
35.动态更新DNS server:
nsupdate < <eof p=""> </eof>
update add $HOST 86400 A $IP
send
EOF
36.递归grep所有目录:
grep -r "some_text" /path/to/dir
37.列出前10个最大的文件:
lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n -u | tail
39.打开Vim并跳到文件末:
vim + some_file_name
40.Git 克隆指定分支(master):
git clone git@github.com:name/app.git -b master
41.Git 切换到其它分支(develop):
git checkout develop
42.Git 删除分支(myfeature):
git branch -d myfeature
43.Git 删除远程分支
git push origin :branchName
44.Git 将新分支推送到远程服务器:
git push -u origin mynewfeature
45.打印历史记录中最后一次cat命令:
!cat:p
46.运行历史记录里最后一次cat命令:
!cat
47.找出/home/user下所有空子目录:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
1.检查远程端口是否对bash开放:
find /home/user -maxdepth 1 -type d -empty
48.获取test.txt文件中第50-60行内容:
< test.txt sed -n '50,60p'
49.运行最后一个命令(如果最后一个命令是mkdir /root/test, 下面将会运行: sudo mkdir /root/test):
sudo !!
50.创建临时RAM文件系统 – ramdisk (先创建/tmpram目录):
mount -t tmpfs tmpfs /tmpram -o size=512m
51.Grep whole words:
grep -w "name" test.txt
52.在需要提升权限的.情况下往一个文件里追加文本:
echo "some text" | sudo tee -a /path/file
53.列出所有kill signal参数:
kill -l
54.在bash历史记录里禁止记录最后一次会话:
kill -9 $$
55.扫描网络寻找开放的端口:
nmap -p 8081 172.20.0.0/16
56.设置git email:
git config --global user.email "me@example.com"
57.To sync with master if you have unpublished commits:
git pull --rebase origin master
58.将所有文件名中含有”txt”的文件移入/home/user目录:
find -iname "*txt*" -exec mv -v {} /home/user \;
59.将文件按行并列显示:
paste test.txt test1.txt
60.shell里的进度条:
pv data.log
61.使用netcat将数据发送到Graphite server:
echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000
62.将tabs转换成空格:
expand test.txt > test1.txt
63.Skip bash history:
< space >cmd
64.去之前的工作目录:
cd -
65.拆分大体积的tar.gz文件(每个100MB),然后合并回去:
split –b 100m /path/to/large/archive /path/to/output/files
cat files* > archive
66.使用curl获取HTTP status code:
curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null
67.设置root密码,强化MySQL安全安装:
/usr/bin/mysql_secure_installation
68.当Ctrl + c不好使时:
Ctrl + \
69.获取文件owner:
stat -c %U file.txt
70.block设备列表:
lsblk -f
71.找出文件名结尾有空格的文件:
find . -type f -exec egrep -l " +$" {} \;
72.找出文件名有tab缩进符的文件
find . -type f -exec egrep -l $'\t' {} \;
73.用”=”打印出横线:全选复制放进笔记
printf '%100s\n' | tr ' ' = ;
Linux shell常用命令
Linux shell常用命令汇总
对于经常使用linux系统的博主来说,基本常用的shell命令是少不了的,下面为大家总结了Linux shell的常用命令,希望对大家有所帮助!
1.检查远程端口是否对bash开放:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
2.让进程转入后台:
Ctrl + z
3、将进程转到前台:
fg
4.产生随机的十六进制数,其中n是字符数:
openssl rand -hex n
5.在当前shell里执行一个文件里的命令:
source /home/user/file.name
6.截取前5个字符:
${variable:0:5}
7.SSH debug 模式:
ssh -vvv user@ip_address
8.SSH with pem key:
ssh user@ip_address -i key.pem
9.用wget抓取完整的网站目录结构,存放到本地目录中:
wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs
10.一次创建多个目录:
mkdir -p /home/user/{test,test1,test2}
11.列出包括子进程的进程树:
ps axwef
12.创建 war 文件:
jar -cvf name.war file
13.测试硬盘写入速度:
dd if=/dev/zero of=/tmp/output.img bs=8k count=256k; rm -rf /tmp/output.img
14.测试硬盘读取速度:
hdparm -Tt /dev/sda
15.获取文本的md5 hash:
echo -n "text" | md5sum
16.检查xml格式:
xmllint --noout file.xml
17.将tar.gz提取到新目录里:
tar zxvf package.tar.gz -C new_dir
18.使用curl获取HTTP头信息:
curl -I http://www.example.com
19.修改文件或目录的时间戳(YYMMDDhhmm):
touch -t 0712250000 file
20.用wget命令执行ftp下载:
wget -m ftp://username:password@hostname
21.生成随机密码(例子里是16个字符长):
LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;
22.快速备份一个文件:
cp some_file_name{,.bkp}
23.访问Windows共享目录:
smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir
24.执行历史记录里的命令(这里是第100行):
!100
25.解压:
unzip package_name.zip -d dir_name
26.输入多行文字(CTRL + d 退出):
cat > test.txt
27.创建空文件或清空一个现有文件:
\> test.txt
28.与Ubuntu NTP server同步时间:
ntpdate ntp.ubuntu.com
29.用netstat显示所有tcp4监听端口:
netstat -lnt4 | awk '{print $4}' | cut -f2 -d: | grep -o '[0-9]*'
30.qcow2镜像文件转换:
qemu-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img \precise-server-cloudimg-amd64-disk1.raw
31.重复运行文件,显示其输出(缺省是2秒一次):
watch ps -ef
32.所有用户列表:
getent passwd
33.Mount root in read/write mode:
mount -o remount,rw /
34.挂载一个目录(这是不能使用链接的情况):
mount --bind /source /destination
35.动态更新DNS server:
nsupdate < <eof p=""> </eof>
update add $HOST 86400 A $IP
send
EOF
36.递归grep所有目录:
grep -r "some_text" /path/to/dir
37.列出前10个最大的文件:
lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB "$9 }' | sort -n -u | tail
39.打开Vim并跳到文件末:
vim + some_file_name
40.Git 克隆指定分支(master):
git clone git@github.com:name/app.git -b master
41.Git 切换到其它分支(develop):
git checkout develop
42.Git 删除分支(myfeature):
git branch -d myfeature
43.Git 删除远程分支
git push origin :branchName
44.Git 将新分支推送到远程服务器:
git push -u origin mynewfeature
45.打印历史记录中最后一次cat命令:
!cat:p
46.运行历史记录里最后一次cat命令:
!cat
47.找出/home/user下所有空子目录:
echo >/dev/tcp/8.8.8.8/53 && echo "open"
1.检查远程端口是否对bash开放:
find /home/user -maxdepth 1 -type d -empty
48.获取test.txt文件中第50-60行内容:
< test.txt sed -n '50,60p'
49.运行最后一个命令(如果最后一个命令是mkdir /root/test, 下面将会运行: sudo mkdir /root/test):
sudo !!
50.创建临时RAM文件系统 – ramdisk (先创建/tmpram目录):
mount -t tmpfs tmpfs /tmpram -o size=512m
51.Grep whole words:
grep -w "name" test.txt
52.在需要提升权限的.情况下往一个文件里追加文本:
echo "some text" | sudo tee -a /path/file
53.列出所有kill signal参数:
kill -l
54.在bash历史记录里禁止记录最后一次会话:
kill -9 $$
55.扫描网络寻找开放的端口:
nmap -p 8081 172.20.0.0/16
56.设置git email:
git config --global user.email "me@example.com"
57.To sync with master if you have unpublished commits:
git pull --rebase origin master
58.将所有文件名中含有”txt”的文件移入/home/user目录:
find -iname "*txt*" -exec mv -v {} /home/user \;
59.将文件按行并列显示:
paste test.txt test1.txt
60.shell里的进度条:
pv data.log
61.使用netcat将数据发送到Graphite server:
echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000
62.将tabs转换成空格:
expand test.txt > test1.txt
63.Skip bash history:
< space >cmd
64.去之前的工作目录:
cd -
65.拆分大体积的tar.gz文件(每个100MB),然后合并回去:
split –b 100m /path/to/large/archive /path/to/output/files
cat files* > archive
66.使用curl获取HTTP status code:
curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null
67.设置root密码,强化MySQL安全安装:
/usr/bin/mysql_secure_installation
68.当Ctrl + c不好使时:
Ctrl + \
69.获取文件owner:
stat -c %U file.txt
70.block设备列表:
lsblk -f
71.找出文件名结尾有空格的文件:
find . -type f -exec egrep -l " +$" {} \;
72.找出文件名有tab缩进符的文件
find . -type f -exec egrep -l $'\t' {} \;
73.用”=”打印出横线:全选复制放进笔记
printf '%100s\n' | tr ' ' = ;下载本文