Archive

Archive for the ‘bash’ Category

Ubuntu distro info

April 24, 2013 Leave a comment

Today I found an Ubuntu-specific command by chance called “ubuntu-distro-info“. Example:

$ ubuntu-distro-info -af
Ubuntu 4.10 "Warty Warthog"
Ubuntu 5.04 "Hoary Hedgehog"
Ubuntu 5.10 "Breezy Badger"
Ubuntu 6.06 LTS "Dapper Drake"
Ubuntu 6.10 "Edgy Eft"
Ubuntu 7.04 "Feisty Fawn"
Ubuntu 7.10 "Gutsy Gibbon"
Ubuntu 8.04 LTS "Hardy Heron"
Ubuntu 8.10 "Intrepid Ibex"
Ubuntu 9.04 "Jaunty Jackalope"
Ubuntu 9.10 "Karmic Koala"
Ubuntu 10.04 LTS "Lucid Lynx"
Ubuntu 10.10 "Maverick Meerkat"
Ubuntu 11.04 "Natty Narwhal"
Ubuntu 11.10 "Oneiric Ocelot"
Ubuntu 12.04 LTS "Precise Pangolin"
Ubuntu 12.10 "Quantal Quetzal"
Ubuntu 13.04 "Raring Ringtail"

This requires the “distro-info” package to be installed.

Update (20130426)
If you want info about your current distro version, use this instead:

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 13.04
Release:        13.04
Codename:       raring
Categories: bash, ubuntu Tags: ,

cpq and mvq: extensions of the Unix commands cp and mv to use a queue

March 28, 2013 Leave a comment

Problem
You browse your hard drive(s) and you want to copy/move several files to somewhere else. If the files are big (e.g. movies), then you need to wait minutes until a copy/move operation finishes. If the files are in different folders, you cannot select them all. If you try to launch each operation in the background, your machine will get very slow since all these operations will run concurrently.

Clearly, copy/move operations should be put in a queue and the next operation should start when the previous one terminates.

Solution
Under Windows you have Total Commander (F5/F6, then F2), but how to do it under Linux? Should I use Total Commander with wine? :) Under Linux I use Midnight Commander but in MC I didn’t find this feature. There is another Norton Commander clone called Krusader that has this feature but I didn’t like it much. I prefer MC :)

So, I came up with the following solution: create two command-line scripts called “cpq” and “mvq” that work like cp and mv (actually they call cp and mv) but they put tasks in a queue. These scripts launch a daemon process if necessary that executes each copy/move operation one after the other.

The project can be found here (https://github.com/jabbalaci/Copy-Queue) where you can also find a description about its installation.

Update (20130403)
Pierre (alias Deimos) wrote about “cpq” and “mvq” in his blog in French: Copy-Queue : un manager de copie de fichiers en ligne de commande.

Categories: bash Tags: , , , , ,

number

March 27, 2013 Leave a comment

Today I found an interesting Unix command by accident. It converts numbers to English:

$ number 961428808
nine hundred sixty-one million.
four hundred twenty-eight thousand.
eight hundred eight.

It works with much bigger numbers too.

Categories: bash Tags:

Set difference between two files

March 8, 2013 Leave a comment

Problem
You have two text files, short.txt and long.txt, and long.txt contains some extra lines. Which are these lines? That is, you want to perform a set difference: L \ S.

Solution
You can do this with the “comm” command:

First, make sure that the two files are sorted. Then:

comm -23 long_sorted.txt short_sorted.txt

Example

$ cat as.txt 
sl-1215
sl-2112
sl-9023
sl-9029
$ 
$ cat bs.txt 
sl-1215
sl-2112
sl-9012
sl-9016
sl-9023
sl-9029
$ 
$ comm -23 bs.txt as.txt 
sl-9012
sl-9016
$

Find more examples on “commhere (like union, etc.)

.xsession-errors can grow HUGE

January 31, 2013 1 comment

Problem
Something funny happened to me today. I logged in to my desktop machine at my workplace when a warning message told me that “disk space is low”. A quick “df -h” indicated that it’s true, my HDD is at 100%! How is it possible, I should occupy no more than 30%. After hunting for 10 minutes for the guilty file/directory, it turned out that the file .xsession-errors in my HOME directory grew more than 200GB! WTF?

Solution
I removed this file and under the name .xsession-errors I put a link on /dev/null. However, after a reboot the symbolic link was gone and .xsession-errors became a regular file again. Tricky :) So I added the following lines to the end of my .bashrc file:

