[How To] Solve “command not found: ^M” Shell Error

Terminal-icon-shell-linux-unixProblem with command not found ^M:

The other day I was trying to execute this simple shell script. The shell script was using Z shell. when I tried to execute the shell script, it threw me the following error:

script.sh:2: command not found: ^M
script.sh:4: parse error near `then^M’

After digging a little bit in to the problem, I found that this character ^M represents an extra useless space. But this was the weird part. There were many spaces in the shell file but it was quite common and most certainly not useless. Even if I tried removing the “troublesome” extra space, it keep on throwing the same error for some other extra space.

Reason:

The reason which was causing this error was that I had used Windows explorer to write the shell script. What I meant that I had my Linux network drive mounted in Windows. I wrote the shell scripting in Windows, saved it as .sh file and tried executing it. And that is where it went wrong.

You see, when I used Windows to write the program, it saved it in Windows format. And this is why Linux could not encode it properly. A simple file command showed the “true color” of this “traitor Windows” file.

Solution:

Simple most solution to get rid of this error is to convert the Windows file to Unix (read Linux) format. dos2unix tool comes handy in this case. It comes pre-installed in many Linux systems. Here is how to use it:

dos2unix script.sh

where script.sh could be your shell script file name.

The output of the command may look like this:

dos2unix: converting file script.sh to UNIX format …

That is it. No need to do anything further. The next time you execute your shell script, it will not show the ^M command not found error. Cheers :)

How to copy all the content of a directory in Unix without “cp: omitting directory” error [Quick Tip]

Problem Scenario:

You want to copy all the contents of a directory (lets say source_dir) which contains lots of other files and subdirectories inside it to another directory (lets say target_dir). If you simply try cp command it may result in the following error:

command: cp source_dir target_dir
output: cp: omitting directory source_dir

If you try recursive copy (using option -r) the directory is copied inside the target directory which you may not be suitable if you want to duplicate the contents of the source directory.

command: cp -r source_dir target_dir
command: ls target_dir
output: source_dir

Solution:

If you want to copy just the contents of the source directory as in duplicating all its contents use the following command:

command: cp -r source_dir/* target_dir

This command will copy all the content (not the directory itself) in the target directory.

How To Install Gnome 3 in Ubuntu 11.10

April 2011 saw a change in Ubuntu where it launched Unity instead of Gnome as its default desktop environment. That did not went down well with many users who wanted to use the newly launched Gnome 3 in Ubuntu. Ubuntu 11.04 did not provide support to Gnome 3 officially. Enthusiast users did find a way to install Gnome 3 though but it resulted in breaking the Unity environment completely which means that a user could have used either Unity or Gnome 3 not both. [Read more…]