commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
Sometimes when copying files from one place to another, the timestamps get lost. Maybe you forgot to add a flag to preserve timestamps in your copy command. You're sure the files are exactly the same in both locations, but the timestamps of the files in the new home are wrong and you need them to match the source.
Using this command, you will get a shell script (/tmp/retime.sh) than you can move to the new location and just execute - it will change the timestamps on all the files and directories to their previous values. Make sure you're in the right directory when you launch it, otherwise all the touch commands will create new zero-length files with those names. Since find's output includes "." it will also change the timestamp of the current directory.
Ideally rsync would be the way to handle this - since it only sends changes by default, there would be relatively little network traffic resulting. But rsync has to read the entire file contents on both sides to be sure no bytes have changed, potentially causing a huge amount of local disk I/O on each side. This could be a problem if your files are large. My approach avoids all the comparison I/O. I've seen comments that rsync with the "--size-only" and "--times" options should do this also, but it didn't seem to do what I wanted in my test. With my approach you can review/edit the output commands before running them, so you can tell exactly what will happen.
The "tee" command both displays the output on the screen for your review, AND saves it to the file /tmp/retime.sh.
Credit: got this idea from Stone's answer at http://serverfault.com/questions/344731/rsync-copying-over-timestamps-only?rq=1, and combined it into one line.
Appends output to the file, some systems require the -a to do this.
What does it mean?
The redirection operator n>&m makes file descriptor n to be a copy of file descriptor m. So, whe are:
- Opening a new file descriptor, 3, that is a copy of file descriptor 1, the standard output;
- Making file descriptor 1 a copy of file descriptor 2, the standard error output;
- Making file descriptor 2 to be a copy of file descriptor 3 (the "backup" of the standard output)
in a short: we swapped the standard output and the standard error output.
Also shows files as they are found. Only works from a tty.
For instance, to add mongodb 10gen package
echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" | sudo tee -a /etc/apt/sources.list
"That's it. Not much to see here. The first command writes any cache data that hasn't been written to the disk out to the disk. The second command tells the kernel to drop what's cached. Not much to it. This invalidates the write cache as well as the read cache, which is why we have the sync command first. Supposedly, it is possible to have some cached write data never make it to disk, so use it with caution, and NEVER do it on a production server. You could ... but why take the risk?
As long as you are running a post 2.6.16 kernel,..."
Show external IP and geolocation information.
Primary feature is the use of tee to echo IP _and_ send to geoiplookup command...Use IP as input for as many commands as you want with more >( [command] )
Requires MaxMind DB and geoiplookup tool.
Sample output has IP obfuscated on first line, lines 2-4 from having MaxMind Country && MaxMind City DBs installed
Calls sudo tee like all the other lines, but also automatically reloads the file.
Optionally you can add
command Wq :execute ':W' | :q
command WQ :Wq
to make quitting easier
Find which directories on your system contain a lot of files.
Edit: much shorter and betterer with -n switch.
If you need to fix a randomly failing test (race condition), you need to run it until you get that hard-to-reproduce failure.
This is a more concise answer to http://blog.commandlinekungfu.com/2011/09/episode-158-old-switcheroo.html in my opinion.
probably just like 1204, but uses tee as a filter (+ I actually understand how this one works)
You can find every file with png extension and redirect its output to file. Later you can find a string inside the file.
extracts path to each md5 checksum file, then, for each path, cd to it, check the md5sum, then cd - to toggle back to the starting directory. greps at the end to remove cd chattering on about the current directory.
Put this command on /etc/rc.local.
Sometimes you might need to have two copies of data that is in tar. You might unpack, and then copy, but if IO is slow, you might lower it by automatically writing it twice (or more times)