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:


but this will not


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


In this case the solution is


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 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.