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/
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.
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:
Create a bunch of random files with random binary content. Basically dd dumps randomly from your hard disk to files random-file*.
Same as above but slooooow it down
Let dd use direct I/O to write directly to the disk without any caching. You'll encounter very different results with different block sizes (try with 1k, 4k, 1M, ... and appropriate count values).
Note, the [remotePort] should be opened in the firewall first. First, start the destination box listening, then fire off the sending box. Data from the /dev/zero device in memory of the source machine is read out using dd, sent over the network with nc, and read back in from the other side of the network with nc, going to the /dev/null device. Essentially, it is a memory-network-memory copy operation, the output of dd will tell you how fast your network really is performing.
Depending on the speed of you system, amount of RAM, and amount of free disk space, you can find out practically how fast your disks really are. When it completes, take the number of MB copied, and divide by the line showing the "real" number of seconds. In the sample output, the cached value shows a write speed of 178MB/s, which is unrealistic, while the calculated value using the output and the number of seconds shows it to be more like 35MB/s, which is feasible.
Solves "tr" issues with non C-locales under BSD-like systems (like OS X)
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 will create a 10 MB file named testfile.txt. Change the count parameter to change the size of the file.
As one commenter pointed out, yes /dev/random can be used, but the content doesn't matter if you just need a file of a specific size for testing purposes, which is why I used /dev/zero. The file size is what matters, not the content. It's 10 MB either way. "Random" just referred to "any file - content not specific"
This shell snippet reads a single keypress from stdin and stores it in the $KEY variable.
You do NOT have to press the enter key!
The key is NOT echoed to stdout!
This is useful for implementing simple text menus in scripts and similar things.
If you don't want your computer to try to boot form a USB stick that used to be used as a boot device (maybe for a live linux distro), you will have to remove the boot loader from your stick other wise the boot will fail each time the device is attached to your PC.
Create a temporary file that acts as swap space. In this example it's a 1GB file at the root of the file system. This additional capacity is added to the existing swap space.
This is a bit to bit copy so if you have a 500GB hard disk it will take a long time even if have Gigabit Ethernet
where /dev/sdb1 is the name of your usb device
Running this code will execute dd in the background, and you'll grab the process ID with '$!' and assign it to the 'pid' variable. Now, you can watch the progress with the following:
while true; do kill -USR1 $pid && sleep 1 && clear; done
The important thing to grasp here isn't the filename or location of your input or output, or even the block size for that matter, but the fact that you can keep an eye on 'dd' as it's running to see where you are at during its execution.
This invokes tar on the remote machine and pipes the resulting tarfile over the network using ssh and is saved on the local machine. This is useful for making a one-off backup of a directory tree with zero storage overhead on the source. Variations on this include using compression on the source by using 'tar cfvp' or compression at the destination via
ssh user@host "cd dir; tar cfp - *" | gzip - > file.tar.gz
Replace (as opposed to insert) hex opcodes, data, breakpoints, etc. without opening a hex editor.
HEXBYTES contains the hex you want to inject in ascii form (e.g. 31c0)
OFFSET is the hex offset (e.g. 49cf) into the binary FILE
[re]verify those burned CD's early and often - better safe than sorry -
at a bare minimum you need the good old `dd` and `md5sum` commands,
but why not throw in a super "user-friendly" progress gauge with the `pv` command -
adjust the ``-s'' "size" argument to your needs - 700 MB in this case,
and capture that checksum in a "test.md5" file with `tee` - just in-case for near-future reference.
*uber-bonus* ability - positively identify those unlabeled mystery discs -
for extra credit, what disc was used for this sample output?
This will create an exact duplicate image of your hard drive that you can then restore by simply reversing the "if" & "of" locations.
sudo dd if=/media/disk/backup/sda.backup of=/dev/sda
Alternatively, you can use an SSH connection to do your backups:
dd if=/dev/sda | ssh email@example.com dd of=~/backup/sda.backup