Connecting to MS SQL Server from Ubuntu


And now, in a break from the previous trend of fluffy posts, we have a tutorial on how to (deep breath): connect PHP to a MSSQL Server 2008 instance over ODBC from Ubuntu Linux using the FreeTDS driver and unixODBC. Theoretically it would also work for SQL Server 2005.

I don’t know whether half of the settings flags are necessary or even correct, but what follows Worked for Me™, YMMV, etc, etc.

In the commands below, I’ll use as the server housing the SQL Server instance, with a SQL Server user name of devuser, password devpass. I’m assuming SQL Server is set up to listen on its default port, 1433. Keep an eye out, because you’ll need to change these things to your own settings.

First, install unixODBC:

sudo apt-get install unixodbc unixodbc-dev

I also installed the following (perhaps necessary) packages:
sudo apt-get install tdsodbc php5-odbc
Then download, untar, compile, and install FreeTDS (warning, the URL may change):
cd /usr/local
tar xvfz freetds-stable.tgz
cd freetds-0.82
./configure --enable-msdblib --with-tdsver=8.0 --with-unixodbc=/usr
make install
make clean

Attempt a connection over Telnet to your SQL Server instance:
telnet 1433

Use the tsql tool to test out the connection:
tsql -S -U devuser

This should prompt you for the password, after which you can hope against hope to see this beautiful sign:

If that worked, I recommend throwing a (coding) party. Next up is some configging. Open the FreeTDS config file.

Add the following entry to the bottom of the file. We’re setting up a datasource name (DSN) called ‘MSSQL’.
host =
port = 1433
tds version = 8.0

Now open the ODBC configuration file:

And add the following MSSQL driver entry (FreeTDS) at the end:
Description = FreeTDS driver
Driver = /usr/local/lib/
FileUsage = 1
UsageCount = 1 

Then, finally, set up the DSN within ODBC in the odbc.ini file here
By adding this bit to the file:
Description = MS SQL Server
Driver = /usr/local/lib/
Server =
UID = devuser
PWD = devpass
ReadOnly = No
Port = 1433

Test out the connection using the isql tool:
isql -v MSSQL devuser 'devpass'
If you see “Connected!” you’re golden, congratulations! If not, I’m truly sorry; see below where there are some resources that might help.

Now restart Apache and test it from PHP using ‘MSSQL’ as the DSN. If something doesn’t work, you might try installing any or all of these packages:
mdbtools libmdbodbc libmdbtools mdbtools-gmdb

Here are some other resources that were helpful to me through this disastrous journey.

80 Linux Monitoring Tools for SysAdmins


By Jonathan Sundqvist, Engineer at Server Density.
Published on the 4th February, 2015.


The industry is hotting up at the moment, and there are more tools than you can shake a stick at. Here lies the most comprehensive list on the Internet (of monitoring tools). Featuring over 80 ways to monitor your machines. Within this article we outline:

  • Command line tools
  • Network related
  • System related monitoring
  • Log monitoring tools
  • Infrastructure monitoring tools

It’s hard work monitoring and debugging performance problems, but it’s easier with the right tools at the right time. But how much of your valuable time do you think it would take you to investigate all of these tools and find out which one is best for you?

Why not check out Server Density first, it has a beautiful UI, an api that’s easy to use and alerts that will keep downtime to a minimum.


Top 10  System Monitoring Tools

1. Top

This is a small tool which is pre-installed in many unix systems. When you want an overview of all the processes or threads running in the system: top is a good tool. You can order these processes on different criteria and the default criteria is CPU.

2. htop

Htop is essentially an enhanced version of top. It’s easier to sort by processes. It’s visually easier to understand and has built in commands for common things you would like to do. Plus it’s fully interactive.

3. atop

Atop monitors all processes much like top and htop, unlike top and htop however it has daily logging of the processes for long-term analysis. It also shows resource consumption by all processes. It will also highlight resources that have reached a critical load.

4. apachetop

Apachetop monitors the overall performance of your apache webserver. It’s largely based on mytop. It displays current number of reads, writes and the overall number of requests processed.

5. ftptop

ftptop gives you basic information of all the current ftp connections to your server such as the total amount of sessions, how many are uploading and downloading and who the client is.

6. mytop

mytop is a neat tool for monitoring threads and performance of mysql. It gives you a live look into the database and what queries it’s processing in real time.

7. powertop

powertop helps you diagnose issues that has to do with power consumption and power management. It can also help you experiment with power management settings to achieve the most efficient settings for your server. You switch tabs with the tab key.

8. iotop

iotop checks the I/O usage information and gives you a top-like interface to that. It displays columns on read and write and each row represents a process. It also displays the percentage of time the process spent while swapping in and while waiting on I/O.

Network related monitoring

9. ntopng

ntopng is the next generation of ntop and the tool provides a graphical user interface via the browser for network monitoring. It can do stuff such as: geolocate hosts, get network traffic and show ip traffic distribution and analyze it.

10. iftop

iftop is similar to top, but instead of mainly checking for cpu usage it listens to network traffic on selected network interfaces and displays a table of current usage. It can be handy for answering questions such as “Why on earth is my internet connection so slow?!”.

11. jnettop

jnettop visualises network traffic in much the same way as iftop does. It also supports customizable text output and a machine-friendly mode to support further analysis.

12. bandwidthd

BandwidthD tracks usage of TCP/IP network subnets and visualises that in the browser by building a html page with graphs in png. There is a database driven system that supports searching, filtering, multiple sensors and custom reports.

13. EtherApe

EtherApe displays network traffic graphically, the more talkative the bigger the node. It either captures live traffic or can read it from a tcpdump. The displayed can also be refined using a network filter with pcap syntax.

14. ethtool

ethtool is used for displaying and modifying some parameters of the network interface controllers. It can also be used to diagnose Ethernet devices and get more statistics from the devices.

15. NetHogs

NetHogs breaks down network traffic per protocol or per subnet. It then groups by process. So if there’s a surge in network traffic you can fire up NetHogs and see which process is causing it.

16. iptraf

iptraf gathers a variety of metrics such as TCP connection packet and byte count, interface statistics and activity indicators, TCP/UDP traffic breakdowns and station packet and byte counts.

17. ngrep

ngrep is grep but for the network layer. It’s pcap aware and will allow to specify extended regular or hexadecimal expressions to match against packets of .

18. MRTG

MRTG was orginally developed to monitor router traffic, but now it’s able to monitor other network related things as well. It typically collects every five minutes and then generates a html page. It also has the capability of sending warning emails.

19. bmon

Bmon monitors and helps you debug networks. It captures network related statistics and presents it in human friendly way. You can also interact with bmon through curses or through scripting.

20. traceroute

Traceroute is a built-in tool for displaying the route and measuring the delay of packets across a network.

21. IPTState

IPTState allows you to watch where traffic that crosses your iptables is going and then sort that by different criteria as you please. The tool also allows you to delete states from the table.

22. darkstat

Darkstat captures network traffic and calculates statistics about usage. The reports are served over a simple HTTP server and gives you a nice graphical user interface of the graphs.

23. vnStat

vnStat is a network traffic monitor that uses statistics provided by the kernel which ensures light use of system resources. The gathered statistics persists through system reboots. It has color options for the artistic sysadmins.

24. netstat

Netstat is a built-in tool that displays TCP network connections, routing tables and a number of network interfaces. It’s used to find problems in the network.

25. ss

Instead of using netstat, it’s however preferable to use ss. The ss command is capable of showing more information than netstat and is actually faster. If you want a summary statistics you can use the command ss -s.

26. nmap

Nmap allows you to scan your server for open ports or detect which OS is being used. But you could also use this for SQL injection vulnerabilities, network discovery and other means related to penetration testing.

27. MTR

MTR combines the functionality of traceroute and the ping tool into a single network diagnostic tool. When using the tool it will limit the number hops individual packets has to travel while also listening to their expiry. It then repeats this every second.

28. Tcpdump

Tcpdump will output a description of the contents of the packet it just captured which matches the expression that you provided in the command. You can also save the this data for further analysis.

29. Justniffer

Justniffer is a tcp packet sniffer. You can choose whether you would like to collect low-level data or high-level data with this sniffer. It also allows you to generate logs in customizable way. You could for instance mimic the access log that apache has.

