Wednesday, 30 January 2013

Copy and rename multiple files

What do you do on Linux when you want to rename, copy or move many files together in the same way, e.g. replacing all files named "donkey01.jpg" as "horse01.jpg" ? There is plenty of GUI utilities, but sometimes a quick and cheap shell hack is needed. Here you go: the magics of sed and shell scripting in a one liner.

for i in `ls *`; do cp $i `echo $i | sed "s/search/replace/g"`; done

From here.

Tuesday, 27 November 2012

Killing all processes that match a given string

If you want to kill all processes matching a given executable, you can easily use killall:

killall -9 my_executable

But what if you don't want to kill processes based on an executable name, but rather on some argument name, or something else? That is, all processes that match some string?

You can. Here is the trick:

kill -9 $(ps aux | grep -v grep | grep my_process | awk '{print $2}') 

(originally from here)

Friday, 12 October 2012

Using wget to get multiple files from http servers

The Unix wget utility support globbing, but only when pointed to Unix FTP servers. So this could work:

wget ftp://something.com/*.jpg

but this will not

wget http://something.com/*.jpg

What do you do when you want to do this for an http server? A common case is where you have numbers for example, like

http://something.com/0.jpg
http://something.com/1.jpg
http://something.com/2.jpg

etc.

In this case the solution is

wget http://something.com/{0,1,2,3,4,5,6,7,8,9}.jpg

In this case the command will be repeated for all numbers and you get all 10 images in one go.

Monday, 14 May 2012

Search your installed Debian packages by repository

If (like me in this exact moment) you have a mess of a Debian (Ubuntu, Mint) system, and you want to know what packages you have installed from what branch / origin (stable, testing, unstable, experimental, backports) you can use Aptitude for that:


aptitude search '?narrow(~i, ~Abackports)'
 
Substitute to "backports" the repo you want to list, and you will obtain a fancy package list.

The ?narrow option tells aptitude to list only the installed packages. If you remove it, you'll get all packages you can install from that branch.

Another way is to install apt-show-versions and then use:


apt-show-versions -b | grep experimental


(again, substitute to "experimental" the branch you're interested in)

Thursday, 11 August 2011

Recursive find

Sometimes you don't have locate , or you want just to find recursively below your current directory. Here is a handy alias:

alias fn='find . -name':
Example usage: $ fn "page*.htm"

Tip from Joe Grossberg.

Sunday, 10 April 2011

Encode video FLV to AVI with one line

Just found on the Ubuntu forums a way to convert FLVs to AVI with a single shell line, using mencoder:

for file in *.flv; do mencoder -oac mp3lame -ovc lavc "$file" -o "`echo $file | sed -e 's/\.flv$/\.avi/'`"; done

Source is this thread.
However with gnome-mplayer it seems I can't always navigate the resulting AVI.

Friday, 18 February 2011

OCR of scanned PDFs in Linux

It seems there is still no quick-and-ready solution, but found a few interesting scripts.

This script based on Tesseract worked well for me. It requires to have Tesseract and ghostscript installed, and returns a number of ASCII text files from the PDF. Given that the OCR engine is the same used by Google, you can be assured it works pretty well.

A bit less comfy solution can be found on this Linux.com article, with some shell script based on Tesseract as well.

Another solution using other engines.

It seems also there is a potentially elegant GUI solution by means of OCRFeeder, but I still haven't tried it. I'll let you know how it works, for now I just bookmark these links.

Tuesday, 8 February 2011

Install True Type fonts on Ubuntu in three steps

It's very easy:

1)Create a .fonts subdirectory in your home directory
cd ~
mkdir .fonts

2)Move the .ttf file in the .fonts directory
mv myfont.ttf ~/.fonts

3)Refresh the font cache
sudo fc-cache -f -v

And your font should be ready to be used.
Credits to Detector Pro -I streamlined the process a bit, being comfortable with the command line.

Wednesday, 26 January 2011

How to symlink all files in a directory

