Category Archives: Tips & Tricks

4 tools to download any file using the command line in Linux

Learn how to download any file using the command line from the internet or FTP servers to your Linux server. Get files in your server in seconds!

How to download any file using command line

There are many times when you want a file on your Linux server from the Internet or FTP server and you are on command line terminal! When using the GUI of Linux, it’s easy to get files by using browsers but for command-line, it’s a little bit difficult.

We have 4 tools here to help you with the task! They are :

  1. wget
  2. curl
  3. elinks
  4. w3m

wget

Most popular utility! wget is a package you can install and use it right out of the box. You can install it with YUM or APT package. Once installed you can use it with supplying URL of the targeted download.

# wget https://kerneltalks.com/image.png
--2017-03-05 06:56:54--  https://kerneltalks.com/image.png
Resolving kerneltalks.com... 208.91.198.91
Connecting to kerneltalks.com|208.91.198.91|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12477 (12K) [image/png]
Saving to: “image.png”

100%[===================================================================================================================>] 12,477      --.-K/s   in 0s

2017-03-05 06:56:55 (782 MB/s) - “image.png” saved [12477/12477]

In the above example, we have downloaded one picture file from the internet! The file will be saved in your present working directory by default.

# wget ftp://rpmfind.net/linux/fedora/linux/development/rawhide/Everything/x86_64/os/Packages/s/systemd-233-2.fc27.x86_64.rpm
--2017-03-05 06:58:54--  ftp://rpmfind.net/linux/fedora/linux/development/rawhide/Everything/x86_64/os/Packages/s/systemd-233-2.fc27.x86_64.rpm
           => “systemd-233-2.fc27.x86_64.rpm.1”
Resolving rpmfind.net... 195.220.108.108
Connecting to rpmfind.net|195.220.108.108|:21... connected.
Logging in as anonymous ... Logged in!
==> SYST ... done.    ==> PWD ... done.
==> TYPE I ... done.  ==> CWD (1) /linux/fedora/linux/development/rawhide/Everything/x86_64/os/Packages/s ... done.
==> SIZE systemd-233-2.fc27.x86_64.rpm ... 3179496
==> PASV ... done.    ==> RETR systemd-233-2.fc27.x86_64.rpm ... done.
Length: 3179496 (3.0M) (unauthoritative)

100%[===================================================================================================================>] 3,179,496   1.85M/s   in 1.6s

2017-03-05 06:58:57 (1.85 MB/s) - “systemd-233-2.fc27.x86_64.rpm.1” saved [3179496]

In this example, we used wget to download file from the FTP server. It used anonymous login to get into the server and download the file!

There are several options which you can use according to your requirement. The listing below a few important ones.

  • -b: send copy progress in the background
  • -c: continue download (broken or paused download resume)
  • -r: recursive (download all files in destination)
  • -A file extension: download only files with the specified extension

curl

Curl is a simple downloader that supports many protocols for file transfer few being FTP, HTTP, HTTPS, telnet, etc. It can be installed using the same above method yum install curl or apt-get install curl.

Curl renders file downloaded to its best-known way. Like if you try to download HTML URL then it will render it and shows you HTML code on terminal :

# curl https://kerneltalks.com
<!DOCTYPE html><html lang="en-US" prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb#"><head ><meta charset="UTF-8" /><title>Kernel Talks - Unix, Linux & scripts.</title><meta name="viewport" content="width=device-width, initial-scale=1" /><meta name="google-site-verification" content="jeFc7PXM8ZxDY5awb8nCCD5-bYwj5S7bwsAIgp1JIgU" /><meta name="msvalidate.01" content="920806CD9A79B08EC8477C0D440658A4" /><meta name="p:domain_verify" content="738d0b16e329ab01cc894a68d2adda34" /><meta name="yandex-verification" content="bd079834c4df4ebf" />
------output clipped-----

