Showing posts with label command-line. Show all posts
Showing posts with label command-line. Show all posts

Sunday, April 9, 2017

Do things Command-Line way in Ubuntu - part 1

Ubuntu Commands for some useful tasks

Below some useful commands for carrying out tasks in ubuntu via the terminal are mentioned :-

  1. Resize image
  2. Resize pdf
  3. Download mp3 audio from a Youtube video
  4. Convert any video file to lossless "flac" audio format
  5. Convert any video file to "mp3" audio format
 
  • Resize image :

convert -resize 50% -quality 99 IMG_20160623_203259.jpg output.jpg

convert is in package imagemagick

===============

  • Resize pdf :

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

If the -dPDFSETTINGS=/screen setting is too low quality to suit your needs, replace it with -dPDFSETTINGS=/ebook for better quality, but slightly larger pdfs. Delete the setting altogether for the high quality default, which you can also explicitly call for with -dPDFSETTINGS=/prepress.

source : http://askubuntu.com/questions/113544/how-can-i-reduce-the-file-size-of-a-scanned-pdf-file

Alternate : pdfsam ( The version in ubuntu respository is old , latest version at http://www.pdfsam.org/downloads/ )

============

  • Rotate pdf :

sudo apt-get install pdftk (22 MB approx.)

pdftk in.pdf cat 1-endS output out.pdf

Alternate : pdfsam ( The version in ubuntu respository is old , latest version at http://www.pdfsam.org/downloads/ )

===========

  • Youtube download as mp3

1) To install it right away for all UNIX users (Linux, OS X, etc.), type:
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl

2) If you do not have curl, you can alternatively use a recent wget:
sudo wget https://yt-dl.org/downloads/latest/youtube-dl -O /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl

3) The command (requires ffmpeg package) : youtube-dl --prefer-ffmpeg --extract-audio --audio-format mp3 --audio-quality 0 "<video_watch_url>"

==========

  • Convert video file to lossless flac format

ffmpeg -i "input.mkv" -vn -sn -acodec flac audio.flac

==========

  • Convert video file to mp3 format

ffmpeg -i "input.mkv" -vn -sn -acodec mp3 audio.mp3

Monday, March 17, 2014

Installing Sun JRE (Java Run-time Environment ) in Ubuntu

Hi Everyone,

Some-times ago, I gave you the installation steps for Sun Java  ( Now Oracle Java ) for the Ubuntu Linux @ my previous post about installing sun jre  . But now that doesn't seems to work even for me as Oracle has removed the binary ( .bin ) file from their website . Even the steps given at https://www.java.com/en/download/help/linux_install.xml  or the PPA method of webupd8 as mentioned in Ubuntu Community Wiki doesn't seems to help.

So, I decided to do something different, which I have formulated in steps as below :-

1) Download the rpm file labelled as "Linux RPM" from the java website

2) Install alien package from Ubuntu repositories.
         sudo apt-get install alien
3) Go to the directory where you have downloaded the rpm file. For example, if your file was downloaded to Downloads directory then :-
         cd ~/Downloads
4) Use alien to Convert the rpm package to deb package. This may take a while.
         sudo alien --to-deb --scripts jre-7u51-linux-i586.rpm
5) Use dpkg to install the newly generated deb package.
         sudo dpkg -i jre_1.7.051-1_i386.deb
    You can also use The Ubuntu-Software-Center or GDebi-Package-Installer for the above purpose.
6) Use following command to verify the installation :- 
          java -version

Output will be something similar to this as show in the above screenshot:-

java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) Client VM (build 24.51-b03, mixed mode, sharing)

7) To install the firefox plugin, do the following :-
    a) Go to Firefox-plugin install directory
            cd /usr/lib/firefox-addons/plugins/
    b) create a soft-link ( shortcut ) for the Java plugin in the directory :-
            sudo ln -s /usr/java/jre1.7.0_51/lib/i386/libnpjp2.so .
    c) To verify, you can launch Firefox and type about:plugins and its ready for use.
8) To install the java plugin for Google chrome, do the following:-
   a) Go to Google chrome installation directory
         cd /opt/google/chrome
   b) create a directory named plugins here ( if its not there ) . At least, it was absent from mine chrome installation ;) 
        sudo mkdir plugins
   c) Now create a soft-link ( shortcut ) for the Java plugin in the directory :-
         sudo ln -s /usr/java/jre1.7.0_51/lib/i386/libnpjp2.so .
   d) Now, you can launch Google chrome and type chrome://plugins/ to verify the installation and start using the plugin.

Note:- The above steps have been tested on The Lubuntu 14.04 beta 1.

Saturday, October 8, 2011

Recovery of a deleted partition ( Part - 2 )

                Starting from where I left in my previous post 
