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

Saturday 13 October 2012

How To Exclude Directory While Compressing With Tar

Quite a handy and useful tip here. Several times, you want to compress files and folders but there might be cases when you want to compress your data excluding some of the directories. Tar command makes the process easier by providing us a exclusion switch.

I was actually backing up data I had downloaded in the remote server and wanted a copy of backup tar file in my system as well. But all those images that resided in the folders deep inside were not necessary for me. So all I did was something like below:

adm@RServ:~$ tar cvf backup.tar test --exclude=image*


The above command effectively excludes all the sub directories from testdirectory having the string image (eg. image, images, images_old in my case) and creates the backup.tar file. Moreover, the --exclude switch also co-operates the regular expressions so you can specify the regex to filter the directories. As an example, the command below excludes the directories a, b, c, d, and e while creating the tarball.

adm@RServ:~$ tar cvf backup.tar test --exclude=[a-e]


You can exploit this switch for ease several times in your daily works. I hope this helps :)


Read more...

Empty Trash From Command Line In Ubuntu

CLI is such a sexy piece so why bother using GUI, even for cleaning up your trash. In this post, you will see how you can empty trash in Ubuntu from command line.

The trash you see in GUI is nothing but just the view for the files deleted by users which are temporarily moved to the special location of user's home directory. For any user, the trash location is ~/.local/share/Trash/. That is, whatever a user deletes gets saved in this location.

samar@samar-Techgaun:~$ rm -rf ~/.local/share/Trash/


I hope this becomes useful :)


Read more...

Add Google Search Support In Gnome-Terminal

Gnome-terminal is my favorite thing in my system and recently I came to know that I could add google search support in gnome-terminal which is totally awesome. Ubuntu Tweak already includes the google search support but if you want google search in your terminal without the whole ubuntu tweak, you can follow this guide.

All you need to do is add the PPA and you can easily install the gnome-terminal with google search support. Fire up the terminal and enter the following commands:

sudo add-apt-repository ppa:tualatrix/personal
sudo apt-get update
sudo apt-get install gnome-terminal




Credits: Ubuntu Tweak


Read more...

Tuesday 2 October 2012

Binary, Hex, Octal and Decimal Conversion Under Linux

Base conversions are easy with linux CLI. No need of fancy GUI-based calculator to perform base conversions when there is our favorite linux terminal.

We will be using bc, a calculator language that supports arbitrary precision numbers with interactive execution of statements. We will exploit the pipelining feature of shell and will let the bc process our query to convert the numbers from one base to other.

From binary to decimal

The syntax is obvious and we will follow the similar syntax for all the conversions. In this first example, we are converting the binary number 1101101 from input base binary to decimal(obase defaults to decimal unless specified).

samar@samar-Techgaun:~$ echo "ibase=2;1101101" | bc
109


From octal to decimal

samar@samar-Techgaun:~$ echo "ibase=8;1101101" | bc
295489


From Hexadecimal to decimal

samar@samar-Techgaun:~$ echo "ibase=16;A1F3DF" | bc
10613727


From N-base to decimal

All you need to do is provide the appropriate ibase value (eg. ibase=4 for 4-base to decimal conversion).

samar@samar-Techgaun:~$ echo "ibase=16;A1F3DF" | bc
10613727


As seen in all the examples above, the conversion to decimal numbers does not require you to specify the obase as obase defaults to decimal. The same thing applies for ibase i.e. ibase defaults to decimal base by default as seen in the examples below.

Now lets try some conversion with decimal numbers as the input base.

From decimal to binary

samar@samar-Techgaun:~$ echo "obase=2;109" | bc
1101101


From decimal to octal

samar@samar-Techgaun:~$ echo "obase=8;295489" | bc
1101101


From decimal to hexadecimal

samar@samar-Techgaun:~$ echo "obase=16;10613727" | bc
A1F3DF


From decimal to N-base

All you need to do is provide the appropriate obase value (eg. obase=4 for decimal to 4-base conversion).

samar@samar-Techgaun:~$ echo "obase=4;121" | bc
1321


Below are few more examples of base conversions to clarify the use of the command.

From binary to octal

samar@samar-Techgaun:~$ echo "ibase=2;obase=8;1111" | bc
17


From hexadecimal to binary

samar@samar-Techgaun:~$ echo "ibase=16;obase=2;AFBE" | bc
1010111110111110


I hope this is helpful ;-)


Read more...

Friday 14 September 2012

How To Find The Location Of Command In Linux

Sometimes you need to find the pathnames or locations of commands you use frequently. In this post, I am going to discuss two useful commands that are useful for locating Linux commands.

The first command to locate the Linux commands is which. This command returns the pathnames of the files or links. However, it does not follow the symbolic links.

samar@Techgaun:~$ which bash
/bin/bash

You can also find the pathnames of multiple commands at once using which command.

samar@Techgaun:~$ which -a bash cat ls iftop
/bin/bash
/bin/cat
/bin/ls
/usr/sbin/iftop

The other command is type command which is useful to determine if a command is an alias, a built-in command or an independent command.

