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 using ssh from sorted by
Terminal - Commands using ssh - 294 results
mysqldump -pyourpass --single-transaction --master-data=2 -q --flush-logs --databases db_for_doslave |tee /home/db_bak.sql |ssh mysqladmin@slave.db.com "mysql"
2014-08-11 05:57:21
User: dragonwei
Functions: ssh tee
0

get master info:

head -n 40 /home/db_bak.sql |awk '$0~/MASTER_LOG_FILE/

slave server:

change master ??.

start slave

tar -vzc /path/to/cool/directory | ssh -q my_server 'tar -vzx -C /'
2014-07-31 18:42:57
User: regulatre
Functions: ssh tar
0

Copy a local directory to a remote server using ssh+tar (assume server is lame and does not have rsync).

tar cvzf - dir | ssh my_server 'tar xzf -'
docker ps -q | xargs -n 1 docker inspect | jq '.[0].NetworkSettings.Ports +{} | map(select(. != null)[0].HostPort) | map("-L \(.):localhost:\(.)") ' | sed -n 's/.*"\(.*\)".*/\1/p' |xargs boot2docker ssh -N
tar -cj / -X /tmp/exclude.txt | cstream -v 1 -c 3 -T 10 | ssh user@host 'tar -xj -C /backupDestination'
2014-07-21 18:52:19
User: fantleas
Functions: ssh tar
0

The files are automatically uncompressed when they reach the destination machine. This is a fast way to backup your server to your local computer while it's running (shutting down services is recommended).

A file named "exclude.txt" is needed at /tmp/ containing the following :

/dev/*

/media/*

/mnt/*

/proc/*

/sys/*

/tmp/*

/home/*/.local/share/Trash

/home/*/.gvfs

/home/*/.cache

/home/*/.thumbnails

/etc/fstab

/lib/modules/*/volatile/.mounted

/var/run/*

/var/lock/*

/var/tmp/*

/var/cache/apt/archives/*

/lost+found/*

ssh -X user@host 'DISPLAY=:0 notify-send "TEST MESSAGE."'
ssh <user>@<ip address> $(echo wmic process where \"name like \'%<process to kill>%\'\" delete)
2014-07-02 04:41:05
User: adanisch
Functions: echo ssh
0

Tested on Windows 8 w/SSH, Cygwin - it can be tricky to quote if you dont use the $(..) syntax to echo it back out

ssh -D 8080 -f -N srv1
2014-06-25 12:46:44
User: fr00tyl00p
Functions: ssh
0

Provides a SOCKS5 proxy on localhost port 8080 which will tunnel all connections through srv1 and run in the background.

ssh -o ProxyCommand="nc -X 5 -x localhost:9050 %h %p" username@remote_host
2014-06-09 08:54:58
Functions: ssh
0

Tunneling an SSH session through TOR for anonymity.

Using openbsd's netcat.

Tested on Ubuntu 14.04

This also works with any proxy type: http, socks4 and socks5

For http proxy, replace -X 5 with -X connect

For socks4 proxy, replace -X 5 with -X 4 (DNS leak may occur)

Proxy host defined after -x

Replace -x localhost:9050 with proxy server of your choice

ssh <source host> "cat file" | ssh <dest host> "cat - > file"
2014-05-27 12:40:08
User: thorko
Functions: ssh
0

Copies files between hosts which don't have access to each other. It uses a proxy host which has access to both of them.

ssh [remote-machine] "cat file" | xclip -selection c
2014-05-19 16:34:44
User: conga
Functions: ssh
1

ssh from local to remote and pipe output of file to the local clipboard

cat ~/.ssh/id_rsa.pub | ssh deployer@xxxxx -p 52201 'cat >> ~/.ssh/authorized_keys'
dpipe /usr/lib/openssh/sftp-server = ssh $REMOTE_HOST sshfs whatever:$LOCAL_PATH $REMOTE_PATH -o slave
2014-03-25 17:40:34
User: em
Functions: ssh
5

While `sshfs $REMOTE_HOST:$REMOTE_PATH $LOCAL_PATH` "pulls" a directory from the remote server to the local host, the above command does the reverse and "pushes" a directory from the local host to the remote server.

This makes use of the "slave" option of sshfs which instructs it to communicate over plain stdin/stdout and the `dpipe` tool from vde2 to connect the sftp-server stdout to the sshfs stdin and vice-versa.

ssh -NL 12345:localhost:631 username@remote_server
ssh HOST '(cd REPO_DIR && git diff --name-only HEAD | cpio -o -Hnewc --quiet)' | cpio -iduv --quiet -Hnewc
2014-02-01 18:40:31
User: ivan4th
Functions: cd cpio diff ssh
Tags: ssh git cpio
0

Copy changed files from remote git repository, _including binary ones_, staged and unstaged alike. Note that this command doesn't handle deleted files properly.

ssh USER@HOST cat REMOTE_FILE.mp4 | tee LOCAL_FILE.mp4 | mplayer -
2013-11-28 11:25:26
User: flatcap
Functions: cat ssh tee
6

Securely stream a file from a remote server (and save it locally).

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

This is an extension of snipertyler's idea.

Note: This command uses an encrypted connection, unlike the original.

for k in `seq -w 1 50` ; do ssh 192.168.100.$k uptime ; done
complete -o default -o nospace -W "$(grep -i -e '^host ' ~/.ssh/config | awk '{print substr($0, index($0,$2))}' ORS=' ')" ssh scp sftp
2013-11-19 14:44:41
User: Wamith
Functions: scp ssh
0

I sue this in my .bashrc file

This will also do auto-completion for scp and sftp

ssh remoteUser@remoteHost "tail -f /var/log/scs/remoteLogName" | tee localLogName
ssh -o ProxyCommand='nc -x ProxyHost:8080 %h %p' TargetHost
2013-08-19 13:08:19
User: UnixSage
Functions: ssh
1

If you are blocked or need to use a Socks proxy

colordiff <(ssh user@host cat /etc/ssh/sshd_config) /etc/ssh/sshd_config
ssh user@remotehost "find basedir -type d" | xargs -I {} -t mkdir -p {}
2013-07-17 07:14:32
User: neomefistox
Functions: mkdir ssh xargs
0

The directories are created in the local host with the same structure below of a remote base directory, including the 'basedir' in case that it does not exists.

You must replace user and remotehost (or IP address) with your proper values

ssh will ask for the password of the user in remotehost, unless you had included properly your hostname in the remote .ssh/known_hosts file.

ssh() { [ $TERM == screen ] && (screen -X title "${1##*@}"; command ssh "$@"; screen -X title '';exit;) || command ssh "$@"; }
2013-06-03 12:31:05
User: djkadu
Functions: command screen ssh
0

By adding this to your bashrc, when SSH'ing to a server while screen is active it will change the window tittle to the name of the server you going to.

SSHPASS='your_password' sshpass -e ssh me@myhost.com
2013-06-03 12:26:40
User: djkadu
Functions: ssh
0

The above is OK if you not worried about security, as per sshpass man pages:

" The -p option should be considered the least secure of all of sshpass's options. All system users can see the password in the command line with a simple "ps" command."

So, instead what I do is use the -e option:

" -e The password is taken from the environment variable "SSHPASS"."