realLifeinfo

linux filesystem structure explained

Nerd, Solutions 
  

The linux filesystem is the structure in which all the information on your Linux computer is stored. In Linux files are organized within a hierarchy of directories, each directory can contain files as well as other directories, you can prefer any file or directory using either a full path or relative file which is relative to your current working directory.

Sponsored

If we were to map out the files and directories in Linux, it would look like an upside down tree. At the top is the root directory, which is represented by a single slash (/). Directories that can be found in this directory are set of common directories in a Linux system such as bin, dev, home, proc, and others.

Some common Linux directories and the data they contain

 Linux Basic Filesystem commands

They are basic file system commands that are crucial to learn if you want to work smoothly with Linux file system structure.

These are some of the command that can be used to deal with a Linux file system structure.

Using meta characters and operators

Sponsored

Command for file system contain some of the meta characters that's making working with files easier.

Using file-matching meta characters

We can use meta character in searching for file in the system.

 Example we can use some of these commands to search for files:

This command will try to list all files that have at least one occurrence of A or more.

This command will try to list all files that have at least one occurrence of c or more.

This command will try to list all files that have at least one occurrence of f or more followed by letter t.

Something to note is, wildcard character are handled by shell it self. Shell will expand the wildcard character into a list of characters before passing the argument to the intended program. Exampe in ls a* this command will find all files that their names starts with a lowercase a. Example shell will expand into file names like apple, abcde, abras and all other combinations that starts with the lowercase a. Ls never knows about the wildcard character, because shell will pass the expanded results and not the wildcard character.

Using file redirection meta characters

Command receive data from standard input and send it to standard output, using pipes, you can redirect standard output from one command to the standard input of another.

Using brace expansion characters

By using curl brace {}, we  can expand out a set of characters across filenames, directory name, or other arguments you give commands, for example if you want to create a set of files as file1, through file10, you can do that as follows:

The touch command is used to create a normal text file. The above command will create ten files with the name file followed by a number between 1 and 10.

Or we can create more meaningfully files that will contain all the specified names. Example:

This command will create files with name formed from the combination of the both pairs. Example touch will create files with the name george-king, george-bush, and george-billy, the same behaviour will be true for all specified names.

 Listing files and directories

To list files and directories we can use the ls command. You can find more information on how to use the ls command by either typing man ls command or ls --help command.

Understanding file permission and ownership

 This is the mechanism that Linux uses to keep users from accessing other users' private files and to protect important system files. The nine bits assigned to each file permission define the access that you and other have to your file, permission bits for a regular file appear as -rwxrwxrwx.

Sponsored

For a regular file, a dash appears in-front of the nine-bit permission indicator, we must see a D for directory, L for a symbolic link, B for a block device, C for character device, S for a socket, P for a named pipe.

Setting read, write and execute permissions

Read view what in the file, see what files and subdirectories it contains. Write change the file's content, rename it, or delete it, and files or subdirectories to the directory, remove files, or directories from the directory. Execute run the file as a program, change the directory as the current directory, search through the directory, or execute a program from the directory, Access file metadata (file size, time stamps, and so on) of files in that directory.

We can simply view the file permission by using this command.

Changing file permissions with chmod command

 If you own the file, you can use the chmod command to change the permissions on it as you please, in one method of doing this each permission (read, write, and execute) is assigned a number r=4, w=2, x=1 and you use set's total number to establish the permission. For example, to make permission wide open for a youself as owner, you would set the first number to 7 (4+2+1) and then you would give the group and other read only permission (4+0+0) so the final number we use in chmod is 744.

These are example of changing file mode:

We can also use chmod command recursively, example if we want to change file directories found in HOME directory we can do it using this command.

This command will change file permission to all files and directories found inside the home directory. The option -R means apply the same permission mode recursively to all files and directory found inside location.

We can also use another chmod command syntax to change file permission. This syntax involves letter instead of numbers. With this syntax we can turn on and off file permission using plus(+) and minus (-) signs respectively along with letter to indicate what changes and for whom, we can change file permissions using code like this one.

Changing file ownership

As a regular use, you can't change ownership of the files or directories to have them belong to another user, you can change ownership as root user.

Chown command that stands for change ownership, will change the owner of the file to the specified user, username specifies who will be the new owner of the file.

We can also change ownership of the file recursively to apply to all files and directories found inside the directory.

This command will assign all files and directories that are found in the home directory to the new owner king.

Moving, Copying and removing files

 Commands for moving, copying, and deleting files are fairly straightforward, to change the location of a file, we use the MV command. To copy a file from one location to another we use the CP command. To remove file we use the RM command.

Some examples:

The above command will move file named test_file to the /home directory. By default, the mv command overwrites any existing files if the file your moving to exists.

Examples of using copy command

The above command will copy the test_file to the home directory.

Examples of using remove command

The above command will remove the file with the filename test_file.

Sponsored