It seems that a simple ln -s /dir/* doesn't work, so:

for f in $(ls -d /base/*); do ln -s $f /target; done && ls -al /target



From commandlinefu.com

Saturday, 8 May 2010

udevinfo and udevadm

If you have ever tried to write UDEV rules , say, to get your external hard drive to mount at your preferred custom mount point, you will have found necessary to use the little utility udevinfo.

Recently this utility has been superseded by udevadm. However there is a way to get the old udevinfo output back with udevadm:

udevadm info -a -p $(udevadm info -q path -n /dev/your-device)

and you will have the same output you would have had with udevinfo. Very useful! Credits to Unlikely Source for the hack.

Sunday, 25 May 2008

Where is my GRUB?

On the Gentoo mailing list, a nice thread evolved a way to find where did you install your bootloader last time. The question is less trivial than it seems - is it on your disk MBR or on your partition? If you have a non-obvious setup, you may forget... and if you try to upgrade/reinstall GRUB without remembering where it is, bad things can happen.

The solution is:
for i in /dev/[hs]d*
dd if=$i bs=446 count=1 2>/dev/null | strings | grep -q GRUB \
&& echo "GRUB found in $i"

Saturday, 24 May 2008

The hacker's paperclip.

On Slashdot, I found a comment giving little, good tips on how to solve annoying hardware problems with a paperclip.

The power supply tester
Creation: Unbend a paperclip, and then bend it into a big U shape.

Usage: When you are unsure whether a PSU works (a) disconnect it from anything it is connected to (b) insert one leg of the U into the hole in the 20/24 pin motherboard power plug for the green wire (c) insert the other leg into a hole for a black wire (d) plug the PSU into power and turn it on.
If the fans spin up, then the PSU at least partially works. At this point you can use a multimeter to verify the voltages of the different rails with no load.

The CD ejector
Creation: Straighten a thick-gauge (strong) paperclip, and then put a loop in one end that is big enough to put your index finger through, at least to the first knuckle (this helps with gripping it during use).

Usage: When you need to eject a CD from a powered-down computer (laptop OR desktop), push the paperclip into the emergency eject hole. On a laptop, this requires very little force, but on a standard (5.25") Desktop CD-ROM drive, this will take quite a bit of effort.

The multipurpose grabber
Creation: Straighten a regular paperclip, and put a loop on the end, as you did for the CD Ejector. On the other end, put a 90 degree bend, 2mm from the tip.

Usage: You can use this tool to remove or move jumpers (very handy for IDE hard drives), and to remove stuck floppy disks from floppy drives (use the R/W hole or 1.44MB hole as an anchor point).
Credit must be due to beav007.

Monday, 12 May 2008

EPS files and pdflatex

There is this odd quirk in LaTeX. The latex executable compiles your .tex files in the old-fashioned DVI format. As such, it accepts by default only .eps (Encapsulated PostScript) images. pdflatex compiles your .tex files in the standard PDF format. For some mysterious quirk, pdflatex accepts raster formats like .png and .jpg , but does not accept .eps!

Sometimes you want the best of both worlds. An undergraduate of my lab, after some googling, found you can force pdflatex to insert .eps files happily:

1)Install texlive-extra packages, or any other package containing the epstopdf utility.

2) Insert the following code in your .tex file:

\newif\ifpdf
\ifx\pdfoutput\undefined
\pdffalse
\else
\pdfoutput=1
\pdftrue
\fi
\ifpdf
\usepackage{graphicx}
\usepackage{epstopdf}
\DeclareGraphicsRule{.eps}{pdf}{.pdf}{`epstopdf #1}
\pdfcompresslevel=9
\else
\usepackage{graphicx}
\fi

3) Compile using pdflatex with the -shell-escape command line option

It seems to work.

Sunday, 11 May 2008

Templates for CD jewel case cover in SVG - free

A quick useful link. If you need to do a CD jewel case cover and if you, like me, are an Inkscape addict, bookmark this link. These are beautiful templates for doing CD/DVD covers in SVG (Scalable Vector Graphics) format, free to download. Thanks to Kevin Deldycke for releasing them!

Saturday, 10 May 2008

Create a .tar.gz archive

Ok, this is admittedly really noobish. But it's something I always forget. While the tar -xzvf spell for unpacking archives is well known, only seldomly I need to create archives.

However, here's the magic:

tar -pczf name_of_archive.tar.gz /path/to/directory

That's more a reminder for me...

Thursday, 1 May 2008

Find and delete broken symlinks

The Gentoo guide to switch from tetex to texlive contains a little command line gem. It allows you to look for broken/dangling symbolic links and delete them interactively:


find /usr/bin -type l ! -xtype f ! -xtype d -ok rm -f {} \;

Friday, 29 February 2008

The trouble with Facebook

It's only a couple of weeks I joined the Facebook network, and even if I find it handy, I certainly cannot be said to be a Facebook addicted and/or fanboy. To me, Facebook it's just a Myspace done right: while Myspace is a slow, disorganized, nearly unusable mess, Facebook has a clear, elegant interface and it is not prone to the horrendous, Flash-ridden bloating of most Myspace pages. Apart from that, Facebook is neither good nor wrong in itself: it is just a social networking tool, with defects and advantages.



I found only today an article of this January on The Guardian about Facebook, pointed out by a friend. No matter how you can hate social networking sites or Facebook itself, the article is a startling example of the inability of a journalist to understand Internet, social networking and the implications behind that.



Mr.Tom Hodgkinson, for a start, just cannot grasp the concept of social networking (or of instant messaging, or email, or the phone, for his arguments apply to nearly every communication protocol you can use to chat with friends). Its article starts like that:Why on God's earth would I need a computer to connect with the people around me? Why should my relationships be mediated through the imagination of a bunch of supergeeks in California? What was wrong with the pub?



If it was for people like Hodgkinson, the Internet should not exist: what was wrong with the library? Here's what's wrong with the pub, mr.Hodgkinson. You can't reach the pub with your friends if your friends live on another city, or continent. I almost have no Facebook contact from my hometown in Italy. I have Facebook contacts of close friends that now work in other Italian cities, in Japan, the United States, Belgium or Holland. Try call all of them in the "pub".



Problem is, Mr.Hodgkinson does not simply misunderstand Facebook. He simply doesn't understand the Net. Here's how he continues: And does Facebook really connect people? Doesn't it rather disconnect us, since instead of doing something enjoyable such as talking and eating and dancing and drinking with my friends, I am merely sending them little ungrammatical notes and amusing photos in cyberspace, while chained to my desk? A friend of mine recently told me that he had spent a Saturday night at home alone on Facebook, drinking at his desk. What a gloomy image. Far from connecting us, Facebook actually isolates us at our workstations.



Mr.Hodgkinson: if you don't like communicating with friends with the Internet, well, don't use that. But telling that "it disconnects us" is so amazingly self-contradictory I had to read that a bunch of times before being sure. Connecting with people, at least for me, is primarily talking with them, exchanging ideas, news and good laughs. You can do it in real life, of course, but there's no reason you can't do that online.


I use to compare it with music. Surely, live music is good, and is a different experience from music on CD. But saying that you're disconnected from people because you are not in physical proximity with them is almost the same to say that recorded music is not "real" music, because you're not actually a few meters apart from a guitarist.



This is without delving into the fact that meeting people on the Net often means to build close relationships in real life too. I had and have real life relationships (even romantic ones) that started on the Internet. I went "eating,dancing and drinking" with these people, and spent unforgettable moments with them, but I wouldn't have never been able to do it without the Internet. I wouldn't ever have known them. Mr.Hodgkinson, how many social occasions have you lost by going everyday to the same pub with the same friends, instead of knowing new ones on the Internet?



And finally, we don't always want to go outside eating,dancing and drinking. I often prefer to stay at home, drink some tea, and chat with close friends in the warmth of my room, with some good music. So I can relax, I don't have to think about coming back home, weather, and the like, I can do other little stuff in the meantime, and keeping contacts with my friends. Try do that without Internet.



Hodgkinson continues: If I put up a flattering picture of myself with a list of my favourite things, I can construct an artificial representation of who I am in order to get sex or approval.. How clever, Tom! But do you need Internet to do that? Can't I just, ehm, cheat in real life too?



And now, here's a pearl: It also encourages a disturbing competitivness around friendship: it seems that with friends today, quality counts for nothing and quantity is king. The more friends you have, the better you are. You are "popular", in the sense much loved in American high schools. So, basically, what Tom is telling us is that Facebook is bad, just because kids on it behave just like they behave in their high school. I agree that the "quantity over quality" thing is superficial and stupid. But, sadly, that's not Facebook (or Myspace, which is infinitely worse in this sense) fault. It's just fault of people using it. It's people using Facebook that have that sense of popularity. Facebook doesn't push you to add kilotons of friends: in fact, on Facebook you usually add people you already know in real life.



In the second part, Hodgkinson goes on a long piece on the personalities behind Facebook. He takes on Peter Thiel, showing him as a ruthless, activist, libertarian neocon. Now, there are a few political movements I despise as much as neoconservatism. Really. Give me Nazism everyday instead of neoconservatism. Yet the fact I disagree with the political campaigns of Facebook stakeholders doesn't make me disagree with Facebook. Facebook is a tool. It works. It may have been done by neocons, Islamic fundamentalists, evil clowns or alien abductors, ok, but: who cares? Tom Hodgkinson not only cares (I suppose he asks the supermarket guys what do they vote, to be sure his money doesn't go in the pockets of people he wouldn't like), but he goes forward, by connecting ties and saying that, behind Facebook, there is the BIG EVIL PLAN: Facebook is a deliberate experiment in global manipulation.



Wohoo. Now, I agree that surely there is a lucrative social networking industry, and that this industry can manipulate you. Just like every other media, like radio or television broadcast, or newspapers and magazines, or advertising, can do. But frankly, Facebook seems the lesser evil in this case. Facebook doesn't bombard me with propaganda or biased information (like TV and magazines routinely do). Facebook doesn't ask me actively to buy stuff. Facebook doesn't incite hatred (quite the contrary). If there is an evil message being slitherily conveyed by mean of Facebook networks, well, let me know.



The article then fires against target advertising. Here Hodgkinson approach to the Net is just unexplicable. It tells you basic, obvious facts, but with an overtone like "DON'T YOU SEE HOW EVIL IT IS!!!1!". It basically boils down on this: Facebook can target advertising with far greater precision than a newspaper. Admit on Facebook that your favourite film is This Is Spinal Tap, and when a Spinal Tap-esque movie comes out, you can be sure that they'll be sending ads your way..



I don't know for sure, but what Tom is saying looks suspiciously like: "OMG! Advertising! And advertising that is currently being targeted at my interests! So I could find myself in the cruel, painful situation I actually find something I like, instead of happily wasting time with ads I would never care about. How bad, a win-win situation! And while we're at it, we don't even mention that it is Google that actually made it mainstream, when Facebook still didn't exist."



And here's the end, enlightening indeed: Why would I want to waste my time on Facebook when I still haven't read Keats' Endymion? And when there are seeds to be sown in my own back yard? I don't want to retreat from nature, I want to reconnect with it. Damn air-conditioning! And if I want to connect with the people around me, I will revert to an old piece of technology. It's free, it's easy and it delivers a uniquely individual experience in sharing information: it's called talking.



I'm all for reading books (and I admittedly should have done it, instead of replying to this pile of bullshit). I understand a bit less the purpose of seeding random things in the backyard and avoiding air-conditioning (try that in the Mediterranean summer), but oh well, anyone has his own little perversions. About talking-only communication protocols, sure, it's cool. If you still feel that the world is something that doesn't exceed the boundaries of your backyard, of course.

Saturday, 29 December 2007

Copying whole partitions preserving hidden files and attributes

How do you easily and coherently copy a whole large and complex directory, preserving attribution, hidden files etc? Good old cp -a could not be enough in this case (hidden files would be skipped), and maybe you don't want to use dd (e.g. you could like to move a ReiserFS partition to an ext3 partition). The trick is hidden in the Debian tips , but can be applied to any Linux (and probably Unix) distribution. Since I got crazy yesterday trying to find it again, after using it to move partitions from my old hard drive to a new one, I repost it here:
find . -depth -print0 | cpio --null --sparse -pvd /new/directory
That's it. A 1:1 copy of your filesystem hierarchy is deposited in the new directory. It saved my day.

Tuesday, 11 December 2007

Removal of Ogg Vorbis and Theora from HTML5

This blog post made some noise recently.

Let's see. HTML 5 is the next revision of the old and beloved HTML. Such a spec is still under development and, among other things, should specify (or at least suggest) what the standard media formats of the Web should be. HTML5 in fact specifies a

Thursday, 1 November 2007

A Linux user fights Mac OS X Leopard. Days 2-4

In the previous post, I wrote about my very first (3 hours) impressions on using Mac OS X, comparing it with the operating system I mostly used before, that is Linux.

Some day has passed, I used Leopard a bit more, and now it's time for some new random thought.

Of dock and stacks. I begin to have problems with the dock. I figured out that nothing like an application menu is easily available on this thing. I tried to "emulate" that by using stacks, but this does not really work. Stacks are an OS X Leopard innovation. Basically, if you drop a folder on the dock, this will show up as single icon. Clicking on it , you see the content of the folder, stacked. See here for example.

There are several problems with stacks. First, the stack icon is basically a superimposition of individual icons of things in the stack. A very bad idea. For example, my Documents folder is represented in the dock by a .zip dull icon, because the first file OS X sees is a zip file. Of course, I cannot customize the icon appearance. Nothing in that icon tells me it's my Documents folder.

Compare it with KDE standard behaviour, where you can place additional menus at will on your panel. Kubuntu uses it to have a standard "Places" menu where you have links to your home folder, the network computers, storage media and other users folder. No Finder clicking needed. See this screenshot for example. Also XFCE implements that. In this, for usability, Linux desktop environments beat OS X hands down.

So, to have an application stack, you have to put your applications in a given folder, then drag this folder on the dock, and you'll find yourself with a crippled menu-like thing that doesn't allow for menu hierarchy, with incomprehensible icons. Don't like that, and the neat visual effects make me cry even more: instead of an awesome-looking menu-like thing, we have a crippled thing that just looks awesome.

The Dock seems also subtly buggy ; it lost application links on me at least a couple of times. Hope it gets fixed soon.

I like the fact that an active application is evidenced on the dock, instead of taking space of an application bar. It seems a bit inconsistent from time to time (now Firefox for example brought another icon of itself on the dock, with a bright spot underneath, instead of adding the spot on the icon I launched it from), but maybe it's me not understanding its logic.

Finder. It seems me and this thing cannot be friends. It's such a retarded file manager that makes Windows Explorer look cool. Tree view is available, but why can only you switch from tree view to icon view to the three-panes view? In Konqueror I have available a tree view of my folders on the left, like the Bookmarks panel of a browser, and then the main icon view of the currently selected folder. So you can have nice previews and you can easily navigate the tree, all in once. Windows too, if I remember correctly, allows this.

If there is a free, decent OS X file manager let me know.

Open source applications. I mostly use open source software, and it is of course repackaged for Mac OS X too. Sadly, OS X looks a bit of a neglected platform for open source, which is very strange given its Unix foundations. I installed Firefox, Thunderbird, VLC, FreeCiv, Celestia, Inkscape, NeoOffice, aMSN and Gimp. Mixed results. Firefox and Thunderbird seem to work perfectly. Java and Flash support is installed by default, which is a good good thing. aMSN also integrates nicely, and is visually much better on OS X than on Linux.

GTK applications, like GIMP and Inkscape, are another story. They work fine, provided the X server is on, but using a GTK app on OS X is tiring. I am accustomed to mix and match look-n-feels on Linux, so aestethics is not the problem. The problem is the mixmatch of two paradigms: the "menu-bar-always-on-top" of native OS X and the "menu-bar-is-in-the-window" of almost all other operating systems. Mixing widget appearance is fine; mixing usability paradigms is an hell.

I think Apple should give the users the possibility to configure OS X so that the menu follows the application. I don't know if it does; it can be some advanced setting, but it has to be made easy. OS X forces its paradigms on the user and applications, disregarding anything away from this paradigm. It would also be nice if other operating systems can follow the OS X paradigm at user will, but the default is not the OS X one, so it's Apple that has do to that first. Good to think different, bad to force everyone to think like you.

I noticed also that OS X packages are left behind in terms of stability and being up to date. Celestia and GIMP crashed on me quite often -GIMP regularly when I try to draw. The GIMP binary is a 2.4 release candidate -no 2.4 final binary is available. Freeciv 2.1.0 is unavailable for OS X, and 2.0.9 doesn't start at all -it shows the initial screen, then silently dies. OpenOffice is unavailable unless in the reincarnation NeoOffice, which is behind at version 2.2. On Linux, most of these packages are rock solid and up to date. I will have to try MacPorts soon, that probably can improve the situation, but it's not an easy install and it's OK only for power users.

Don't know who's in fault here, but the situation could improve.

iTunes. I wanted to play an mp3 folder from a DVD-r. So I inserted the disk and dragged the folder on iTunes. That bitch, instead of simply playing, copied all the mp3s in the Music folder, reorganized them on her way, and only after begun playing. Didn't. Ask. Me. Anything. This is an insult -I never asked you to copy a file, let alone to copy in a location I didn't told you and to reorganize them as you (not me) find pleasurable!! It's incredible such a totalitarian approach is default behaviour. iTunes should at the very least have asked if I wanted to copy-and-reorganize folders, or not. I didn't touch it anymore. I hope someone can point me to a better music player.

Terminal. Didn't use it a lot, but it's OK. Its default appearance is terrible, but it can be customized and has true ARGB transparency. It also has tabs, which are a really useful thing. It seems thing are not color-coded as much as in Linux, which is a bit sad, but not essential.

Spaces. Spaces is Mac OS X Leopard implementation of virtual desktops. Virtual desktops are something I simply cannot live without. Unix had it from decades, and I'm extremly happy to see them implemented by default and easily activable. Only little drawback is they lack a clickable preview on the menu bar or the dock. Virtual desktops lose a bit of usability if you can't see what's in them. Again, KDE (but also GNOME) here lead the pack.

Network management. As far as I can see, wireless and DHCP connections Just Work. Good.

Suspend. I still haven't understood what happens when I close the lid. Does it go to suspend-to-ram or suspend-to-disk? Can I choose? Can I freely bring the laptop with me without shutting it down? Probably it's just me not being accustomed to laptops, but OS X should give me a better control on this. However it works seamlessly, something that I doubt is on Linux.

Login screen. I haven't figured out a way to change the login screen background. Also, when choosing the picture login avatar, OS X doesn't rescale it if it's larger than the default, but allows only you to zoom it in. A minor detail, however.

General impressions. This review was much on the negative side, but it's a simple selection effect. What I had to say in detail was on the negative. There is a positive side, of course. OS X is a usable and decent workplace, and beats standard Windows most of time. Aestethically, it's really pleasant and day to day work can be done. The problem for me is that there is not much to be enthusiast about. If the OS X interface was one of the Linux desktop managers, it would probably be considered cool but not really superior.

What really I dislike of OS X is its "totalitarian" approach. Do you want a different skin for the window manager, an application menu, a different menu layout? Too bad, you can't. If you like the OS X way, all is fine. If you don't, Apple simply does not care. I know that third-party applications can overcome most of these limitation, but it's simply wrong that I have to look for third party, unsupported apps to do things that every other OS allows me to do by default.

Linux surely is still behind in terms of overall usability. Hardware support has incredibly improved but it's still flaky here and there, there are inconsistencies, things are not as perfectly polished as they should be. And Linux must be installed by hand, thus putting users in the realm of the unknown. However, from the interface productivity and usability point of view, Apple in my opinion has no more the high ground. They are more or less equal; strenghts and weaknesses being differently distributed but quantitatively not much different. It seems to me that now Apple has as much to learn from open source desktops as open source desktops learned from it.