Dec 30, 2012

Unix Shortcuts 1


a) !!    #to run the previous command 

e.g.,
>>pwd
>>!!  #executes "pwd" command

b) Control+P #move up through history list, one command at a time. (Up arrow key) 


c) Control+N #move down (or Down arrow key). 

d) !n #run nth command in history list. where n is a number from your history list. 


e.g.,
>>history

1 ls

2 pwd
3 vim test.txt
4 vim ~/aliases
5 vim ~/.vimrc
6 history

>>!3 #runs 3rd command in the history i.e., vim test.txt

>>!-2 #runs 2nd command from the end of history i.e., vim ~/.vimrc

e) !n:p   #only preview command number n (will not execute the command). 

f) !string   #Run most recent command starting with characters in string.


e.g.,

>>vim text.txt

>>pwd
>>ls -lrt
>>mkdir test
>>!vim     press<Enter> #Runs most recent command starting with "vim"
>>!ls press<Enter> #Runs most recent command starting with "ls"

g) Move the cursor to beginning of line in Unix Terminal
Beginning of line     Ctrl+a

h) Move the cursor to end of line in Unix Terminal
End of line           Ctrl+e


Find Files Using a Wildcard in UNIX


- Find command checks recursively in all the folders present with in that path

find files ending with "py" in current directory recursively
find . -iname \*.py

find files starting with prel_log in "/" directory recursively
find / -name perl_log\* -print

- The backslash \ character is important
- It tells the shell not to treat the wildcard character as a wildcard.







Execte the Perl script within the edited perl file


Suppose you edited a perl file in Vim editor
You can execute the perl script there itself without going back to command prompt, which saves you time

e.g.,

vim test.txt

press <Escape>
! perl %

- It executes the script which already edited

- if you want to execute a different script, mention the file name

press <Escape>
! perl test2.txt


- press Enter again to return to the edited file








Dec 17, 2012

vim edit multiple files


How to open multiple files in different tabs in Vim

]# vim -p file1.txt file2.txt file3.txt



How to access multiple files from the same window 

]# vim  test_1.txt        #Which opens test_1.txt

After editing this file, without closing or coming out of this file, do you know how to reopen a file.

:e test_2.txt       #Which opens test_2.txt

Now you want to reopen the previous file test_1.txt

:e#                 #Now you have already opened test_2.txt, but this reopens test_1.txt

after this, if you try to give the command again, it reopens previous file test_2.txt

:e#                 #Now you have already opened test_1.txt, but this reopens test_2.txt



Suppose if you want to open some other file, just give the file name and access
:e  test_3.txt


This trick helps you a lot when you have to work switching back to back two files and this way you can easily reopen the previous file without much typing effort


Want to learn more about Vim/Vi Commands, please refer to the following:
http://perlexamples.blogspot.mx/2012/11/vim-commands.html



Dec 5, 2012

Unix How to Find a Particular Directory


We know how to find a file in Unix directories using

This will search sample.txt in current directory

#]find . -iname sampe.txt


But if we want to find out a particular directory in the unix, type -d denotes search for directory

#] find / -name mysql -type d
/var/log/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/etc/mysql
/usr/lib/perl5/DBD/mysql
/usr/lib/perl5/auto/DBD/mysql
/usr/share/mysql


In order to narrow down the search, you could mention exact path (/etc) where you want to search

#] find /etc -name mysql -type d
/etc/mysql



Unix For Loop


#]mkdir for_test_dir1

#]mkdir for_test_dir2

#]mkdir for_test_dir3

#]cat for_test_dir1/for_test.txt
content 1
content 1
content 1

#]cat for_test_dir2/for_test.txt
content 2
content 2
content 2

#]cat for_test_dir3/for_test.txt
content 3
content 3
content 3
...
...

#] ls
for_test_dir1 for_test_dir2 for_test_dir3 for_test_dir4 for_test_dir5
......
......


#]foreach var (`ls`)
foreach? ls -ltr $var/for_test*
foreach? end

O/P:
It will list all the for_test files present in for_test_dir's

Dec 1, 2012

pushd popd example

When you have  to work with navigate different directories quickly, its difficult to remember the whole path or type the whole thing again and again.

There is concept in Linux/Unix to manipulate directory stack, you can use directory stack to push directories into it and later pop directory from the stack.

1) dirs : Display the current directory stack without pushing or popping a new directory

2) pushd : Push directory into the Stack

3) popd : Pop directory from the Stack

Using pushd and popd effectively can help you get around your command-line environment quickly


E.g.,

]# mkdir /usr/local/dir1

]# mkdir /usr/local/dir2

]# mkdir /usr/local/dir3

]# dir
~

]# pushd /usr/local/dir1

]# dir
/usr/local/dir1   ~


]# pushd /usr/local/dir2


]# dir
/usr/local/dir2   /usr/local/dir1   ~



]# pushd /usr/local/dir3

]# dir
/usr/local/dir3   /usr/local/dir2   /usr/local/dir1   ~


]# popd

]# pwd
/usr/local/dir3

]# dir
/usr/local/dir2   /usr/local/dir1   ~


]# popd

]# pwd
/usr/local/dir2

 ]# dir
/usr/local/dir1   ~


]# popd


]# pwd
/usr/local/dir1

 ]# dir
~