Home » 2017 » January » 21 » Useful tips and tricks while working in Linux.

7:33 PM
Useful tips and tricks while working in Linux.

Hi Linux lovers,

 

I am back with some cools tools from my box of Linux.

We all want that our work should be easy and fun and we always witness that many of the tasks are automated reducing manual efforts. We shall talk about some useful logic/ideas that will enable you ease your daily work in LINUX./UNIX/AIX environment.

 

1. Use as many aliases as you can

There are several command that we have to enters very very frequently. like going to certain folder, checking some process is running or not listing latest 10 files in a directory etc. so why to enter the entire command again and again?

Create custom and logical aliases for them. 

Eg. going to some folder, lets saty path is long:

alias cdl='/path/to/the/folder/'

Check if TWS is running

alias tws='ps -ef|grep -i tws'

Listing latest 10 files in a directory.

alias lt='ls -ltr|tail'

Add them to .profile file in your home directory to make them permanent.

 

2. Find and replace certain path in all scripts in AIX.

You may find it easy to find and replce a pattern in Linux using sed as below:

sed -i.bak 's/find/replace/g' file1

The above command will create a backup of file with .bak extension and also perform find replace in the file. But unfortunately -i option (to take backup and make changes in file) is not there in AIX.

So, if we do below command on a file, it will just print the updated file on the console but the file remain unaltered. Question is how to do it in AIX, i f we have to do it several files in a folder and sub-folders.

We can use simple trick to do that.

step 1: find . -name "*.sh" -type f -exec ls -l {} \;

The aboce command will list all sh file in a folder/subfolders.

Step 2: for i in `find . -name "*.sh" -type`; do sed -e 's/findpattern/replacePattern/g' $i; done

The above command will perform find/replace in all files but they will be printed on console the original file remains the same. Lets move one step further.

Step 2: for i in `find . -name "*.sh" -type`; do sed -e 's/findpattern/replacePattern/g' $i > $i.new; done

The above command will create a filename.sh.new file for each file with the updated content. But the original file will also be there. Now we try another command.

 

step 3: for i in `find . -name "*.sh.new" -type f`; do echo $i|cut -d "." -f2,3`;  done

The above command will print the new name which will cut .new from the file name form rear. Run this command first and check if you are getting filename.sh.

step 4: for i in `find . -name "*.sh.new" -type f`; do newFilename=`echo $i|cut -d "." -f2,3`; mv $i .$newFilename; done

The above final command will replace all file names filename.sh.new back to filename.sh and that's all we want.

Note:- For The above logic to work, there should be no "." in the file name except only in extension.


2. FC command in AIX

fc has many beautiful usage in aix. Its like history command only but it has better edges.Edit and run last command:

fc -e vi

Suppose we write a long command later we come to know that there is typo. Use above command, it will open vi editor to edit the last command. Make necessary changes and save and quit. The modified command will be executed.

fc -l

To list the last 10 commands executed.


3. Vi editor to open multiple files simultaneously on singe editor

We can open more than one file side by side (vertically) or in stack manner (horizontally).

vi -o a b c 

vi -o3 a b c

vi -o *.sh

The above command will open 3 files a b c in stack manner. we can specify N as the number of sections to open

vi -O a b c

vi -O3 a b c

vi -O *.sh

The above command will open 3 files a b c in vertically i.e. side by side. we can specify N as the number of sections to open.

To switch from one section to other:

Press CTRL+w and then press <- (left) to go to left section and do your editing.

Press CTRL+w and then press -> (right) to go to right section.


4. Touch command to modify timestamp of a file.

We mostly know that the touch command is used to touch a file at the current timestamp. We might need to modify a file but we don't want to change the file's timestamp. We can keep the files' old timestamp while modifying the file actually.

1. Note the current timestamp of the file

2. Modify the file.

3. set the timestamp of the file back to previous timestamp.

touch -t yyyymmddhhmm file

eg. touch -t 201702241628 file1

 

 

To be continued.......

 
 

Category: Open System-Linux | Views: 1515 | Added by: shanky | Tags: scripting, unix stuffs, tips, cool ideas, Tricks, shell | Rating: 0.0/0

Related blogs


You may also like to see:


[2014-02-12][Open System-Linux]
SCP a secure copy program in Linux
[2015-06-19][Open System-Linux]
How to create swap file in Linux : Adding extra swap space
[2015-01-18][Open System-Linux]
The JAR archiving tool in Linux
[2017-08-13][Open System-Linux]
Perl trick to remove Ctrl - M charater from a file
[2014-09-26][Open System-Linux]
Traceroute command in Linux

Total comments: 0
ComForm">
avatar