See the above example where it shows the HTML code of URL. Same way if you get text file it will show you text file content on the terminal.

To only download the file without trying to read/open it on terminal use option -o with curl.

# curl -O  ftp://rpmfind.net/linux/fedora/linux/development/rawhide/Everything/x86_64/os/Packages/s/systemd-233-2.fc27.x86_64.rpm
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 3104k  100 3104k    0     0   361k      0  0:00:08  0:00:08 --:--:--  618k

It will download file and progress will be shown on terminal in real time.

elinks

elinks is a text-based browser that supports colors, rendering, tabbed menus, etc. Mostly it’s preloaded with Installations but if not you can install it using yum or apt-getLet try to download website using elinks https://kerneltalks.com :

elinks renders URL

Above example shows elinks renders website in text mode (kind of) on terminal!

If you try to download image (or any type of) file it will show you below the GUI screen (within the terminal) with options to choose from what to do next. If you choose to save then it will download a file and keep it.

w3m

The last tool of this article to download internet-based files is w3m. w3m is a text-based www (world wide web) client. Installation steps remain same yum/apt-get install w3m

It also opens up a text-mode GUI screen like elinks and gives you interactive options to choose actions. w3m ftp://rpmf...../...86_64.rpm opens :

w3m menu

If you right-click on the terminal window (normally we don’t!!) it does show you a menu you can use to perform various actions.

You can navigate through this menu using keyboard arrow keys or even using mouse clicks. You can even use short cut keys defined for each menu item in brackets beside them.

All user interactive commands/options are shown in the lower-left corner of the terminal and choices can be submitted there only.

In all, if you are looking for a simple tool, less eye-rolling on-screen, a fast way to get the file on the server then wget is the choice you should make IMHO! Let me know which command-line tool you use for downloading internet files in comments.

How to list open ports on Linux/Unix server

Learn to list all open ports on Linux or Unix system. Also, view associated processes with them. A handy tip to troubleshoot service connectivity issues.

Many times in the life of sysadmin, you need to check which all ports open on your system. Sometimes you need to check if a particular port is listening on the server or not. If a particular service is communicating on a configured port or not. If a particular port has established connection or not.

All these things can be analysed with below commands.

netstat command :

Obviously first command is none other than netstat command. Use netstat with 4 options :

  • -a : Shows all sockets
  • -p : Show related PID
  • -t : TCP
  • -u : UDP
# netstat -ptau
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 *:58573                     *:*                         LISTEN      1013/rpc.statd
tcp        0      0 *:sunrpc                    *:*                         LISTEN      991/rpcbind
tcp        0      0 *:ssh                       *:*                         LISTEN      1208/sshd
tcp        0      0 localhost:ipp               *:*                         LISTEN      1069/cupsd
tcp        0      0 localhost:smtp              *:*                         LISTEN      1287/master
tcp        0      0 ip-12-31-28-246.ap-:telnet 112.197.214.169:49648       ESTABLISHED 3213/in.telnetd
tcp        0      0 ip-12-31-28-246.ap-:telnet 200-163-187-49.scrce2:53440 ESTABLISHED 3215/in.telnetd
tcp        0      0 ip-12-31-28-246.ap-sou:ssh 59.182.17:49413             ESTABLISHED 1441/sshd
tcp        0    288 ip-12-31-28-246.ap-sou:ssh 59.182.17:50729             ESTABLISHED 1694/sshd
tcp        0      0 *:sunrpc                    *:*                         LISTEN      991/rpcbind
tcp        0      0 *:ssh                       *:*                         LISTEN      1208/sshd
tcp        0      0 *:telnet                    *:*                         LISTEN      1618/xinetd
tcp        0      0 localhost:ipp               *:*                         LISTEN      1069/cupsd
tcp        0      0 localhost:smtp              *:*                         LISTEN      1287/master
tcp        0      0 *:56954                     *:*                         LISTEN      1013/rpc.statd
udp        0      0 localhost:766               *:*                                     1013/rpc.statd
udp        0      0 *:39730                     *:*                                     1013/rpc.statd
udp        0      0 *:bootpc                    *:*                                     884/dhclient
udp        0      0 *:netrcs                    *:*                                     991/rpcbind
udp        0      0 *:sunrpc                    *:*                                     991/rpcbind
udp        0      0 *:ipp                       *:*                                     1069/cupsd
udp        0      0 *:60991                     *:*                                     1013/rpc.statd
udp        0      0 *:netrcs                    *:*                                     991/rpcbind
udp        0      0 *:sunrpc                    *:*                                     991/rpcbind