samar@Techgaun:~$ type gedit
gedit is /usr/bin/gedit
samar@Techgaun:~$ type grep
grep is aliased to `grep --color=auto'
samar@Techgaun:~$ type -t iftop
file

You can play more with the type command. I hope this helps :)


Read more...

Monday 3 September 2012

Preventing Accidental Overwriting Of Files In Bash Shell

How many times has this happened to you? It used to happen once in a while with me. A Linux user learns to use the redirection operators such as '>' and '>>' but accidental overwriting starts to become common in commands you use and shell scripts you write.

The accidental overwriting of files that happens unintentionally is known as clobbering and it commonly happens while using the '>' redirection operator.

samar@Techgaun:~$ mycmd > myfile

In the above example, the mycmd clobbers any existing data in the myfile file if that file exists already. Worse things may happen sometime. Imagine accidentally typing

samar@Techgaun:~$ mycmd > /etc/passwd

instead of possibly using other redirection operators (like >> or <). Thankfully, you could recover /etc/passwd from either /etc/passwd- or /var/backups/passwd.bak if you hadn't rm'd these files.

To prevent such accidental overwriting, we can set the noclobber environment variable. Below is a session of enabling this variable:

samar@Techgaun:~/Desktop/test$ echo "www.techgaun.com" > myfile
samar@Techgaun:~/Desktop/test$ echo "Overwriting techgaun.com" > myfile
samar@Techgaun:~/Desktop/test$ set -o noclobber
samar@Techgaun:~/Desktop/test$ echo "Retrying to overwrite" > myfile
-bash: myfile: cannot overwrite existing file

As seen above, you have to turn on the noclobber variable using the set -o noclobber command in your shell. However, you might want to intentionally overwrite contents of certain files even when the noclobber is turned on.

samar@Techgaun:~$ mycmd >| myfile

Notice the >| in place of your normal > redirection operator. Using this operator, you can however overwrite the existing files even if the noclobber is turned on.

If you want to turn off the noclobber variable, type the following:

samar@Techgaun:~$ set +o noclobber

You can also permanently turn on the noclobber by the following command:

samar@Techgaun:~$ echo "set -o noclobber" >> ~/.bashrc

Moreover, such accidental overwriting can be prevented by enabling the interactive mode which is available in most of the linux commands. For example, you can write the alias for many commands that are likely to cause accidental overwriting. See some examples of aliases below:

samar@Techgaun:~$ alias rm=rm -i
samar@Techgaun:~$ alias mv=mv -i

You could even keep these aliases in your ~/.bashrc file permanently. Enabling such interactive modes by default in the commands that are more likely to cause accidental overwriting can prevent clobbering in many cases.

I hope this proves useful to you :)


Read more...

Sunday 2 September 2012

How To Search Manual Pages In Linux

Linux system consists of hundreds of binaries, several syscalls, and other stuffs that do have manual page. What if you want to locate or find the commands by searching through the manual pages? In this post, I am going to talk about one such useful command to search through the manual page names and short descriptions.

The command I am talking about is the apropos command. The best way to learn any linux command is to read its corresponding manual and go through the help (-h or --help) so lets poke through the help of apropos itself.

samar@Techgaun:~$ apropos -h
Usage: apropos [OPTION...] KEYWORD...

  -d, --debug                emit debugging messages
  -v, --verbose              print verbose warning messages
  -e, --exact                search each keyword for exact match
  -r, --regex                interpret each keyword as a regex
  -w, --wildcard             the keyword(s) contain wildcards
  -a, --and                  require all keywords to match
  -l, --long                 do not trim output to terminal width
  -C, --config-file=FILE     use this user configuration file
  -L, --locale=LOCALE        define the locale for this search
  -m, --systems=SYSTEM       use manual pages from other systems
  -M, --manpath=PATH         set search path for manual pages to PATH
  -s, --section=SECTION      search only this section
  -?, --help                 give this help list
      --usage                give a short usage message
  -V, --version              print program version

Mandatory or optional arguments to long options are also mandatory or optional
for any corresponding short options.

The --regex option is enabled by default.

Report bugs to cjwatson@debian.org.

Particularly, the -e switch is quite useful to filter out your search. See the example below:

samar@Techgaun:~$ apropos -e tar
bf_tar (1)           - shell script to write a tar file of a bogofilter direc...
bf_tar-bdb (1)       - shell script to write a tar file of a bogofilter direc...
git-tar-tree (1)     - Create a tar archive of the files in the named tree ob...
lz (1)               - gunzips and shows a listing of a gzip'd tar'd archive
mxtar (1)            - Wrapper for using GNU tar directly from a floppy disk
ptar (1)             - a tar-like program written in perl
tar (1)              - The GNU version of the tar archiving utility
tar (5)              - format of tape archive files
tgz (1)              - makes a gzip'd tar archive
uz (1)               - gunzips and extracts a gzip'd tar'd archive

Each command has its associated short description and the apropos command searches the short description section of appropriate manual page for the provided keyword. You can also specify the search keywords in the form of regular expression for more flexibility. I hope this command counts as useful one :)


Read more...

Friday 27 July 2012

Determine Your SATA Disk Model And Vendor In Ubuntu

Sometimes you need to determine the model and vendor of your hard disk and here is the small tips on how to find those information.

All you have to do is type one of the following commands for the respective outputs:
cat /sys/class/block/sda/device/model

cat /sys/class/block/sda/device/vendor
I hope this becomes useful sometimes. :)


Read more...