(http://meandmyubuntulinux.blogspot.com/2011/10/recovery-of-deleted-partition.html  )  , Testdisk has done wonderful job in revealing my HD's partitions and thus correcting the partition table. But..... still something was missing out there as I was still not able to boot into the system. It simply displayed a cursor which keeps on blinking forever and no other error message. 

               So, I had to again boot with the help of the Lucid Lynx ( Ubuntu 10.04 LTS ) live usb to figure out what is happening. Interesting or say luckily, when I booted the live system, It easily discovered all my partitions and and I was also able to access all of my data !!!


             This was quite confusing situation as the Ubuntu live disk was able to detect partitions properly but still my system was not able to boot at all. So, I checked with GParted Partition editor to see if there is still any problem in the partition table. Yes there was.......... it says “A disk cannot have partitions outside the disk”.


So, I dumped my partition table to a file called PT.txt for editing :

                                 sudo sfdisk -d /dev/sda > PT.txt


The output was :


--------------------------------------------------------------------------------------------
# partition table of /dev/sda
unit: sectors
/dev/sda1 : start= 63, size= 62926542, Id= 7, bootable
/dev/sda2 : start= 62926605, size=425481525, Id= f
/dev/sda3 : start= 0, size= 0, Id= 0
/dev/sda4 : start= 0, size= 0, Id= 0
/dev/sda5 : start= 62926668, size= 31471272, Id= 7
/dev/sda6 : start= 94398003, size=136323528, Id=83
/dev/sda7 : start=230723584, size= 41947136, Id= 7
/dev/sda8 : start=272671308, size=104872257, Id= 7
/dev/sda9 : start=377544704, size=107634360, Id=83
/dev/sda10: start=485179128, size= 3215608, Id=82
---------------------------------------------------------------------------------------------

              Now let me explain the above output. The 1st column containing /dev/sd? are the partitions. If sfdisk doesn't find a partition corresponding to any number it simply put start, size and id = 0 for that partition. Its not an error. Start and size given here are in units of sectors. A particular Id corresponds to a particular partition type. For example, here, Id=7 is NTFS , Id=f is extended partition , Id=83 is EXT4 and Id=82 is Linux Swap partition.






Now time for some maths.......



As per the link in the reference post link that I gave above :

  1. There is a 63 sector gap between the start of an extended partition and the start of a logical partition.
  2. There is a 63 sector gap between the end of a logical partition and the start of the next logical partition.
  3. There is no gap at all between primary partitions.
  4. It is possible to have larger gaps between partitions, but never smaller than 63 sectors for logical partitions, (and obviously) never smaller than 0 for primary partitions.
  5. Partitions must not overlap.

         In my case, /dev/sda1 is a primary partition, /dev/sda2 is a primary extended partition and /dev/sda5 to /dev/sda10 are logical partitions.

               Now, checking the above conditions with my partition table.

  1. /dev/sda1 is ok as it starts at sector 63. No problem in that.
  2. Considering /dev/sda1 end range is ok, /dev/sda2 ( being a primary partition ) should start at sector=63+62926542=62926605 and so is the case . So, the start range of /dev/sda2 is also ok. Leaving aside the end range of /dev/sda2 as that will be the end of disk.
  3. Now, for /dev/sda5 ( which is a logical partition ), the start range ( as per condition 1) should be sector=62926605+63=62926668 and therefore , /dev/sda5 is also ok.
  4. For /dev/sda6 start sector= 62926668 + 31471272 + 63 = 94398003 implies that /dev/sda6 is ok.
  5. Similarly checking start sector of all the partitions are found ok.
  6. Now, time for the last check i.e. end sector of last partition i.e. /dev/sda10 and end sector of the extended partition /dev/sda2. Both should be same.
    End sector of /dev/sda2 will be sector = 62926605 + 425481525 =488408130
    End sector of /dev/sda10 will be sector = 485179128+ 3215608 = 488394736

                 So, here is a difference. So, either of two range is wrong. So, had to go with hit and trial with both the possibilities as I couldn't find a way to determine the total sector size of my HD :-( .

At first backing up the dump of original partition table.

                                            cp PT.txt PT.txt_bkp


and transferred it to the external hard disk for backup.


                 Now, trying to go with lesser of two range. So, end of /dev/sda10 will be ok but end sector of /dev/sda2 will have to be changed.


New Size of /dev/sda2 will be=end sector of /dev/sda10–start sector of /dev/sda2
                                               = 488394736 - 62926605

                                               = 425468131


                  So, editing the PT.txt file using vi editor and using the obtained value , the new partition table looked like following :

-------------------------------------------------------------------------------------------
# partition table of /dev/sda
unit: sectors
/dev/sda1 : start= 63, size= 62926542, Id= 7, bootable
/dev/sda2 : start= 62926605, size=425468131, Id= f
/dev/sda3 : start= 0, size= 0, Id= 0
/dev/sda4 : start= 0, size= 0, Id= 0
/dev/sda5 : start= 62926668, size= 31471272, Id= 7
/dev/sda6 : start= 94398003, size=136323528, Id=83
/dev/sda7 : start=230723584, size= 41947136, Id= 7
/dev/sda8 : start=272671308, size=104872257, Id= 7
/dev/sda9 : start=377544704, size=107634360, Id=83
/dev/sda10: start=485179128, size= 3215608, Id=82
------------------------------------------------------------------------------------------


Please note the change in size of /dev/sda2.



                      Now time to write the new partition table using the new file PT.txt . It can be done by :


                     sudo sfdisk --no-reread -f /dev/sda -O PT.save < PT.txt


here, "--no-reread" means don't check if disk is unmounted

           -f force

          "-O PT.save" means save a backup of original partition table in PT.save.


( PT.save is in binary format. To restore the partition table using PT.save:


                           sudo sfdisk --force -I PT.save /dev/sda )



                           Now lets reboot and see if the problem is resolved. Still no luck as the cursor still blinks forever. Now at least , the GParted doesn't detect any error in the partition table and all the error messages are gone and I am able to easily access my all data using the Live USB disk.



                            So, will I have to re-install my operating systems all over again or still something can be done ???



To be continued............




Saturday, September 24, 2011

Installing a 1 GB swap without partitioning

              Some software that you install e.g. oracle database XE requires you to have greater than one GB swap space. So, if you have not created a swap partition of greater than 1GB just because you have lot of RAM, you may land in a fix so as what do now . Although  , in ubuntu , you can easily use GParted to create a new swap space but some times you don't have enough free space or don't want to take risk of using GParted . Also , GParted is not installed by default ( u can get it from Ubuntu Live Disk ). So, we need some process by which we can have some extra swap space without using GParted.

To install a 1 GB swapfile named swapfile in /  ( root directory) , use the following steps in the terminal :

1) sudo -s 

   ( to gain root privilage, its optional , but if you don't do this then you have to append sudo to each of the following commands )

2) dd if=/dev/zero of=/swapfile bs=1024 count=1048576  

   (this may take a while)  ( 1048576 == 1 x 1024 x 1024 , 1GB == 1048576 KB)

