Tag: linux

Bash : Delete some characters from a file with TR

in this little example of usage of the command tr
we want to clean a file so the values contained in it can be used
Example File

In my example i wish to only keep the value and deleting the character < and >

the tr command used with the -d (delete) option will allow you to delete the list of characters contained between the quotes.

You will obtain the following result

Re-apply .bashrc

After you made some changes on your .bashrc file (like creating some new aliases) You will notice that the changes are not applied upon the saving of the .bashrc file

After having saved the file , first insure you are in your home folder, by typing cd .

and to reapply your .bashrc config file , launch a :

after this you can test you alias right away , it should work

 

Display your current external IP address with WGET

To know your ip address , you can use the command ifconfig
You will know the ip address for each of your network interfaces.

But if you are not directly connected to the internet and use a Gateway that’s nating the internet traffic for you , ifconfig is not going to be helpful to know the ip that connect you to the internet.

with this command the current external ip address associated with you default gateway will display

bash : Put the source of a webpage in a variable

When you do some bash scripting , this is often useful to get data from  webserver because they are a very simple way to exchange data from computer from computer.
to put the data that’s available on a webserver into a $var you just have to use the command :

this will fetch the data from http://server.com/file.htm in a non verbose way and put it in variable named var .

you can now do whatever you want with that $var , like greping it for example.

 

Extract values separated by character ( : , . – )

In a bash script if you find yourself with values presented that way

you might want to keep only the second or third of this values that are separated by the : character

like always , in bash there is a lot of ways to accomplish that , but i like to use a one that use the AWK utility because it’s very easy to read and understand.

my list of value is contained in a var name $var let’s extract the third value which is 18.5

Will return the value 18.5

but i can do some math operation in the utility, like :

this will return 326.5 the result of 345 – 18.5

awk is a fast and powerful text editor , there is a lot things you can do with it.

Linux : make an USB bootable thumb with an ISO file

you downloaded the iso of some distrib and you want to make a flash thumbdrive bootable by writing the ISO on the thumb ,

a simple copy and paste is not going to work :

On linux , there is a very simple command line utility to do just that.

first plug your usb thumbdrive on your computer , it will probably automount.

you need to unmount that drive before continuing.

to find out the name of thumbdrive , you can type :

You will get a result that look like that

/dev/sda1 on /media/flash type vfat (rw,relatime,fmask=0022,dmask=0022,codepage=cp437,iocharset=ascii,shortname=mixed,errors=continue)

my flash drive is mounted at /media/flash  the name of the device is /dev/sda

to unmount the drive , jsut do a :

then you need to

after a little while , the utility will exist

your thumbdrive is now a bootable thumbdrive.

Using sed to delete a word from a string

For example in a bash script you need to delete one or more word from the string , you can use sed to do just that

sed is searching for “word to delete” and replacing it with nothing ,

you can of course pipe to that command ,

example :

the var $visitor contain the string : ‘current number of users: 234’

But only the number (234) is usefull to you ,

your can do :

the result is going to be : 234

 

Renewing your IP address on Linux with dhclient

Let’s say you need to change you ip address because there was a change on the network and now ip settings changed,
you can at anytime request with the command : ‘dhclient‘ a new address to a dhcp server ,

if you only have a single network adapter, the command is very simple
just launch the command

to release the address currently associated to you
then just launch

and you computer will broadcast a dhcp request
if you have multiple adapter ,
you can do

dhclient wlan0[/code]
to spécify that you want a new ip address for you wlan0 adapter
by default the command dhclient is silent , meaning that you will not get any information about what the dhcp server answered.
to add verbosity to the command , just add a -v parameter

Linux epoch time or Unix time


If you need a convenient way to store the date in a database you will certainly use the unix format.
it’s very convenient because it doesn’t change notation from country to country.

You can later convert it back to a format that’s human readable.

in a shell on linux if you wish to display the current unix time. just type

date +%s

But if you need the unix time for a specific date ,

date –d=’03/14/2016 00:00:01′ +%s
or
date –d=’14 march 2016 00:00:01′ +%s

There is also the function to input an interval

date –d ‘1 hour ago’ +%s
date –d ‘1 month ago’ +%s