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.


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.

Universal configuration monitoring and system of record for IT.
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

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Psst. Open beta.

Wow, didn't really expect you to read this far down. The latest iteration of the site is in open beta. It's a gentle open beta-- not in prime-time just yet. It's being hosted over at UpGuard (link) and you are more than welcome to give it a shot. Couple things:

  • » The open beta is running a copy of the database that will not carry over to the final version. Don't post anything you don't mind losing.
  • » If you wish to use your user account, you will probably need to reset your password.
Your feedback is appreciated via the form on the beta page. Thanks! -Jon & CLFU Team

Append output to the beginning of a file.

Terminal - Append output to the beginning of a file.
command > tmp && cat logfile.txt >> tmp && tmp > logfile.txt && rm tmp
2009-04-05 22:00:32
User: akoumjian
Functions: cat command rm
-2
Append output to the beginning of a file.

Adds the stdout (standard output) to the beginning of logfile.txt. Change "command" to whatever command you like, such as 'ls' or 'date', etc. It does this by adding the output to a temporary file, then adding the previous contents of logfile.txt to the temp file, then copying the new contents back to the logfile.txt and removing the temp file.

Alternatives

There are 2 alternatives - vote for the best!

Terminal - Alternatives

Know a better way?

If you can do better, submit your command here.

What others think

Or:

cat <(command) logfile.txt >tmp && mv tmp logfile.txt
Comment by TheMightyBuzzard 386 weeks and 2 days ago

You can make that even slicker by using the mktemp command.

cat <(command) logfile.txt >$(tmp=$(mktemp blah.XXX) && echo $tmp) && mv $tmp logfile.txt
Comment by isaacs 386 weeks and 2 days ago

I can't think of it now, but there must be some way to to this using 'tac' which is the opposite of cat, meaning it spits out a file's *lines* in reverse order.

Comment by bwoodacre 386 weeks and 2 days ago

Why stop there? How about

( sed -n '1,4p' logfile.txt; some_command; sed -n '4,$p' logfile.txt ) > tmp; mv tmp logfile.txt

This *inserts* the output into the middle! For example, if you had a ChangeLog with some header text, but a last-entry-first list.

Also, I put the commands in a sub-shell () so I could redirect the output of all of them at once.

Comment by flatcap 386 weeks and 2 days ago

Sorry, typo in the above, my sed ranges overlap. They should have been, '1,4p' and '5,$p' for a 4 line header and 'the rest'.

Comment by flatcap 386 weeks and 2 days ago

Thanks flatcap! I didn't know the sed command until now and wish I had found it when I was trying to figure this out. Very slick.

Guess that's why I got a minus 2 rating. This is a harshly judging community.

Comment by akoumjian 386 weeks and 1 day ago

this is my two cents for appending an output on top of an existing file by using sed and awk

command | awk '{system("sed "'NR'"i\""$0"\" -i logfile.txt")}'
Comment by alperyilmaz 386 weeks and 1 day ago

Your point of view

You must be signed in to comment.