HOW TO SET FILE ASSOCIATIONS OF FILE WITH COMMAND LINE IN LINUX

QUES: Why would one choose to use command line for changing file associaiton……..?
ANS: Several times you come across a situation when you have to setup file associations from the command line. Like sometimes, file managers just won’t work, other times you may have a corrupted file or bad line in your file.It involves just 3 steps as follows :

STEP 1) Then query the file to figure out the mimetype syntax

[user@linux:~]$xdg-mime query filetype /home/linux/Desktop/vid.flv
[user@linux:~]$video/x-flv

STEP 2) Check what the default program is of that mimetype.

[user@linux:~]$xdg-mime query default video/x-flv

STEP 3) Set the file association

[user@linux:~]$xdg-mime default vlc.desktop

NOTE :– Here I knew the programme name to be associated with the new filetype, so I jumped to the step 3 after the step 2. If you dont know the programme names and there association then follow these steps.

ADDITIONAL STEP :Figure out what the programname.desktop of the program you want to use is.The simplest way to do this is by checking your mimeapps.list file located here.

[user@linux:~]$vi /home/linux/.local/share/applications/mimeapps.list

If that doesn’t work try searching for the file with a similar command.

[user@linux:~]$find / -iname '*vlc.desktop*' 2>/dev/null

If the programname.desktop file has a weird name then you will need to grep for that file.Here I will use the vlc media player.