In above output you can see :

  • The first column is a protocol
  • Fourth column local address includes local IP, port, service
  • Fifth column destination IP, port, etc
  • The sixth column is the current state
  • The last column is PID and process name which owns that socket

Another way is to use netstat with an option:

# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address               Foreign Address             State
tcp        0      0 0.0.0.0:8001                0.0.0.0:*                   LISTEN
tcp        0      0 127.0.0.1:9633              0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:7937                0.0.0.0:*                   LISTEN
tcp        0      0 127.0.0.1:9634              0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:7938                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:9443                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:9444                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:9060                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:9061                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:14502               0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:14503               0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:8359                0.0.0.0:*                   LISTEN
tcp        0      0 127.0.0.1:199               0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:810                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:9100                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:9101                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:9133                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:8080                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:8880                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:6000                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:8881                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:9043                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:9044                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:9080                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:9081                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:2809                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:2810                0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:443                 0.0.0.0:*                   LISTEN
tcp        0      0 0.0.0.0:6556                0.0.0.0:*                   LISTEN
tcp        0      0 10.10.5.160:42124         10.10.5.199:35994         ESTABLISHED
tcp        0      0 10.10.5.160:42376         10.10.5.199:39154         ESTABLISHED
tcp        0      0 10.10.5.160:8881          10.10.5.160:55117         TIME_WAIT
tcp        0      0 10.10.5.160:44367         10.10.5.199:48181         ESTABLISHED
tcp        0      0 10.10.5.160:36671         10.10.5.199:58137         ESTABLISHED
tcp        1      0 10.10.5.160:56253         10.10.5.160:9081          CLOSE_WAIT
tcp        0      0 10.10.5.160:57168         10.10.5.52:1521           ESTABLISHED
tcp        0      0 10.10.5.160:8880          10.10.5.160:55035         TIME_WAIT
tcp        0      0 10.10.5.160:6556          10.100.22.173:51544         ESTABLISHED
udp        0      0 0.0.0.0:7938                0.0.0.0:*
udp        0      0 0.0.0.0:37909               0.0.0.0:*
udp        0      0 0.0.0.0:161                 0.0.0.0:*
udp        0      0 0.0.0.0:804                 0.0.0.0:*
udp        0      0 0.0.0.0:807                 0.0.0.0:*
udp        0      0 0.0.0.0:177                 0.0.0.0:*
udp        0      0 0.0.0.0:5353                0.0.0.0:*
udp        0      0 0.0.0.0:111                 0.0.0.0:*
udp        0      0 203.127.98.134:123          0.0.0.0:*
udp        0      0 10.10.5.160:123           0.0.0.0:*
udp        0      0 127.0.0.1:123               0.0.0.0:*
udp        0      0 0.0.0.0:123                 0.0.0.0:*
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node Path
unix  2      [ ACC ]     STREAM     LISTENING     7690   @ISCSIADM_ABSTRACT_NAMESPACE
unix  2      [ ACC ]     STREAM     LISTENING     11363  @/tmp/fam-root-
unix  2      [ ACC ]     STREAM     LISTENING     11003  /var/run/avahi-daemon/socket
unix  2      [ ACC ]     STREAM     LISTENING     8701   @/var/run/hald/dbus-RL64SjEdUd
unix  2      [ ACC ]     STREAM     LISTENING     247409367 /jas/HTTPServer/logs/cgisock.14073
unix  2      [ ACC ]     STREAM     LISTENING     247409370 /jas/HTTPServer/logs/siddport
unix  2      [ ACC ]     STREAM     LISTENING     11221  /tmp/.gdm_socket
unix  14     [ ]         DGRAM                    8008   /dev/log
unix  2      [ ACC ]     STREAM     LISTENING     7668   @ISCSID_UIP_ABSTRACT_NAMESPACE
unix  2      [ ]         DGRAM                    1797   @/org/kernel/udev/udevd
unix  2      [ ]         DGRAM                    8709   @/org/freedesktop/hal/udev_event
unix  2      [ ACC ]     STREAM     LISTENING     8700   @/var/run/hald/dbus-mO28j2Fpoe
unix  2      [ ACC ]     STREAM     LISTENING     7963   /var/run/audispd_events
unix  2      [ ACC ]     STREAM     LISTENING     10761  /tmp/.font-unix/fs7100
unix  2      [ ACC ]     STREAM     LISTENING     10709  /dev/gpmctl
unix  2      [ ACC ]     STREAM     LISTENING     8608   /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     8675   /var/run/acpid.socket
unix  2      [ ACC ]     STREAM     LISTENING     11248  /tmp/.X11-unix/X0
unix  2      [ ]         DGRAM                    322339652
unix  2      [ ]         STREAM     CONNECTED     284330078
unix  2      [ ]         STREAM     CONNECTED     83187468
unix  2      [ ]         DGRAM                    11482214
unix  2      [ ]         STREAM     CONNECTED     98040
unix  3      [ ]         STREAM     CONNECTED     11366  @/tmp/fam-root-
unix  3      [ ]         STREAM     CONNECTED     11365
unix  3      [ ]         STREAM     CONNECTED     11356  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     11355
unix  3      [ ]         STREAM     CONNECTED     11333  /tmp/.X11-unix/X0
unix  3      [ ]         STREAM     CONNECTED     11332
unix  3      [ ]         STREAM     CONNECTED     11306  /tmp/.X11-unix/X0
unix  3      [ ]         STREAM     CONNECTED     11305
unix  3      [ ]         STREAM     CONNECTED     11283  /tmp/.font-unix/fs7100
unix  3      [ ]         STREAM     CONNECTED     11282
unix  2      [ ]         DGRAM                    11254
unix  3      [ ]         STREAM     CONNECTED     11286  /tmp/.X11-unix/X0
unix  3      [ ]         STREAM     CONNECTED     11253
unix  3      [ ]         STREAM     CONNECTED     11252  /var/run/acpid.socket
unix  3      [ ]         STREAM     CONNECTED     11251
unix  3      [ ]         STREAM     CONNECTED     11006  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     11005
unix  3      [ ]         STREAM     CONNECTED     11000
unix  3      [ ]         STREAM     CONNECTED     10999
unix  2      [ ]         DGRAM                    10997
unix  2      [ ]         DGRAM                    10851
unix  2      [ ]         DGRAM                    10686
unix  2      [ ]         DGRAM                    10661
unix  2      [ ]         DGRAM                    10594
unix  2      [ ]         DGRAM                    10569
unix  2      [ ]         DGRAM                    10453
unix  3      [ ]         STREAM     CONNECTED     10344  /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     10343
unix  3      [ ]         STREAM     CONNECTED     10298  @/var/run/hald/dbus-mO28j2Fpoe
unix  3      [ ]         STREAM     CONNECTED     10297
unix  3      [ ]         STREAM     CONNECTED     10117  @/var/run/hald/dbus-mO28j2Fpoe
unix  3      [ ]         STREAM     CONNECTED     10113
unix  3      [ ]         STREAM     CONNECTED     10096  /var/run/acpid.socket
unix  3      [ ]         STREAM     CONNECTED     10093
unix  3      [ ]         STREAM     CONNECTED     10063  @/var/run/hald/dbus-mO28j2Fpoe
unix  3      [ ]         STREAM     CONNECTED     10059
unix  3      [ ]         STREAM     CONNECTED     8704   @/var/run/hald/dbus-RL64SjEdUd
unix  3      [ ]         STREAM     CONNECTED     8703
unix  3      [ ]         STREAM     CONNECTED     8612
unix  3      [ ]         STREAM     CONNECTED     8611
unix  3      [ ]         STREAM     CONNECTED     8466
unix  3      [ ]         STREAM     CONNECTED     8465
unix  2      [ ]         DGRAM                    8343
unix  2      [ ]         DGRAM                    8016
unix  3      [ ]         STREAM     CONNECTED     7954
unix  3      [ ]         STREAM     CONNECTED     7953

