Posts

How to Compress file/folder in Linux

Introduction

A backup, or the process of backing up, refers to the copying and archiving of computer data so it may be used to restore the original after a data loss event. The verb form is to back up in two words, whereas the noun is backup. Backups have two distinct purposes.

  • The primary purpose is to recover data after its loss, be it by data deletion or corruption.
  • Data loss can be a common experience of computer users.

Following are the methods through which we can compress the file and folders.

1) ZIP a file/folder

#zip file.zip file1 file2
#zip file.zip dir1
#zip -option file.zip dir1

How to zip a file with excluding file/folder

zip -r conference_01-01-2015_14-30.zip /var/www/conference/* -x home_backup/

2) TAR a file/folder

tar -cvf /backup/etc.tar /etc/*
tar -cvf /backup/etc.tar

3) GZIP a file/folder

gzip is the preferred compression tool for Linux. To compress the tar file using gzip, following is the command:

gzip etc.tar

To decompress the tar file using gzip, following is the command:

gzip -d etc.tar.gz

4) BZIP2 a file/folder

There is also another tool that is rapidly gaining acceptance in the Linux world: bzip2. This is supposed to become the official way of doing it in the near future, so it may be a good idea to get to know ‘bzip2’
To compress the tar file using bzip2, following is the command:

bzip2 etc.tar

To decompress the tar file using bzip2, following is the command:

bzip2 -d etc.tar.bz2

File/Folder Permissions in Linux

Introduction

Linux has inherited from UNIX the concept of ownerships and permissions for files. When applying permissions to directories on Linux, the permission bits have different meanings than on regular files.

  • The write bit allows the affected user to create, rename, or delete files within the directory, and modify the directory’s attributes
  • The read bit allows the affected user to list the files within the directory
  • The execute bit allows the affected user to enter the directory, and access files and directories inside
  • The sticky bit states that files and directories within that directory may only be deleted or renamed by their owner (or root)

Following are the useful commands for modifying file permissions and ownership:

  • chmod – modify file access rights
  • su – temporarily become the superuser
  • chown – change file ownership
  • chgrp – change a file’s group ownership

The Permission Groups used are:

  • User (u): The owner of file
  • Group (g): Other user who are in group (to access files)
  • Other (o): Everyone else

Octal numbers and permissions

There are three types of modes in file system i.e

  • Read (r) – 4
  • Write (w) – 2
  • Execute (x) – 1

For Example if we want to change the file permission to 777, 666, 700

  • rwx rwx rwx = 111 111 111 = 777
  • rw- rw- rw- = 110 110 110 = 666
  • rwx — — = 111 000 000 = 700

Change Ownership of Directory/File

To change the ownership of any directory or file use the  “chown” command:

# chown wheel:wheel filename

Change Permission of Directory/File

To setup a file readable by anyone and writable by the owner only:

# chmod 644 file

To setup a file readable/executable by everyone and writable by the owner only:

# chmod 755 file

Note: By default the file permission of file in linux is 644 and 755 for folder.

Recursively Change:

We can change file permissions recursively with the following command:
For Directories:

# find /path/to/your/wordpress/install/ -type d -exec chmod 755 {} ;

For Files:

find /path/to/your/wordpress/install/ -type f -exec chmod 644 {} ;