[user@linux:~]$grep -ri "vlc media player" /usr/share/applications/* 2>/dev/null
[user@linux:~]$/usr/share/applications/vlc.desktop:Name=VLC media player

For Centos/Fedora/Rhel users if they find this error while changing the file types —

[user@linux:~]$/usr/bin/xdg-mime: line 532: kde-config: command not found

follow this link(https://bugs.freedesktop.org/show_bug.cgi?id=25563#c2)

[user@linux:~]$find / -iname '*vlc.desktop*' 2>/dev/null
Tagged with: , , ,
Posted in Linux

LINUX SHELL/TERMINAL CHEAT SHEET

Shell Cheat Sheet linuxadminhelp.blogspot.in
Useful Editing Keystrokes for shell
KEYSTROKE(s)
FUNCTIONS
Up
Move back one command in the history list
Down
Move forward one command in the history list
Left
Move back one character.
Right
Move forward one character
Esc + f
Move forward one word
Esc + b
Move forward one word
Ctrl + A
Move to the beginning of line
Ctrl + E
Move to the end of line
Ctrl + D
Delete current character
Backspace
Delete previous character
Esc + d
Delete current word
Ctrl + U
Delete from beginning of line
Esc + k
Delete to end of line
Ctrl + Y
Retrieve last item deleted
Esc
Insert last word of previous command
Ctrl + L
Clear the screen, placing the current line at the top of the screen
Tab
Attempt to complete the current word, interpreting it as a filename, username, variable name, hostname, or command as determined by the context
Esc + ?
List the possible completions
Ctrl + C
Sends an interrupt signal to the currently executing command, which generally responds by terminating itself
Ctrl + D
Sends an end of file to the currently executing command. Use this keystroke to terminate console input
Ctrl + Z
Suspends the currently executing program
exit
Exit from shell
Shell special characters
CHARACTER
FUNCTIONS
#
Marks the command as a comment, which the shell ignores
;
Separates commands, letting you enter several commands on a single line
&
Placed at the end of a command, causes the command to execute as a background process, so that a new shell prompt appears immediately after the command is entered
()
execute commands in subshell
{}
execute commands in current shell
$var
Substitue the declare variable with its value while execution of the command
special characters used in filename globbing(filename metacharacters)
METACHARACTER
FUNCTIONS
*
Matches a string of zero or more characters
?
Matches exactly one character
[ abc …]
Matches any of the characters specified
[ a – z ]
Matches any character in the specified range
[! abc …]
Matches any character other than those specified
[! a – z ]
Matches any character not in the specified range
~
The home directory of the current user
~ userid
The home directory of the specified user
~+
The current working directory
~-
The previous working directory
SHELL ALIASES
alias name=’command’ (like –> alias mv=’mv -i’)
unalias <alias name>
special scripts
SCRIPT
FUNCTIONS
/etc/profile
Executed when the user logs in
~/.profile
Executed when the user logs in
~/.bashrc
Executed when BASH is launched
~/.bash_logout
Executed when the user logs out
Shell evironment variables
VARIABLE
FUNCTIONS
USER
The user’s current username; may differ from the login name if the user executes the su command
DISPLAY
The X display to be used; for example, localhost:0
HOME
The absolute path of the user’s home directory
HOSTNAME
Internet name of the host
LOGNAME
The user’s login name
MAIL
The absolute path of the user’s mail file
PATH
The search path
SHELL
The absolute path of the current shell
TERM
The terminal type
printenv
Print environment values
export <variable>
To make the value of a shell variable availabl e to the programs invoked by the shell
Variable=
To remove the value associated with shell variable, give the variable an empty value (though it will appear in the output of set cmd)
Unset <variable>
To dispense the variable from the shell
CONTROLLING OPERATION OF THE Shell
CHARACTER
FUNCTIONS
Characters within a pair of single quotes are interpreted literally; that is, their metacharacter meanings (if any) are ignored. Similarly, the shell does not replace references to shell or environment variables with the value of the referenced variable
Characters within a pair of double quotes are interpreted literally; that is, their metacharacter meanings (if any) are ignored. However, the shell does replace references to shell or environment variables with the value of the referenced variable
` `
Text within a pair of back quotes is interpreted as a command, which the shell executes before executing the rest of the command line. The output of the command replaces the original back-quoted text
\
The following character is interpreted literally; that is, its metacharacter meaning (if any) is ignored. The backslash character has a special use as a line continuation character. When a line ends with a backslash, the line and the following line are considered part of a single line
Input/Output Redirection and Piping (stdin,stdout,stderr)
REDIRECTOR
FUNCTIONS
cmd > file
Redirects standard output stream to specified file (same as cmd 1> file)
cmd 2> file
Redirects standard error stream to specified file
cmd >> file
Redirects standard output stream to specified file, appending output to the file if the file already exists
cmd 2>> file
Redirects standard error stream to specified file, appending output to the file if the file already exists
cmd &> file
Redirects standard output and error streams to the specified file
cmd > file 2>&1
Another way to redirect both stdout and stderr of cmd to a file
cmd < file
Redirects standard input stream to the specified file
cmd << text
Reads standard input until a line matching text is found, at which point end of file is posted
cmd > /dev/null
Discard stdout of cmd
cmd 2> /dev/null
Discard stderr of cmd
cmd &> /dev/null
Discard stdout and stderr of cmd
{ cmd1; cmd2; } > file
Redirect stdout from multiple commands to a file
cmd << EOL
line1
line2
EOL
Redirect a bunch of lines to the stdin. If ‘EOL’ is quoted, text is treated literally. This is called a here-document
cmd1 | cmd2
Takes the standard input of cmd2 from the standard output of cmd1 (also known as the pipe redirector)
cmd1 |& cmd2
Redirect stdout and stderr of cmd1 to stdin of cmd2 (bash 4.0+ only)
cmd | tee file
Redirect stdout of cmd to a file and print it to screen
A cheat sheet by Shiwang Kalkhanda (shiwangkalkhanda@gmail.com) 2012.
Released under GNU Free Document License
YOU CAN ALSO DOWNLOAD IT AS PDF FROM HERE
Tagged with: , , ,
Posted in Linux

HOW TO CONFIGURE NETWORK CONNECTION FROM COMMAND LINE

STEP 1) ASSIGN IP ADDRESS AND GATEWAY

Although, we have got a wonderful tool “NetworkManager” to manage our interface connection from GUI,but when we are using console then how to manage those interfaces and configuring them becomes a headache for a naive linux user.These are the following steps you should follow in order to configure your interface properly.
 

[user@linux:~]$ifconfig eth0 192.168.1.10 netmask 255.255.255.0
To add the default gateway in the routing table type --
[user@linux:~]$route add default gw 192.168.1.254
[user@linux:~]$route -n
(to print the routing info table on terminal)
or we can also use
[user@linux:~]$netstat -anr

STEP 2) ASSIGN DNS SERVER

Here I will use “8.8.8.8” , because it one of the most use public DNS  address.
You are free to choose any.

[user@linux:~]$echo "nameserver 8.8.8.8" > /etc/resolv.conf
Now a little hack, suppose you want to change your Mac Address, then that can be 
done in very easily from command line (not like windows where first you have to find 
some software and then had to pay for it…)

[user@linux ~]$ifconfig eth0 down hw ether 00:11:1a:2b:3c:33
(give the new ethernet address)
[user@linux ~]$ifconfig eth0 up

To restore the original MAC address give the following commands : –

[user@linux ~]$ifconfig eth0 down
[user@linux ~]$ifconfig eth0 up

Tagged with: , , , ,
Posted in Linux

Intro to linux, floss and linux vs windows

Tagged with: , ,
Posted in Linux

FIND YOUR PUBLIC IP FROM COMMAND LINE

Lots of times you need to determine your public IP address, if you are using Linux operating system to power your PC, you may use these one liners to find your IP –>Using wget

[root@test ~]# wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

Using curl

[root@test ~]# curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

Using Lynx

[root@test ~]# lynx -dump checkip.dyndns.org                                           
OR
[root@test ~]# lynx -dump www.whatismyip.com | grep 'Your IP Add'               
OR
[root@test ~]# lynx -dump http://www.ip2location.com |grep -e 'IP Address' -e '\[_\] '     
ORThough at any point of time you can open the browser and enter any of the following URL to the same effect but I would rather prefer to Unleash the power of Linux command lines/shell.Here are some examples of Python scripts which give the same result.

Using Python scripts as follows :–

#!/usr/bin/python
import urllib 
import string
import re

f = urllib.urlopen("http://checkip.dyndns.org")
s = f.read()
f.close()
pattern = re.compile('<body>(.*?)</body>',re.I|re.S)
for each in pattern.findall(s):
    print each

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

#!/usr/bin/python
import lxml.html
import string

source = lxml.html.parse("http://www.minip.no")
title_tag = source.find(".//title").text
title = string.split(title_tag, " ")
ip = title[3]
print ip
Tagged with: , , , ,
Posted in Python script

I/O REDIRECTION

There are always three default files open, stdin (the keyboard), stdout (the screen), and stderr (error messages output to the screen). Redirection simply means capturing output from a file, command, program, script, or even code block within a script and and sending it as input to another file, command, program, or script.

Numeric handles for file descriptors:

STDIN = 0 Keyboard input 
STDOUT = 1 Text output 
STDERR = 2 Error text output UNDEFINED = 3-9

REDIRECTION
command > filename                                               Redirect command output to a file
command >> filename                                             APPEND into a file 
command < filename                                               Pass a command from a text file
command A |  command B                                      Pipe the output from command A into command B 
command A & command B                                     Run command A and then run command B
command A && command B                                  Run command A, if it succeeds then run command B 
command A || command B                                      Run command A, if it fails then run command B
command 2 > filename                                            Redirect any error message into a file
command 2 >> filename Append any error message into a file
command > file 2>&1 Redirect errors and output to one file
command > file 2<&1 Redirect output and errors to one file
command > fileA 2> fileB Redirect output and errors to separate files
command 2>&1 >filename -------------->Wrong command

Redirect to NULL (hide errors)

command 2> /dev/null Redirect error messages to NUL
command >/dev/null 2>&1 Redirect error and output to NUL
command >filename 2> /dev/null Redirect output to file but suppress error
 
We can also redirect to a printer with > PRN or >LPT1 
 
For more info read here and to read commands of linux shell and vbscript read here
 
Tagged with: , , , ,
Posted in Linux

HOW TO RECOVER UBUNTU PASSWORD

Why would you want to reset a password……….?

1.)   Someone gave you a computer with Ubuntu installed on it but not the password for the user account.
2.)   You just installed Ubuntu and forgot what password you selected during the installation process.
3.)   You have too many passwords in your life and can’t keep track of them all. Well, then certainly you are at right place to find a solution to the problem, i.e. to  reset your Ubuntu user account password, regardless of what reason you have for resetting it.

STEP 1)   First, you have to reboot into recovery mode.

If you have a single-boot (Ubuntu is the only operating system on your computer), to get the boot menu to show, you have to hold down the Shift key during bootup.
If you have a dual-boot (Ubuntu is installed next to Windows, another Linux operating system, or Mac OS X etc and you choose at boot time which operating system to boot into), the boot menu should appear without the need to hold down the Shift key as shown below.

STEP 2)   From the boot menu, select recovery mode, which is usually the second boot option.




STEP 3)   After you select recovery mode and wait for all the boot-up processes to finish, you’ll be presented with a few options. In this case, you want the Drop to root shell prompt option so press the Down arrow to get to that option, and then press Enter to select it.
In recent versions of Ubuntu, the filesystem is mounted as read-only, so you need to enter the follow command to get it to remount as read-write, which will allow you to make changes:

[user@linux ~]$ mount -o rw,remount /

If you have forgotten your username as well, type
[user@linux ~]$ ls /home

You will see a list of the users on your Ubuntu installation. Select a user and enter the following commands
[user@linux ~]$ passwd <username>

where username is the username you want to reset.You’ll then be prompted for a new password.Type the password and hit Enter when you’re done. You’ll be prompted to retype the password. Do so and hit Enter again.Now the password should be reset. 

  

 

STEP 4)   Type exit to return to the recovery menu.

[user@linux ~]$exit

After you get back to the recovery menu, select resume normal boot, and use Ubuntu as you normally would .



 Note: Recovery mode makes me root user. Isn’t that a security risk?

Well, if you have several people using your computer, you can put small obstacles in their way by setting a root password, setting a Grub password, or setting a BIOS password. Still, anyone who has physical access to your computer and a little know-how practically has root access anyway. He can boot a live CD and mount your partition or even just physically remove the hard drive from your computer and put it in another computer. There’s a certain amount of trust you automatically give anyone by allowing him to sit at your computer.

Tagged with: , ,
Posted in Linux

REMOVE 32 bit PACKAGES FROM 64bit(CENTOS)

CentOS follows the upstream source in this respect, and the x86_64 installation by default will install ix86 32-bit packages on a 64-bit installation for compatibility purposes. Many server system administrators (and some desktop users) want a pure 64-bit system and so remove all 32-bit packages. This can be accomplished as follows:

yum remove \*.i\?86

To keep any 32-bit packages from being installed in future updates, edit your /etc/yum.conf and add the line:

exclude = *.i?86

Be aware that 32-bit applications, including some browser plugins that may only be available in 32-bit versions, will no longer work after this procedure.

You may also want to do this:

yum reinstall \*

The reason is that sometimes the /usr/share/ items (shared between BOTH packages) get removed when removing the 32-bit RPM packages.
Tagged with: , , ,
Posted in Linux

RPM MANAGEMENT TIPS

REBUILD THE RPM FROM SRPM
============================
The pre-requisite packages required are —
[user@linux ~]$yum groupinstall “Development Tools” 
[user@linux ~]$yum install yum-utils
OR
you can install only rpm-build.For CentOS most SRPMs targetted to be rebuilt on CentOS also need certain rpmbuild build macros and helper scripts, which are contained in package: redhat-rpm-config
[user@linux ~]$yum install redhat-rpm-config
[user@linux ~]$yum install rpm-build
The quickest way to rebuild the SRPM is to use the rpmbuild –rebuild command. This command will unpack the SRPM file into the specfile and the source files, and then it will build the RPM from the instructions on the specfile
[user@host ~]$ rpmbuild –rebuild /tmp/mypackage-1.0.0-1.src.rpm
If no error occurs, you can find mypackage-1.0.0-1.i386.rpm file under the ~/rpmbuild/RPMS/<architecture of your system> directory (for 64 bit it is x84_64 if 32 bit then it is i386). For more details read here
CHECK WHAT PACKET OWNS A PARTICULAR FILE
===========================================
[user@host~]$rpm -qf filename
[user@host~]$rpm -qf /usr/bin/crontab
cronie-1.4.4-7.el6.x86_64
CHECK WHAT CONFIGURATION FILE EXISTS FOR A PACKAGE
=====================================================
[user@host~]$rpm -qc packagename
CHECK IF THE FILES INSTALLED BY A PACKAGE ARE STILL IN THE SAME STATE AS THEY WERE WHEN INSTALLED
==============================================================================================
[user@host~]$rpm -qs packagename
This command will display all of the files installed by the package with a notation of “normal,” “replaced,” “not installed” and so on.
Tagged with: , , , , ,
Posted in Linux

BASH SCRIPTING


CREATE INFINITE WHILE LOOP WITH EMPTY EXPRESSION
==================================================

#!/bin/bash

while :
do
echo “infinite loops [ hit CTRL+C to stop]”
done

OR
#!/bin/bash
chekpt=5

while [ “$chekpt” -lt 6 ]
do

echo “infinite loops [ hit CTRL+C to stop]”
done
CREATE A FILE WITH PRESENT DATE -TIMEGROUP
============================================
 
#!/bin/bash

filename=`/bin/date +%d%m%y%H%M%S`

/bin/touch /tmp/$filename
 

Tagged with: , , , , ,
Posted in shell scripting
  • An error has occurred; the feed is probably down. Try again later.
  • An error has occurred; the feed is probably down. Try again later.
  • An error has occurred; the feed is probably down. Try again later.