lsof command :

Using lsof command also you can trace current open ports on the system. lsof mainly lists open files. Since sockets are treated as network files at the kernel level and they are treated as open when communicating, sockets can be listed using lsof!

lsof has -i option specifically to list network files.

# lsof -i
COMMAND    PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
dhclient   884     root    5u  IPv4  10791      0t0  UDP *:bootpc
rpcbind    991      rpc    6u  IPv4  11109      0t0  UDP *:sunrpc
rpcbind    991      rpc    7u  IPv4  11111      0t0  UDP *:netrcs
rpcbind    991      rpc    8u  IPv4  11112      0t0  TCP *:sunrpc (LISTEN)
rpcbind    991      rpc    9u  IPv6  11114      0t0  UDP *:sunrpc
rpcbind    991      rpc   10u  IPv6  11116      0t0  UDP *:netrcs
rpcbind    991      rpc   11u  IPv6  11117      0t0  TCP *:sunrpc (LISTEN)
rpc.statd 1013  rpcuser    6u  IPv4  11206      0t0  UDP localhost:766
rpc.statd 1013  rpcuser    7u  IPv4  11210      0t0  UDP *:39730
rpc.statd 1013  rpcuser    8u  IPv4  11214      0t0  TCP *:58573 (LISTEN)
rpc.statd 1013  rpcuser    9u  IPv6  11218      0t0  UDP *:60991
rpc.statd 1013  rpcuser   10u  IPv6  11222      0t0  TCP *:56954 (LISTEN)
cupsd     1069     root    6u  IPv6  11412      0t0  TCP localhost:ipp (LISTEN)
cupsd     1069     root    7u  IPv4  11413      0t0  TCP localhost:ipp (LISTEN)
cupsd     1069     root    9u  IPv4  11416      0t0  UDP *:ipp
sshd      1208     root    3u  IPv4  11960      0t0  TCP *:ssh (LISTEN)
sshd      1208     root    4u  IPv6  11962      0t0  TCP *:ssh (LISTEN)
master    1287     root   11u  IPv4  12158      0t0  TCP localhost:smtp (LISTEN)
master    1287     root   12u  IPv6  12160      0t0  TCP localhost:smtp (LISTEN)
sshd      1441     root    3r  IPv4  12964      0t0  TCP ip-12-31-28-246.ap-south-1.compute.internal:ssh-59.184.179.68:49413 (ESTABLISHED)
sshd      1444 ec2-user    3u  IPv4  12964      0t0  TCP ip-12-31-28-246.ap-south-1.compute.internal:ssh-59.184.179.68:49413 (ESTABLISHED)
xinetd    1618     root    5u  IPv6  13908      0t0  TCP *:telnet (LISTEN)
sshd      1694     root    3r  IPv4  14812      0t0  TCP ip-12-31-28-246.ap-south-1.compute.internal:ssh-59.184.179.68:50729 (ESTABLISHED)
sshd      1697 ec2-user    3u  IPv4  14812      0t0  TCP ip-12-31-28-246.ap-south-1.compute.internal:ssh-59.184.179.68:50729 (ESTABLISHED)
in.telnet 3420     root    0u  IPv4  35294      0t0  TCP ip-12-31-28-246.ap-south-1.compute.internal:telnet->5ec3c900.skybroadband.com:39192 (ESTABLISHED)
in.telnet 3420     root    1u  IPv4  35294      0t0  TCP ip-12-31-28-246.ap-south-1.compute.internal:telnet->5ec3c900.skybroadband.com:39192 (ESTABLISHED)
in.telnet 3420     root    2u  IPv4  35294      0t0  TCP ip-12-31-28-246.ap-south-1.compute.internal:telnet->5ec3c900.skybroadband.com:39192 (ESTABLISHED)
in.telnet 3422     root    0u  IPv4  35326      0t0  TCP ip-12-31-28-246.ap-south-1.compute.internal:telnet->188-24-133-29.dynamic.brasov.rdsnet.ro:apwi-rxserver (ESTABLISHED)
in.telnet 3422     root    1u  IPv4  35326      0t0  TCP ip-12-31-28-246.ap-south-1.compute.internal:telnet->188-24-133-29.dynamic.brasov.rdsnet.ro:apwi-rxserver (ESTABLISHED)
in.telnet 3422     root    2u  IPv4  35326      0t0  TCP ip-12-31-28-246.ap-south-1.compute.internal:telnet->188-24-133-29.dynamic.brasov.rdsnet.ro:apwi-rxserver (ESTABLISHED)

