7 - regular files and links.

Lecture



Regular files are filesystem objects that contain data. In other words, these are ordinary files that you used to deal with: text files, musical compositions, drawings, video files - all of these are regular files . You can view the contents of such files at *** s am cat, less . You can create a file in several ways. The first method is using the touch command. In fact, the main purpose of this command is to change the access time and file changes. Run com *** at ls -l in your home directory. The sixth column shows the date of the last file change. Select a file with a date other than the current one and execute a com *** at touch the name of your file (it may be necessary to execute a com *** at as root ). Now again, ls -l and see that the date has changed to the current one. The touch command has a slight side effect. If the name of a non-existent file is passed to it as a parameter, then it will still execute what is asked for it (time changes), and since there is no file, it will create it. Run the *** command on touch with the name of a non-existent file and make sure that it is there by the command ls . The file will be created empty. You can edit this file with any text editor ( vim, nano ). Another way is to transfer some text to a file like this echo Hello world! > test.txt . As a result of this command, a file will be created named test.txt with the contents of Hello world! Thus, the result of the execution of any command can be redirected to the file. For example, com *** and ls> list.txt will create a list.txt file which will contain the list of contents of the current directory. Another way is with any text editor.

File naming rules. The first thing you need to clearly understand and remember: Linux (unlike Windows ) in the file names is case sensitive. That is, Test, test, tEst, TEST - these will all be different files. The second distinctive (from Windows ) feature is the file extension. In Linux, it simply does not exist. The dot in the file name is the same symbol as the others. Creating a *** *** touch file with the names test.txt, test.doc or test.jpg we will still create a text file. Extensions are more needed for cross-platform applications, so that they find “their” files.

A very important question is which characters can be used for file names, and which can not. This question could be given half a lecture, since there are quite a few options when one or other symbols can or cannot be used. I suggest remembering the following: you can always use the following characters in file names: numbers, letters (Latin is better), full stop, underscore. The set of these characters is quite enough to call your files. Therefore, I intentionally will not specify which characters can not be used. Just remember what you can and use them. Curious can find additional information on the Internet. The length of the file name is 255 bytes.

In order to rename or transfer a file there is a com *** and mv (short for move ). Here are some examples of using this command:

mv test.txt text.tst - rename the test.txt file to text.tst in the current directory.
mv / home / igor / docs / test / home / igor / docs / text - rename the test file in the / home / igor / docs / directory
mv test1.txt ./docs/ - move the test1.txt file to the / docs / directory

To copy files, use the *** and cp (abbreviated from copy ). Its form is similar to the com *** th mv . A few examples.

cp test ./docs/ - copy the test file to the directory ./docs
cp ./docs/test1.txt. - copying the file test1.txt to the current directory (note the dot at the end of the command).

It's time to explain what the point means in the examples. If you execute a com *** for ls -a for any directory (the key and allows you to display hidden files), you will always see two characters at the top:. - point and .. - two points in a row. These are also directory names. A dot indicates the current directory, and two dots indicate the parent directory. You can execute com *** in ls .. and see the list of files in the parent directory for the current one. To navigate the directories, use the com *** a cd . Typing *** at cd / boot / grub / will take you to the grub directory, which is located in the boot directory. By the command cd .. , you will go to the parent directory for the current one, that is, to boot . Thus, you can use the cd .. command to move up through the file system tree. To go to your home directory you need to type a com *** at cd ~ (“tilde” means home directory). Change to the / usr directory now ( cd / usr / ). Run com *** at ls . In the directory among others there is a directory / bin . How to view its contents? Typing, ls / bin - you will display the contents of the / bin directory , which is located in the root of the operating system. You must either specify the full path: ls / usr / bin / or write ls ./bin/ as follows. If there were no these special names, then *** cp ./docs/test1.txt. would have to write as follows: cp /home/igor/docs/test1.txt / home / igor / .

Deleting files. Here you need to be extremely attentive and to accustom yourself to the idea that there are no “baskets” in Linux and deleting the file, you delete it forever. Therefore, before you delete something, think not once, but twice, or even three. To delete files, there is a com *** and rm . Com *** and universal in the sense that it allows you to delete all types of files. The format is simple: rm [keys] file name . The most important key for this command is -i . With this key, the *** key will ask you if you really want to delete. Without this key, someone *** will delete the file without question. Create several files and try deleting them with and without the i key. If in your distribution kit com *** and rm without keys issues a request, then the alias mechanism is used. If you do not want to receive a request for deletion, then the *** command from rm must be executed with the –f key. But more about that later. To delete a directory, you must use the *** command from rm with the -r key. Also there is a com *** and rmdir to delete a directory.

Now let's look at how delete, copy and move operations can be performed for a group of files. To do this, you must use the symbols of group operations. Create a temp directory ( cd ~; mkdir temp ) in your home directory, go to it ( cd ~ / temp / ) and create several files: touch file1.txt file2.txt file3.txt; touch file1.jpg file2.jpg file3.jpg; touch abcd asdf azxs adcd .

Now more about the symbols of group operations. The symbol * denotes any number of any characters. For verification, we will use the com *** in ls with the corresponding characters.

ls * .jpg - as a result all files that end in .jpg will be displayed. Result: file1.jpg file2.jpg file3.jpg .

ls file * - all files starting with file will be displayed. Result: file1.jpg file1.txt file2.jpg file2.txt file3.jpg file3.txt .

Character ? - replaces any single character.

ls a? cd - the result will be all files consisting of 4 characters, the first - a , the third, the fourth - cd , and the second can be any. Result: abcd adcd .

[] - in brackets you can specify certain characters or a range of characters.

