A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀
[personal profile] sophie
Sometimes you want to paste the output of a grep command into IRC or IM, and don't want each match on a separate line. Fortunately, it's easy to convert it to a comma-separated list instead - simply pipe the output through xargs echo | sed 's/ /, /g'. So, for example, instead of:

Sophie@Sophie-Laptop:~/primtionary$ grep cuddle american-english-insane
cuddle
cuddleable
cuddled
cuddler
cuddlers
cuddles
cuddlesome
scuddle
scuddled
scuddles
upscuddle


...you get:

Sophie@Sophie-Laptop:~/primtionary$ grep cuddle american-english-insane | xargs echo | sed 's/ /, /g'
cuddle, cuddleable, cuddled, cuddler, cuddlers, cuddles, cuddlesome, scuddle, scuddled, scuddles, upscuddle


Very useful sometimes :D
A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀
[personal profile] sophie
Just a quickie - today I learned that instead of piping something through sort and then piping the output of that through uniq, you can just use sort -u, which will do both operations at once.

Unfortunately this doesn't help when you're doing something like sort | uniq -c | sort -n to sort by the number of times a line appears, but it's still a nice tip. :)
A close up of my eye, upside down.
[personal profile] foxfirefey
UNIX tips: Learn 10 good UNIX usage habits, as follows:

  • Make directory trees in a single swipe.
  • Change the path; do not move the archive.
  • Combine your commands with control operators.
  • Quote variables with caution.
  • Use escape sequences to manage long input.
  • Group your commands together in a list.
  • Use xargs outside of find .
  • Know when grep should do the counting -- and when it should step aside.
  • Match certain fields in output, not just lines.
  • Stop piping cats.


I learned a couple of things from this one!
[personal profile] tara_hanoi
Cross-posted from my main blog

I'm a fan of twitter, and one of the features of the territory is shortened links. The twitter web interface generally does a half-decent job of expanding these posts, but I don't always get to see the expanded link.

Sometimes I want to see where the link leads without actually visiting the site in case it's hosting malicious scripts. After a little bit of digging I found that wget can do exactly what I want. I have a little throwaway directory (in my case, '/export/home/sketchy', which allows me to see where the link points to.

For example, let's say I see a link (it leads to a blog post of mine, so nothing very interesting) and want to know where it leads:

tara_hanoi@tara_babel:/export/home/sketchy$ wget --max-redirect=0 http://t.co/8xED8dz
--2011-07-25 16:01:10-- http://t.co/8xED8dz
Resolving t.co... 199.59.148.12
Connecting to t.co|199.59.148.12|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://bit.ly/guxtdh [following]

0 redirections exceeded.


The bit in bold is where you should be paying attention. In this case, the t.co link points to another shortener, bit.ly - if I want to follow that on, I don't have to paste that back into wget, I can just increase the 'max-redirect' parameter:

tara_hanoi@tara_babel:/export/home/sketchy$ wget --max-redirect=1 http://t.co/8xED8dz
--2011-07-25 16:03:50-- http://t.co/8xED8dz
Resolving t.co... 199.59.148.12
Connecting to t.co|199.59.148.12|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://bit.ly/guxtdh [following]
--2011-07-25 16:03:51-- http://bit.ly/guxtdh
Resolving bit.ly... 168.143.172.53, 2001:418:e801::12:1, 2001:418:e801::15:1, ...
Connecting to bit.ly|168.143.172.53|:80... connected.
HTTP request sent, awaiting response... 301 Moved
Location: http://tara-hanoi.dreamwidth.org/1508.html [following]

1 redirections exceeded.


Ok, so it leads to my own DW entry, so I'm pretty sure it's ok.

This might be useful for folks as a quick and dirty way to expand shortened URLs.
SplitMirror
[personal profile] chebe
Hey all, don't think I've seen this here before, and I know there are many ways of doing this, so I thought, in a fit of geekery, that it might be fun to try and collect them all! Afterall everyone does have their own favourite programs.

