Sunday, 21 June 2015

How to create/remove directory/folder in Linux

mkdir command is used to create directory/folder on linux file system.

Syntax:
mkdir [OPTION] DIRECTORY

Refer below options used in mkdir command:

-m, --mode=MODE Set file mode (as with the chmod command).
-p, --parents Create parent directories as necessary. When this option is used, no error is reported if a specified DIRECTORY already exists.
-v, --verbose Verbose output; print a message for each created directory.
--help Display a help message, and exit.

Note: If directory already exists then command will modify the creation time of directory.

Examples:

#Creates a new directory called linuxinanutshell whose parent is the current directory.
mkdir linuxinanutshell

#Create the linuxinanutshell directory, and set its permissions such that all users may read, write, and execute the contents.
mkdir -m a=rwx linuxinanutshell

#Creates the directory /home/linuxinanutshell/a/b/c. If the parent directory /home/linuxinanutshell/a/b/c does not already exist, mkdir will create that directory first.
mkdir -p /home/linuxinanutshell/a/b/c


To remove directory, use rmdir command

The rmdir utility removes the directory entry specified by each directory argument, provided the directory is empty.


Arguments are processed in the order given. In order to remove both a parent directory and a subdirectory of that parent, the subdirectory must be specified first so the parent directory is empty when rmdir tries to remove it.

Syntax:
rmdir [-p] directory



--ignore-fail-on-non-empty ignore any failure which occurs solely because a directory is non-empty.
-p Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the lastmost component. (See rm for fully non-discriminant recursive removal.)
-v, --verbose Display verbose information for every directory processed.
--help Display a help message, and exit.

Example:
rmdir linuxinanutshell


Wednesday, 17 June 2015

How to Change permission of a file in linux

chmod command is used to change the permissions of files or directories. In Linux, there is a set of rules for each file which defines who can access that file, and how they can access it. These rules are called file permissions or file modes. The command name chmod stands for "change mode", and it is used to define the way a file can be accessed.

Syntax:
chmod [options] <permissions> <filename>

Note: If no options are specified, chmod modifies the permissions of the file specified by filename to the permissions specified by permissions.
Permissions defines the permissions for the owner of the file (the "user"), members of the group who owns the file (the "group"), and anyone else ("others"). There are two ways to represent these permissions: with symbols (alphanumeric characters), or with octal numbers (the digits 0 through 7).

Example[with symbols]: chmod u=rwx,g=rx,o=r test_file
chmod_symbolic_permissions

This is an example of using symbolic permissions notation. The letters u, g, and o stand for "user", "group", and "other". The equals sign ("=") means "set the permissions exactly like this," and the letters "r", "w", and "x" stand for "read", "write", and "execute", respectively.

Example[with octal numbers]: chmod 754 test_file

Here the digits 7, 5, and 4 each individually represent the permissions for the user, group, and others, in that order. Each digit is a combination of the numbers 4, 2, 1, and 0:
4 stands for "read",
2 stands for "write",
1 stands for "execute", and
0 stands for "no permission."
So 7 is the combination of permissions 4+2+1 (read, write, and execute), 5 is 4+0+1 (read, no write, and execute), and 4 is 4+0+0 (read, no write, and no execute).

Sunday, 14 June 2015

How to extract archive file in linux

tar command is used for extracting content from archive file. "tar" stands for tape archive, which is used to create, maintain, modify, and extract files that are archived in the tar format.

Syntax:
tar [options] [pathname]

Following options are widely used with tar utility

A, --catenate, --concatenate Append tar files to an archive.
c, --create Create a new archive.
d, --diff, --compare Calculate any differences between the archive and the file system.
-v, --verbose Operate verbosely.
t, --list List the contents of an archive.
x, --extract, --get Extract files from an archive.


Refer below tar examples:

#Create a new tar archive.
$ tar cvf linux_archive.tar dirname/

Note: In above example, c means create a new archive, v means verbosely list files which are processed and f means following is the archive file name

#Extract from an existing tar archive.
$ tar xvf linux_archive.tar

#View an existing tar archive.
$ tar tvf linux_archive.tar

#To estimate the tar file size ( in KB ) before you create the tar file.
$ tar -cf - /directory/to/archive/ | wc -c
O/P: 20480

How to use crontab in linux

cron is the system process which will automatically perform tasks for you according to a set schedule. The schedule is called the crontab, which is also the name of the program used to edit that schedule.

crontab is the program used to edit, remove or list the tables used to drive the cron daemon. cron jobs can be allowed or disallowed for individual users, as specified in the files cron.allow and cron.deny, located in the directory /etc. If the cron.allow file exists, a user must be listed there in order to be allowed to use a given command. If the cron.allow file does not exist but the cron.deny file does, then a user must not be listed there in order to use a given command. If neither of these files exists, only the superuser will be allowed to use a given command.

Syntax:
crontab [-u user] [-l | -r | -e] [-i] [-s]

Example:
Let's say you have a script which backs up important files, or creates a report about system statistics, for example. Let's say the script is called /tmp/linuxinanutshell/scripts/do-every-day.sh, and you want to run it every morning at 5 A.M.

To edit the crontab, use this command:
crontab -e

This will open the crontab in a text editor (Usually this is vi or vim, but it may be something else depending on your Linux distribution). Save below entry in opened file:

0 5 * * * /tmp/linuxinanutshell/scripts/do-every-day.sh

Cron Command Format

Each cron command in the crontab file has five time and date fields, followed by a user name if it is the system crontab file, followed by a command. Commands are executed by cron when the minute, hour, and month of year fields match the current time, and at least one of the two day fields (day of month, or day of week) match the current time.

Field Allowed values
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names)
day of week 0-7 (0 or 7 is Sun, or use names)

Refer other examples:

#This example checks the status of the database everyday (including weekends) during the working hours 9 a.m – 6 p.m
00 09-18 * * * /tmp/linuxinanutshell/scripts/db-status

#This example will check the disk space every 10 minutes.
*/10 * * * * /tmp/linuxinanutshell/scripts/disk-check.sh

#This example will execute the shell script tape-backup at 00:00 on 1st of every month.
@monthly /tmp/linuxinanutshell/scripts/backup.sh