3) mkswap /swapfile

    ( ignore any error )

4) swapon /swapfile
 
    ( to turn on the swap )
 
5) cp /etc/fstab /etc/fstab.orig


    ( back-up the fstab before editing )

6) echo '/swapfile swap swap defaults 0 0' >> /etc/fstab
 
    ( adds the entry of  new swap to fstab for mounting the file at boot time )

7) To  Verify:

   swapon -a ; swapon -s


If at anytime u want to delete the swap then :

   sudo swapoff -a
  
   sudo rm /swapfile


   sudo cp /etc/fstab /etc/fstab.changed ; sudo cp /etc/fstab.orig /etc/fstab

   sudo swapon -a ; swapon -s

Friday, September 23, 2011

locating and correcting bad blocks/sectors of pen-drive

              Sometimes, we drop our pen-drive in some water container and our pen-drive either refuses to get detected or files not getting copied to it. We can use our Incredible Ubuntu operating system to get it working. Find below the steps for the same.

Note : 
1) It is a time-consuming process. Use the 2nd steps only if you actually find any bad-block.
2) All steps need Ubuntu live disk and back-up of data to avoid any loss.

3) The process can be well applied to a hard-disk.

 Steps for locating and correcting bad blocks/sectors of the pen-drive :

1) Boot from Ubuntu live disk , open Gparted and check for partition name ( e.g. /dev/sdh1 )  and use the same to replace /dev/sdh1

2) Looking for bad blocks ( via terminal )

The first step is to locate the bad blocks of each partition. Type in the following command to do a search using the non-destructive way (-n), showing the progress (-s) and dumping the bad blocks in the file "badblocks_1GB01.txt" .

sudo badblocks -n -s -v /dev/sdh1 -o badblocks_1GB01.txt

2) Formatting the drive

You have to pass the list of blocks obtained in the previous step, for which the command to use to format in FAT32 is:

sudo mkdosfs -F32 -v -n "1GB" -l badblocks_1GB01.txt /dev/sdh1

Here -F32 is for FAT32 partition and "1GB" is the partition size .

This will render the badblocks un-available for use leaving all the usable blocks on the pen-drive for use.

Wednesday, September 21, 2011

Firewall in Ubuntu

     Ubuntu , as with all post 2.2/2.4 kernel Linux distributions comes with the netfilter/iptables framework. This framework is a set of kernel modules that can be utilized to create packet filtering rules at the kernel level. Rules are written in iptables format, which is the method of conveyance of instructions to netfilter, and in essence the Linux Kernel.

        Ubuntu also includes an application called Uncomplicated FireWall (UFW). This application is a userspace application that essentially can be used to create iptables rules. There is also a GUI for UFW called GUFW. It provides a graphical interface for UFW. 
         The command-line interface of UFW is installed by default in ubuntu.

Manual page can be seen by command :

                                    man ufw

If you want to see its current status, then it can be done by command :

                                  sudo ufw status
For enabling the firewall :

                                  sudo ufw enable

Note : The command for enabling the firewall, also puts it into the list of startup programs.