GAURAV KUMAR

Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Sunday, April 24, 2011

Rename multiple files with bash script

Hi there,
Recently I came up with a problem related to filename of downloaded files.In my college I use DC(Direct Connect) client to download files from other peers.Recently I downloaded a large number of music videos which remained in temporary directory and did not move to music directory.Although It downloaded completely, It remained in temporary directory and hence name of files was in the format "filename.(avi/mpg/flv etc.).(some chars).dctmp". So I had to rename them.Definitely, I couldn't do it manually as there were more than 1000 files.Hence I tried to write a bash script for renaming all of them at once.
Initially I tried to store the filename as "*.*.dctmp" in which I would trim the "*.dctmp" part.Hence the remaining part would the desired filename(with proper externsion).But storing the filename like that did not work out.
Then I saw that in temp files, there is a common pattern that before "dctmp" extension, there are exactly 39 chars. Then I tried a different method of trimming filename which is explained below with code.


code :

for i in *.dctmp
do
len="${#i}" ;
let len-=46
#echo "$len";
name="${i:0:len}" ;
#echo "$name"
mv "$i" "$name" ;
done

save it with name.sh and run "bash name.sh" on terminal.The script should be run in the same directory in which temp files are present.

e.g. Guns N' Roses - Welcome To The
jungle.mpg.47IMRQGAGFWOA7GMLZTK3X6QFIGR4P4EKHDRHKI.dctmp
became Guns N' Roses - Welcome To The Jungle.mpg

Worked on ubuntu 10.10

Monday, March 21, 2011

password reset from shadow file in ubuntu 10.10

Hi,
Recently I was trying to write a script for changing password automatically for a user.I required something else for this but I found one similar script while searching over Internet.While I was experimenting with this on my ubuntu 10.10 system, I did a terrible mistake.At the place of username, I put my main account username.Not only this,I used "pwgen" program to generate random password which was there in the script and I blindly used it.

The script I used is below:-

touch pass.txt
chmod 600 pass.txt

# generate a nice secure password and put it in a file
pwgen > pass.txt

# get the password hash
ph=$(makepasswd --clearfrom=pass.txt --crypt-md5 |awk '{print $2}')

# Set the password
usermod -p $ph "username"


Now, first time I used it using sudo, It asked for password.Now this script randomly generated a new passowrd and set for my account.Now, a new password was hashed in /etc/shadow file which I didn't know.Next time, when I used it again,it asked for password which was changed randomly.So,I could not give correct password.


Now, I thought how would I login after next reboot.I asked someone who told me to use a live cd of ubuntu to change shadow file.In shadow file, password is stored encrypted corresponding to each username.Now, I was preparing for a back up.Still, I wasn't sure it will work.

When I rebooted,something clicked in my mind.I booted in recovery mode.Then using root shell, I opened /etc/shadow file.Usually it opens in read-only mode.But since I was root that time, I removed the hash of my username.I removed everything that was there between first 2 colon(:). I did it only for my username and root.Hence, password was omitted.Now I reboot again and reset the password using passwd command and it didn't ask for current password.I didn't know that we could reset our password in recovery mode.

I knew that this similar method worked in ubuntu 8.04. But I thought they removed this as a bug.Now it worked for me.I can't decide if it is a good or bad thing.Since I changed my password in emergency,It is cool. But someone else can access my account by resetting my password.

Saturday, April 10, 2010

GPG public key

Hi,
As I was searching why my gpg public key is not working,I suddenly found answer.Actually gpg public key contains some extra words also which I neglected.public key can be viewed in terminal by typing :

gpg --armor --export [your public key id]

Now complete output is the public gpg key including "-----BEGIN PGP PUBLIC KEY BLOCK-----" and "-----END PGP PUBLIC KEY BLOCK-----".Initially I was taking only middle part.One more thing public key requires public key id which can be viewed by typing "gpg --list-keys". The pub part shows id.This command displays whole list of keys.So, if someone has made more than one key pairs,then it is useful to see which public key is for which public key id.As I successfully generated my public key, I am going to share it. It is right below :-

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.9 (GNU/Linux)

