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.
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.
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
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:
Very handy and time-saving.
Do a 'ctrl+ r' on command prompt. You will see a "(reverse-i-search)`':" mark. Just type any sub-string of the command you want to search(provided you have used it sometime in the same session).
Keep on searching by repeatedly pressing ctrl+r. Press enter once you get the desired command string.
This is great if you are behind a router that does not support dynamic dns updates!
Example: AT&T u-verse gateway...
Put this in a script and run it as a cron job.
You can replace "sort -nu" with "sort -u" for a word list sorted or "sort -R" for a random-sorted line
(edit: corrected)
with 'mii-tool -w eth0' you can watch the interface for changes of the link status
somewhat faster version to see the size of our directories. Size will be in Kilo Bytes. to view smallest first change '-k1nr' to '-k1n'.
readlink -f accepts a relative, noncanonical path and emits the corresponding canonical, absolute path.
Before doing this, back-up all data on any ext3 partitions that are to be converted to ext4.
After running previous command you MUST run fsck, is needed to return the filesystem to a consistent state.
fsck -pDf /dev/yourpartition
Edit /etc/fstab and change the 'type' from ext3 to ext4 for any partitions that are converted to ext4.
If you should happen to find yourself needing some binary numbers, this is a quickie way of doing it. If you need more digits, just add more "{0..1}" sequences for each digit you need. You can assign them to an array, too, and access them by their decimal equivalent for a quickie binary to decimal conversion (for larger values it's probably better to use another method). Note: this works in bash, ksh and zsh. For zsh, though, you'll need to issue a setopt KSH_ARRAYS to make the array zero-based.
binary=({0..1}{0..1}{0..1}{0..1})
echo ${binary[9]}
order the files by modification (thanks stanishjohnd) time, one file per output line and filter first 10
ditto can be used to "thinnify" Mach-O Universal binaries to separate unncessary code from your machine. For example, doubleTwist.app shaved off around 10 MB while Google Earth went from 112 MB to 62 MB after extracting only the i386 code from them.
Thankfully, the ssh command allows you to specify multiple tunnels through the same server in one command.
Remeber if you want a priviliged port on your machine, you must use root or sudo account.
Intended for dynamic ip OpenDNS users, this command will update your OpenDNS network IP.
For getting your IP, you can use one of the many one-liners here on commandlinefu.
Example:
I use this in a script which is run by kppp after it has successfully connected to my ISP:
---
#!/bin/bash
IP="`curl -s http://checkip.dyndns.org/ | grep -o '[[:digit:].]\+'`"
PW="hex-obfuscated-pw-here"
if [ "$IP" == "" ] ; then echo 'Not online.' ; exit 1
else
wget -q --user=topsecret --password="`echo $PW | xxd -ps -r`" 'https://updates.opendns.com/nic/update?hostname=myhostname&myip='"$IP" -O -
/etc/init.d/ntp-client restart &
fi
---
PS: DynDNS should use a similar method, if you know the URL, please post a comment. (Something with members.dyndns.org, if I recall correctly)
This command gives you the number of lines of every file in the folder and its subfolders matching the search options specified in the find command. It also gives the total amount of lines of these files.
The combination of print0 and files0-from options makes the whole command simple and efficient.
It works like a "tail -f" on several files.
Use the number keys 0-9 to set a baseline in the numbered window.
Pressing "b" let you scroll back in one of the windows.
Uses logger in a while loop to log memory statistics frequently into the local syslog server.
Command binds a set of commands to the F12 key.
Feel free to alter the dashboard according to your own needs.
How to find the key codes?
Type
read
Then press the desired key (example: F5)
^[[15~
Try
bind '"\e[15~"':"\"ssh [email protected]\C-m"""
or
bind '"\e[16~"':"\"apachectl -k restart\C-m"""
!* is all of the arguments to the previous command rather than just the last one.
This is useful in many situations.
Here's a simple example:
vi cd /stuff
oops!
[exit vi, twice]
!*
expands to: cd /stuff
This command securely erases all the unused blocks on a partition.
The unused blocks are the "free space" on the partition.
Some of these blocks will contain data from previously deleted files.
You might want to use this if you are given access to an old computer and you do not know its provenance.
The command could be used while booted from a LiveCD to clear freespace space on old HD.
On modern Linux LiveCDs, the "ntfs-3g" system provides ReadWrite access to NTFS partitions thus enabling this method to also be used on Wind'ohs drives.
NB depending on the size of the partition, this command could take a while to complete.
This command displays a clock on your terminal which updates the time every second. Press Ctrl-C to exit.
A couple of variants:
A little bit bigger text:
watch -t -n1 "date +%T|figlet -f big"
You can try other figlet fonts, too.
Big sideways characters:
watch -n 1 -t '/usr/games/banner -w 30 $(date +%M:%S)'
This requires a particular version of banner and a 40-line terminal or you can adjust the width ("30" here).
sudo pmset -a hibernatemode 1 sets hiberate on.
sudo pmset -a hibernatemode 0 sets hibernate off.
from the pmset man page:
0001 (bit 0) enables hibernation; causes OS X to write memory state to
hibernation image at sleep time. On wake (without bit 1 set) OS X will
resume from the hibernation image. Bit 0 set (without bit 1 set) causes
OS X to write memory state and immediately hibernate at sleep time.
I often change my MacBook's sleep state. So I created a function for bash to make it a little easier.
Usage:
hibernate (on | off)
"hibernate on" will set your laptop to hibernate if the lid is closed.
"hibernate off" will set your laptop to sleep if the lid is closed.
### note : "proper" indentation isn't preserved on the website
function hibernate()
{
case "${1}" in
on)
sudo pmset -a hibernatemode 1
echo Hibernate mode on.
;;
off)
sudo pmset -a hibernatemode 0
echo Hiberate mode off.
;;
*)
echo "I'm sorry Dave, but I can't do that."
;;
esac
}
To make things easier, add the proper line in your /etc/sudoers file so that your user may invoke pmset without a password. Here's how mine looks:
bwayne luna = NOPASSWD: /usr/bin/pmset
Don't forget that you must edit sudoers with `sudo visudo` from Terminal.app, and not by another text editor.
Sorry this is so Mac OS specific.