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/*


$ 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


$ 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 {} \;

Types of Filesystem, differences and dealing with them using Perl

If you are looking to write code in Perl which will work on multiple platforms then you must have the knowledge of filesystem of each platform. Filesystems are categoriesed in 3 parts.

1. Filesystem of Unix

FFS (Berkeley Fast File System) is the ancestor of all current filesystems of Unix variants. The filesystem has been extended by different vendors according to their needs. Like for better security they have been extended to provide support for POSIX ACL (access control list).

In Unix filesystem the root is denoted by / (forward slash). The path of any directory in Unix filesystem starts from / and goes deeper in the system. The directory names or file names in Unix filesystem are case-sensitive.

Path Example: /root/home/user/Desktop/file.txt

2. Filesystem of Windows

Operating systems which are based on Windows supports 3 filesystems: FAT (File Allocation Table), NTFS (NT FileSystem), FAT32 (Advance version of FAT).

FAT filesystem is not case-sensitive. It uses \\ (backward slash) as path separator. In the FAT systems the direcotries and files contains some flags, these flags are known as attributes like “Read Only”. NTFS supports unicode feature.

Path Example: C:\\home\\user\\Desktop\\file.txt

3. Filesystem of Mac OS

Classic Mac OS used HFS (Hierarchical File System). The version 8.1 of Mac OS uses HFS+ which is an advance version of HFS. The current filesystem looks similar to Unix filesystem. The method of showing paths is same in both OS. The difference between these is that Unix file system is case-sensitive whereas HFS+ is not case-sensitive.


OS and FileSystem Path Separator Filename length Absolute path format Relative path format Unique features
Unix (Berkeley FFS and others) / OS-dependent number of chars /dir/file dir/file OS-variant dependent additions
Mac OS (HFS+) / 255 Unicode chars /dir/file dir/file Mac OS legacy support, BSD extended attributes
Windows based OS (NTFS) \\ 255 Unicode chars Drive:\\dir\\file dir\\file File encryption and compression
DOS (basic FAT) \\ 8.3 Drive:\\dir\\file dir\\file Attributes


Using Perl to deal with different Filesystems

By now we know all the filesystems differ from each other and to deal with these in Perl we use File::Spec module. This module is used to hide the differences between these filesystems. Let us learn how we use this module. We will use the catfile method to get the path of the file.


use File::Spec;
my $path= File::Spec->catfile(qw{home user Desktop file.txt});

In this way the scalar variable $path get sets to home\user\desktop\file.txt in a Windows operating system. and in Unix based system it gets set to home/user/Desktop/file.txt. The File::Spec module also contains methods like curdir and updir to get the current directory (.) and up-directory (..).

You may also use the moudle Path::Class which is an another good wrapper. You will have to install it before using.

Below is an example of how it works:

use Path::Class;
my $file = file (qw{home user Desktop file.txt});
my $dir = file (qw{home user Desktop});

The $file and $dir are scalar variable which contains the path to the file.txt file and Desktop directory respectively.

Here’s the path

print $file;
print $dir;

The output will depend on operating system. If it is Unix then it will yield

home/user/desktop/file.txt and home//user//desktop

and in Windows based systems it will give

home\\user\\desktop\\file.txt and home\\user\\desktop

Here $file and $dir are objects which have several methods which can be applied on them for some use. Like absoulte method gives the absolute path and slurp method slurps through the file’s contents.

my $abspath = $file->absolute;
my $content = $file->slurp;
$file->remove(); #delete the file

When you want to write a code for a particular system and you want to make this system understand the path of other operating system then you may use foreign_file() and foreign_dir() methods. These will return the path based the argument which you have to specify explicitly.

use Path::Class qw(foreign_file foreign_dir);
my $foreignfile = foreign_file('Win32', qw{home user Desktop file.txt});
my $foreigndir = foreign_dir('Win32', qw{home user});

Now $foreignfile will contain home\\user\\desktop\\file.txt even if the code is run from a Unix based system. This approach is very handy and useful. In the next article we will continue and expand our discussion.

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.

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 :

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.

Now go in that direcotry.

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.

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

  • Configure
  • Compile
  • Install

For pre-installation configuration:
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”.

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.

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.

Understanding file permissions and access rights in Linux

In Linux everything is a file. To set access rights on the specific files we use chmod command. To see what are the access rights/ permissions of a specific file we use ls -l command. Below we will learn how to see permission of a file and change it.

Note: To change permissions of a file you must be login as root user.

Suppose you have a file named as linuxstall.txt in a directory named as LinuxStall which is in /tmp. Use ls -l command to see its permissions as:

ls -l /tmp/LinuxStall/linuxstall.txt

The output will be as

-rwxr--r--. 1 root root 0 Jan 5 15:39 /tmp/LinuxStall/linuxstall.txt

Let us first understand what does the above line means by breaking it down in different parts.

The left most bit can be of two types either d or .
If it is d then it means that it is a directory.
If it is then it means that it is a file.

Here in our example it is means it is a file.

Part 1: r w x (Shows owner’s access rights)
First bit is which means it is a file, if it is d then it means it is directory.
First bit is r which means read i.e the owner can open this file.
Second bit is w which means write i.e the owner can edit this file.
Third bit is x which means execute i.e the owner can execute this file.

Part 2: r – – (shows group’s access rights)
First bit is r which means read i.e the group members can open this file.
Second bit is which means group members can not edit this file.
Third bit is which means group members can not execute this file.

Part 3: r – – (shows other users which are neither owner nor group members)
First bit is r which means read i.e the other users can open this file.
Second bit is which means other users can not edit this file.
Third bit is which means other users can not execute this file.


Changing file permission using chmod command

chmod stands for change file mode bits

There are two methods of changing file permission using chmod command.

Syntax: chmod permission path_of_file

Method 1: Numerical Method

In this method we use numeric bits which corresponds to r w x. The numeric values for these are

r = 4, w = 2, x = 1

chmod 444 /tmp/LinuxStall/linuxstall.txt

will change the file permission of file linuxstall.txt to

-r – – r – – r – – which means owner, group, others can only read this file.

chmod 555 /tmp/LinuxStall/linuxstall.txt

will change the file permissions of file linuxstall.txt to

-r – x r – x r – x which means owner, group, other can read and execute the file.

chmod 666 /tmp/LinuxStall/linuxstall.txt

will change the file permissions of file linuxstall.txt to

-r w – r w – r w – which means owner, group, other can read and edit the file.

chmod 644 /tmp/LinuxStall/linuxstall.txt

will change the file permissions of file linuxstall.txt to

-r w – r – – r – – which means owner can read and edit the file, group and other can only read the file.

Hence we saw that first numeric bit is for owner, second numeric bit is for group and third one is for other users. You can set the numeric bits to 1,2,4 for execute, write and read purpose respectively.

Method 2: Alphabetical Method

There are 4 alphabets associated with it, they are u,g,o and a.

u stands for owner

g stands for group

o stands for others

a stands for all

To add permission we use + and to remove permission we use

Example 1: To grant read permission to all group users

chmod g+r /tmp/LinuxStall/linuxstall.txt

Example 2: To remove read permission from group users

chmod g-r /tmp/LinuxStall/linuxstall.txt

Example 3: To grant read,write,execute permission to owner

chmod u+rwx /tmp/LinuxStall/linuxstall.txt

Example 4: To remove execute permission from other users

chmod o-x /tmp/LinuxStall/linuxstall.txt

Example 5: To grant read and write permission to all users

chmod a+rw /tmp/LinuxStall/linuxstall.txt

Noticed use of a? It sets permission for all users.


Conclusion: We saw that each file in Linux has access rights associated with it. It is divided in 3 parts, one is owner, second is group users and third are other users. We can use chmod command to change the file permission. There are two methods, one is numerical and other is alphabetical method. Numerical method of setting permission is preferred though.

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
#chmod 0755 SomeScriptFile

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

-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/

For executing it in ksh shell:
$ ksh SomeScriptFile

For executing in bash shell :
$ bash SomeScriptFile


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

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

./ 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 $HOME/bin

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


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%  /