Shell Programming Blog Shell Programming Blog

Wednesday, 17 October 2012

Linux Bash Scripting: how to rename or delete a file which has special characters in its filename

Unknown | 19:46 | | Be the first to comment!


Bash Linux Unix shell scripting

Suppose if you have created a file with a special character in its name because of a typo, you may not be able to access it. You may not even be able to delete the file. For example, see the below files.



If you see the output of ls command, you will not be able to identify the space at the end of the file. Hence you may not be able to access the file unless you specify a space at the end of the file using quotes.

The ? is a wild character for which any character can be substituted. Hence you may end up deleting or reading some other file.

To handle this situation, we can make use the inode of the file. Every file has a unique inode number with which we can identify the file. 



To rename the file:

You can specify the inode number in the find command as below to rename the files. The below command renames the file using  the inode number.



To delete the file:

You can delete the file using the inode number of the file with the help of "find" command as below.

 

Monday, 15 October 2012

Linux Bash Scripting: Perform floating point calculation/arithmetic/operation using BC

Unknown | 03:07 | | Be the first to comment!


 Bash Linux Unix shell scripting

As we have discussed on performing Arithmetic operations on our previous post, now we will look at the floating point operations in the shell script in this post.

You might want to look at : 
Perform Arithmetic operations or mathematical calculations in Shell scripting - expr command

A huge limitation in the Bash shell is that it supports only integer arithmetic. There are several solutions to overcome this limitation. The famous solution is Bash Calculator.

Basics of BC(Bash Calculator):

The bash calculator is actually a programming language that allows you to enter floating point expressions at the command line and then interprets the expressions, calculates and then returns the result. The Bash calculator recognizes the following:
  • Numbers (both integer and floating point)
  • Variables (both simple variables and arrays)
  • Comments (lines starting with a pound sign or the C language /* */ pair
  • Expressions
  • Programming statements (such as if‐then statements)
  • Functions
 The Bash calculator can be invoked from the command line using "bc" command.



As you can see from the above example, each time you enter the expression, you get the result immediately. To exit the BC you need to type quit.

Floating point decimal is controlled by a variable called "scale". Using this variable we can specify no. of decimal places that we would like to see in our result.



The default value of the scale is zero. As you can see from the above example, the Bash calculator returns the answer with zero decimal places. After setting the scale to 4, it displays the result with 4 decimal places.

The -q parameter suppresses the lengthy welcome banner of the Bash Calculator. In addition to the normal numbers, the bash calculator also understands the variables.



Once a variable is defined you can use the variable throughout the Bash calculator session. The "print" statement is used to print the variables and numbers.

Using BC in the scripts:

We can use the BC in our script in two ways.

1) Using the back tick character to run the "bc" command and assign the result to the variable as shown below.

        variable=‘echo “options; expression” | bc’

Below is a sample script which illustrates the above usage.




This method will be useful for small calculations. For the calculations which involves more complex expressions, the next method will be useful.

2) Using the inline input redirection, you can perform calculations which involves more numbers. The syntax of this method would be,

        variable=‘bc << EOF
        options
        statements
        expressions
        EOF
        ’

The EOF text string indicates the beginning and end of the inline redirection data. Remember that the backtick characters are still needed to assign the output of the bc command to the variable.

Below is an example script to show the usage of input redirection in BC.



Note: The variables declared inside the Bash calculator is valid only inside the BC and can't be used in the shell script.

Sunday, 14 October 2012

Linux Bash Scripting: How to ignore specific commands from being stored in the history in Linux

Unknown | 19:39 | | 3 Comments so far


Bash Linux Unix shell scripting

If you see the history of your commands using the "history" command, you can see all the commands that you executed in the command line. If you don't want certain basic commands to be stored in your history, you can ignore them using HISTIGNORE. You can specify all the commands that you want to ignore in the HISTIGNORE. 

In the below code, I have ignored the commands pwd, ls, ls -ltr and l using HISTIGNORE. Then I am executing some commands in the command line. Finally I'm checking the output of the history command.


[Note that history did not record ls, l, ls -ltr and pwd]

Please note that adding ls does not exclude the command ls -al from being included in the history. So, you need to specify the complete command to make it as an exception.

Linux bash scripting: How to repeat the previous command quickly in the Linux command line

Unknown | 11:32 | | Be the first to comment!


There are four different ways by which we can repeat the previous commands that we executed in the command line.

1) use the "UP arrow" to view the previous command and press Enter to execute it.
2) Type !! and press enter from the command line
3) Type !-1 and press enter from the command line.
4) Press Control+P to display the previous commands. You can keep pressing the Ctrl+P to older commands. When you see the required command you can press enter to execute it

Saturday, 13 October 2012

Linux bash scripting: How to search the command history (previously executed commands)

Unknown | 10:47 | | Be the first to comment!


Bash Linux Unix shell scripting

We often need to type long commands in the command line which we typed earlier. Instead of typing it again we can search the command history for the command we executed and execute it again. This technique will come handy when you are required to type a long command that you executed previously.

To search the command history and execute the command without editing it, Press Ctrl+R and type the keyword present in the previously executed command.

In the below example, I searched for the word "work", which displayed the previous command "cd /home/sittik/test/DDI/work" in the history that contained the word work.



Suppose if you need to edit the command before executing it, you can use the left or right arrow key without pressing enter. For example, you can search for "httpd" and which will display the command "service httpd stop" from the command history. You can change this command to "service httpd start" and execute it as shown below.




linux Bash Scripting: how to display the history of commands along with the timestamp

Unknown | 10:35 | | Be the first to comment!


Bash Linux Unix shell scripting

Usually the history command will display the command number and the command when you execute the command from the command line. Suppose if you want to add the timestamp along with the command history for auditing purpose, you can do it using the HISTTIMEFORMAT as shown below.



Friday, 12 October 2012

Linux Bash Scripting: How to rename multiple files in a single go

Unknown | 10:39 | 1 Comment so far


Bash Linux Unix shell scripting

You might come across a situation where you need to rename lot of files with similar file names either by editing the file name or by truncating the last part of the file name. There are two ways by which you can accomplish the task.

Method 1:

You can use the "rename" command to rename multiple files in a single go.

Syntax:

rename from to file...

- from is the word that you want to change in the file name
- to is the word that you want to replace with
- file is the file that want to rename ( you can use regular expression to select multiple files here

Example:

rename .html .htm *.html

This will change the extension of all the html files present in the current directory. The from and to keywords need not be the last characters of the file names. It can be anywhere in the file names

Method 2:

This method utilizes the "for" loop and "mv" command to rename all the files in a single go.




The above script will change all the html file name from *.html to *.htm

Windows tips and Tricks

Unknown | 10:16 | Be the first to comment!


I have personally tried and tested most of the tweaks discussed in this guide. However, I request fellow readers to be cautious while trying it out with your system. Always take a backup copy of all important data/registry before attempting to change the system/registry settings. Remember, prevention is always better than cure.



 

Shell Programming Copyright © 2012 Shell Programming theme is Designed by Abusittik, Shell Programming