Absolutely Tech

[HowTo] Suspend Ubuntu from terminal or keyboard shortcuts

When learning to make bash scripts, at one point or other you’d like to know how to suspend your computer from terminal. Well, there are quite some methods available for Ubuntu. You can choose whatever suits your needs. I prefer method 3 (look below).

Method 1:

sudo sh /etc/acpi/sleep.sh force

Method 2:

sudo pm-suspend

This command might not be available in older versions of Ubuntu. It works perfectly fine on Lucid and Maverick.

Method 3:

This command does not require sudo so it might come in handy. For example, you can map it to a keyboard shortcut for easy access.

dbus-send --print-reply --system --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend

One thing to notice here is none of the commands I mentioned here will ask your password on resuming. For that you’ll have to lock your computer before suspending. You’ll have to use gnome-screensaver-command --lock before suspending to lock the computer

Create a suspend script:

Open a new file in gedit:

sudo gedit /usr/bin/suspend-comp

Copy and paste the following code and save it:

#!/bin/sh 
sleep $1;
dbus-send --print-reply --system --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend

If you want the script to ask for a password on resuming, copy-paste this code instead:

#!/bin/sh 
sleep $1;
gnome-screensaver-command --lock
dbus-send --print-reply --system --dest=org.freedesktop.UPower /org/freedesktop/UPower org.freedesktop.UPower.Suspend

Add the executable bit:

sudo chmod +x /usr/bin/suspend-comp

Now suspend-comp time-in-seconds can be used in terminal. For example, if I want to have my computer suspended after 20 seconds I can simply type:

suspend-comp 20

Mapping it to keyboard shortcut for easy access:

Go to System->Preferences->Keyboard Shortcuts. Click Add. Type Suspend for Name and suspend-comp 0 in Command field. Click Apply.

Now scroll to the bottom of the list to find Suspend. Click on ‘Disabled’ on the right to ‘Suspend’. Now press any key combination to map the command to it. I used Ctrl+Alt+Shift+s.

Now simply pressing Ctrl+Alt+Shift+s will suspend your computer.

Cheers!

[HowTo] Find number of files inside a folder in Linux

I wanted to find out the number of files inside a folder. I didn’t want the folders to be included in the result.
A blog post on zimbio.com shows us how to do it with ls command which is not only downright inaccurate, it is also slower than what I am going to show you.
I am going to show you how to do it using find command which is extremely powerful once you know how to use it.
Executing the command below will print the number of files (excluding folders) in current directory (including all subdirectories):

find ./ -type f | wc -l

This can be used to find number of directories too:

find ./ -type d | wc -l

You can do more complex counting for example finding out the number of files which are of filesize 1Mb and more.

find ./ -type f -size +1M | wc -l

Finding number of symlinks:

find ./ -type l | wc -l

You can also see number of files accessed within last 1 hour from current directory:

find ./ -type f -amin -60 | wc -l

Here -60 means less than 60 minutes ago.

Only imagination is the limit with what you can achieve using find command.

[SOLVED] ‘Unknown’ filesystem in gparted

I had a primary NTFS partition with windows 7 installed on it. Somehow, its bootloader went corrupt so I used install-mbr in ubuntu to fix that but it only made the problem worse. Now my system didn’t recognise the partition as NTFS partition and I couldn’t access the partition.

