Sunday, December 5
Shadow

Linux Files Basics

Everything in Linux is a file. A file is a sequence of bytes. A byte is 8 bits long. The Linux system does not impose any structure or meaning on a file. It fully depends on the program that interprets the file. To learn more about files, let’s create one.

I created a file name sample.txt using vi editors. The file contains two lines. To see the file on the terminal, use the cat command followed by the file name.

Sample.txt is a file with 73 bytes. 73 bytes means there are 73 characters in the file including spaces and periods. To check the size of the Linux files with the permissions and owner, use the ls -l command followed by the file name.

The command od (octal dump) provides you a visible representation of all the bytes of a file.

If you append an option i.e. -c with od command, it will interpret bytes as characters.

Option -x will represents the bytes in hexadecimal.

Adding option -b will show the bytes as octal numbers.

\n is the representation of a new line with an octal value 012. This is the ASCII newline character, it is put by the system whenever you press Enter in the file. You can check the octal values of all the characters by using the command man ascii.

The newline (\n) is the most common example of a special character. There are other special characters as well, like:

  • backspace whose octal value is 010 and printed as \b.
  • tab whose octal value is 011 and printed as \t.
  • vertical line whose octal value is 013 and printed as \v.
  • escape whose octal value is 033.

The format of a file is identified by the program, that uses it. In Linux, you will have a wide variety of file types. The kernel cannot tell you the type of file, that it does not aware of. To get information about the files, we can use the file command followed by the folder or file name.

file command is totally reliable on naming conventions. It merely scans the name of the file and on the basis of that, it generates the result on the terminal.

Linux Files Permission

All the files in Linux has some set of permissions associated with them. It describes which user can do what with the file. It’s a type of security using which user can protect there file from any alteration or deletion or modification. But there is one user who can read all the files no matter who owns it, that user is super-user (root).

Any other ordinary user can also act as super-user using su command, if they know the password of super-user. The -l option of ls prints the permissions information, among other details.

The second line shows below things:

  • sample.txt file is having -rw-rw-r– permission
  • 1 denote that it has one link
  • file owned by user linuxhawks
  • The size of the file is 73 bytes long
  • It was last modified on Feb 1 at 17:33

Let’s talk about the permission string i.e. -rw-rw-r–. It is consists of 9 characters, telling us everything about the file. Here is what we need to keep in mind:

  • The first ‘-‘ symbol or sign means, this file is an ordinary file.
  • If it was a directory, there would be a ‘d’.
  • The next three characters i.e. ‘rw-‘, describe the file owner’s permission, which is read, write and execute. User linuxhawks can read and write the file, but cannot execute it.
  • The next three characters i.e. ‘rw-‘ describe the group permissions on the file. all the users who reside under that group will have those permissions.
  • The last three characters describe the permission for everyone else, those who are not owners or members of the group. They can only read the file as per the permissions mentioned over here.

We have one special permission at the place of ‘x’ i.e. ‘s’. s means ‘set-uid’. If any files has this character present in the permissions, that means any user can execute that file. For sometime the control will pass to the logged in user to run the command and finish the task. the set-uid is simple idea but resolves many security problems.

Changing Permission For Linux Files

You can change the permissions and ownership of the file using commands, chmod, and chown.

Chmod command syntax is: chmod permissions filename

The permissions can be specified in two ways, one is octal numbers and the other is a symbolic representation. The octal number is easy to use. Let’s say if you have a file name test1.txt and you want to provide permission to the file.

You can simply issue a command, chmod 666 test.txt in the terminal. It means, the file is having read, and write permission for the user, group, and everyone else. The octal modes are specified by adding together a 4 for read, 2 for write, and 1 for execute permission.

If you want to do it with symbolic representation, its slightly different from the octal one. In this you need to mention the character associated with the permission. ‘+’ means adding permission to the file and ‘-‘ means removing the permissions from the file.

E.g. chmod u+x test.txt, means adding execute permission to the file. Here is the notations for the file, ‘u’ means owner, ‘g’ means group and ‘o’ means everyone else. If you need to remove any permission, just issue chmod command with a minus sign and you are done.

If you do not mention any character prior to ‘+’ or ‘-‘ sign, that means you are making changes for all i.e. owner, group, and others. So, be careful about that and issue commands wisely.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)