mQGiBEu+azsRBACGVGTbi3GHDKa8rkzOWP8lQYg0omH6mU5tIHe+0fLiTMRzbr1o
/lTT2w+GksRhziWL03+zTc36R/gNAPJvp8djhaeDpYD0DCh1JuFNLPAZWVMQ1TJf
INgQ59KoBr/sWHWa0DXTFoTK1o/0wLsUioH54nN4rfxsPQgKMA5f/FYEzwCgitK5
WeQWsm8RMkZURaROcq7kHE0D/0GsXazBA0n+iQFVbbjblOpzs0/nvJfnEWKEbyCv
e9NUrbNqvttA501uy+VeIlMIbVzIOHAMeIpgWMT+z6vVYcBmKrZGCRv0i9gBzrd/
8HVWbVrZDwz7Mk076f/1wo5iLv9OP657O6i3B4F/1fiBmJubTQk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e+Eir49efX+gMivn+ttupOZ2t7OcCH6IaCr0Y6E1Iz6
BCveiEkEGBECAAkFAku+azsCGwwACgkQdFo67FlOrR0zFACfdSLkUkkvO++A6XyH
2ItqsnYVgCQAn0iB1mZ1eI7xuUyR6y+teREIc4Bf
=+Uo3

-----END PGP PUBLIC KEY BLOCK-----



And Now I am working on sending a mail with encryption by my private key.Hope,It works.

Thursday, July 23, 2009

symbolic link(linux) Virus Incident

hello friends,
today I would like to discuss a very simple topic related to linux and how it bothered me.recently I was working with linux trying to learn "symbolic links".Symbolic link is a special file which points to another file in the system.it is similar to shortcut in windows.

if you give the command ls -l in your terminal then you can view link files.if output starts with l then it's symbolic link.now,to create a symbolic link we have to use "ln" command with -s option."ln" is for link and "-s" is for symbolic option.syntex is like this ln -s source destination.

now, source means the path of file or directory where it originally is. and destination means the path where you want the link to be.you have to specify the name of link and this should not exist in system otherwise it will give error.

But I did something which I forgot later.I made a symbolic link by typing this ln -s /home/gaurav /home/gaurav/h. since every file of linux was in "gaurav" folder ,when i went in "h" folder inside "gaurav",i found all the files again in this folder including "h".when i again double-clicked "h" it took me to same folder which contained all files including "h".I didn't remember about symbolic links and instantly I was worried that there is some virus in my system.I had heard earlier that there is a virus which creates a folder in each folder including itself.

after some days when I opened shell and typed ls -l, I found "h" showing l which means it's a link only.then i was relieved and deleted this. then I typed ln -s /home/gaurav/videos/jal.mp4 /home/gaurav/Desktop/jal. by this I am able to view this video directly through desktop. I need not go anywhere.and this is done without copying the video or without "cut and paste" from its original destination.

this was a new thing for me.so, I thought it would be good to share this.May be someone new like me would get help from this.But , if you are master of shell, you may laugh at me.


thanks everybody to read this post.

Wednesday, July 15, 2009

CLI over GUI

hello folks,
I am learning a bit linux nowadays.so, I thought it would be better to write something related to that.Don't worry , i will not teach you commands here(there are so many sources for that). I was thinking that CLI(command line interface) seems to be boring and cumbersome to use and I know so many people would say the same.Most of us are used to of windows' GUI(graphical user interface).Around 5 months ago my friend bought a laptop and got windows vista as default OS by that company.When i asked him to use linux,he said that as he had been using windows since childhood,he was more comfortable with that.Now i think that how can you compare two things without knowing both. If you also admire GUI then this is for you.CLI has really got some advantages over GUI.lets get it one by one
  • with a GUI, only the options provided by the interface are available to the user

  • lack of space on a screen can limit the number of options that a GUI displays

  • no standard methods exist within GUIs for performing standard command-line tasks, such as linking commands in sequences, redirecting output from one destination to another, or collecting commands into scripts


Shell keeps history of recent commands.its very useful.and, if you have to perform certain operations in sequence then you can create shell scripts also.shell script is just like batch files in windows.

there is one more thing that i have recently come to know.shell is more useful in hardware terms.Shells are much less resources-intensive than GUI's.shell requires less memory.It means that command issued in shell will run more quickly than in GUI.

Only thing that i feel difficult is to remember commands.well,once you get used to it,its much easier than you think.
That's it for today.hope,you got something useful.