Category: Linux

Bash script to clean movies name

The issue when you download movies online, is that most of the time the file name is going to be complete trash , filled with useless info like the team that made the rip or the website from where the movie was downloaded.
This is very annoying when you sort by name and the website made the idiotic decision to put their name in front of the actual film name.

for example here a typical movie name
[ ] Dont.Think.Twice.2016.FRENCH.WEBRip.XviD-NEWCiNE.avi
the actual useful data that i wish to keep is Dont.Think.Twice.2016.avi

for this purpose i’ve developped a small bash script that will list all the files in a chosen directory then , if necessary , will rename the file.

this is not a very efficient script
but it’s fulfill it’s purpose and very rarely encounter a filename that he is unable to clean.

BASH:check if a number is even or odd

In a bash script you might need to check if a var is even or odd , 

in my case im going to use a if to make that check

You can use this simple code to make that check and display the result.

the result will be : 

BASH : Make a SQL query from bash

MySQL is a very convenient way to store data .
To use stored MySQL data in a BASH script you can use this simple command

this will display the result in you terminal without ASCII decorations.
if you need to use that data for something , you can store it in a var

BASH : IP validation 

if you have a list of ip to validate ,
you can use this little bash script ,
this will open your IP list ,
then test if all the 4 numbers that make a standard IP.are beeten 0 and 255

if the ip is valid then the script will return valid , else it will return : not valid

after execution , you will get the following result :

Use AWK to display x-th and y-th lines after a regex match

In a script you might be interested to keep 2 or more lines after a regex match ,
the line that interest you are not always going to be right after the match ,
the first one might be X lines after the match and the second one Y lines afters the match

in this example , i’m interested in keeping the lines with the price and the number in stock
i’m going to use awk to search for item desc , and display the 4th line after the match, and 6th line.

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


If you try to connect using a key system on ssh instead of a classic password login, you might encounter this error message.

this message tells you that because other users can read the content of the key , shh refuse to use it to try authentification on you host ssh.

to fix that problem you should change the permissions on you file so that only you can read it

the 3 numbers in the chmod command are like this
the First is for you
the second is for your group .
and the last is for other users.

If you want to limit the access of others to you ssh key
you could do a

after this command , you can relaunch you ssh command , and you should connect without providing a password.

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


BASH : Delete the last X chars from a string

sed delete last x chars

When you scrap data in HTML , it’s common , depending on the quality of you regex to end up with a string with some useless character at the end (Or the Beginning),

If you string looks like that :

My useful data</a> <

You can use the command head to drop the X last character of your string ,
So in my Case , to Keep only the string My useful data and drop the </a> <

I’m going to pipe my string to

This will delete the last 6 chars of my string , giving the data that i need later in my script

but in some implementation of the head command this will not work ,

you can use an alternative with sed

This will delete the last 4 characters of your line ,  or alternatively