I posted this at my own journal when I first came across it, but here's a new one as well;

sed -i 's/\r//' $file_name

perl -pi -e 's/\r\n/\n/g' $file_name
Dreamwidth: Home is where the heart is
[personal profile] pixel
Using UbuntuOne to sync dotfiles across several, possibly remote machines. I don't know if this would work with say, Dropbox, because I've never used it but I guess it could be adapted to any of those cloud services if you can access them from the command line, and most importantly make a symbolic link to that directory.

The genesis for this is the fact that I like to use a)taskwarrior and b)my netbook. So I happen to have multiple computers that run Ubuntu and maybe I'm in the minority in that. I wanted my tasks to be available wherever I was. I still haven't figured out how to pull them onto my Android phone but I'll keep working on that.

Taskwarrior uses some text files stored in ~/.task/ by default. I initially attempted to just sync the directory but that won't work. At some point I asked in launchpad I think and never got an answer.

In the end I got it working like so... )
Silouhette of figure, vines, planet in background
[personal profile] brownbetty
I want a command line text editor that can do a soft-word wrap. By "soft," I mean that long lines won't scroll past the right edge of my screen, but when I save my file, I won't discover it's had a mess of line breaks inserted into my text every seventy-odd chars.

I'd be mostly writing in natural English, so emacs or vi are rather more complicated than I'm looking for, but if you tell me one of them is my only solution, I will cry and then suck it up and learn to use whichever.
A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀
[personal profile] sophie
Most people in here will probably know how to pipe output from one command to another:

command 1 | command2

However, what if command2 doesn't allow reading from standard input, and only supports filenames? How can you do this without writing to a file?

It turns out that you can do this in bash and ksh by using the <(command) syntax. For example, the above command can be written:

command2 <(command1)

This will execute command1 in a subshell, and at the same time, call command2 with a file descriptor looking something like /dev/fd/63. When command2 reads from that, it'll get the output of command1.

At first this doesn't seem too useful, but this means that you can do nifty things like this:

diff -u <(sort filea.txt) <(sort fileb.txt)

Which will sort filea.txt and fileb.txt, and then diff the outputs - all without writing a single file.

Note that if the subshells require user input, this isn't going to work, so you can't use this to capture user input and pass it to a script which would otherwise require a filename. However, as long as this isn't the case, everything should work smoothly.

[edit: Oh, and I should mention that, unlike piping, you can execute several commands in the subshell. For example:

rev <(echo wheeness; sleep 2; echo blarg)

This also demonstrates how both the subshell and the main process run simultaneously; the output is "sseneehw", followed by a delay of 2 seconds, followed by "gralb".]

[edit 2: See this comment for an example of how to run multiple commands via piping!]
A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀
[personal profile] sophie
If you've ever tried using xargs with a list of filenames, you've probably at some point come across errors like these:

xargs: unmatched single quote; by default quotes are special to xargs unless you use the -0 option

ls: cannot access /home/sophie/directory: No such file or directory
ls: cannot access with: No such file or directory
ls: cannot access spaces: No such file or directory
ls: cannot access in/blah.txt: No such file or directory


Both of these errors are due to xargs by default interpreting some characters as special; in the first one, it won't allow apostrophes, and in the second it treats spaces as if they meant separate arguments. And the -0 switch doesn't help at all if you're using newlines.

Both of these errors can be resolved very easily: just add the -d"\n" switch before the command you want xargs to execute. This tells xargs that you don't want xargs to mess with your input at all except to treat newline characters as delimiters. This time, you should find that both apostrophes and spaces are accepted properly.
Stylized face based on Dreamwidth logo
[personal profile] karmag

Hey, a command line community? That's a pretty neat thing to have.

So to introduce myself, here's something I figured out pretty recently. It's in one of those eyes-glazing-over parts of the shell man page (but I had a specific problem to solve so I had to figure it out). Behold! It is a piece of "parameter expansion" magic:

