Menu Close

管理员最最常用的50个linux命令及应用举例分析

该篇文章介绍linux50个常用的命令。并给出每个命令的举例。

1. tar command 举例

产生一个新的压缩文件, C – create 产生


解压一个压缩文件, x – extract 解压


观看一个压缩文件 t – view


 

2. grep command 举例

搜索一个文件内的字符串 ( 大小写敏感)

grep -i “the” demo_file

打印出含有字符串的行,并在该行后留出三行


r – recursively

在一个目录内搜索所有文件的给定字符串


 

3. find command举例

发现文件通过文件名 (大小写敏感)


对发现的文件进行md5sum操作


home 目录下发现所有空文件


 

4. ssh command 举例

登入遥远的服务器 remotehost.example.com


Debug ssh client


Display ssh client version

显示SSH终端的版本号


 

5. sed command 举例

When you copy a DOS file to Unix, you could find \r\n in the end of each line. This example converts the DOS file format to Unix file format using sed command.

当你拷贝DOS文件到Unix时,在每一个文件后有\r\n。下例子说明如何把DOS文件格式转化成Unix文件格式。


Print file content in reverse order

相反顺序打印出文件内容


Add line number for all non-empty-lines in a file

打印出一个文件内各非空行文件的行号。


 

6. awk command 举例

利用awk命令删除重复的行


打印出/etc/passwd 中含有同样uid 和gid的所有行


打印出一个文件的特定范围。


 

7. vim command 举例

光标到第143行


光标到第一个选定的match


以只读形式打开文件


8. diff command举例

比较时忽略空格


 

9. sort command 举例

Sort a file in ascending order


Sort a file in descending order


Sort passwd file by 3rd field.

10. export command 举例

查看oracle相关的环境变量


输出一个环境变量

11. xargs command 举例

把所有图文件拷贝到外部的硬盘。


搜索所有 jpg图文件并压缩


Download all the URLs mentioned in the url-list.txt file

下载所有的URL在url-list.txt文件内

12. ls command 举例 (最常用的命令)

Display filesize in human readable format (e.g. KB, MB etc.,)

人类可读的模式显示文件大小


Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr (修改时间)


Visual Classification of Files With Special Characters Using ls -F


13. pwd 举例

pwd 是打印当前工作目录。最常用的命令

14. cd command 举例 (最常用的命令)

Use “cd -” 两个目录间切换

Use “shopt -s cdspell” to 自动修改打错的目录 automatically correct mistyped directory names on cd

15. gzip command 举例

To create a *.gz compressed file:


To uncompress a *.gz file:


Display compression ratio of the compressed file using gzip -l

16. bzip2 command 举例

To create a *.bz2 compressed file:


To uncompress a *.bz2 file:


17. unzip command举例

To extract a *.zip compressed file: (解压一个压缩文件)


View the contents of *.zip file (Without unzipping it):

18. shutdown command 举例

Shutdown the system and turn the power off immediately.


Shutdown the system after 10 minutes.


Reboot the system using shutdown command.


Force the filesystem check during reboot.


19. ftp command 举例

Both ftp and secure ftp (sftp) has similar commands. To connect to a remote server and download multiple files, do the following.


To view the file names located on the remote server before downloading, mls ftp command as shown below.


20. crontab command 举例

View crontab entry for a specific user


Schedule a cron job every 10 minutes.


21. service command 举例

Service command is used to run the system V init scripts. i.e Instead of calling the scripts located in the /etc/init.d/ directory with their full path, you can use the service command.

Check the status of a service:


Check the status of all the services.


Restart a service.

22. ps command 举例

ps command is used to display information about the processes that are running in the system.

While there are lot of arguments that could be passed to a ps command, following are some of the common ones.

To view current running processes.


To view current running processes in a tree structure. H option stands for process hierarchy.

23. free command 举例

This command is used to display the free, used, swap memory available in the system.

Typical free command output. The output is displayed in bytes.


If you want to quickly check how many GB of RAM your system has use the -g option. -b option displays in bytes, -k in kilo bytes, -m in mega bytes.


If you want to see a total memory ( including the swap), use the -t switch, which will display a total line as shown below.

24. top command 举例

top 命令显示系统中排名靠前的进程(默认按 CPU 使用率排序)。要按任何列对顶部输出进行排序,请按 O(大写 O),它将显示您可以排序的所有可能列,如下所示。


To displays only the processes that belong to a particular user use -u option. The following will show only the top processes that belongs to oracle user.


25. df command 举例

Displays the file system disk space usage. By default df -k displays output in bytes.


df -h displays output in human readable form. i.e size will be displayed in GB’s.


Use -T option to display what type of file system.

26. kill command 举例

Use kill command to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running Linux process as shown below. You can also use killall, pkill, xkill to terminate a unix process.


27. rm command 举例

Get confirmation before removing the file.


It is very useful while giving shell metacharacters in the file name argument.

Print the filename and get confirmation before removing the file.


Following example recursively removes all files and directories under the example directory. This also removes the example directory itself.

28. cp command 举例

Copy file1 to file2 preserving the mode, ownership and timestamp.


Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.

29. mv command 举例

Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.


Note: mv -f is just the opposite, which will overwrite file2 without prompting.