In the above output, you can see which command being run by which user using which socket and state of the port at the end!

If you have any other trick to list open ports on the system, please let us know in the comments. We will add it to this post.

3 tricks to get multiple commands output in the same row

Three tricks to get two commands output in a single row. Normally two commands outputs will be always separated by a carriage return in Linux terminal. 

One of the major hurdles in scripting is to get your outputs formatted well according to requirements. In Linux/Unix each command stdout its output always in a new line. This is a hurdle in many situations where the coder wants two outputs, two terms, two variables in a single row for example CSV format. In this post, we will see how to present two or more commands output in a single row.

Read also : Scripting related posts

Normally when we execute more than one command, they will display outputs in different rows:

# date ; hostname
Sat Dec 24 01:35:50 EDT 2016
testsrv2

# echo text1; echo text2; echo text3
text1
text2
text3

In the above example, commands have outputs per row. We are going to accomplish all outputs in one row using the below tricks.

Trick 1:

Using the translate function. AS I said earlier each command output is accompanied by a carriage return. We will be translating carriage return with tab character hence getting two or more commands output in a single row.

# (date; hostname) |tr '\n' '\t'
Sat Dec 24 01:36:22 EDT 2016    testsrv2

# (echo text1; echo text2; echo text3) |tr '\n' '\t'
text1   text2   text3

