How to delete spaces from file name in Linux?

If your file name has spaces in it and there are many such files that it is not possible for you to rename each file’s name manually then you may use the command given below. It will rename all the file names which have spaces by replacing the spaces with an underscore ( _ ).

for FILE in *; do mv "$FILE" "$(echo "$FILE"|tr ' ' '_')"; done

or

for file in *; do mv "$file" `echo $file | sed -e 's/ */_/g' -e 's/_-_/-/g'`; done

I had two files named a b c.txt and x y z.txt in my directory. I used the command given above to delete spaces from file name. It changed the file names to a_b_c.txt and x_y_z.txt respectively. See the screenshot below:

How to install rpm package on linux ?

install rpm package

install rpm package

RPM was the most famous way of installing packages on a Red Hat based linux box, nowadays other distros are also using RPM to support their software, Suse and Mandriva fall in the zone. RPM stands for Red Hat Package Manager. RPM lets you install a package, upgrade and uninstall the same. It also allows a querying functionality which lets one know about already packages installed on the system.

RPM packages are those with a .rpm extension. RPM packages contains the actual software to be installed and other files that are needed to carry out its installation. RPM packages are distributed distro specifically. Any attempt to install one distro-specific package on another distro might result in undesired consequences. You have been warned !

Graphical installation tools install a package in few clicks but here discussed is old school command line method which has its own legacy.

From here on, you need to have root’s privileges.

To install rpm package, rpm command is used along with -i option which clearly stands for “install”:

rpm -i package.rpm

Packages are subjected to regular upgradations, to update an installed rpm package, engage following code.

rpm -U package.rpm

On a fine day, everything will go well and the rpm package will be installed on your system ready to be launched from command line. Installed software, rather than creating its own directory, goes into pre-created linux directories. Executable files find their path to specific bin directories. To shoot the program from command line, the whole path is not needed just the program name is sufficient.

If things go south during installation, it can be very irritating. Most famous error that comes up is due to failed dependency. Dependencies are the packages that are needed for installing package to mover further in installation process. They are just software that are needed for proper functioning of the package being installed. While installing, the database is checked for needed packages, if not present, it stops the installation process due to failed dependency.

To rectify, one way is to install/upgrade those missing packages (don’t worry it will tell you which packages are not present ) then continue with the current installation. Some times, this error is as meaningless as ‘ay’ in ‘okay’. An entry is made in database only when the package is installed via rpm. If any other method is used, the entry is not made and despite presence of files, “failed dependency” error shows up.

When you are sure that the needed files are already on your system, you can skip the dependency check and install rpm package anyway.

rpm -i package.rpm --nodeps –nodeps option installs it without checking for dependency.

If you try to fool your system by going with –nodeps right away, the program won’t work at all.

Removing the rpm package

rpm command along with -e option is used to remove a package installed with rpm.

rpm -e package.rpm -e is for “erase”

Note: While installing rpm package we included the whole package name but at the time of removing- only the program name can be used ignoring the extension.

rpm -e package This code works the same as above.

It removes all the files related to that package and also deletes the entry from database. Manually removing individual files will not affect database which can lead to dependency errors in future.

Making a query to database:

As mentioned earlier in the very article, the database knows what packages are installed on the system. The database can be queried to check if certain package exists on the database. For a single package query, command goes like:

rpm -q package

If the package is present, it returns the version of package installed, else, it says the package is not installed.

To list all the rpm packages installed on the system, run :

rpm -qa

To check for certain package in the list, “grep” can be used. We will cover that later, stay tuned.

How to find the CPU information from terminal in Linux?

lscpu output

You can use lscpu command or can view the file /proc/cpuinfo to get the CPU information from terminal.

Man page description of lscpu command:

lscpu gathers CPU architecture information like number of CPUs, threads, cores, sockets, NUMA nodes, information about CPU caches, CPU family, model and stepping from sysfs and /proc/cpuinfo, and prints it in human-readable format. Alternatively, it can print out in parsable format including how different caches are shared by different CPUs, which can also be fed to other programs.

$ lscpu

or

$ cat /proc/cpuinfo

Output:

How to delete all files of a directory in Linux?

How to delete all files of a directory in Linux?

To delete all files of a directory in Linux there are 2 methods available.

Suppose you want to delete all files of the directory dirname whose path is /home/usr/dirname then to delete all files of this directory you may use the 2 methods give below:

Method 1: Use rm command

