网站搜索

适合中级 Linux 用户的 20 个高级命令


您可能会发现第一篇文章非常有用,本文是对 Linux 新手的 20 个有用命令的扩展。第一篇文章面向新手,本文面向中级用户高级用户。在这里您将了解如何自定义搜索,了解进程运行指南以杀死它们,如何使您的Linux终端高效工作是一个重要方面,以及如何编译c,< nix 中的strong>c++、java 程序。

21. 命令:查找

在给定目录中搜索文件,从父目录开始分层搜索到子目录。

root@tecmint:~# find -name *.sh 

./Desktop/load.sh 
./Desktop/test.sh 
./Desktop/shutdown.sh 
./Binary/firefox/run-mozilla.sh 
./Downloads/kdewebdev-3.5.8/quanta/scripts/externalpreview.sh 
./Downloads/kdewebdev-3.5.8/admin/doxygen.sh 
./Downloads/kdewebdev-3.5.8/admin/cvs.sh 
./Downloads/kdewebdev-3.5.8/admin/ltmain.sh 
./Downloads/wheezy-nv-install.sh

注意:-name”选项使搜索区分大小写。您可以使用“-iname”选项来查找某些内容,无论大小写。 (* 是通配符,搜索所有扩展名为“.sh”的文件,您可以使用文件名或文件名的一部分来自定义输出)。

root@tecmint:~# find -iname *.SH ( find -iname *.Sh /  find -iname *.sH)

./Desktop/load.sh 
./Desktop/test.sh 
./Desktop/shutdown.sh 
./Binary/firefox/run-mozilla.sh 
./Downloads/kdewebdev-3.5.8/quanta/scripts/externalpreview.sh 
./Downloads/kdewebdev-3.5.8/admin/doxygen.sh 
./Downloads/kdewebdev-3.5.8/admin/cvs.sh 
./Downloads/kdewebdev-3.5.8/admin/ltmain.sh 
./Downloads/wheezy-nv-install.sh
root@tecmint:~# find -name *.tar.gz 

/var/www/modules/update/tests/aaa_update_test.tar.gz 
./var/cache/flashplugin-nonfree/install_flash_player_11_linux.i386.tar.gz 
./home/server/Downloads/drupal-7.22.tar.gz 
./home/server/Downloads/smtp-7.x-1.0.tar.gz 
./home/server/Downloads/noreqnewpass-7.x-1.2.tar.gz 
./usr/share/gettext/archive.git.tar.gz 
./usr/share/doc/apg/php.tar.gz 
./usr/share/doc/festival/examples/speech_pm_1.0.tar.gz 
./usr/share/doc/argyll/examples/spyder2.tar.gz 
./usr/share/usb_modeswitch/configPack.tar.gz

注意:上述命令会搜索根目录及其所有子目录(包括已挂载的设备)中所有扩展名为“tar.gz”的文件。

阅读 Linux 'find' 命令的更多示例,请参阅 35 Linux 中的查找命令示例

22.命令:grep

grep’命令在给定文件中搜索包含与给定字符串或单词匹配的行。在“/etc/passwd”中搜索“howtoing”用户。

root@tecmint:~# grep tecmint /etc/passwd 

tecmint:x:1000:1000:Tecmint,,,:/home/tecmint:/bin/bash

忽略单词大小写和所有其他带有“-i”选项的组合。

root@tecmint:~# grep -i TECMINT /etc/passwd 

tecmint:x:1000:1000:Tecmint,,,:/home/tecmint:/bin/bash