Bypassing outputs to translate function we achieved single row output. Here we instructed tr function to translate all carriage returns (\n) to tab (\t).

If you want to output in CSV format (comma-separated value) then replace tab with a comma and you will get it.

# (date; hostname) |tr '\n' ','
Sat Dec 24 01:43:09 EDT 2016,testsrv2,

# (echo text1; echo text2; echo text3) |tr '\n' ','
text1,text2,text3,

You can observe one trailing comma which shows every command output ends with \n and hence it gets replaced by ,

Trick 2:

By defining each command output as a variable. Here we will store the output of each command in one variable and then we will echo those variables in a single line.

# mydate=`date`

# myname=`hostname`

# echo $myname $mydate
testsrv2 Sat Dec 24 01:46:04 EDT 2016

Here we stored date and hostname command’s output in mydate and myname variables. In the last command, we echoed both variables with space in between. Note that commands output can be stored in a variable by executing the command using backticks (in-line execution)!

Trick 3:

By echoing in-line execution outputs. In-line execution i.e. using backticks to execute commands within another command. We will be using this trick to echo outputs in a single row.

# echo "`date` `hostname`"
Sat Dec 24 01:50:36 EDT 2016 testsrv2

In the above example, first, we have executed commands and got outputs. But those outputs are fed into echo command. Hence echo command stdout them in a single row.

