Linux基础命令题(ps/ls + grep)

最近遇到两道题目,均是关于Linux的基础命令的。

1. 如何查找并杀死一个进程:

1 derek@derek-VirtualBox:~$ ps -ef | grep eric
2 derek     2097     1 10 10:32 ?        00:00:01 /usr/bin/python /usr/share/eric/modules/eric4.py
3 derek     2105  2097  0 10:32 ?        00:00:00 /usr/bin/python /usr/share/eric/modules/DebugClients/Python/DebugClient.py  39680 1 127.0.0.1
4 derek     2107  1807  0 10:32 pts/2    00:00:00 grep --color=auto eric
5 derek@derek-VirtualBox:~$ ps -ef | grep eric | grep -v grep
6 derek     2097     1  5 10:32 ?        00:00:01 /usr/bin/python /usr/share/eric/modules/eric4.py
7 derek     2105  2097  0 10:32 ?        00:00:00 /usr/bin/python /usr/share/eric/modules/DebugClients/Python/DebugClient.py  39680 1 127.0.0.1
8 derek@derek-VirtualBox:~$ 

PS列出所有进程 参数 -e代表全部进程,-f代表全部信息,将结果输出到通道并利用grep查找包含关键字"eric"的进程。

这里有个地方要注意,进程2107正是现在使用grep查找eric的终端的进程。使用 grep -v grep搜索没有“grep”关键字的进程。防止列表出grep的进程号。

然后用kill + 进程号杀死进程。

2. 列出目录下所有.py的文件:

 1 derek@derek-VirtualBox:~/QSTK$ ls | grep .py
 2 epydoc-3.0.1
 3 __init__.py
 4 __init__.pyc
 5 derek@derek-VirtualBox:~/QSTK$ ls | grep *.py
 6 __init__.py
 7 __init__.pyc
 8 derek@derek-VirtualBox:~/QSTK$ ls | grep -w *.py
 9 __init__.py
10 derek@derek-VirtualBox:~/QSTK$ 

这里要注意的是使用-w表示整个单词匹配,不然会列出.pyX的文件。

3.一些文件和目录操作:

(1) 复制子文件和子文件夹到另一个目录。

cp -r ~/Documents/Aptana\ Studio\ 3\ Workspace/* ~/share/workspaces

注意-r表示递归复制Aptana文件夹下的子文件和子文件目录, \表示转义,在命令行中使用空格必须转义。

(2) 目录和子文件的权限:

https://help.ubuntu.com/community/FilePermissions

原文地址:https://www.cnblogs.com/techyc/p/2957240.html