System related monitoring

30. nmon

nmon either outputs the data on screen or saves it in a comma separated file. You can display CPU, memory, network, filesystems, top processes. The data can also be added to a RRD database for further analysis.

31. conky

Conky monitors a plethora of different OS stats. It has support for IMAP and POP3 and even support for many popular music players! For the handy person you could extend it with your own scripts or programs using Lua.

32. Glances

Glances monitors your system and aims to present a maximum amount of information in a minimum amount of space. It has the capability to function in a client/server mode as well as monitoring remotely. It also has a web interface.

33. saidar

Saidar is a very small tool that gives you basic information about your system resources. It displays a full screen of the standard system resources. The emphasis for saidar is being as simple as possible.

34. RRDtool

RRDtool is a tool developed to handle round-robin databases or RRD. RRD aims to handle time-series data like CPU load, temperatures etc. This tool provides a way to extract RRD data in a graphical format.

35. monit

Monit has the capability of sending you alerts as well as restarting services if they run into trouble. It’s possible to perform any type of check you could write a script for with monit and it has a web user interface to ease your eyes.

36. Linux process explorer

Linux process explorer is akin to the activity monitor for OSX or the windows equivalent. It aims to be more usable than top or ps. You can view each process and see how much memory usage or CPU it uses.

37. df

df is an abbreviation for disk free and is pre-installed program in all unix systems used to display the amount of available disk space for filesystems which the user have access to.

38. discus

Discus is similar to df however it aims to improve df by making it prettier using fancy features as colors, graphs and smart formatting of numbers.

39. xosview

xosview is a classic system monitoring tool and it gives you a simple overview of all the different parts of the including IRQ.

40. Dstat

Dstat aims to be a replacement for vmstat, iostat, netstat and ifstat. It allows you to view all of your system resources in real-time. The data can then be exported into csv. Most importantly dstat allows for plugins and could thus be extended into areas not yet known to mankind.

41. Net-SNMP

SNMP is the protocol ‘simple network management protocol’ and the Net-SNMP tool suite helps you collect accurate information about your servers using this protocol.

42. incron

Incron allows you to monitor a directory tree and then take action on those changes. If you wanted to copy files to directory ‘b’ once new files appeared in directory ‘a’ that’s exactly what incron does.

43. monitorix

Monitorix is lightweight system monitoring tool. It helps you monitor a single machine and gives you a wealth of metrics. It also has a built-in HTTP server to view graphs and a reporting mechanism of all metrics.

44. vmstat

vmstat or virtual memory statistics is a small built-in tool that monitors and displays a summary about the memory in the machine.

45. uptime

This small command that quickly gives you information about how long the machine has been running, how many users currently are logged on and the system load average for the past 1, 5 and 15 minutes.

46. mpstat

mpstat is a built-in tool that monitors cpu usage. The most common command is using mpstat -P ALL which gives you the usage of all the cores. You can also get an interval update of the CPU usage.

47. pmap

pmap is a built-in tool that reports the memory map of a process. You can use this command to find out causes of memory bottlenecks.

48. ps

The ps command will give you an overview of all the current processes. You can easily select all processes using the command ps -A

49. sar

sar is a part of the sysstat package and helps you to collect, report and save different system metrics. With different commands it will give you CPU, memory and I/O usage among other things.

50. collectl

Similar to sar collectl collects performance metrics for your machine. By default it shows cpu, network and disk stats but it collects a lot more. The difference to sar is collectl is able to deal with times below 1 second, it can be fed into a plotting tool directly and collectl monitors processes more extensively.

51. iostat

iostat is also part of the sysstat package. This command is used for monitoring system input/output. The reports themselves can be used to change system configurations to better balance input/output load between hard drives in your machine.

52. free

This is a built-in command that displays the total amount of free and used physical memory on your machine. It also displays the buffers used by the kernel at that given moment.

53. /Proc file system

The proc file system gives you a peek into kernel statistics. From these statistics you can get detailed information about the different hardware devices on your machine. Take a look at the full list of the proc file statistics

54. GKrellM

GKrellm is a gui application that monitor the status of your hardware such CPU, main memory, hard disks, network interfaces and many other things. It can also monitor and launch a mail reader of your choice.

55. Gnome system monitor

Gnome system monitor is a basic system monitoring tool that has features looking at process dependencies from a tree view, kill or renice processes and graphs of all server metrics.

Log monitoring tools

56. GoAccess

GoAccess is a real-time web log analyzer which analyzes the access log from either apache, nginx or amazon cloudfront. It’s also possible to output the data into HTML, JSON or CSV. It will give you general statistics, top visitors, 404s, geolocation and many other things.

57. Logwatch

Logwatch is a log analysis system. It parses through your system’s logs and creates a report analyzing the areas that you specify. It can give you daily reports with short digests of the activities taking place on your machine.

58. Swatch

Much like Logwatch Swatch also monitors your logs, but instead of giving reports it watches for regular expression and notifies you via mail or the console when there is a match. It could be used for intruder detection for example.

59. MultiTail

MultiTail helps you monitor logfiles in multiple windows. You can merge two or more of these logfiles into one. It will also use colors to display the logfiles for easier reading with the help of regular expressions.

System tools

60. acct or psacct

acct or psacct (depending on if you use apt-get or yum) allows you to monitor all the commands a users executes inside the system including CPU and memory time. Once installed you get that summary with the command ‘sa’.

61. whowatch

Similar to acct this tool monitors users on your system and allows you to see in real time what commands and processes they are using. It gives you a tree structure of all the processes and so you can see exactly what’s happening.

62. strace

strace is used to diagnose, debug and monitor interactions between processes. The most common thing to do is making strace print a list of system calls made by the program which is useful if the program does not behave as expected.

63. DTrace

DTrace is the big brother of strace. It dynamically patches live running instructions with instrumentation code. This allows you to do in-depth performance analysis and troubleshooting. However, it’s not for the weak of heart as there is a 1200 book written on the topic.

64. webmin

Webmin is a web-based system administration tool. It removes the need to manually edit unix configuration files and lets you manage the system remotely if need be. It has a couple of monitoring modules that you can attach to it.

65. stat

Stat is a built-in tool for displaying status information of files and file systems. It will give you information such as when the file was modified, accessed or changed.

66. ifconfig

ifconfig is a built-in tool used to configure the network interfaces. Behind the scenes network monitor tools use ifconfig to set it into promiscuous mode to capture all packets. You can do it yourself with ifconfig eth0 promiscand return to normal mode with ifconfig eth0 -promisc.

67. ulimit

ulimit is a built-in tool that monitors system resources and keeps a limit so any of the monitored resources don’t go overboard. For instance making a fork bomb where a properly configured ulimit is in place would be totally fine.

68. cpulimit

CPUlimit is a small tool that monitors and then limits the CPU usage of a process. It’s particularly useful to make batch jobs not eat up too many CPU cycles.

69. lshw

lshw is a small built-in tool extract detailed information about the hardware configuration of the machine. It can output everything from CPU version and speed to mainboard configuration.

70. w

W is a built-in command that displays information about the users currently using the machine and their processes.

71. lsof

lsof is a built-in tool that gives you a list of all open files and network connections. From there you can narrow it down to files opened by processes, based on the process name, by a specific user or perhaps kill all processes that belongs to a specific user.

Infrastructure monitoring tools

72. Server Density

Our linux monitoring tool! It has a web interface that allows you to set alerts and view graphs for all system and network metrics. You can also set up monitoring of websites whether they are up or down. Server Density allows you to set permissions for users and you can extend your monitoring with our plugin infrastructure or api. The service already supports Nagios plugins.

73. OpenNMS

OpenNMS has four main functional areas: event management and notifications; discovery and provisioning; service monitoring and data collection. It’s designed to be customizable to work in a variety of network environments.

74. SysUsage

SysUsage monitors your system continuously via Sar and other system commands. It also allows notifications to alarm you once a threshold is reached. SysUsage itself can be run from a centralized place where all the collected statistics are also being stored. It has a web interface where you can view all the stats.

75. brainypdm