递归搜索(-r读取每个目录下的所有文件以查找字符串“127.0.0.1”。

root@tecmint:~# grep -r "127.0.0.1" /etc/ 

/etc/vlc/lua/http/.hosts:127.0.0.1
/etc/speech-dispatcher/modules/ivona.conf:#IvonaServerHost "127.0.0.1"
/etc/mysql/my.cnf:bind-address		= 127.0.0.1
/etc/apache2/mods-available/status.conf:    Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/ldap.conf:    Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/info.conf:    Allow from 127.0.0.1 ::1
/etc/apache2/mods-available/proxy_balancer.conf:#    Allow from 127.0.0.1 ::1
/etc/security/access.conf:#+ : root : 127.0.0.1
/etc/dhcp/dhclient.conf:#prepend domain-name-servers 127.0.0.1;
/etc/dhcp/dhclient.conf:#  option domain-name-servers 127.0.0.1;
/etc/init/network-interface.conf:	ifconfig lo 127.0.0.1 up || true
/etc/java-6-openjdk/net.properties:# localhost & 127.0.0.1).
/etc/java-6-openjdk/net.properties:# http.nonProxyHosts=localhost|127.0.0.1
/etc/java-6-openjdk/net.properties:# localhost & 127.0.0.1).
/etc/java-6-openjdk/net.properties:# ftp.nonProxyHosts=localhost|127.0.0.1
/etc/hosts:127.0.0.1	localhost

注意:您可以将以下选项与 grep 一起使用。

  1. -w 表示单词(egrep -w ‘word1|word2‘ /path/to/file)。
  2. -c 用于计数(即模式匹配的总次数)(grep -c ‘word’ /path/to/file)。
  3. –color 用于彩色输出(grep –color server /etc/passwd)。

23.指挥:男人

man”是系统的手动寻呼机。 Man 提供了命令及其用法的所有可能选项的在线文档。几乎所有命令都带有相应的手册页。例如,

root@tecmint:~# man man

MAN(1)                                                               Manual pager utils                                                              MAN(1)

NAME
       man - an interface to the on-line reference manuals

SYNOPSIS
       man  [-C  file]  [-d]  [-D]  [--warnings[=warnings]]  [-R  encoding]  [-L  locale]  [-m  system[,...]]  [-M  path]  [-S list] [-e extension] [-i|-I]
       [--regex|--wildcard] [--names-only] [-a] [-u] [--no-subpages] [-P pager] [-r prompt] [-7] [-E encoding] [--no-hyphenation] [--no-justification]  [-p
       string] [-t] [-T[device]] [-H[browser]] [-X[dpi]] [-Z] [[section] page ...] ...
       man -k [apropos options] regexp ...
       man -K [-w|-W] [-S list] [-i|-I] [--regex] [section] term ...
       man -f [whatis options] page ...
       man -l [-C file] [-d] [-D] [--warnings[=warnings]] [-R encoding] [-L locale] [-P pager] [-r prompt] [-7] [-E encoding] [-p string] [-t] [-T[device]]
       [-H[browser]] [-X[dpi]] [-Z] file ...
       man -w|-W [-C file] [-d] [-D] page ...
       man -c [-C file] [-d] [-D] page ...
       man [-hV]

手册页本身的手册页,类似“man cat”(cat 命令的手册页)和“man ls”(命令 ls 的手册页)。

注意:手册页旨在供命令参考和学习。

24.命令:ps

ps进程)通过称为PID的唯一Id给出正在运行的进程的状态。

root@tecmint:~# ps

 PID TTY          TIME CMD
 4170 pts/1    00:00:00 bash
 9628 pts/1    00:00:00 ps

要列出所有进程的状态以及进程 idPID,请使用选项“-A”。

root@tecmint:~# ps -A

 PID TTY          TIME CMD
    1 ?        00:00:01 init
    2 ?        00:00:00 kthreadd
    3 ?        00:00:01 ksoftirqd/0
    5 ?        00:00:00 kworker/0:0H
    7 ?        00:00:00 kworker/u:0H
    8 ?        00:00:00 migration/0
    9 ?        00:00:00 rcu_bh
....

注意:当您想知道哪些进程正在运行或者有时可能需要PID来杀死进程时,此命令非常有用。您可以将其与“grep”命令一起使用来查找自定义输出。例如,

root@tecmint:~# ps -A | grep -i ssh

 1500 ?        00:09:58 sshd
 4317 ?        00:00:00 sshd

这里“ps”与“grep”命令进行管道连接,以查找我们需要的定制且相关的输出。

25.命令:杀

好吧,从命令的名称你可能已经明白这个命令的用途了。该命令用于终止现在不相关或没有响应的进程。这是一个非常有用的命令,而不是一个非常非常有用的命令。您可能熟悉频繁的 Windows 重新启动,因为大多数情况下,正在运行的进程无法被终止,并且如果被终止,它需要 Windows 重新启动,以便更改可以生效,但在 Linux 世界中,没有这样的事情。在这里您可以杀死一个进程并启动它,而无需重新启动整个系统。

你需要一个进程的 pid (ps) 来杀死它。

假设您想要终止可能没有响应的程序“apache2”。与 grep 命令一起运行“ps -A”。

root@tecmint:~# ps -A | grep -i apache2

1285 ?        00:00:00 apache2

找到进程“apache2”,记下其pid杀死它。例如,在我的例子中,“apache2” pid 是“1285”。

root@tecmint:~# kill 1285 (to kill the process apache2)

注意:每次重新运行进程或启动系统时,都会为每个进程生成一个新的pid,您可以了解当前正在运行的进程及其 >pid 使用命令“ps”。

终止同一进程的另一种方法是。

root@tecmint:~# pkill apache2

