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.
» 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:
This line unbuffers the interactive output of rsync's --progress flag
creating a new line for every update.
This output can now be used within a script to make actions (or possibly piped into a GUI generator for a progress bar)
rsync is the best command ever and I am interested what the rest of you think is the best command
rsync by itself doesn't support copying between two remote hosts, but if you use sshfs you can pretend one of them is local. If you have a passphrase-less ssh-key, you can even put this script into a cron job.
A faster alternative is to run ssh-keygen on remote1 and put the pubkey into remote2:~/.ssh/authorized_keys, running rsync on remote1 (or vice versa), but the problem with that is that now a hacker on remote1 can access remote2 at any time. The above method ensures your local computer stays the weak link.
This creates an archive that does the following:
rsync::
(Everyone seems to like -z, but it is much slower for me)
-a: archive mode - rescursive, preserves owner, preserves permissions, preserves modification times, preserves group, copies symlinks as symlinks, preserves device files.
-H: preserves hard-links
-A: preserves ACLs
-X: preserves extended attributes
-x: don't cross file-system boundaries
-v: increase verbosity
--numeric-ds: don't map uid/gid values by user/group name
--delete: delete extraneous files from dest dirs (differential clean-up during sync)
--progress: show progress during transfer
ssh::
-T: turn off pseudo-tty to decrease cpu load on destination.
-c arcfour: use the weakest but fastest SSH encryption. Must specify "Ciphers arcfour" in sshd_config on destination.
-o Compression=no: Turn off SSH compression.
-x: turn off X forwarding if it is on by default.
Flip: rsync -aHAXxv --numeric-ids --delete --progress -e "ssh -T -c arcfour -o Compression=no -x" [source_dir] [dest_host:/dest_dir]
Copies the complete root-dir of a linux server to another one, where the new harddisks formated and mountet. Very useful to migrate a root-server to another one.
rsync will copy the source directory into destination and any subsequent run will synchronize only the changes from the source.
Useful when upgrading my Linux distro and trying to copy only "settings" from the old home folder to the new one.
a : to keep files permissions
--no-whole file : use rsync?s delta-transfer algorithm
--inplace : writes the updated data directly to the destination file
optionnal -> add --remove-source-files to mv instead of cp
Yes, rsync(1) supports local directories. And, should anything change, it's trivial to run the command again, and grab only the changes, instead of the full directory.
Transfer files with rsync over ssh on a non-standard port, showing a progress bar and resuming partial transfers.
An example config file is placed in the sample output along with the command line call to use it.
The rsync daemon here is setup on the destination, thus requiring the read only = false flag. Also it uses uid and gid of root, change as required.
Clone a root partition. The reason for double-mounting the root device is to avoid any filesystem overlay issues. This is particularly important for /dev.
Also, note the importance of the trailing slashes on the paths when using rsync (search the man page for "slash" for more details). rsync and bash add several subtle nuances to path handling; using trailing slashes will effectively mean "clone this directory", even when run multiple times. For example: run once to get an initial copy, and then run again in single user mode just before rebooting into the new disk.
Using file globs (which miss dot-files) or leaving off the trailing slash with rsync (which will create /mnt/target/root) are traps that are easy to fall into.
cloning root filesystem without suffering to possible interruptions. useful when moving a running system to a new partition. also works as a solid backup solution.
-r for recursive (if you want to copy entire directories)
src for the source file (or wildcards)
dst for the destination
--progress to show a progress bar
Applying filter rules is what makes this a really useful command. It's usually a pain to figure out how to sync ONLY files matching a particular pattern, and often one reverts to goofy stuff like find .. -exec rsync ..
The filter hides all folders from the transfer, so that only the matching folders that store the filename are left for the sync.
The command copies a file from remote SSH host on port 8322 with bandwidth limit 100KB/sec;
--progress shows a progress bar
--partial turns partial download on; thus, you can resume the process if something goes wrong
--bwlimit limits bandwidth by specified KB/sec
--ipv4 selects IPv4 as preferred
I find it useful to create the following alias:
alias myscp='rsync --progress --partial --rsh="ssh -p 8322" --bwlimit=100 --ipv4'
in ~/.bash_aliases, ~/.bash_profile, ~/.bash_login or ~/.bashrc where appropriate.
'-mtime -10' syncs only files newer 10 days (-mtime is just one example, use whatever find expressions you need)
printf %P: File's name with the name of the command line argument under which it was found removed.
this way, you can use any src directory, no need to cd into your src directory first.
using \\0 in printf and a corresponding --from0 in rsync ensures that even filenames with newline characters work (thanks syssyphus for #3808).
both, #1481 and #3808 just work if you either copy the current directory (.) , or the filesystem root (/), otherwise the output from find and the source dir from rsync just don't match. #7685 works with an arbitrary source directory.