brainypdm is a data management and monitoring tool that has the capability to gather data from nagios or another generic source to make graphs. It’s cross-platform, has custom graphs and is web based.

76. PCP

PCP has the capability of collating metrics from multiple hosts and does so efficiently. It also has a plugin framework so you can make it collect specific metrics that is important to you. You can access graph data through either a web interface or a GUI. Good for monitoring large systems.

77. KDE system guard

This tool is both a system monitor and task manager. You can view server metrics from several machines through the worksheet and if a process needs to be killed or if you need to start a process it can be done within KDE system guard.

78. Munin

Munin is both a network and a system monitoring tool which offers alerts for when metrics go beyond a given threshold. It uses RRDtool to create the graphs and it has web interface to display these graphs. Its emphasis is on plug and play capabilities with a number of plugins available.

79. Nagios

Nagios is system and network monitoring tool that helps you monitor monitor your many servers. It has support for alerting for when things go wrong. It also has many plugins written for the platform.

80. Zenoss

Zenoss provides a web interface that allows you to monitor all system and network metrics. Moreover it discovers network resources and changes in network configurations. It has alerts for you to take action on and it supports the Nagios plugins.

81. Cacti

(And one for luck!) Cacti is network graphing solution that uses the RRDtool data storage. It allows a user to poll services at predetermined intervals and graph the result. Cacti can be extended to monitor a source of your choice through shell scripts.

82. Zabbix

Zabbix Monitoring
Zabbix is an open source infrastructure monitoring solution. It can use most databases out there to store the monitoring statistics. The Core is written in C and has a frontend in PHP. If you don’t like installing an agent, Zabbix might be an option for you.

Bonus section:

Thanks for your suggestions. It’s an oversight on our part that we’ll have to go back trough and renumber all the headings. In light of that, here’s a short section at the end for some of the Linux monitoring tools recommended by you:

83. collectd

Collectd is a Unix daemon that collects all your monitoring statistics. It uses a modular design and plugins to fill in any niche monitoring. This way collectd stays as lightweight and customizable as possible.

84. Observium

Observium is an auto-discovering network monitoring platform supporting a wide range of hardware platforms and operating systems. Observium focuses on providing a beautiful and powerful yet simple and intuitive interface to the health and status of your network.

85. Nload

It’s a command line tool that monitors network throughput. It’s neat because it visualizes the in and and outgoing traffic using two graphs and some additional useful data like total amount of transferred data. You can install it with


84. SmokePing

SmokePing keeps track of the network latencies of your network and it visualises them too. There are a wide range of latency measurement plugins developed for SmokePing. If a GUI is important to you it’s there is an ongoing development to make that happen.

85. MobaXterm

If you’re working in windows environment day in and day out. You may feel limited by the terminal Windows provides. MobaXterm comes to the rescue and allows you to use many of the terminal commands commonly found in Linux. Which will help you tremendously in your monitoring needs!

86. Shinken monitoring

Shinken is a monitoring framework which is a total rewrite of Nagios in python. It aims to enhance flexibility and managing a large environment. While still keeping all your nagios configuration and plugins.




