In every Linux based system (on embedded platforms as well), all the files reside in a container called a Filesystem. The Filesystem itself, in most cases, is just another file, which resides somewhere in a storage device (RAM, flash, flash drive, hard disk, cd, etc.) and mounted for use by the kernel. In this article, we will review several file types and their properties.
Common file types:
- Executables: Files with usually no extension. Contain binary code for execution on the machine. These files have the eXecution bit on (will be discussed later). These files are not readable. If you’ll try to cat them, you’ll get garbage characters. These files will mostly reside in /bin, /sbin, /usr/bin or /usr/sbin.
- Libraries: Files with the extension of “.so”, which contain binary code that is shared among more than one executable. These files are not standalone, and can not be activated manually.
- Data files: Similar files, with the eXecution bit off. They also contain non readable characters (usually), but these are not machine instruction, but data that a program uses and updates.
- Text files: Files that contain human readable text. Sometimes they have an extension of .txt, but not always.
- Script files: A combination of text and executables. These are text commands that the system’s Shell understands and interprets to perform some automated actions. These files are readable, and their eXecution bit is on. Usually, these files have the extension of .sh.
- Directories: Directories are containers (or tables) for files. Each directory can contain files or other directories.
- Symbolic links: Also called Symlinks. A symlink is just a pointer to another file. Performing an action on a symlink, is actually performing the action on the real file, which may reside somewhere else in the filesystem.
- Special files: Files that are used as gateways to communicate with the system’s drivers in the kernel. A program may access these files to read or write data from or to the kernel space.
- Socket files: These files are used as gateways for inter-process communication. A program may create such a file, and another program who wishes to communicate with it, could send and receive data using this file.
- Named Pipe files: Similar files to socket files, which are used to transfer data from one program to another.
The following table summarizes the symbols per each file type. Note that the first four file types are considered “Regular”.
Each file entry has the following properties:
- File name
- File size
- Owner ID
- Group ID
- Creation date
- Access Permissions
- Number of links the file has
Each file has a name that distinguishes it from the rest. Unlike Windows, a file name in Linux environment is case sensitive. The maximum file name is 255 bytes. Another difference from Windows environment, is the fact that there is no actual meaning to the file extension name (like .exe in Windows), where the dot character in the file name is used for convenience and could repeat several times. Theoretically, an executable could have any extension. The standard notation for executables, is to leave them without any extension name.
The amount of bytes which are allocated by the file. In some cases, where compression is applied, the actual file size is smaller. In other cases, the actual file size is bigger, where the file is padded until the next memory allocated unit (for example, in 4KB units).
The ID of the user who created the file.
The group that the owner belongs to.
The date and time that the file was created or modified.
File Access Permissions
Linux provides access permission for files (and directories). The permissions define weather the file could be accessed for read, write or execute, per each user id, group id and the rest of the world. The RWX combination is represented by 3 bits per each category, where “1” in the corresponding location enables the specific permission. In order to see what is the permission of a specific file, use the “ls –l” command.
- An executable file which can be accessed as read, write and execute for the owner, and read and execute only for the rest of groups has the permission value of 755.
- A data file which can be accessed as read and write for the owner, and read only for the rest, has the value of 644.
- A read only file has the value of 444.
- A file without an access has the value of 0.
The permission values for a directory apply to the access inside of it. For example, if the W bit is turned off, the user will not be able to write or create any file inside of it.
Number of Links
Each file has a counter of links. A regular file normally has 1 link, where a directory may have more.
Listing files with the ls command
The ls command is used to list the files in the current directory. If no parameter is specified, it lists only the file names of non-hidden files. Here are some of the mostly used parameters used with ls command:
- ls: List all files except hidden files. Show only file names.
- ls -l: Lists the files in a long (detailed) format.
- ls -a: Lists all files, including the hidden files.
- ls -R: List files recursively. List files in all sub directories as well.
- ls -S: Sort the list by the file size.
- ls -t: Sort the list by the creation/modification time.
- ls -r: Reverse the sort.
- ls -1: List only 1 file per line.
It is possible to combine parameter flags in order to customize the output according to our needs. For example ls -laSR will list in long format, all the files, including hidden files, in the current directory and in all sub-directories, and sort the list by the file size. The complete flag listing is available in the ls man page.
Here’s an example for the long listing format. You can find here a script, a directory, a data file, a C source file and an executable:
# ls –l -rwxr-xr-x 1 user group 312 Apr 4 2007 plugin.sh drwxr-xr-x 2 user group 2048 May 6 2008 portmap_6.0 -rw-r--r-- 1 user group 22251 May 5 2008 portmap-6.0.gz -rw-r--r-- 1 user group 324 Apr 2 2009 printbuf.c -rwxr-xr-x 1 user group 7088 Apr 6 2009 a.out
The long listing columns provide the following information:
- File type and permissions (owner, group, rest).
- Number of links.
- Owner name.
- Owner’s group.
- File size.
- Creation / modification date.
- File name.
Changing a file permission with the chmod command
Changing a file permission is done using chmod command. Either specify the permission value (like 755), or +/- with the corresponding bit (like +x), and the file name. Read the chmod man page for more details.
|Check out the ads, there could be something that may interest you there. The ads revenue helps me to pay for the domain and storage.|