注意:Kill 需要作业 ID/进程 ID 来发送信号,与 pkill 一样,您可以选择使用模式、指定流程所有者等

26.命令:whereis

whereis”命令用于定位命令的二进制手册页。例如,要查找命令 'ls' 和 '二进制手册页 >杀死'。

root@tecmint:~# whereis ls 

ls: /bin/ls /usr/share/man/man1/ls.1.gz
root@tecmint:~# whereis kill

kill: /bin/kill /usr/share/man/man2/kill.2.gz /usr/share/man/man1/kill.1.gz

注意:有时这对于了解手动编辑的二进制文件的安装位置很有用。

27. 命令:服务

服务”命令控制“服务”的启动停止重新启动 >'。此命令可以在不重新启动系统的情况下启动重新启动停止服务以使更改生效。

在 Ubuntu 上启动 apache2 服务器

root@tecmint:~# service apache2 start

 * Starting web server apache2                                                                                                                                 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
httpd (pid 1285) already running						[ OK ]

在 Ubuntu 上重新启动 apache2 服务器

root@tecmint:~# service apache2 restart

* Restarting web server apache2                                                                                                                               apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
 ... waiting .apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName  [ OK ]

在 Ubuntu 上停止 apache2 服务器

root@tecmint:~# service apache2 stop

 * Stopping web server apache2                                                                                                                                 apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
 ... waiting                                                           		[ OK ]

注意:所有进程脚本都位于“/etc/init.d”中,并且该路径可能需要包含在某些系统上,即尽管运行“ service apache2 start”你会被要求运行“/etc/init.d/apache2 start”。

28.命令:别名

alias 是一个内置的 shell 命令,可让您为长命令或常用命令指定名称。

我经常使用“ls -l”命令,该命令包含 5 个字符(包括空格)。因此,我为此创建了一个别名“l”。

root@tecmint:~# alias l='ls -l'

检查它是否有效。

root@tecmint:~# l

total 36 
drwxr-xr-x 3 tecmint tecmint 4096 May 10 11:14 Binary 
drwxr-xr-x 3 tecmint tecmint 4096 May 21 11:21 Desktop 
drwxr-xr-x 2 tecmint tecmint 4096 May 21 15:23 Documents 
drwxr-xr-x 8 tecmint tecmint 4096 May 20 14:56 Downloads 
drwxr-xr-x 2 tecmint tecmint 4096 May  7 16:58 Music 
drwxr-xr-x 2 tecmint tecmint 4096 May 20 16:17 Pictures 
drwxr-xr-x 2 tecmint tecmint 4096 May  7 16:58 Public 
drwxr-xr-x 2 tecmint tecmint 4096 May  7 16:58 Templates 
drwxr-xr-x 2 tecmint tecmint 4096 May  7 16:58 Videos

要删除别名“l”,请使用以下“unalias”命令。

root@tecmint:~# unalias l

检查“l”是否仍然是别名。

root@tecmint:~# l

bash: l: command not found

让这个命令变得有趣一点。将某些重要命令设置为其他重要命令的别名。

alias cd='ls -l' (set alias of ls -l to cd)
alias su='pwd' (set alias of pwd to su)
....
(You can create your own)
....

现在,当您的朋友输入“cd”时,请想象一下,当他获得目录列表而不是目录更改时,该有多有趣。当他尝试“su”时,他得到的只是工作目录的位置。您可以稍后使用命令“unalias”删除别名,如上所述。

29. 命令:df

报告文件系统的磁盘使用情况。对于用户和系统管理员跟踪其磁盘使用情况很有用。 “df”通过检查目录条目来工作,这些条目通常仅在文件关闭时更新。

root@tecmint:~# df

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1       47929224 7811908  37675948  18% /
none                   4       0         4   0% /sys/fs/cgroup
udev             1005916       4   1005912   1% /dev
tmpfs             202824     816    202008   1% /run
none                5120       0      5120   0% /run/lock
none             1014120     628   1013492   1% /run/shm
none              102400      44    102356   1% /run/user
/dev/sda5         184307   79852     94727  46% /boot
/dev/sda7       95989516   61104  91045676   1% /data
/dev/sda8       91953192   57032  87218528   1% /personal

有关“df”命令的更多示例,请阅读文章 12 Linux 中的 df 命令示例。

30. 命令:du

估计文件空间使用情况。按层次结构(即以递归方式)输出每个文件的磁盘使用情况摘要。

root@tecmint:~# du