ls [abcd] [abcd] * - the result will be all the files of the first and second characters which are a, b, c or d. Result: abcd adcd .

ls * [23] * - the result will be all files in which there is a digit 2 or 3. Result: file2.jpg file2.txt file3.jpg file3.txt .

ls file [1-3] * - the result will be all files starting with file , followed by a digit from the range 1-3. Result: file1.jpg file1.txt file2.jpg file2.txt file3.jpg file3.txt .

I did not accidentally show the work with the symbols of group operations on the example of the ls command . I recommend at the initial stage of learning Linux when using group operations (or templates) for the rm command to use the com *** ls command to check what you will delete. Create a few more files in our ./temp/ directory touch file1txt file2txt file3txt file4txt file5txt . Now let's say you want to delete the files just created. For this you can use the com *** th rm file [1-5] * txt . But this com *** will remove not only files file1txt file2txt file3txt file4txt file5txt , but also files file1.txt file2.txt file3.txt ! And we do not need this at all. Having executed the com *** at ls file [1-5] * txt you will see that this template affects the necessary files. And *** should be done as rm file [1-5] txt . Do not be lazy to check your templates until you feel confident in writing them.

Links

Links are also a special type of files in Linux as directories or device files. There are two types of links: symbolic links and hard links . A symbolic link is a file inside which contains the path to the file system object. A symbolic link is created using the ln command with the -s key.

ln -s object path name link

Go to the home directory ( cd ~ ) and execute the com *** from mkdir -p ./object/folder1/folder2/folder3 . A com *** and mkdir with the -p option allows you to create several nested directories. Create a touch ./object/folder1/folder2/folder3/myfile file. And now create a symbolic link to the created file:

one
ln -s ./object/folder1/folder2/folder3/myfile mylink

Run com *** at ls -l and find the symbolic link created - mylink . You should see something like:

one
lrwxrwxrwx 1 igor igor 39 2009-10-06 17:24 mylink> ./object/folder1/folder2/folder3/myfile

The symbol l (the very first in the first column) indicates that the file type is link . And also we can see which object the link refers to. The number 39 indicates the size of the file mylink . If you count the number of characters in the path to the object, then there will be 39 of them. That is, our file does not really contain anything other than a link to myfile . If you execute the *** at cat mylink , you will not see anything, since you will open for viewing not the file mylink , but the file myfile to which the link refers. To edit the file myfile, you can execute a com *** with nano ./object/folder1/folder2/folder3/myfile , or you can nano mylink - as a result, the same file will open. If you delete the file myfile , the link will not disappear anywhere, but when you try to execute the *** at cat mylink, you will receive a message that the file does not exist. If you again create the file myfile in the place where the link refers, you can access it again using the symbolic link. If you delete a symbolic link pointing to a file, nothing will happen to the file itself - it will remain in place. You can create links not only for files, but also for directories. For example:

one
ln -s ./object/folder1/folder2/folder3/ mydirlink

By executing the *** cd ./mydirlink/ we will get into the directory ./object/folder1/folder2/folder3/ .

Now several features for working with symbolic links. When creating symbolic links try to point to the full path to the file (without a dot as in the examples above). If you move the link in which there will be a relative path to another folder, it will stop working. I think the reasons are clear to you. Now about the operations of moving and copying symbolic links. I suggest you to independently consider the peculiarities of the work of the cp and mv commands with symbolic links. Try to copy and move links to files and directories and look at the results.

To understand what hard links are, you need to understand a little how the file system works. I will try to explain as simply and clearly as possible, and you can study the details yourself if you wish. The hard disk space in the file system is logically divided into identical blocks, which are called clusters. The file system has a hidden table which contains information about the objects in the file system (see Figure 1).

  7 - regular files and links.

Picture 1

The information in the table contains the most diverse, but we will be interested in two fields: the name of the object and inode . In this context, we will assume that the inode is a number that uniquely identifies the file in the file system space (including the location). Suppose we have a file named file1 . It corresponds to inode1 , which indicates where the file is located on the disk. Create a file with the name file1 . And now create hard links to this file (in the same directory) with the names file4 and file5 and one symbolic link with the name link1 :

one
ln file1 file4; ln file1 file5; ln -s file1 link1

Now run the com *** at ls -li , which will show you the file names and their inodes (the first column).

one
2
3
four
370923 -rw-r - r-- 3 igor igor 27 2009-10-08 12:28 file1
370923 -rw-r - r-- 3 igor igor 27 2009-10-08 12:28 file4
370923 -rw-r - r-- 3 igor igor 27 2009-10-08 12:28 file5
293812 lrwxrwxrwx 1 igor igor 5 2009-10-08 13:02 link1> file1

We look at the result and see that our hard links are file4 and file5 , the inode is the same as the inode of file1 . That is, it turns out that file1, file4, and file5 are physically the same file, but with different names that are absolutely equivalent. On the hard disk, they occupy the same place (see Figure 1). If you execute a com *** with rm file1 , the following will happen: in the table (see Figure 1) the entry related to file1 will disappear. But on the disk, the data pointed to by inode file1 will remain, and we will be able to access them by the names file4 or file5 . The number 3 in the third result column of the ls -li command indicates that there are three names for the specified inode. Delete file1 and verify that the number has decreased by one and is now 2.

In the same results, you can see that link1 is a file that occupies a separate physical disk space and has its own inode .

Analyzing the above, we can come to the following conclusions:

Hard links can not point to a non-existent object. Symbolic links - can.

Hard links work only within one logical partition. You can not make a hard link to another section of your disk, as there is a table with its own inode . Symbolic links can link to other partitions and file systems.

Practice creating symbolic and hard links and figure out when to apply them.


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

LINUX operating system

Terms: LINUX operating system