I modified the partition ID using fdisk but that didn’t work. Fdisk recognised it as NTFS but gparted didn’t, nor did Ubuntu. To fix it, I followed the following steps:

    • Download testdisk and extract the files to a folder. The files are compiled executable files so there is no need for any installation. I extracted it in a folder named testdisk on my home directory. Ubuntu users can install it using sudo apt-get install testdisk
      cd ~/testdisk/linux
      Execute the testdisk:

      sudo ./testdisk_static
      Select [Create] and press enter. On the resulting screen, select your harddrive using cursor keys and press enter. On the next screen select the partition table type. It’d generally be Intel if you are using a PC and Mac if you are on Mac. Select the correct one and press enter.
      Now you’ll be presented with lots of options. Use cursor keys to select Advanced and press enter. Select the partition which shows unknown type. Use left and right arrow keys to select [Type] and press enter. You’ll see the list of partition types. In the list the HPFS/NTFS has partition id 07. Press enter, type 7 for NTFS or another partition ID corresponding to your partition type. Press enter.
      Now use the right arrow key to select the [Boot] and press enter. Use right arrow key to select [Rebuild BS] and press enter. Select [Write] and press enter. Now type y. The partition should be fixed by now. You can now quit the testdisk
      Refresh partition list

      sudo partprobe
  • The disk should be accessible again now

    Cheers!

    [HowTo]Extract almost any archive through terminal using a single command in linux

    I came across this simple script on ubuntuforums which I thought was really very useful and worth sharing it on my blog. You can either make a function out of it and put it in .bashrc file or make an executable script and put it in /usr/bin/.

    Method 1:

    Open your ~/.bashrc file using any editor.

    gedit ~/.bashrc

    Copy and paste the following code at the end of it:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    extract-file () {
    if [ -f $1 ] ; then
    case $1 in
    *.tar.bz2) tar xjvf $1 ;;
    *.tar.gz) tar xzvf $1 ;;
    *.bz2) bunzip2 $1 ;;
    *.rar) rar x $1 ;;
    *.gz) gunzip $1 ;;
    *.tar) tar xf $1 ;;
    *.tbz2) tar xjvf $1 ;;
    *.tgz) tar xzvf $1 ;;
    *.zip) unzip $1 ;;
    *.Z) uncompress $1 ;;
    *.7z) 7z x $1 ;;
    *) echo "'$1' cannot be extracted via extract-file" ;;
    esac
    else
    echo "'$1' is not a valid file"
    fi
    }

    Now you can use the following command to extract any archive:

    extract-file

    The command extract-file would be available only to terminals which have been opened after saving the .bashrc file with the above code. Also this code is user-specific, so if another user logs in he cannot use this command.

    Method 2:

    Use the following command to create a new file in /usr/bin directory and launch the gedit.

    sudo gedit /usr/bin/extract-file

    Read the rest of this entry »

    [HOWTO] 5 methods to find out your linux distribution name and version through command line

    There are lots of commands to find out your ditribution name and distribution version. Some work on some distros, some work on others. Here are some of the methods:

    • lsb_release -a

    • cat /etc/*release

    • cat /etc/issue

    • cat /proc/version

    • uname -a

    One of these methods will surely tell you your linux distribution name and current version.

    Cheers!

    Killing Xorg, when things get out of control

    Today I experienced a rather strange problem in Ubuntu. It crashed. Actually it wasn’t completely crashed, the mouse clicks weren’t working, I couldn’t switch using Alt+Tab, I could only use the program which was on the screen. The panels weren’t working, though the shortcut keys were. Fortunately enough, I had assigned shortcut key to open the terminal and it was working, so I could even access the terminal.

    Fortunately, firefox  (shiretoko) was running as the frontmost application and I was able to access the internet. I searched for How to kill X and was presented with many solutions.

    I tried killall X in  terminal but it didn’t work. Suddenly, I remembered that X was called Xorg in jaunty. I did this:

    Read the rest of this entry »

    Killing applications in Ubuntu

    Well all OSes have bad programs, Linux has it too. Some times they stop responding and unlike windows, they don’t make the whole OS unresponsive. The application alone is unresponsive but you can still use other applications normally. Killing an unresponsive application is fairly easy job in Ubuntu.

    Bring the unresponsive application to the front, the app must be having a desaturated look because its unresponsive (if its not, check again… it must not be unresponsive). Launch the terminal and type:

    $ xkill

    The mouse cursor will change to a cross, click anywhere on the unresponsive application and it will be killed.

    To get things done faster, you can type xkill in “Run application” dialog box too. Press Alt+F2 to bring the run dialog box and type xkill and enter. The mouse cursor will change to cross and click on unresponsive app to kill it.

    Alternatively, Read the rest of this entry »