# echo "`echo text1` `echo text2` `echo text3`"
text1 text2 text3

Make a note that we executed each command in separate back tick quotes.

How to get directory size in Linux

Learn how to get directory total size in Linux without calculating each and every file’s size within. A very handy command to troubleshoot mount point utilization.

Many times we need to check specific directory size to hunt down the culprit of mount point utilization. There are scenarios where mount points keep getting full and we need to investigate which file or directory is hogging most of the space.

Collectively to check the highest file or directories, I already briefed on post Highest size files in the mount point. Let’s see how we can get directory’s collective size in one go.

We will be using disk usage command du and below of its options :

  • -s: Display the only summary of each element
  • -h: Human readable format for size i.e. KB, MB, GB
  • -c: Produce grand total i.e.  display the total size
# du -sch /dump1/test/mydir
13G     /dump1/test/mydir
13G     total

Here, the specified directory is 13GB. It’s a size of /dump1/test/mydir directory not of /dmp1.

If you want to check the size of every object beneath the specified directory then you can skip -s i.e. summery option from the command.

# du -ch /tmp
4.0K    /tmp/hsperfdata_oracle11
4.0K    /tmp/orbit-root

----- output clipped -----

4.0K    /tmp/keyring-VPiB3D
16K     /tmp/lost+found
652K    /tmp
652K    total

In the above output, you can see each and every object’s size is given and finally at the end total is given which is the final size of the specified directory!

Let me know if you have any queries/questions/feedback in the comments. Also, drop us a message on our contact form.

Run command on multiple linux servers from windows

Learn how to run the same/repetitive command on multiple Linux servers from the Windows machine. This trick uses command line putty ‘plink’ utility.

One of the major concerns for sysadmin is to run the same/repetitive command on multiple Linux servers in infra when there is no centralized tool available. In this post, we are going to see how to run a command on multiple Linux servers in one go. There is no need of saving your account password anywhere and even no need to have expect function in your source machine!

Pre-requisite

  • Windows machine with plink downloaded on it (download plink here)
  • Linux servers should be reachable from a windows machine

How to do it

plink is a putty command-line utility. Using plink we will be able to connect to the server by supplying IP, username, password on the command line. plink can be invoked from a command prompt on windows.

C:\Users\noname\Desktop>plink -ssh user1@10.10.1.11 -pw password@123 (hostname; date)
testserver
Tue Nov  1 12:54:33 IST 2016

C:\Users\noname\Desktop>

Goto Windows command prompt by typing cmd in a run window (windows key + r). Navigate to folder where plink executable is kept and then type in plink command as above.

  • ssh: protocol to connect
  • id@server IP
  • pw: account password
  • Commands to execute on Linux server in braces.

This is how plink works. Now to execute the same command on multiple servers we see below example. We will connect to 3 servers and execute hostname & date command.

First, put all 3 server’s IP addresses in single file ip_list.txt. Then execute simple for loop on that file as below in command prompt. This is a very basic batch script.

C:\Users\noname\Desktop>FOR /F "tokens=1,2* delims=," %G IN (C:\Users\noname\Desktop\ip_list.txt) DO plink -ssh user1@%G -pw password@123 (hostname; date)

testserver
Tue Nov  1 12:54:33 IST 2016

testserver1
Tue Nov  1 12:54:36 IST 2016

testserver2
Tue Nov  1 12:54:39 IST 2016

C:\Users\noname\Desktop>

Voila! all server’s command output is there! Commands executed on all servers in one go.

This is very useful when you need to check some single line outputs from all servers or if you want to run account refresh commands on all servers in one go.