# .xsession-errors can grow huge... remove it
if [ ! -h $HOME/.xsession-errors ]
then
    /bin/rm $HOME/.xsession-errors
    ln -s /dev/null $HOME/.xsession-errors
fi

if [ ! -h $HOME/.xsession-errors.old ]
then
    /bin/rm $HOME/.xsession-errors.old
    ln -s /dev/null $HOME/.xsession-errors.old
fi

It verifies if .xsession-errors is a symbolic link. If not, remove it and replace it with a symbolic link.

Lock and unlock the screen from command line

January 30, 2013 Leave a comment

Question
Is there a way to lock/unlock the screen from a script?

Answer
The answer is “yes”. Lock the screen:

gnome-screensaver-command -l

Unlock the screen:

gnome-screensaver-command -d

Demo (lock the screen, then unlock it in 5 seconds):

gnome-screensaver-command -l && sleep 5 && gnome-screensaver-command -d

Tip from here.

VLC gets stuck

January 29, 2013 Leave a comment

Problem
On my Linux box, VLC gets stuck sometimes and only the good old “kill -9” can shoot it out. Is there a single command to kill it?

Solution

alias killvlc='kill -9 `ps ux | grep vlc | grep -v grep | tr -s " " | cut -d" " -f2`'
Categories: bash Tags: ,

Find window by its name and activate it (bring to foreground)

January 10, 2013 1 comment

Problem
From a script, I wanted to bring a specific window to the foreground.

Solution
There is a command-line X11 automation tool called “xdotool”. You will have to install it via apt-get. This command can do a lot, it’s worth checking out its man page.

Find the window ID of a window by its window title (example):

$ xdotool search --name "Rocket Launcher"
37748739

You can also use regular expressions.

Put the focus on this window and bring it in the foreground, i.e. activate it (example):

xdotool windowactivate 37748739

Other goodies
Window ID of the currently active window (example) #1:

$ xdotool getactivewindow
50331675

Window ID of the currently active window (example) #2:

$ xdpyinfo | grep focus
focus:  window 0x300001b, revert to PointerRoot

(Where 0x300001b == 50331675). “xdpyinfo” can also give you information about the X.Org version, number of screens, screen dimensions, etc.

Get window information (example):

$ xwininfo -id 50331675

xwininfo: Window id: 0x300001b "jabba : bash – Konsole"

  Absolute upper-left X:  0
  Absolute upper-left Y:  24
  Relative upper-left X:  0
  Relative upper-left Y:  0
  Width: 1920
  Height: 1032
  Depth: 32
  Visual: 0x23
  Visual Class: TrueColor
  Border width: 0
  Class: InputOutput
  Colormap: 0x3000001 (not installed)
  Bit Gravity State: NorthWestGravity
  Window Gravity State: NorthWestGravity
  Backing Store State: NotUseful
  Save Under State: no
  Map State: IsViewable
  Override Redirect State: no
  Corners:  +0+24  -0+24  -0-24  +0-24
  -geometry 1920x1032+0+24

Screenshot from command line

January 1, 2013 Leave a comment

This entry is based on this post.

Install:

sudo apt-get install scrot

Screenshot:

$ scrot /tmp/out.jpg

Screenshot in 5 seconds:

$ scrot -cd 5 /tmp/out.jpg
Categories: bash Tags: ,

Hide the cursor in terminal

December 7, 2012 Leave a comment

Problem
You want to hide the cursor in terminal.

Solution
Hide:

setterm -cursor off

Bring it back:

setterm -cursor on

Found here.

Categories: bash Tags: ,
Follow

Get every new post delivered to your Inbox.

Join 44 other followers