$ TEST='foo:bar' ; echo ${TEST%:*} ${TEST#*:}
foo bar

Ooh, exciting! No wait, it's not. But here's the problem I had: How can I loop over pairs of values in a script or one-liner?

The solution I came up with looked something like this:

$ for f in url1:filename1 url2:filename2 url3:filename3;
do URL=${f%:*}; FILENAME=${f#*:} ;
...
; done

...and so for each pair, URL and FILENAME gets set to their respective portion. (And you'll be glad to learn that I got my Youtube-mediated crime drama fix in the end.)

gummy
[personal profile] razputnik
I noticed a few people making posts about editing their shell history with vi - so I thought I'd contribute this little trick that someone may or may not have mentioned already:

If you add "set -o vi" to the end of your .bashrc, it changes your command line editing mode to vi keybindings, meaning you can use hjkl to navigate through your history and edit lines using vi oldies but goldies like cw and c$. It's a bit of a pain to get used to at first, but it's good if you're the type who keeps typing cw when you want to change an argument and then get annoyed when it just inserts that as text.

:wq
Cartooney crab holding drink
[personal profile] pauamma
Context was someone asking why
print "Hello, world!\n" && die;
doesn't output anything. Someone replied with a nifty trick to help figure this out:
perl -MO=Deparse -e 'print "Hello, world!\n" && die;'
or
perl -MO=Deparse,-p -e 'print "Hello, world!\n" && die;'
(go ahead and try it).

Note: -p won't make a difference here, but it helps if you suspect perl's notion of operator priorities is different from yours.

(crossposted from http://dw-dev-training.dreamwidth.org/23661.html at pne's suggestion)
Senora Sabasa Garcia, by Goya
[personal profile] jadelennox
I've been a tcsh user now for... well, for a very long time, and a csh user before that. Understandably, that means that I'm at least a low-level tcsh power user. However, because I do anything that requires a complex control flow in bash, I've been thinking about switching to bash for my everyday commandline needs.

But it's scary, because I don't want to switch out of a shell where I am a power user. Does anybody have any links to good resources for switching from tcsh to bash? Handholding, tips, or the like?
A cartoon-like representation of a girl standing on a hill, with brown hair, blue eyes, a flowery top, and blue skirt. ☀
[personal profile] sophie
A quick tip for bash users that I use all the time:

If you have a command in your history that you want to re-execute or edit, but don't want to keep tapping the up key, hit Ctrl-R and then start typing part of the line. bash will find the last line matching your input on the fly. If it's not the one you want, either keep typing until it is, or hit Ctrl-R and bash will show you the next match.

Once you've got it, you have two options: to execute, you can then just press RETURN; to edit, use a movement key (left/right arrows, Home/End, etc) and bash will drop you back to a standard command line with the line you just found.

This trick also works if you know the middle of the line but not the beginning, since bash searches the entire line.

[edited to add: BTW, Fey, do you realise that nobody can add new tags to posts in this community? The only tags in here are ones first used by you. :)]
Adam Lambert looking up with a smile.
[personal profile] pixel
This is one I've been on the lookout for.
Pianobar plays your pandora.com stations from the command line. BYE BYE FLASH :D

There are some vague directions for various flavors of linux on the home page.
Here's how I installed on Ubuntu 10.4 )
Screenshot of pianobar running, lots of options... )
Tim gazes upon Dick's manly chest.  "Wow!"
[personal profile] brownbetty
[personal profile] pixel's post below reminded me that I have meant to post this for a while: Screen! Screen seems sort of like the secret weapon of command-line users; somehow it takes one ages to discover it, and if one is not far enough along in one's command-line usage, it just ends up seeming sort of mean-spirited and baffling. It's documented, but it's such a swiss-army knife that it's easy to get lost in the maze of documentation. However, at a certain level of usage, it suddenly becomes the most useful program ever.

So I wanted to ask people their favourite things to do with screen, and to share their .screenrc files, where they've modified them in interesting ways.

My favourite thing to do with screen )

from my .screenrc )
txt: talknerdy
[personal profile] pixel
Three very nifty tools come together to make my (and your!) life easier. Or use them separately, it's all the same to me...