mv -v will print what is happening during file rename, which is useful while specifying shell metacharacters in the file name argument.

30. cat command 举例

You can view multiple files at the same time. Following example prints the content of file1 followed by file2 to stdout.


While displaying the file, following cat -n command will prepend the line number to each line of the output.

31. mount command 举例

To mount a file system, you should first create a directory and mount it as shown below.


You can also add this to the fstab for automatic mounting. i.e Anytime system is restarted, the filesystem will be mounted.

32. chmod command 举例

chmod command is used to change the permissions for a file or directory.

Give full access to user and group (i.e read, write and execute ) on a specific file.


Revoke all access for the group (i.e read, write and execute ) on a specific file.


Apply the file permissions recursively to all the files in the sub-directories.


33. chown command 举例

chown command is used to change the owner and group of a file.

To change owner to oracle and group to db on a file. i.e Change both owner and group at the same time.


Use -R to change the ownership recursively.

34. passwd command 举例

Change your password from command line using passwd. This will prompt for the old password followed by the new password.


Super user can use passwd command to reset others password. This will not prompt for current password of the user.


Remove password for a specific user. Root user can disable password for a specific user. Once the password is disabled, the user can login without entering the password.

35. mkdir command 举例

以下示例在您的主目录下创建一个名为 temp 的目录。


Create nested directories using one mkdir command. If any of these directories exist already, it will not display any error. If any of these directories doesn’t exist, it will create them.

36. ifconfig command 举例

使用 ifconfig 命令查看或配置 Linux 系统上的网络接口。

查看所有接口以及状态。


Start or stop a specific interface using up and down command as shown below.


37. uname command 举例

uname 命令显示有关系统的重要信息,例如 — 内核名称、主机名、内核版本号、处理器类型等,

来自 Ubuntu 笔记本电脑的示例 uname 输出如下所示。

38. whereis command 举例

当你想找出某个特定的 Unix 命令存在于何处(例如,ls 命令存在于何处?)时,可以执行以下命令。


When you want to search an executable from a path other than the whereis default path, you can use -B option and give path as argument to it. This searches for the executable lsmk in the /tmp directory, and displays it, if it is available.

39. whatis command 举例

Whatis command displays a single line description about a command.

40. locate command 举例

Using locate command you can quickly search for the location of a specific file (or group of files). Locate command uses the database created by updatedb.

The example below shows all files in the system that contains the word crontab in it.

41. man command 举例

Display the man page of a specific command.


当命令的手册页位于多个部分下时,您可以从特定部分查看该命令的手册页,如下所示。


手册页中提供了以下 8 个部分:

  1. 一般命令
  2. 系统调用
  3. C 库函数
  4. 特殊文件(通常是设备,位于 /dev 中的那些)和驱动程序
  5. 文件格式和约定
  6. 游戏和屏保
  7. 各种各样的小
  8. 系统管理命令和守护进程

例如,当您执行 whatis crontab 时,您会注意到 crontab 有两个手册页(第 1 节和第 5 节)。要查看 crontab 手册页的第 5 部分,请执行以下操作。

42. tail command 举例

默认情况下打印文件的最后 10 行。


Print N number of lines from the file named filename.txt


View the content of the file in real time using tail -f. This is useful to view the log files, that keeps growing. The command can be terminated using CTRL-C.


43. less command 举例

less 在查看巨大的日志文件时非常有效,因为它不需要在打开时加载完整的文件。


One you open a file using less command, following two keys are very helpful.


44. su command 举例

Switch to a different user account using su command. Super user can switch to any other user without entering their password.


从不同的帐户名执行单个命令。在以下示例中,john 可以作为 raj 用户名执行 ls 命令。执行命令后,它将返回到 john 的帐户。


Login to a specified user account, and execute the specified shell instead of the default shell.

45. mysql command 举例

mysql 可能是 Linux 上使用最广泛的开源数据库。即使您没有在服务器上运行 mysql 数据库,您最终也可能使用 mysql 命令 (client) 连接到远程服务器上运行的 mysql 数据库。

连接到远程 192.168.1.2 mysql 数据库。这将提示输入密码。


To connect to a local mysql database.

链接到本地的mysql 数据库


If you want to specify the mysql root password in the command line itself, enter it immediately after -p (without any space).

46. yum command 举例

To install apache using yum. (Linux环境下安装apache)


To upgrade apache using yum.


To uninstall/remove apache using yum.

47. rpm command 举例

To install apache using rpm. (Linux环境下安装apache)


To upgrade apache using rpm. (更新apache)


To uninstall/remove apache using rpm. (下载删除apache)


48. ping command 举例

ping出5个packages.

Ping a remote host by sending only 5 packets.


49. date command 举例

Set the system date: (设置系统时间)


Once you’ve changed the system date, you should syncronize the hardware clock with the system date as shown below.

50. wget command 举例

The quick and effective method to download software, music, video from internet is using wget command.

很快的从URL处下载软件,音乐,视频,和压缩文件


Download and store it with a different name.

下载并保存在不同文件下


Did I miss any frequently used Linux commands? Leave a comment and let me know.

除教程外,本网站大部分文章来自互联网,如果有内容冒犯到你,请联系我们删除!
Posted in Linux 教程