diff --git a/sources/tech/20150504 Useful Commands to Create Commandline Chat Server and Remove Unwanted Packages in Linux.md b/sources/tech/20150504 Useful Commands to Create Commandline Chat Server and Remove Unwanted Packages in Linux.md deleted file mode 100644 index cbcd668b14..0000000000 --- a/sources/tech/20150504 Useful Commands to Create Commandline Chat Server and Remove Unwanted Packages in Linux.md +++ /dev/null @@ -1,184 +0,0 @@ -translating wi-cuckoo -Useful Commands to Create Commandline Chat Server and Remove Unwanted Packages in Linux -================================================================================ -Here we are with the next part of Linux Command Line Tips and Tricks. If you missed our previous post on Linux Tricks you may find it here. - -- [5 Linux Command Line Tricks][1] - -In this post we will be introducing 6 command Line tips namely create Linux Command line chat using Netcat command, perform addition of a column on the fly from the output of a command, remove orphan packages from Debian and CentOS, get local and remote IP from command Line, get colored output in terminal and decode various color code and last but not the least hash tags implementation in Linux command Line. Lets check them one by one. - -![Linux Commandline Chat Server](http://www.tecmint.com/wp-content/uploads/2015/04/linux-commandline-chat-server.jpg) -6 Useful Commandline Tricks and Tips - -### 1. Create Linux Commandline Chat Server ### - -We all have been using chat service since a long time. We are familiar with Google chat, Hangout, Facebook chat, Whatsapp, Hike and several other application and integrated chat services. Do you know Linux nc command can make your Linux box a chat server with just one line of command. -What is nc command in Linux and what it does? - -nc is the depreciation of Linux netcat command. The nc utility is often referred as Swiss army knife based upon the number of its built-in capabilities. It is used as debugging tool, investigation tool, reading and writing to network connection using TCP/UDP, DNS forward/reverse checking. - -It is prominently used for port scanning, file transferring, backdoor and port listening. nc has the ability to use any local unused port and any local network source address. - -Use nc command (On Server with IP address: 192.168.0.7) to create a command line messaging server instantly. - - $ nc -l -vv -p 11119 - -Explanation of the above command switches. - -- -v : means Verbose -- -vv : more verbose -- -p : The local port Number - -You may replace 11119 with any other local port number. - -Next on the client machine (IP address: 192.168.0.15) run the following command to initialize chat session to machine (where messaging server is running). - - $ nc 192.168.0.7 11119 - -![Linux Commandline Chat with nc Command](http://www.tecmint.com/wp-content/uploads/2015/04/Chat-on-Linux-Commandline.gif) - -**Note**: You can terminate chat session by hitting ctrl+c key and also nc chat is one-to-one service. - -### 2. How to Sum Values in a Column in Linux ### - -How to sum the numerical values of a column, generated as an output of a command, on the fly in the terminal. - -The output of the ‘ls -l‘ command. - - $ ls -l - -![Sum Numerical Values](http://www.tecmint.com/wp-content/uploads/2015/04/Sum-Values.gif) - -Notice that the second column is numerical which represents number of symbolic links and the 5th column is numerical which represents the size of he file. Say we need to sum the values of fifth column on the fly. - -List the content of 5th column without printing anything else. We will be using ‘awk‘ command to do this. ‘$5‘ represents 5th column. - - $ ls -l | awk '{print $5}' - -![List Content Column](http://www.tecmint.com/wp-content/uploads/2015/04/List-Content-Column.gif) - -Now use awk to print the sum of the output of 5th column by pipelining it. - - $ ls -l | awk '{print $5}' | awk '{total = total + $1}END{print total}' - -![Sum and Print Columns](http://www.tecmint.com/wp-content/uploads/2015/04/Sum-Columns.gif) - -### How to Remove Orphan Packages in Linux? ### - -Orphan packages are those packages that are installed as a dependency of another package and no longer required when the original package is removed. - -Say we installed a package gtprogram which was dependent of gtdependency. We can’t install gtprogram unless gtdependency is installed. - -When we remove gtprogram it won’t remove gtdependency by default. And if we don’t remove gtdependency, it will remain as Orpahn Package with no connection to any other package. - - # yum autoremove [On RedHat Systems] - -![Remove Orphan Packages in CentOS](http://www.tecmint.com/wp-content/uploads/2015/04/Remove-Orphan-Packages-in-CentOS1.gif) - - # apt-get autoremove [On Debian Systems] - -![Remove Orphan Packages in Debian](http://www.tecmint.com/wp-content/uploads/2015/04/Remove-Orphan-Packages-in-Debian.gif) - -You should always remove Orphan Packages to keep the Linux box loaded with just necessary stuff and nothing else. - -### 4. How to Get Local and Public IP Address of Linux Server ### - -To get you local IP address run the below one liner script. - - $ ifconfig | grep "inet addr:" | awk '{print $2}' | grep -v '127.0.0.1' | cut -f2 -d: - -You must have installed ifconfig, if not, apt or yum the required packages. Here we will be pipelining the output of ifconfig with grep command to find the string “intel addr:”. - -We know ifconfig command is sufficient to output local IP Address. But ifconfig generate lots of other outputs and our concern here is to generate only local IP address and nothing else. - - # ifconfig | grep "inet addr:" - -![Check Local IP Address](http://www.tecmint.com/wp-content/uploads/2015/04/Check-Local-IP-Address.gif) - -Although the output is more custom now, but we need to filter our local IP address only and nothing else. For this we will use awk to print the second column only by pipelining it with the above script. - - # ifconfig | grep “inet addr:” | awk '{print $2}' - -![Filter Only IP Address](http://www.tecmint.com/wp-content/uploads/2015/04/Filter-IP-Address.gif) - -Clear from the above image that we have customised the output very much but still not what we want. The loopback address 127.0.0.1 is still there in the result. - -We use use -v flag with grep that will print only those lines that don’t match the one provided in argument. Every machine have the same loopback address 127.0.0.1, so use grep -v to print those lines that don’t have this string, by pipelining it with above output. - - # ifconfig | grep "inet addr" | awk '{print $2}' | grep -v '127.0.0.1' - -![Print IP Address](http://www.tecmint.com/wp-content/uploads/2015/04/Print-IP-Address.gif) - -We have almost generated desired output, just replace the string `(addr:)` from the beginning. We will use cut command to print only column two. The column 1 and column 2 are not separated by tab but by `(:)`, so we need to use delimiter `(-d)` by pipelining the above output. - - # ifconfig | grep "inet addr:" | awk '{print $2}' | grep -v '127.0.0.1' | cut -f2 -d: - -![Customized IP Address](http://www.tecmint.com/wp-content/uploads/2015/04/Custome-IP-Address.gif) - -Finally! The desired result has been generated. - -### 5. How to Color Linux Terminal ### - -You might have seen colored output in terminal. Also you would be knowing to enable/disable colored output in terminal. If not you may follow the below steps. - -In Linux every user has `'.bashrc'` file, this file is used to handle your terminal output. Open and edit this file with your choice of editor. Note that, this file is hidden (dot beginning of file means hidden). - - $ vi /home/$USER/.bashrc - -Make sure that the following lines below are uncommented. ie., it don’t start with a #. - - if [ -x /usr/bin/dircolors ]; then - test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dirc$ - alias ls='ls --color=auto' - #alias dir='dir --color=auto' - #alias vdir='vdir --color=auto' - - alias grep='grep --color=auto' - alias fgrep='fgrep --color=auto' - alias egrep='egrep --color=auto' - fi - -![User .bashrc File](http://www.tecmint.com/wp-content/uploads/2015/04/bashrc-file.gif) - -Once done! Save and exit. To make the changes taken into effect logout and again login. - -Now you will see files and folders are listed in various colors based upon type of file. To decode the color code run the below command. - - $ dircolors -p - -Since the output is too long, lets pipeline the output with less command so that we get output one screen at a time. - - $ dircolors -p | less - -![Linux Color Output](http://www.tecmint.com/wp-content/uploads/2015/04/Linux-Color-Output.gif) - -### 6. How to Hash Tag Linux Commands and Scripts ### - -We are using hash tags on Twitter, Facebook and Google Plus (may be some other places, I have not noticed). These hash tags make it easier for others to search for a hash tag. Very few know that we can use hash tag in Linux command Line. - -We already know that `#` in configuration files and most of the programming languages is treated as comment line and is excluded from execution. - -Run a command and then create a hash tag of the command so that we can find it later. Say we have a long script that was executed in point 4 above. Now create a hash tag for this. We know ifconfig can be run by sudo or root user hence acting as root. - - # ifconfig | grep "inet addr:" | awk '{print $2}' | grep -v '127.0.0.1' | cut -f2 -d: #myip - -The script above has been hash tagged with ‘myip‘. Now search for the hash tag in reverse-i-serach (press ctrl+r), in the terminal and type ‘myip‘. You may execute it from there, as well. - -![Create Command Hash Tags](http://www.tecmint.com/wp-content/uploads/2015/04/Create-Command-Hash-Tags.gif) - -You may create as many hash tags for every command and find it later using reverse-i-search. - -That’s all for now. We have been working hard to produce interesting and knowledgeable contents for you. What do you think how we are doing? Any suggestion is welcome. You may comment in the box below. Keep connected! Kudos. - --------------------------------------------------------------------------------- - -via: http://www.tecmint.com/linux-commandline-chat-server-and-remove-unwanted-packages/ - -作者:[Avishek Kumar][a] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出 - -[a]:http://www.tecmint.com/author/avishek/ -[1]:http://www.tecmint.com/5-linux-command-line-tricks/ diff --git a/translated/tech/20150504 Useful Commands to Create Commandline Chat Server and Remove Unwanted Packages in Linux.md b/translated/tech/20150504 Useful Commands to Create Commandline Chat Server and Remove Unwanted Packages in Linux.md new file mode 100644 index 0000000000..cd90e2bfb7 --- /dev/null +++ b/translated/tech/20150504 Useful Commands to Create Commandline Chat Server and Remove Unwanted Packages in Linux.md @@ -0,0 +1,177 @@ +Linux中,创建聊天服务器、移除冗余软件包的实用命令 +============================================================================= +这里,我们来看Linux命令行实用技巧的下一个部分。如果你错过了Linux Tracks之前的文章,可以从这里找到。 + +- [5 Linux Command Line Tracks][1] + +本篇中,我们将会介绍6个命令行小技巧,包括使用Netcat命令创建Linux命令行聊天,从某个命令的输出中对某一列做加法,移除Debian和CentOS上多余的包,从命令行中获取本地与远程的IP地址,在终端获得彩色的输出与解码各样的颜色,最后是Linux命令行里井号标签的使用。让我们来一个一个地看一下。 + +![Linux Commandline Chat Server](http://www.tecmint.com/wp-content/uploads/2015/04/linux-commandline-chat-server.jpg) +6个实用的命令行技巧 + +### 1. 创建Linux命令行聊天服务 ### +我们大家使用聊天服务都有很长一段时间了。对于Google Chat,Hangout,Facebook Chat,Whatsapp,Hike和其他一些应用与集成的聊天服务,我们都很熟悉了。那你知道Linux的nc命令可以使你的Linux盒子变成一个聊天服务器,而仅仅只需要一行命令吗。什么是nc命令,它又是怎么工作的呢? + +nc是Linux netcat命令的旧版。nc就像瑞士军刀一样,内建呢大量的功能。nc可用做调式工具,调查工具,使用TCP/UDP读写网络连接,DNS正向/反向检查。 + +nc主要用在端口扫描,文件传输,后台和端口监听。nc可以使用任何闲置的端口和任何本地网络源地址。 + +使用nc命令(在192.168.0.7的服务器上)创建一个命令行即时信息传输服务器。 + + $ nc -l -vv 11119 + +对上述命令的解释。 + +- -v : 表示 Verbose +- -vv : 更多的 Verbose +- -p : 本地端口号 + +你可以用任何其他的本地端口号替换11119。 + +接下来在客户端机器(IP地址:192.168.0.15),运行下面的命令初始化聊天会话(信息传输服务正在运行)。 + + $ nc 192.168.0.7:11119 + +![Linux Commandline Chat with nc Command](http://www.tecmint.com/wp-content/uploads/2015/04/Chat-on-Linux-Commandline.gif) + +**注意**:你可以按下ctrl+c终止会话,同时nc聊天是一个一对一的服务。 + +### 2. Linux中如何统计某一列的总值 ### + +如何统计在终端里,某个命令的输出中,其中一列的数值总和, + +‘ls -l’命令的输出。 + + $ ls -l + +![Sum Numerical Values](http://www.tecmint.com/wp-content/uploads/2015/04/Sum-Values.gif) + +注意到第二列代表软连接的数量,第五列则是文件的大小。假设我们需要汇总第五列的数值。 + +仅仅列出第五列的内容。我们会使用‘awk’命令做到这点。‘$5’即代表第五列。 + + $ ls -l | awk '{print $5}' + +![List Content Column](http://www.tecmint.com/wp-content/uploads/2015/04/List-Content-Column.gif) + +现在,通过管道连接,使用awk打印出第五列数值的总和。 + + $ ls -l | awk '{print $5}' | awk '{total = total + $1}END{print total}' + +![Sum and Print Columns](http://www.tecmint.com/wp-content/uploads/2015/04/Sum-Columns.gif) + +### 在Linux里如何移除废弃包 ### + +废弃包是指那些作为其他包的依赖而被安装,但是当源包被移除之后就不再需要的包。 + +假设我们安装了gtprogram,依赖是gtdependency。除非我们安装了gtdependency,否则安装不了gtprogram。 + +当我们移除gtprogram的时候,默认并不会移除gtdependency。并且如果我们不移除gtdependency的话,它就会遗留下来成为废弃包,与其他任何包再无联系。 + + # yum autoremove [On RedHat Systems] + +![Remove Orphan Packages in CentOS](http://www.tecmint.com/wp-content/uploads/2015/04/Remove-Orphan-Packages-in-CentOS1.gif) + + # apt-get autoremove [On Debian Systems] + +![Remove Orphan Packages in Debian](http://www.tecmint.com/wp-content/uploads/2015/04/Remove-Orphan-Packages-in-Debian.gif) + +你应该经常移除废弃包,保持Linux机器仅仅加载一些需要的东西。 + +### 4. 如何获得Linux服务器本地的与公网的IP地址 ### + +为了获得本地IP地址,运行下面的一行脚本。 + + $ ifconfig | grep "inet addr:" | awk '{print $2}' | grep -v '127.0.0.1' | cut -f2 -d: + +你必须安装了ifconfig,如果没有,使用apt或者yum工具安装需要的包。这里我们将会管道连接ifconfig的输出,并且结合grep命令找到包含“intel addr:”的字符串。 + +我们知道对于输出本地IP地址,ifconfig命令足够用了。但是ifconfig生成了许多的输出,而我们关注的地方仅仅是本地IP地址,不是其他的。 + + # ifconfig | grep "inet addr:" + +![Check Local IP Address](http://www.tecmint.com/wp-content/uploads/2015/04/Check-Local-IP-Address.gif) + +尽管目前的输出好多了,但是我们需要过滤出本地的IP地址,不含其他东西。针对这个,我们将会使用awk打印出第二列输出,通过管道连接上述的脚本。 + + # ifconfig | grep “inet addr:” | awk '{print $2}' + +![Filter Only IP Address](http://www.tecmint.com/wp-content/uploads/2015/04/Filter-IP-Address.gif) + +上面图片清楚的表示,我们已经很大程度上自定义了输出,当仍然不是我们想要的。本地环路地址 127.0.0.1 仍然在结果中。 + +我们可以使用grep的-v选项,这样会打印出不匹配给定参数的其他行。每个机器都有同样的环路地址 127.0.0.1,所以使用grep -v打印出不包含127.0.0.1的行,通过管道连接前面的脚本。 + + # ifconfig | grep "inet addr" | awk '{print $2}' | grep -v '127.0.0.1' + +![Print IP Address](http://www.tecmint.com/wp-content/uploads/2015/04/Print-IP-Address.gif) + +我们差不多得到想要的输出了,仅仅需要从开头替换掉字符串`(addr:)`。我们将会使用cut命令单独打印出第二列。一二列之间并不是用tab分割,而是`(:)`,所以我们需要使用到域分割符选项`(-d)`,通过管道连接上面的输出。 + + # ifconfig | grep "inet addr:" | awk '{print $2}' | grep -v '127.0.0.1' | cut -f2 -d: + +![Customized IP Address](http://www.tecmint.com/wp-content/uploads/2015/04/Custome-IP-Address.gif) + +最后!期望的结果出来了。 + +### 5.如何在Linux终端彩色输出 ### + +你可能在终端看见过彩色的输出。同时你也可能知道在终端里允许/禁用彩色输出。如果都不知道的话,里可以参考下面的步骤。 + +在Linux中,每个用户都有`'.bashrc'`文件,被用来管理你的终端输出。打开并且编辑该文件,用你喜欢的编辑器。注意一下,这个文件是隐藏的(文件开头为点的代表隐藏文件)。 + + $ vi /home/$USER/.bashrc + +确保以下的行没有被注释掉。ie.,行开头没有#。 + + if [ -x /usr/bin/dircolors ]; then + test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dirc$ + alias ls='ls --color=auto' + #alias dir='dir --color=auto' + #alias vdir='vdir --color=auto' + + alias grep='grep --color=auto' + alias fgrep='fgrep --color=auto' + alias egrep='egrep --color=auto' + fi + +![User .bashrc File](http://www.tecmint.com/wp-content/uploads/2015/04/bashrc-file.gif) + +完成后!保存并退出。为了让改动生效,需要注销账户后再次登录。 + +现在,你会看见列出的文件和文件夹名字有着不同的颜色,根据文件类型来决定。为了解码颜色,可以运行下面的命令。 + + $ dircolors -p | less + +![Linux Color Output](http://www.tecmint.com/wp-content/uploads/2015/04/Linux-Color-Output.gif) + +### 6.如何用井号标记和Linux命令和脚本 ### + +我们一直在Twitter,Facebook和Google Plus(可能是其他我们没有提到的地方)上使用井号标签。那些井号标签使得其他人搜索一个标签更加容易。可是很少人知道,我们可以在Linux命令行使用井号标签。 + +我们已经知道配置文件里的`#`,在大多数的编程语言中,这个符号被用作注释行,即不被执行。 + +运行一个命令,然后为这个命令创建一个井号标签,这样之后我们就可以找到它。假设我们有一个很长的脚本,就上面第四点被执行的命令。现在为它创建一个井号标签。我们知道ifconfig可以被sudo或者root执行,因此用root来执行。 + + # ifconfig | grep "inet addr:" | awk '{print $2}' | grep -v '127.0.0.1' | cut -f2 -d: #myip + +上述脚本被’mytag‘给标记了。现在在reverse-i-search(按下ctrl+r)搜索一下这个标签,在终端里,并输入’mytag‘。你可以从这里开始执行。 + +![Create Command Hash Tags](http://www.tecmint.com/wp-content/uploads/2015/04/Create-Command-Hash-Tags.gif) + +你可以创建很多的井号标签,为每个命令,之后使用reverse-i-search找到它。 + +目前就这么多了。我们一直在辛苦的工作,创造有趣的,有知识性的内容给你。你觉得我们是如何工作的呢?欢迎咨询任何问题。你可以在下面评论。保持联络!Kudox。 + +-------------------------------------------------------------------------------- + +via: http://www.tecmint.com/linux-commandline-chat-server-and-remove-unwanted-packages/ + +作者:[Avishek Kumar][a] +译者:[wi-cuckoo](https://github.com/wi-cuckoo) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出 + +[a]:http://www.tecmint.com/author/avishek/ +[1]:http://www.tecmint.com/5-linux-command-line-tricks/