来源: [选项] [目录名 | 列出相关目录下的所有目录和文件

<code>-a  列出包括.a开头的隐藏文件的所有文件
-A  通-a,但不列出"."和".."
-l  列出文件的详细信息
-c  根据ctime排序显示
-t  根据文件修改时间排序
---color[=WHEN] 用色彩辨别文件类型 WHEN 可以是'never'、'always'或'auto'其中之一
</code> [选项] 源文件或目录 目录或多个源文件 | 移动或重命名文件

<code>-b  覆盖前做备份
-f  如存在不询问而强制覆盖
-i  如存在则询问是否覆盖
-u  较新才覆盖
-t  将多个源文件移动到统一目录下,目录参数在前,文件参数在后
    mv a /tmp/ 将文件a移动到 /tmp目录下
    mv a b 将a命名为b
    mv /home/zenghao test1.txt test2.txt test3.txt

3.cp [选项] 源文件或目录 目录或多个源文件 | 将源文件复制至目标文件,或将多个源文件复制至目标目录。

<code>-r -R 递归复制该目录及其子目录内容
-p  连同档案属性一起复制过去
-f  不询问而强制复制
-s  生成快捷方式
-a  将档案的所有特性都一起复制

4.scp [参数] [原路径] [目标路径] | 在Linux服务器之间复制文件和目录

<code>-v  详细显示输出的具体情况
-r  递归复制整个目录
(1) 复制文件:  
scp local_file remote_username@remote_ip:remote_folder  
scp local_file remote_username@remote_ip:remote_file  
scp local_file remote_ip:remote_folder  
scp local_file remote_ip:remote_file  
(2) 复制目录:  
scp -r local_folder remote_username@remote_ip:remote_folder  
scp -r local_folder remote_ip:remote_folder  
    从 本地 复制到 远程
    scp /home/daisy/full.tar.gz root@ 
    从 远程 复制到 本地
    scp root@/ /home/daisy/full.tar.gz

5.rm [选项] 文件 | 删除文件

<code>-r  删除文件夹
-f  删除不提示
-i  删除提示
-v  详细显示进行步骤

6.touch [选项] 文件 | 创建空文件或更新文件时间

<code>-a  只修改存取时间
-m  值修改变动时间
-r  eg:touch -r a b ,使b的时间和a相同
-t  指定特定的时间 eg:touch -t 201211142234.50 log.log 
    -t time [[CC]YY]MMDDhhmm[.SS],C:年前两位

7.pwd 查看当前所在路径 改变当前目录

<code>- :返回上层目录
.. :返回上层目录
回车  :返回主目录
/   :根目录

9.mkdir [选项] 目录… | 创建新目录

<code>-p  递归创建目录,若父目录不存在则依次创建
-m  自定义创建目录的权限  eg:mkdir -m 777 hehe
-v  显示创建目录的详细信息

10.rmdir 删除空目录

<code>-v  显示执行过程
-p  若自父母删除后父目录为空则一并删除

11.rm [选项] 文件… | 一个或多个文件或目录

<code>-f  忽略不存在的文件,不给出提示
-i  交互式删除
-r  将列出的目录及其子目录递归删除
-v  列出详细信息


<code>-n  输出后不换行
-e  遇到转义字符特殊处理  
        echo "he\nhe"   显示he\nhe
        ehco -e "he\nhe"    显示he(换行了)he
</code> [选项] [文件]..| 一次显示整个文件或从键盘创建一个文件或将几个文件合并成一个文件

<code>-n  编号文件内容再输出
-E  在结束行提示$

14.tac | 反向显示

15.more | 按页查看文章内容,从前向后读取文件,因此在启动时就加载整个文件

<code>+n  从第n行开始显示
-n  每次查看n行数据
+/String    搜寻String字符串位置,从其前两行开始查看
-c  清屏再显示
-p  换页时清屏

16.less | 可前后移动地逐屏查看文章内容,在查看前不会加载整个文件

<code>-m  显示类似于more命令的百分比
-N  显示行号
/   字符串:向下搜索“字符串”的功能
?   字符串:向上搜索“字符串”的功能
n   重复前一个搜索(与 / 或 ? 有关)
N   反向重复前一个搜索(与 / 或 ? 有关)
b   向后翻一页
d   向后翻半页
</code> [选项]… [文件]… | 将输出内容自动加上行号
-b a 不论是否有空行,都列出行号(类似 cat -n)
-b t 空行则不列行号(默认)
-n 有ln rn rz三个参数,分别为再最左方显示,最右方显示不加0,最右方显示加0

18.head [参数]… [文件]… | 显示档案开头,默认开头10行

<code>-v  显示文件名
-c number   显示前number个字符,若number为负数,则显示除最后number个字符的所有内容
-number/n (+)number     显示前number行内容,
-n number   若number为负数,则显示除最后number行数据的所有内容

19.tail [必要参数] [选择参数] [文件] | 显示文件结尾内容

<code>-v  显示详细的处理信息
-q  不显示处理信息
-num/-n (-)num      显示最后num行内容
-n +num 从第num行开始显示后面的数据
-c  显示最后c个字符
-f  循环读取
</code> 编辑文件

<code>:w filename 将文章以指定的文件名保存起来  
:wq 保存并退出
:q! 不保存而强制退出
    按「i」切换进入插入模式「insert mode」,按"i"进入插入模式后是从光标当前位置开始输入文件;








21.which 可执行文件名称 | 查看可执行文件的位置,在PATH变量指定的路径中查看系统命令是否存在及其位置

22.whereis [-bmsu] [BMS 目录名 -f ] 文件名| 定位可执行文件、源代码文件、帮助文件在文件系统中的位置

<code>-b   定位可执行文件。
-m   定位帮助文件。
-s   定位源代码文件。
-u   搜索默认路径下除可执行文件、源代码文件、帮助文件以外的其它文件。
-B   指定搜索可执行文件的路径。
-M   指定搜索帮助文件的路径。
-S   指定搜索源代码文件的路径。

23.locate | 通过搜寻数据库快速搜寻档案

<code>-r  使用正规运算式做寻找的条件

24.find find [PATH] [option] [action] | 在文件树种查找文件,并作出相应的处理

1. 与时间有关的选项:共有 -atime, -ctime 与 -mtime 和-amin,-cmin与-mmin,以 -mtime 说明
    -mtime n :n 为数字,意义为在 n 天之前的『一天之内』被更动过内容的档案;
    -mtime +n :列出在 n 天之前(不含 n 天本身)被更动过内容的档案档名;
    -mtime -n :列出在 n 天之内(含 n 天本身)被更动过内容的档案档名。
    -newer file :file 为一个存在的档案,列出比 file 还要新的档案档名

2. 与使用者或组名有关的参数:
    -uid n :n 为数字,这个数字是用户的账号 ID,亦即 UID
    -gid n :n 为数字,这个数字是组名的 ID,亦即 GID
    -user name :name 为使用者账号名称!例如 dmtsai
    -group name:name 为组名,例如 users ;
    -nouser :寻找档案的拥有者不存在 /etc/passwd 的人!
    -nogroup :寻找档案的拥有群组不存在于 /etc/group 的档案!

3. 与档案权限及名称有关的参数:
    -name filename:搜寻文件名为 filename 的档案(可使用通配符)
    -size [+-]SIZE:搜寻比 SIZE 还要大(+)或小(-)的档案。这个 SIZE 的规格有:
        c: 代表 byte
        k: 代表 1024bytes。所以,要找比 50KB还要大的档案,就是『 -size +50k 』
    -type TYPE :搜寻档案的类型为 TYPE 的,类型主要有:
        一般正规档案 (f)
        装置档案 (b, c)
        目录 (d)
        连结档 (l)
        socket (s)
        FIFO (p)
    -perm mode :搜寻档案权限『刚好等于』 mode的档案,这个mode为类似chmod的属性值,举例来说,-rwsr-xr-x 的属性为4755!
    -perm -mode :搜寻档案权限『必须要全部囊括 mode 的权限』的档案,举例来说,
        我们要搜寻-rwxr--r-- 亦即 0744 的档案,使用-perm -0744,当一个档案的权限为 -rwsr-xr-x ,亦即 4755 时,也会被列出来,因为 -rwsr-xr-x 的属性已经囊括了 -rwxr--r-- 的属性了。
    -perm +mode :搜寻档案权限『包含任一 mode 的权限』的档案,举例来
        说,我们搜寻-rwxr-xr-x ,亦即 -perm +755 时,但一个文件属性为 -rw-------也会被列出来,因为他有 -rw.... 的属性存在!
4. 额外可进行的动作:
    -exec command :command 为其他指令,-exec 后面可再接额外的指令来处理搜寻到的结果。
    -print :将结果打印到屏幕上,这个动作是预设动作!
        find / -perm +7000 -exec ls -l {} \; ,额外指令以-exec开头,以\;结尾{}代替前面找到的内容
    | xargs 
        -i  默认的前面输出用{}代替 
            find . -name "*.log" | xargs -i mv {} test4

25.grep ‘正则表达式’ 文件名 | 用正则表达式搜索文本,并把匹配的行打印出来

<code>-c  只输出匹配行的计数。
-I  不区分大小写(只适用于单字符)。
-l  只显示文件名
-v  显示不包含匹配文本的所有行。
-n  显示匹配行数据及其行号

25.file | 判断文件类型

26.gzip [-cdtv#] 檔名 | 压缩、解压缩,源文件都不再存在

<code>-d  进行解压缩
-c  将压缩的数据输出到屏幕上
-v  :显示原档案/压缩文件案的压缩比等信息
-#  :压缩等级,-1最快,但压缩比最差,=9最慢,但压缩比最好

27.gunzip | 解压缩

28.bzip2 | 压缩、解压缩

<code>-d  :解压
-z  :压缩
-k  :保留源文件
-c :将压缩的过程产生的数据输出到屏幕上!
-v :可以显示出原档案/压缩文件案的压缩比等信息;
-# :与 gzip 同样的,都是在计算压缩比的参数, -9 最佳, -1 最快!

29.bzcat 读取数据而无需解压

30.tar [主选项+辅选项] 文件或者目录 | 多个目录或档案打包、压缩成一个大档案

    -c  建立打包档案,可搭配 -v 来察看过程中被打包的档名(filename)
    -t  察看打包档案的内容含有哪些档名,重点在察看『档名』就是了;
    -x  解打包或解压缩的功能,可以搭配 -C (大写) 在特定目录解开
    -j  透过 bzip2 的支持进行压缩/解压缩:此时档名最好为 *.tar.bz2
    -z  透过 gzip 的支持进行压缩/解压缩:此时档名最好为 *.tar.gz
    -v  在压缩/解压缩的过程中,将正在处理的文件名显示出来!
    -f filename -f 后面要立刻接要被处理的档名!
    -C 目录   这个选项用在解压缩,若要在特定目录解压缩,可以使用这个选项。
    --exclude FILE:在压缩打包过程中忽略某文件 eg: tar --exclude /home/zenghao -zcvf myfile.tar.gz /home/* /etc
    -p  保留备份数据的原本权限与属性,常用于备份(-c)重要的配置文件
    -P(大写)  保留绝对路径,亦即允许备份数据中含有根目录存在之意;
    压 缩:tar -jcvf filename.tar.bz2 要被压缩的档案或目录名称
    查 询:tar -jtvf filename.tar.bz2
    解压缩:tar -jxvf filename.tar.bz2 -C 欲解压缩的目录

31.exit 退出当前shell
32.logout 退出登录shell
33.shutdown -h now

34.users 显示当前登录系统地用户

35.who 登录在本机的用户与来源

<code>-H或--heading  显示各栏位的标题信息列。

36.w 登录在本机的用户及其运行的程序

<code>-s  使用简洁格式列表,不显示用户登入时间,终端机阶段作业和程序所耗费的CPU时间。
-h  不显示各栏位的标题信息列。

37.write 给当前联机的用户发消息

38.wall 给所有登录再本机的用户发消息

39.last 查看用户的登陆日志

40.lastlog 查看每个用户最后的登陆时间

41.finger [选项] [使用者] [用户@主机] | 查看用户信息

<code>-s 显示用户的注册名、实际姓名、终端名称、写状态、停滞时间、登录时间等信息
-l 除了用-s选项显示的信息外,还显示用户主目录、登录shell、邮件状态等信息,以及用户主目录下的.plan、.project和.forward文件的内容。
-p 除了不显示.plan文件和.project文件以外,与-l选项相同

42.hostname 查看主机名

43.alias ii = “ls -l” | 添加别名

44.unalias ii | 清除别名

45.useradd [-u UID] [-g 初始群组] [-G 次要群组] [-c 说明栏] [-d 家目录绝对路径] [-s shell] 使用者账号名 | 新增用户

<code>-M  不建立用户家目录!(系统账号默认值)
-m  建立用户家目录!(一般账号默认值)
-r  建立一个系统的账号,这个账号的 UID 会有限制 
-e  账号失效日期,格式为『YYYY-MM-DD』
-D  查看useradd的各项默认值

46.passwd | 修改密码

<code>-l  使密码失效
-u  与-l相对,用户解锁
-S  列出登陆用户passwd文件内的相关参数
-n  后面接天数,shadow 的第 4 字段,多久不可修改密码天数
-x  后面接天数,shadow 的第 5 字段,多久内必须要更动密码
-w  后面接天数,shadow 的第 6 字段,密码过期前的警告天数
-i  后面接『日期』,shadow 的第 7 字段,密码失效日期
使用管道刘设置密码:echo "zeng" | passwd --stdin zenghao

47.userdel 删除用户

<code>-r  用户文件一并删除

48.chage [-ldEImMW] 账号名 | 修改用户密码的相关属性

<code>-l  列出该账号的详细密码参数;
-d  后面接日期,修改 shadow 第三字段(最近一次更改密码的日期),格式YYYY-MM-DD
-E  后面接日期,修改 shadow 第八字段(账号失效日),格式 YYYY-MM-DD
-I  后面接天数,修改 shadow 第七字段(密码失效日期)
-m  后面接天数,修改 shadow 第四字段(密码最短保留天数)
-M  后面接天数,修改 shadow 第五字段(密码多久需要进行变更)
-W  后面接天数,修改 shadow 第六字段(密码过期前警告日期)

49.usermod [-cdegGlsuLU] username | 修改用户的相关属性

<code>-c  后面接账号的说明,即 /etc/passwd 第五栏的说明栏,可以加入一些账号的说明。
-d  后面接账号的家目录,即修改 /etc/passwd 的第六栏;
-e  后面接日期,格式是 YYYY-MM-DD 也就是在 /etc/shadow 内的第八个字段数据啦!
-f  后面接天数为 shadow 的第七字段。
-g  后面接初始群组,修改 /etc/passwd 的第四个字段,亦即是GID的字段!
-G  后面接次要群组,修改这个使用者能够支持的群组
-l  后面接账号名称。亦即是修改账号名称, /etc/passwd 的第一栏!
-s  后面接 Shell 的实际档案,例如 /bin/bash 或 /bin/csh 等等。
-u  后面接 UID 数字啦!即 /etc/passwd 第三栏的资料;
-L  冻结密码
-U  解冻密码
</code> [username] | 查看用户相关的id信息,还可以用来判断用户是否存在

51.groups 查看登陆用户支持的群组, 第一个输出的群组为有效群组

52.newgrp 切换有效群组

53.groupadd [-g gid] 组名 | 添加组

<code>-g  设定添加组的特定组id

54.groupmod [-g gid] [-n group_name] 群组名 | 修改组信息

<code>-g  修改既有的 GID 数字
-n  修改既有的组名

55.groupdel [groupname] | 删除群组

56.gpasswd | 群组管理员功能

    -gpasswd groupname 设定密码
    -gpasswd [-A user1,...] [-M user3,...] groupname
        -A  将 groupname 的主控权交由后面的使用者管理(该群组的管理员)
        -M  将某些账号加入这个群组当中
    -gpasswd [-r] groupname
        -r  将 groupname 的密码移除
    - gpasswd [-ad] user groupname 
        -a  将某位使用者加入到 groupname 这个群组当中
        -d  将某位使用者移除出 groupname 这个群组当中


58.mount [-t vfstype] [-o options] device dir

<code>-ro 采用只读方式挂接设备
-rw 采用读写方式挂接设备
eg:mount /home/mydisk.iso /tmp/mnt 通过mnt访问mydisk内的内容

59.umount 取消挂载


<code>-b :以字节为单位进行分割。这些字节位置将忽略多字节字符边界,除非也指定了 -n 标志。
-c :以字符为单位进行分割。
-d :自定义分隔符,默认为制表符。
-f  :与-d一起使用,指定显示哪个区域。


<code>-n   依照数值的大小排序。
-o&lt;输出文件&gt;   将排序后的结果存入指定的文件。
-r   以相反的顺序来排序。
-t&lt;分隔字符&gt;   指定排序时所用的栏位分隔字符。
-k  选择以哪个区间进行排序。

62.wc 统计指定文件中的字节数、字数、行数, 并将统计结果显示输出

<code>-l filename 报告行数
-c filename 报告字节数
-m filename 报告字符数
-w filename 报告单词数

63.uniq 去除文件中相邻的重复行

&> 正确、错误都重定向过去


64.set 显示环境变量和普通变量
65.env 显示环境变量
66.export 把普通变量变成环境变量
67.unset 删除一个环境变量

aaa(){} 定义函数

<code>-p  接提示字符
-t  接等待的秒数


<code>-i 声明为整数
-a 声明为数组
-f 声明为函数
-r 声明为只读

70.ulimit 限制使用者的某些系统资源

<code>-f  此 shell 可以建立的最大档案容量 (一般可能设定为 2GB)单位为 Kbytes eg: ulimit -f 1024 限制使用者仅能建立 1MBytes 以下的容量的档案

71.df [选项] [文件] | 显示指定磁盘文件的可用空间,如果没有文件名被指定,则所有当前被挂载的文件系统的可用空间将被显示

<code>-a  显示全部文件系统
-h  文件大小友好显示
-l  只显示本地文件系统
-i  显示inode信息
-T  显示文件系统类型

72.du [选项] [文件] | 显示每个文件和目录的磁盘使用空间

<code>-h  方便阅读的方式
-s  只显示总和的大小

73.ln [参数] [源文件或目录] [目标文件或目录] | 某一个文件在另外一个位置建立一个同步的链接

<code>-s  建立软连接   
-v  显示详细的处理过程

74.diff [参数] [文件1或目录1] [文件2或目录2] | 比较单个文件或者目录内容

<code>-b  不检查空格字符的不同。
-B  不检查空白行。
-i  不检查大小写
-q  仅显示差异而不显示详细信息
eg: diff a b &gt; parch.log 比较两个文件的不同并产生补丁
</code> [参数]… [+格式] | 显示或设定系统的日期与时间

<code>%H 小时(以00-23来表示)。 
%M 分钟(以00-59来表示)。 
%D 日期(含年月日)
%U 该年中的周数。
date -s “2015-10-17 01:01:01″ //时间设定
date +%Y%m%d         //显示前天年月日
date +%Y%m%d --date="+1 day/month/year"  //显示前一天/月/年的日期
date +%Y%m%d --date="-1 day/month/year"  //显示后一天/月/年的日期
date -d '2 weeks' 2周后的日期
</code> [参数] 月份] [年份] | 查看日历

<code>-1  显示当月的月历
-3  显示前、当、后一个月的日历
-m  显示星期一为一个星期的第一天
-s  (默认)星期天为第一天
-j  显示当月是一年中的第几天的日历
-y  显示当前年份的日历
</code> | 列出当前进程的快照

<code>a   显示所有的进程
-a  显示同一终端下的所有程序
e   显示环境变量
f   显示进程间的关系
-H  显示树状结构
r   显示当前终端的程序
T   显示当前终端的所有程序
-au 显示更详细的信息
-aux    显示所有包含其他使用者的行程 
-u  指定用户的所有进程
</code> [参数] | 显示当前系统正在执行的进程的相关信息,包括进程ID、内存占用率、CPU占用率等

79.kill [参数] [进程号] | 杀死进程 [参数] | 显示Linux系统中空闲的、已用的物理内存及swap内存,及被内核使用的buffer

81.vmstat | 对操作系统的虚拟内存、进程、CPU活动进行监控

82.iostat [参数] [时间t] [次数n](每隔t时间刷新一次,最多刷新n次)| 对系统的磁盘操作活动进行监视,汇报磁盘活动统计情况,同时也会汇报出CPU使用情况

<code>-p[磁盘] 显示磁盘和分区的情况
</code> [参数] [命令] |重复执行某一命令以观察变化

<code>-n  时隔多少秒刷新
-d  高亮显示动态变化
</code> [参数] [时间] | 在一个指定的时间执行一个指定任务,只能执行一次

<code>HH:MM[am|pm] + number [minutes|hours|days|weeks] 强制在某年某月某日的某时刻进行该项任务
atq 查看系统未执行的任务
atrm n 删除编号为n的任务
at -c n 显示编号为n的任务的内容

85.crontab | 定时任务调度

<code>file    载入crontab
-e  编辑某个用户的crontab文件内容
-l  显示某个用户的crontab文件内容
-r  删除某个用户的crontab文件

86.ifconfig [网络设备] [参数] | 查看和配置网络设备

87.route | 显示和操作IP路由表 [参数] [主机名或IP地址] | 测试与目标主机的连通性

<code>-q  只显示最后的结果

89.netstat | 显示与IP、TCP、UDP和ICMP协议相关的统计数据

90.telnet [参数] [主机] | 用于远程登录,采用明文传送报文,安全性不好

91.rcp [参数] [源文件] [目标文件] | 远程文件拷贝

<code>-r  递归复制
-p  保留源文件的属性
usage: rcp –r remote_hostname:remote_dir local_dir

92.wget [参数] [URL地址] | 直接从网络上下载文件

<code>-o FILE 把记录写到FILE文件中    eg : wget -O a.txt URL
wget --limit-rate=300k URL  限速下载


<code>-F 分隔符  以分隔符分隔内容
{}  要执行的脚本内容 eg:cat /etc/passwd |awk  -F ':'  '{print $1"\t"$7}'

94.sed 对数据行进行替换、删除、新增、选取等操作

<code>a   新增,在新的下一行出现
c   取代,取代 n1,n2 之间的行 eg: sed '1,2c Hi' ab
d   删除
i   插入,在新的上一行出现

95.paste 合并文件,需确保合并的两文件行数相同

<code>-d  指定不同于空格或tab键的域分隔符
-s  按行合并,单独一个文件为一行
</code> [参数] user | 切换登陆

<code>-l  切换时连同环境变量、工作目录一起改变
-c command  执行command变回原来的使用者

97.sudo | 以特定用户的权限执行特定命令

<code>-l  列出当前用户可执行的命令
-u username#uid 以指定用户执行命令

Linux Shell 文本处理工具集锦



find 文件查找

  • 查找txt和pdf文件
    <code><span class="pln">  find </span><span class="pun">.</span><span class="pln"> \( </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.txt"</span> <span class="pun">-</span><span class="pln">o </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.pdf"</span><span class="pln"> \) </span><span class="pun">-</span><span class="kwd">print</span></code>
  • 正则方式查找.txt和pdf
    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">regex  </span><span class="str">".*\(\.txt|\.pdf\)$"</span></code>

    -iregex: 忽略大小写的正则

  • 否定参数

    <code><span class="pln">   find </span><span class="pun">.</span> <span class="pun">!</span> <span class="pun">-</span><span class="pln">name </span><span class="str">"*.txt"</span> <span class="pun">-</span><span class="kwd">print</span></code>
  • 指定搜索深度

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">maxdepth </span><span class="lit">1</span> <span class="pun">-</span><span class="pln">type f  </span></code>


  • 按类型搜索:
    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type d </span><span class="pun">-</span><span class="kwd">print</span>  <span class="com">//只列出所有目录</span></code>

    -type f 文件 / l 符号链接

  • 按时间搜索:
    -atime 访问时间 (单位是天,分钟单位则是-amin,以下类似)
    -mtime 修改时间 (内容被修改)
    -ctime 变化时间 (元数据或权限变化)

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">atime </span><span class="lit">7</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="kwd">print</span></code>
  • 按大小搜索:
    w字 k M G

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">size </span><span class="pun">+</span><span class="lit">2k</span></code>


    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">perm </span><span class="lit">644</span> <span class="pun">-</span><span class="kwd">print</span> <span class="com">//找具有可执行权限的所有文件</span></code>


    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">user weber </span><span class="pun">-</span><span class="kwd">print</span><span class="com">// 找用户weber所拥有的文件</span></code>


  • 删除:

    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.swp"</span> <span class="pun">-</span><span class="kwd">delete</span></code>
  • 执行动作(强大的exec)
    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">user root </span><span class="pun">-</span><span class="kwd">exec</span><span class="pln"> chown weber </span><span class="pun">{}</span><span class="pln"> \; </span><span class="com">//将当前目录下的所有权变更为weber</span></code>


    <code><span class="pln">  find </span><span class="pun">.</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">mtime </span><span class="pun">+</span><span class="lit">10</span> <span class="pun">-</span><span class="pln">name </span><span class="str">"*.txt"</span> <span class="pun">-</span><span class="kwd">exec</span><span class="pln"> cp </span><span class="pun">{}</span><span class="pln"> OLD \;</span></code>
  • 结合多个命令
    tips: 如果需要后续执行多个命令,可以将多个命令写成一个脚本。然后 -exec 调用时执行脚本即可;

    <code>  <span class="pun">-</span><span class="kwd">exec</span> <span class="pun">./</span><span class="pln">commands</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">{}</span><span class="pln"> \;</span></code>


-print0 使用’\0’作为文件的定界符,这样就可以搜索包含空格的文件;

grep 文本搜索

grep match_patten file // 默认访问匹配行

  • 常用参数
    -o 只输出匹配的文本行 VS -v 只输出没有匹配的文本行
    -c 统计文件中包含文本的次数

    <code><span class="pln">  grep </span><span class="pun">-</span><span class="pln">c </span><span class="str">"text"</span><span class="pln"> filename</span></code>

    -n 打印匹配的行号
    -i 搜索时忽略大小写
    -l 只打印文件名

  • 在多级目录中对文本递归搜索(程序员搜代码的最爱):
    <code><span class="pln">  grep </span><span class="str">"class"</span> <span class="pun">.</span> <span class="pun">-</span><span class="pln">R </span><span class="pun">-</span><span class="pln">n</span></code>
  • 匹配多个模式
    <code><span class="pln">  grep </span><span class="pun">-</span><span class="pln">e </span><span class="str">"class"</span> <span class="pun">-</span><span class="pln">e </span><span class="str">"vitural"</span><span class="pln"> file</span></code>
  • grep输出以\0作为结尾符的文件名:(-z)
    <code><span class="pln">  grep </span><span class="str">"test"</span><span class="pln"> file</span><span class="pun">*</span> <span class="pun">-</span><span class="pln">lZ</span><span class="pun">|</span><span class="pln"> xargs </span><span class="pun">-</span><span class="lit">0</span><span class="pln"> rm</span></code>

xargs 命令行参数转换

xargs 能够将输入数据转化为特定命令的命令行参数;这样,可以配合很多命令来组合使用。比如grep,比如find;

  • 将多行输出转化为单行输出
    cat file.txt| xargs
    \n 是多行文本间的定界符
  • 将单行转化为多行输出
    cat single.txt | xargs -n 3


-d 定义定界符 (默认为空格 多行的定界符为 \n)
-n 指定输出为多行
-I {} 指定替换字符串,这个字符串在xargs扩展时会被替换掉,用于待执行的命令需要多个参数时

<code><span class="pln">cat file</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> xargs </span><span class="pun">-</span><span class="pln">I </span><span class="pun">{}</span> <span class="pun">./</span><span class="pln">command</span><span class="pun">.</span><span class="pln">sh </span><span class="pun">-</span><span class="pln">p </span><span class="pun">{}</span> <span class="pun">-</span><span class="lit">1</span></code>


<code><span class="pln">find source_dir</span><span class="pun">/</span> <span class="pun">-</span><span class="pln">type f </span><span class="pun">-</span><span class="pln">name </span><span class="str">"*.cpp"</span> <span class="pun">-</span><span class="pln">print0 </span><span class="pun">|</span><span class="pln">xargs </span><span class="pun">-</span><span class="lit">0</span><span class="pln"> wc </span><span class="pun">-</span><span class="pln">l</span></code>

sort 排序

-n 按数字进行排序 VS -d 按字典序进行排序
-r 逆序排序
-k N 指定按第N列排序

<code><span class="pln">sort </span><span class="pun">-</span><span class="pln">nrk </span><span class="lit">1</span><span class="pln"> data</span><span class="pun">.</span><span class="pln">txt
sort </span><span class="pun">-</span><span class="pln">bd data </span><span class="com">// 忽略像空格之类的前导空白字符</span></code>

uniq 消除重复行

  • 消除重复行
    <code><span class="pln">  sort unsort</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> uniq </span></code>
  • 统计各行在文件中出现的次数
    <code><span class="pln">  sort unsort</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> uniq </span><span class="pun">-</span><span class="pln">c</span></code>
  • 找出重复行
    <code><span class="pln">  sort unsort</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span><span class="pln"> uniq </span><span class="pun">-</span><span class="pln">d</span></code>

    可指定每行中需要比较的重复内容:-s 开始位置 -w 比较字符数


  • 通用用法
    <code><span class="pln">  echo </span><span class="lit">12345</span> <span class="pun">|</span><span class="pln"> tr </span><span class="str">'0-9'</span> <span class="str">'9876543210'</span> <span class="com">//加解密转换,替换对应字符</span><span class="pln">
      cat text</span><span class="pun">|</span><span class="pln"> tr </span><span class="str">'\t'</span> <span class="str">' '</span>  <span class="com">//制表符转空格</span></code>
  • tr删除字符
    <code><span class="pln">  cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">d </span><span class="str">'0-9'</span> <span class="com">// 删除所有数字</span></code>

    -c 求补集

    <code><span class="pln">  cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">c </span><span class="str">'0-9'</span> <span class="com">//获取文件中所有数字</span><span class="pln">
      cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">d </span><span class="pun">-</span><span class="pln">c </span><span class="str">'0-9 \n'</span>  <span class="com">//删除非数字数据</span></code>
  • tr压缩字符
    tr -s 压缩文本中出现的重复字符;最常用于压缩多余的空格

    <code><span class="pln">  cat file </span><span class="pun">|</span><span class="pln"> tr </span><span class="pun">-</span><span class="pln">s </span><span class="str">' '</span></code>
  • 字符类
    使用方法:tr [:class:] [:class:]

    <code><span class="pln">  eg</span><span class="pun">:</span><span class="pln"> tr </span><span class="str">'[:lower:]'</span> <span class="str">'[:upper:]'</span></code>

cut 按列切分文本

  • 截取文件的第2列和第4列:
    <code><span class="pln">  cut </span><span class="pun">-</span><span class="pln">f2</span><span class="pun">,</span><span class="lit">4</span><span class="pln"> filename</span></code>
  • 去文件除第3列的所有列:
    <code><span class="pln">  cut </span><span class="pun">-</span><span class="pln">f3 </span><span class="pun">--</span><span class="pln">complement filename</span></code>
  • -d 指定定界符:
    <code><span class="pln">  cat </span><span class="pun">-</span><span class="pln">f2 </span><span class="pun">-</span><span class="pln">d</span><span class="str">";"</span><span class="pln"> filename</span></code>
  • cut 取的范围
    N- 第N个字段到结尾
    -M 第1个字段为M
    N-M N到M个字段
  • cut 取的单位
    -b 以字节为单位
    -c 以字符为单位
    -f 以字段为单位(使用定界符)
  • eg:
    <code><span class="pln">  cut </span><span class="pun">-</span><span class="pln">c1</span><span class="pun">-</span><span class="lit">5</span><span class="pln"> file </span><span class="com">//打印第一到5个字符</span><span class="pln">
      cut </span><span class="pun">-</span><span class="pln">c</span><span class="pun">-</span><span class="lit">2</span><span class="pln"> file  </span><span class="com">//打印前2个字符</span></code>

paste 按列拼接文本


<code><span class="pln">cat file1
</span><span class="lit">1</span>
<span class="lit">2</span><span class="pln">

cat file2

paste file1 file2
</span><span class="lit">1</span><span class="pln"> colin
</span><span class="lit">2</span><span class="pln"> book</span></code>

paste file1 file2 -d “,”

wc 统计行和字符的工具

wc -l file // 统计行数
wc -w file // 统计单词数
wc -c file // 统计字符数

sed 文本替换利器

  • 首处替换
    <code><span class="pln">  seg </span><span class="str">'s/text/replace_text/'</span><span class="pln"> file   </span><span class="com">//替换每一行的第一处匹配的text</span></code>
  • 全局替换
    <code><span class="pln">   seg </span><span class="str">'s/text/replace_text/g'</span><span class="pln"> file</span></code>


    <code><span class="pln">  seg </span><span class="pun">-</span><span class="pln">i </span><span class="str">'s/text/repalce_text/g'</span><span class="pln"> file</span></code>
  • 移除空白行:
    <code><span class="pln">  sed </span><span class="str">'/^$/d'</span><span class="pln"> file</span></code>
  • 变量转换

    <code><span class="pln">echo </span><span class="kwd">this</span> <span class="kwd">is</span><span class="pln"> en example </span><span class="pun">|</span><span class="pln"> seg </span><span class="str">'s/\w+/[&amp;]/g'</span><span class="pln">
    $</span><span class="pun">&gt;[</span><span class="kwd">this</span><span class="pun">]</span>  <span class="pun">[</span><span class="kwd">is</span><span class="pun">]</span> <span class="pun">[</span><span class="pln">en</span><span class="pun">]</span> <span class="pun">[</span><span class="pln">example</span><span class="pun">]</span></code>
  • 子串匹配标记
    第一个匹配的括号内容使用标记 \1 来引用

    <code><span class="pln">  sed </span><span class="str">'s/hello\([0-9]\)/\1/'</span></code>
  • 双引号求值

    <code><span class="pln">  sed </span><span class="str">'s/$var/HLLOE/'</span> </code>


    <code><span class="pln">eg</span><span class="pun">:</span><span class="pln">
    p</span><span class="pun">=</span><span class="pln">patten
    r</span><span class="pun">=</span><span class="pln">replaced
    echo </span><span class="str">"line con a patten"</span> <span class="pun">|</span><span class="pln"> sed </span><span class="str">"s/$p/$r/g"</span><span class="pln">
    $</span><span class="pun">&gt;</span><span class="pln">line con a replaced</span></code>
  • 其它示例
    字符串插入字符:将文本中每行内容(PEKSHA) 转换为 PEK/SHA

    <code><span class="pln">  sed </span><span class="str">'s/^.\{3\}/&amp;\//g'</span><span class="pln"> file</span></code>

awk 数据流处理工具

  • awk脚本结构
    awk ‘ BEGIN{ statements } statements2 END{ statements } ‘
  • 工作方式
  • 使用不带参数的print时,会打印当前行;
    <code><span class="pln">  echo </span><span class="pun">-</span><span class="pln">e </span><span class="str">"line1\nline2"</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'BEGIN{print "start"} {print } END{ print "End" }'</span> </code>
  • print 以逗号分割时,参数以空格定界;
    <code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">' {var1 = "v1" ; var2 = "V2"; var3="v3"; \
    print var1, var2 , var3; }'</span><span class="pln">
    $</span><span class="pun">&gt;</span><span class="pln">v1 V2 v3</span></code>
  • 使用-拼接符的方式(””作为拼接符);
    <code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">' {var1 = "v1" ; var2 = "V2"; var3="v3"; \
    print var1"-"var2"-"var3; }'</span><span class="pln">
    $</span><span class="pun">&gt;</span><span class="pln">v1</span><span class="pun">-</span><span class="pln">V2</span><span class="pun">-</span><span class="pln">v3</span></code>

特殊变量: NR NF $0 $1 $2


<code><span class="pln">echo </span><span class="pun">-</span><span class="pln">e </span><span class="str">"line1 f2 f3\n line2 \n line 3"</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print NR":"$0"-"$1"-"$2}'</span></code>
  • 打印每一行的第二和第三个字段:
    <code><span class="pln">  awk </span><span class="str">'{print $2, $3}'</span><span class="pln"> file</span></code>
  • 统计文件的行数:
    <code><span class="pln">  awk </span><span class="str">' END {print NR}'</span><span class="pln"> file</span></code>
  • 累加每一行的第一个字段:
    <code><span class="pln">  echo </span><span class="pun">-</span><span class="pln">e </span><span class="str">"1\n 2\n 3\n 4\n"</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'BEGIN{num = 0 ;
      print "begin";} {sum += $1;} END {print "=="; print sum }'</span></code>


<code><span class="kwd">var</span><span class="pun">=</span><span class="lit">1000</span><span class="pln">
echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print vara}'</span><span class="pln"> vara</span><span class="pun">=</span><span class="pln">$var </span><span class="com">#  输入来自stdin</span><span class="pln">
awk </span><span class="str">'{print vara}'</span><span class="pln"> vara</span><span class="pun">=</span><span class="pln">$var file </span><span class="com"># 输入来自文件</span></code>


awk ‘NR < 5’ #行号小于5
awk ‘NR==1,NR==4 {print}’ file #行号等于1和4的打印出来
awk ‘/linux/’ #包含linux文本的行(可以用正则表达式来指定,超级强大)
awk ‘!/linux/’ #不包含linux文本的行


awk -F: ‘{print $NF}’ /etc/passwd



<code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{"grep root /etc/passwd" | getline cmdout; print cmdout }'</span> </code>


for(i=0;i<10;i++){print $i;}
for(i in array){print array[i];}


<code><span class="pln">seq </span><span class="lit">9</span><span class="pun">|</span><span class="pln"> \
awk </span><span class="str">'{lifo[NR] = $0; lno=NR} \
END{ for(;lno&gt;-1;lno--){print lifo[lno];}
} '</span></code>


  • head:
    <code><span class="pln">  awk </span><span class="str">'NR&lt;=10{print}'</span><span class="pln"> filename</span></code>
  • tail:
    <code><span class="pln">  awk </span><span class="str">'{buffer[NR%10] = $0;} END{for(i=0;i&lt;11;i++){ \
      print buffer[i %10]} } '</span><span class="pln"> filename</span></code>


  • awk方式实现:
    <code><span class="pln">  ls </span><span class="pun">-</span><span class="pln">lrt </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{print $6}'</span></code>
  • cut方式实现
    <code><span class="pln">  ls </span><span class="pun">-</span><span class="pln">lrt </span><span class="pun">|</span><span class="pln"> cut </span><span class="pun">-</span><span class="pln">f6</span></code>


  • 确定行号
    <code><span class="pln">  seq </span><span class="lit">100</span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'NR==4,NR==6{print}'</span></code>
  • 确定文本
    打印处于start_pattern 和end_pattern之间的文本;

    <code><span class="pln">  awk </span><span class="str">'/start_pattern/, /end_pattern/'</span><span class="pln"> filename</span></code>


    <code><span class="pln">seq </span><span class="lit">100</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'/13/,/15/'</span><span class="pln">
    cat </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">passwd</span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'/mai.*mail/,/news.*news/'</span></code>



<code><span class="pln">echo </span><span class="pun">|</span><span class="pln"> awk </span><span class="str">'{"grep root /etc/passwd" | getline cmdout; print length(cmdout) }'</span> </code>

printf 类似c语言中的printf,对输出进行格式化

<code><span class="pln">seq </span><span class="lit">10</span> <span class="pun">|</span><span class="pln"> awk </span><span class="str">'{printf "-&gt;%4s\n", $1}'</span></code>


1. 迭代文件中的每一行

  • while 循环法
    <code><span class="kwd">while</span><span class="pln"> read line</span><span class="pun">;</span>
    <span class="kwd">do</span><span class="pln">
    echo $line</span><span class="pun">;</span>
    <span class="kwd">done</span> <span class="pun">&lt;</span><span class="pln"> file</span><span class="pun">.</span><span class="pln">txt
    </span><span class="pun">改成子</span><span class="pln">shell</span><span class="pun">:</span><span class="pln">
    cat file</span><span class="pun">.</span><span class="pln">txt </span><span class="pun">|</span> <span class="pun">(</span><span class="kwd">while</span><span class="pln"> read line</span><span class="pun">;</span><span class="kwd">do</span><span class="pln"> echo $line</span><span class="pun">;</span><span class="kwd">done</span><span class="pun">)</span></code>
  • awk法:
    cat file.txt| awk ‘{print}’


<code><span class="kwd">for</span><span class="pln"> word </span><span class="kwd">in</span><span class="pln"> $line</span><span class="pun">;</span>
<span class="kwd">do</span><span class="pln"> 
echo $word</span><span class="pun">;</span>
<span class="kwd">done</span></code>

3. 迭代每一个字符


<code><span class="kwd">for</span><span class="pun">((</span><span class="pln">i</span><span class="pun">=</span><span class="lit">0</span><span class="pun">;</span><span class="pln">i</span><span class="pun">&lt;</span><span class="pln">$</span><span class="pun">{#</span><span class="pln">word</span><span class="pun">};</span><span class="pln">i</span><span class="pun">++))</span>
<span class="kwd">do</span><span class="pln">
echo $</span><span class="pun">{</span><span class="pln">word</span><span class="pun">:</span><span class="pln">i</span><span class="pun">:</span><span class="lit">1</span><span class="pun">);</span>
<span class="kwd">done</span></code>





本文我们收集了对 Linux 用户非常有用的 10 个工具,其中包括网络监控、系统审计或其它有用命令,这 10 个 Linux工具可以帮助大家提高工作和使用效率,非常实用。

1. w

对,你没看错,就是 w 命令。使用该命令我们可以查看到当前登录系统的用户是谁,以及执行了哪些命令。


2. nmon

Nmon 是一个可以监控当前系统性能的小工具,使用之前需要先用如下命令进行安装:

  1. sudo aptget install nmon

安装好后执行 nmon 命令即可打开:

  1. nmon


nmon 可以查看网络、CPU、内存和磁盘的使用情况。

打开之后按 c 查看 CPU 信息:


打开之后按 n 查看网络信息:


3. ncdu

ncdu 命令可以用来查看和分析 Linux 中各目录对磁盘空间占用情况的工具,请使用如下命令进行安装:

  1. aptget install ncdu


  1. ncdu /


注意:执行上述命令会占用大量磁盘 I/O



我们可以在结果界面按 n 按名称进行排序或按 s 按大小进行排序。

4. slurm

slurm 是一个网卡带宽监控命令行实用程序,它会自动生成 ASCII 图形输出。使用之前先用如下命令进行安装:

  1. aptget install slurm


  1. slurm i <网卡名称>


slurm 界面中可以执行如下选项:

  • I:显示lx/tx状态
  • c:切换到经典界面
  • r:手动刷新界面
  • q:退出工具

5. findmnt

Findmnt 是一个 Linux 内置的命令行工具,它主要用于查找挂载的文件系统状态。Findmnt 可以查看到当前系统中已挂载的设备,在必要时还可进行 mount 或 unmount 操作。

执行 findmnt 命令后会看到如下输入:



  • findmnt -l :以列表方式进行输出
  • findmnt -s :输出 fstab 中挂载的设备
  • findmnt -t ext4 :按文件系统类型进行输出

6. dstat

dstat 是一个可以非常灵活使用和进行组合使用的工具,它可用于监控内存、进行、网络及磁盘性能,可用于替代  ifstat、iostat、dmstat等工具。使用之前需先执行如下命令进行安装:

  1. aptget install dstat


  1. dstat



  • dstat -c : 监控CPU
  • dstat -cdl -D sda1 :监控CPU详细信息
  • dstat -d :监控磁盘

7. saidar

saidar 是另一个 CLI 系统数据监控和统计工具,可提供有关磁盘、网络、存储和 SWAP 的监控信息。使用之前需先使用如下命令进行安装:

  1. sudo aptget install saidar

安装完成后可直接执行 saidar 进行输出,但我们通常使用带参数的命令生成带颜色输出:

  1. saidar c


8. ss

ss 全称 socket statistics,是一个可以替代 netstat 的网络连接查看工具。

直接执行 ss 即可进行查看:



ss -A tcp :指定查看协议

ss -ltp :显示进程名称和 PID

9. ccze

ccze 非常有用,它可以用不同颜色高亮日志,协助管理员进行区分和查看分析。使用之前需先使用如下命令进行安装:

  1. aptget install ccze


  1. tailf /var/log/syslog | ccze


而使用 ccze -l 参数可以查看其支持的日志类型。



我们最后介绍的 是一个 python 工具,它可以以图形方式显示系统活动。

要使用该工具需要先安装 python 语言支持:

  1. sudo aptaddrepository ppa:fkrull/deadsnakes
  2. sudo aptget update
  3. sudo aptget install python3.2


  1. wget
  2. unzip && cd ranwhenmaster


  1. python3.2



本文介绍的 10 大好用 Linux 实用工具都还比较有意思,希望大家能喜欢。