8       ./Daily Pics/wp-polls/images/default_gradient
8       ./Daily Pics/wp-polls/images/default
32      ./Daily Pics/wp-polls/images
8       ./Daily Pics/wp-polls/tinymce/plugins/polls/langs
8       ./Daily Pics/wp-polls/tinymce/plugins/polls/img
28      ./Daily Pics/wp-polls/tinymce/plugins/polls
32      ./Daily Pics/wp-polls/tinymce/plugins
36      ./Daily Pics/wp-polls/tinymce
580     ./Daily Pics/wp-polls
1456    ./Daily Pics
36      ./Plugins/wordpress-author-box
16180   ./Plugins
12      ./May Articles 2013/Xtreme Download Manager
4632    ./May Articles 2013/XCache

注意:“df”仅报告文件系统的使用统计信息,而“du”则测量目录内容。有关更多“du”命令示例和用法,请阅读 10 du(磁盘使用)命令。

31.命令:rm

命令“rm”代表删除。 rm 用于删除文件和目录。

删除目录

root@tecmint:~# rm PassportApplicationForm_Main_English_V1.0

rm: cannot remove `PassportApplicationForm_Main_English_V1.0': Is a directory

该目录不能简单地通过“rm”命令删除,您必须使用“-rf”开关和“rm”。

root@tecmint:~# rm -rf PassportApplicationForm_Main_English_V1.0

警告:如果您不小心进入了错误的目录,“rm -rf”命令是一个破坏性命令。一旦你“rm -rf”一个目录,所有文件和目录本身就会突然永远丢失。请谨慎使用。

32.命令:回显

echo 顾名思义,在标准输出上回显文本。它与shell无关,shell也不读取echo命令的输出。然而,在交互式脚本中,echo 通过终端将消息传递给用户。它是脚本编写、交互式脚本编写中常用的命令之一。

root@tecmint:~# echo "linux-console.net is a very good website" 

linux-console.net is a very good website
创建一个小的交互式脚本

1. 在桌面上创建一个名为“interactive_shell.sh”的文件。 (记住‘.sh’扩展名是必须的)。
2.复制并粘贴以下脚本,完全相同,如下所示。

#!/bin/bash 
echo "Please enter your name:" 
   read name 
   echo "Welcome to Linux $name"

接下来,设置执行权限并运行脚本。

root@tecmint:~# chmod 777 interactive_shell.sh
root@tecmint:~# ./interactive_shell.sh

Please enter your name:
Ravi Saive
Welcome to Linux Ravi Saive

注意:“#!/bin/bash”告诉 shell 它是一个脚本,将其包含在脚本的顶部总是一个好主意。 ‘read’读取给定的输入。

33.命令:passwd

这是一个重要的命令,对于在终端中更改自己的密码很有用。显然,出于安全原因,您需要知道当前的密码。

root@tecmint:~# passwd 

Changing password for tecmint. 
(current) UNIX password: ******** 
Enter new UNIX password: ********
Retype new UNIX password: ********
Password unchanged   [Here was passowrd remians unchanged, i.e., new password=old password]
Enter new UNIX password: #####
Retype new UNIX password:#####

34.命令:lpr

此命令将命令行上指定的文件打印到指定的打印机。

root@tecmint:~# lpr -P deskjet-4620-series 1-final.pdf

注意:“lpq”命令可让您查看打印机的状态(无论是否启动)以及等待打印的作业(文件)。

35.命令:cmp

比较任何类型的两个文件并将结果写入标准输出。默认情况下,“cmp”如果文件相同则返回 0;如果它们不同,则报告出现第一个差异的字节和行号。

为了提供此命令的示例,让我们考虑两个文件:

文件1.txt
root@tecmint:~# cat file1.txt

Hi My name is Tecmint
文件2.txt
root@tecmint:~# cat file2.txt

Hi My name is tecmint [dot] com

现在,让我们比较两个文件并查看命令的输出。

root@tecmint:~# cmp file1.txt file2.txt 

file1.txt file2.txt differ: byte 15, line 1

36.命令:wget

Wget 是一个免费实用程序,用于从 Web 进行非交互式(即可以在后台工作)下载文件。它支持HTTPHTTPSFTP协议和HTTP代理。

使用 wget 下载 ffmpeg

root@tecmint:~# wget http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2

--2013-05-22 18:54:52--  http://downloads.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2
Resolving downloads.sourceforge.net (downloads.sourceforge.net)... 216.34.181.59
Connecting to downloads.sourceforge.net (downloads.sourceforge.net)|216.34.181.59|:80... connected.
HTTP request sent, awaiting response... 302 Found
Location: http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2 [following]
--2013-05-22 18:54:54--  http://kaz.dl.sourceforge.net/project/ffmpeg-php/ffmpeg-php/0.6.0/ffmpeg-php-0.6.0.tbz2
Resolving kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)... 92.46.53.163
Connecting to kaz.dl.sourceforge.net (kaz.dl.sourceforge.net)|92.46.53.163|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 275557 (269K) [application/octet-stream]
Saving to: ‘ffmpeg-php-0.6.0.tbz2’

100%[===========================================================================>] 2,75,557    67.8KB/s   in 4.0s   

2013-05-22 18:55:00 (67.8 KB/s) - ‘ffmpeg-php-0.6.0.tbz2’ saved [275557/275557]

37.命令:挂载

Mount是一个重要的命令,用于挂载不挂载自身的文件系统。您需要 root 权限才能安装设备。

插入文件系统后,首先运行“lsblk”,识别您的设备并记下设备分配的名称。

root@tecmint:~# lsblk 

NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT 
sda      8:0    0 931.5G  0 disk 
├─sda1   8:1    0 923.6G  0 part / 
├─sda2   8:2    0     1K  0 part 
└─sda5   8:5    0   7.9G  0 part [SWAP] 
sr0     11:0    1  1024M  0 rom  
sdb      8:16   1   3.7G  0 disk 
└─sdb1   8:17   1   3.7G  0 part

从这个屏幕可以清楚地看出,我插入了一个4 GB随身碟,因此“sdb1”是我要安装的文件系统。成为root来执行此操作,并更改为挂载所有文件系统的/dev目录。

root@tecmint:~# su
Password:
root@tecmint:~# cd /dev

创建一个任意名称的目录,但应与参考相关。

root@tecmint:~# mkdir usb

现在将文件系统“sdb1”挂载到目录“usb”。

root@tecmint:~# mount /dev/sdb1 /dev/usb

现在,您可以从终端或X-windows系统导航到/dev/usb,并从挂载的目录访问文件。

38.命令:gcc

gcc是Linux环境下‘c’语言的内置编译器。一个简单的c程序,将其保存在桌面上作为Hello.c(记住'.c'扩展名是必须的)。

#include <stdio.h>
int main()
{
  printf("Hello world\n");
  return 0;
}
编译它
root@tecmint:~# gcc Hello.c
运行
root@tecmint:~# ./a.out 

Hello world

注意:编译c程序时,输出会自动生成到新文件“a.out”,并且每次编译 c 程序相同的文件“a.out”被修改。因此,最好在编译期间定义输出文件,这样就不会有覆盖输出文件的风险。

按这样编译
root@tecmint:~# gcc -o Hello Hello.c

这里“-o”将输出发送到“Hello”文件,而不是“a.out”。再次运行它。

root@tecmint:~# ./Hello 

Hello world

39.命令:g++

g++ 是第一个面向对象编程语言“C++”的内置编译器。一个简单的c++程序,将其保存在桌面上,命名为Add.cpp(记住“.cpp”扩展名是必须的)。

#include <iostream>

using namespace std;

int main() 
    {
          int a;
          int b;
          cout<<"Enter first number:\n";
          cin >> a;
          cout <<"Enter the second number:\n";
          cin>> b;
          cin.ignore();
          int result = a + b;
          cout<<"Result is"<<"  "<<result<<endl;
          cin.get();
          return 0;
     }
编译它
root@tecmint:~# g++ Add.cpp
运行
root@tecmint:~# ./a.out

Enter first number: 
...
...

注意:编译c++程序时,输出会自动生成到新文件“a.out”,并且每次编译 c++ 程序相同的文件“a.out”被修改。因此,最好在编译期间定义输出文件,这样就不会有覆盖输出文件的风险。

按这样编译
root@tecmint:~# g++ -o Add Add.cpp
运行
root@tecmint:~# ./Add 

Enter first number: 
...
...

40.命令:java

Java 是世界上使用率最高的编程语言之一,被认为是快速、安全且可靠的。当今大多数基于 Web 的服务都在 Java 上运行。

通过将以下测试粘贴到名为 howtoing.java 的文件中来创建一个简单的 java 程序(记住“.java”扩展名是必须的)。

class tecmint {
  public static void main(String[] arguments) {
    System.out.println("Tecmint ");
  }
}
使用javac编译它
root@tecmint:~# javac tecmint.java
运行
root@tecmint:~# java tecmint

注意:几乎每个发行版都附带了gcc编译器,大部分发行版都内置了g++java编译器,虽然有些可能没有。您可以aptyum所需的软件包。

不要忘记提及您的宝贵意见以及您想在这里看到的文章类型。我很快就会回来,带来一个有趣的话题,关于 Linux 鲜为人知的事实。