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 screen from sorted by
Terminal - Commands using screen - 38 results
$ screen -S test -d -m -- sh -c 'date; exec $SHELL'
2014-02-17 08:11:45
User: awek
Functions: exec screen sh test
0

The command creates new session "test", executes 'date' and then start your default shell (to keep the detached session alive). Change 'date' to fit your needs.

screen -r test

will attach the created session.

screen
2013-11-12 10:06:01
User: TwoStarII
Functions: screen
0

Screen is a full-screen window manager that multiplexes a physical terminal between several processes.

Print to screen all the directories above the pwd, showing: mountpoints, symbolic links, and permissions (with colors)
2013-07-25 22:55:08
User: malathion
Functions: screen
Tags: sed namei
-1

Useful for finding out quickly if you're still in the same filesystem or are in a descendant of a symbolic link or mountpoint. For plain text output, unset the color substitutions or strip them from the function.

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.

screen -R -D -S sessionname -q
screen -r irc || screen -S irc irssi
2012-10-19 12:12:54
User: daneoshiga
Functions: screen
Tags: bash screen irssi
-2

Tries to reattach to screen, if it's not available, creates one.

created an alias "irc" for it, since sometimes i forget if there already is a screen session running with irssi, this way I avoid creating a new one by mistake.

screen !!
2012-10-15 12:58:38
User: bartonski
Functions: screen
1

I often find myself wanting to open screen on whatever command I'm currently running. Unfortunately, opening a fresh screen session spawns a new bash session, which doesn't keep my history, so calling screen directly with the previous command is the only way to go.

s() { screen -d -RR -m -S "$1" -t "$USER"@"$1" ssh "$1"; }
2012-09-07 23:02:52
User: salamando
Functions: screen ssh
Tags: ssh screen Linux
5

Use as: $ s host1

Will ssh to remote host upon first invocation. Then use C-a d to detatch. Running "s host1" again will resume the shell session on the remote host. Only useful in LAN environment. You'd want to start the screen on the remote host over a WAN.

Adapted from Hack 34 in Linux Server Hacks 2nd Addition.

for s in /tmp/screens/S-$USER/*; do screen -r "$(basename "$s")"; done
screen -x $(screen -ls | awk 'NR == 2 { print $1 }')
screen -S [name] -d -m [<command>]
2012-08-10 09:30:22
User: bones
Functions: screen
0

Starts a detached screen with the given screen-name.

Can be useful for automatic started scripts and init.d-scripts.

rlwrap -S "$STY> " sh -c 'while read; do screen -S "'"$STY"'" -X $REPLY; done'
2012-04-16 21:49:06
User: bandie91
Functions: screen sh
Tags: screen rlwrap
0

run it inside a screen session, you send commands to screen itself!

screen -D -R
2012-03-01 15:28:21
User: marek158
Functions: screen
Tags: gnu screen
12

man screen:

"-D -R Attach here and now. In detail this means: If a session is running, then reattach. If necessary detach and logout remotely first. If it was not running create it and notify the user. This is the author?s favorite."

screen -RR
2012-03-01 15:27:44
User: jlaunay
Functions: screen
Tags: gnu screen
2

-RR option is used to resume the first appropriate detached screen session

screen -x `screen -ls | grep Detached | cut -c -10`
2012-03-01 14:13:09
User: peter4512
Functions: cut grep screen
Tags: gnu screen
-3

I alias this as "tach":

alias tach='screen -x `screen -ls | grep Detached | cut -c -10`'

If you have several detached sessions it will just grab the first one. If you're running nested screens you can open new outer windows and run tach repeatedly to grab all the detached sessions into that one.

screen -ls | grep D
2012-03-01 14:05:40
User: peter4512
Functions: grep screen
-1

If you have many screen sessions, it can be difficult to find the id of the one you just detached from so you can re-attach using `screen -x -S `

screen -x <screen_id>
# su - <user> ; script /dev/null ; screen -r
2011-07-04 16:26:10
User: dmmst19
Functions: screen script su
Tags: screen su pty
2

Normally, if you su to another user from root and try to resume that other user's screen session, you will get an error like "Cannot open your terminal '/dev/pts/0' - please check." This is because the other user doesn't have permission for root's pty. You can get around this by running a "script" session as the new user, before trying to resume the screen session. Note you will have to execute each of the three commands separately, not all on the same line as shown here.

Credit: I found this at http://www.hjackson.org/blog/archives/2008/11/29/cannot-open-your-terminal-dev-pts-please-check.

alias screenr='screen -r $(screen -ls | egrep -o -e '[0-9]+' | head -n 1)'
screen /dev/ttyS0 9600
2011-03-09 07:56:34
User: cp
Functions: screen
6

e.g., 'screen -L /dev/ttyUSB0 38400' listens to your Holux M-241 GPS logger and turns on automatic logging

screen /dev/tty<device> 9600
for pid in `screen -ls | grep -v $STY | grep tached | awk '{print $1;}' | perl -nle '$_ =~ /^(\d+)/; print $1;'`; do screen -x $pid; done
2010-06-22 23:06:31
User: tmsh
Functions: awk grep perl screen
0

Personally, I save this in a one line script called ~/bin/sci:

#!/bin/bash

for pid in `screen -ls | grep -v $STY | grep tached | awk '{print $1;}' | perl -nle '$_ =~ /^(\d+)/; print $1;'`; do screen -x $pid; done

I also use:

alias scx='screen -x'

alias scl='screen -ls | grep -v $STY'

screen -d -m command &
2010-06-22 18:24:22
Functions: command screen
5

The improvement is that you can re-attach to the screen at a later point.

screen -raAd
2010-04-12 22:54:58
User: rkulla
Functions: screen
11

By default, screen tries to restore its old window sizes when attaching to resizable terminals. This command is the command-line equivalent to typing ^A F to fit an open screen session to the window.

screen -x
2009-09-09 11:30:56
User: flart
Functions: screen
5

Force the user you want to watch doing things into doing his things in a screen session. Then simply attach yourself to that session with the command shown above.

Works only if you are on the same machine, of course