Guake is a 'dropdown' terminal in the style of Quake chat (for GNOME.) I'm not even a gamer and I love it. It's always there, ready for me with one keystroke. I tried Tilda as well but Tilda kept eating screen so that was a no-go. There are similar programs if you're running KDE but I'm not sure about XFCE.

There are tabs available in Guake itself but I can't make it NOT look clunky, so I buckled down and started re-learning screen. If you're a command line fan, give it a try, so many things available at a keystroke. The real magic is that I can disconnect and reconnect to screens on a whim. For example I cribbed and modified a script that launches a bunch of useful programs for RoR development in screen, so I have one project running in an instance of screen. I could launch another project in another instance of screen and flip back and forth. I could open up another terminal app and reconnect to my session there. I could go downstairs with my netbook, ssh in and reconnect to my screen there.

Finally the newest shiniest kid on the block: Taskwarrior (task) a super easy to use, flexible, feature rich command line todo list. This is the todo list I've been waiting for my whole life. See a basic demo on youtube. That's an older version (1.0, latest is 1.9.2) so there are a bunch more new features in more recent versions and it's still in active development.

So on any given day, I've got a screen session open for taskwarrior connected in Guake. My todo list is right there, and I can get to it anywhere with my netbook. I'm playing with getting tasks to show up on conky, if anyone is interested in that, I can let you know what I come up with. Now to tweak it and make it available off my home network....
pic#533217
[personal profile] sigflup
Soon to be released, my terminal-emulator called "Hack The Planet TERM". It's coming real close to becoming beta and I'll release it in the next Uber Leet Hacker Force RADIO show. Here's a video of it:

youtube link

If you're like me and don't use flash I suggest using the command-line youtube downloader, "youtube-dl"

This video was shot some while ago. While I don't have access to a camera now I can at least show you a screen-shot of what it currently looks like:



I'm currently looking for testers. I'm developing on openbsd and have a 32-bit ia linux lappy here. If anyone has a 64-bit machine and/or freebsd machines email pantsbutt@gmail.com and I'll give you the alpha if you help me getting it to work properly on your system.
tony baldwin
[personal profile] tonybaldwin
I often have to quick math on the fly while generating estimates for clients and doing other off the cuff calculations.
Previously, while using openbox or fluxbox, I had a keybinding bring up a calculator and did the math and then killed the calculator with ctrl+q.
But, I’m all about efficiency, and lately have been learning more and more of the powerful tools in bash to do various things, from navigating the file system to handling files and manipulating text. In wmii, I always have at least one bash terminal open (my preferred terminal emulator currently being roxterm).
So, I figured there had to be an efficient means of doing math without bringing up a gui calculator, too, but bash doesn’t like floating point numbers so well.
Now, with expr or echo or let one can do some basic math (ie. expr 220+34, or echo $((220+34))), but not with floating point numbers (with decimal points), which I need.
But bc can do it. One would have to type in something like:
echo ‘5467 * 0.09′ | bc
or
bc -l <<< 5467*0.09

to get the result....
Not really quick-n-dirty...
So, I scripted it:

#!/bin/bash
# do math with bc
echo “Enter your equation:”
read e
echo “The result is:”
bc -l <<< $e

I called the script ‘M’ (for ‘Math’), and stuck it in /usr/local/bin.
Now, I just type
$ M
and I see:

Enter your equation:
(enter equation here)
The result is:
(result appears)
$
all done.
I type 1 letter (two keys, shift+m), and my equation.

tony@deathstar:~$ M
Enter your equation:
3452*0.09
The result is:
310.68
tony@deathstar:~$

./tony


posted with Xpostulate
Page generated Jan. 28th, 2012 11:25 pm
Powered by Dreamwidth Studios