$ rm /home/usr/dirname/*

or

$ cd /home/usr/dirname/
$rm *

Note: The above method will not delete files from sub-directories of dirname. To remove the files from sub-directories use

$ rm -r /home/usr/dirname/*

Use f option to force delete the files.

rm -rf /home/usr/dirname/*

Method 2: Use Find command

$ find /home/usr/dirname -type f -delete

or

$ cd /home/usr/dirname
$ find . -type f -delete

Note: Using the above method will delete all the files from sub-directories of dirname but it will not delete the sub-directories.

Edit: find and delete all files and folders (sub folders) [credit: ubungu]

find path_to_find -name ‘*test*’ -exec rm -rf {} \;

How to install software in linux from source file (.zip, .tar.gz, .tar.bz2) ?

Before getting your hands on it, here is a heads up, installing package from source is not the most preferred method. Few might find in a difficult method. It is recommended for experienced Linux users or those who have a thing for exploring more. Generally mostly used software are shipped with the OS and the rest can be easily installed from their respective package managers.

Moving forward, standing by the definition of open source, some packages are in the form of source code. They are distributed this way. One can download the source file for the desired application, it is then unpacked, compiled to turn it into binary. Upon completion you would find that it is not that tough job. Most of the software you would require are still distributed in source form.

The source files all over the Internet are found (not only) in the zip file for tarball. Extensions like “tar.gz” or “tar.bz2” or “.zip” are a common view. It is recommended to follow this procedure from your home directory. From now on, it is assumed that the downloaded file resides in “src” in home directory. Lets create one:

mkdir $HOME/src

Navigate to “src” direcotry through “cd” command.
cd $HOME/src

Lets see what we have in their by using “ls” command which is used for listing directory contentss.
ls

We can see our recently downloaded source file. We now need to unpack it. Different methods are employed to unpack different kinds of files.

For .zip :
unzip

For .tar.gz :
tar -xvzf

For .tar.bz2 :
tar -jxvf

Now you will see a new directory having all source files. Use ls to see if it exists.
ls

Now go in that direcotry.
cd

At this stage, few applications have an INSTALL file and some don’t.
If it is there(use ls command to check) then you can read it by “more” command.
more README

Lets proceed with installation, it is completed in three stages:

  • Configure
  • Compile
  • Install

For pre-installation configuration:
./configure
This will check your system for essential requirements and is responsible for creation on “makefile”. “MakeFile” is needed by “make” utility and directs it for how the compilation should be carried out.

If you encounter an error, forums are always at your disposal. Be sure to search for it before posting. Also include the output you are getting in the post along with your system specifications. OR you can post in here in comments as we will be pleased to help you with that.

When you are done with configuration phase, next comes Compilation. Use “make” to compile it, compilation is carried out by following the instructions in “MakeFile”.
make

Final step, install binaries in the system. To do so, you need to be root. “su” command is used to switch user. Enter the password when prompted.
su

To finalize the installation, execute:
make install

And voila ! Now you know how to install software in linux. You might want to check the software documentation.

How to run a Linux Shell Script?

Have you ever wondered how you can run a script in Linux?
Every script that needs to run should have a execute permission. By default it is refrained from such permission. One has to assign execute bit manually. In order to do so execute following command:

#chmod +x SomeScriptFile
OR
#chmod 0755 SomeScriptFile

ls command can be used to check permissions on files:
# ls -l /bin/uname

Output:
-rwxr-xr-x. 1 root root 25948 Feb 8 2011 /bin/uname

[uname is being used for an example, it can be replaced with any script]

To run script:
$ ./SomeScriptFile [Enter]

You can also execute by specifying its full path
$ /full/path/here/SomeScriptFile.sh

For executing it in ksh shell:
$ ksh SomeScriptFile

For executing in bash shell :
$ bash SomeScriptFile

Illustration:

Creating a test shell script test.sh, any text editor can be used.
#!/bin/bash
echo “ Hello, Free World”
echo “ You are at : $(pwd)”

After saving this file, set the execute bit:
$ chmod +x test.sh
$ ./test.sh

./ can be omitted if current directory has its entry in PATH variable. It is advised to have your own bin directory. Add this directory to PATH variable using export command. Move your recently created script in this directory and save all the hassle of writing whole path, now you can execute this command from anywhere. This procedure is shown below:

$ mkdir $HOME/bin
$ export PATH=$PATH:$HOME/bin
$ mv test.sh $HOME/bin
$ test.sh

How to install RPM package on Ubuntu?

install rpm on ubuntu

Ubuntu users are used to using Synaptic for their software needs and the other user chunk has apt-get via command line. But Ubuntu users don’t have to be sad for those packages which are still distributed in RPM format. Installation of RPM packages can be checked here.

If an Ubuntu user wishes to install a RPM package, a utility is created for this purpose and is called Alien. This doesn’t guarantees that the package will work, its actually a software that converts format of the package. In order to install Alien some prerequisite software are also required including gcc and make.

To install Alien utility, following code should be executed:

sudo apt-get install alien dpkg-dev debhelper build-essential

CONVERTING RPM TO DEBIAN

For performing conversion, run the following command:

sudo alien SomePackage.rpm

To continue with the installation, dpkg tool can be used as follows.

sudo dpkg -i SomePackage.deb

How to find the partition of hard disk belonging to a specific file?

How to check to which partition a particular file belongs?

Following procedure will guide you to discover that partition on which a “file” resides.

We do this with the help of df command. It is used get a report of space utilized on disk and also mentions files/directories.

This command is used as follows:

$ df
$ df /etc/TestDirectory
$ df /etc/TestFile

df can be followed by any path to desired location. Lets understand with an example, we are here finding out location of /etc/gtk-2.0/

$ df -T /etc/gtk-2.0/

This command generates an output which looks like following:

Filesystem   Type 1K-blocks Used    Available Use% Mounted on
/dev/sda7    ext4 18141972  9862580 8095148   55%  /