In the previous step, you used ls -l to see file permissions. Another powerful command for getting detailed information about files and file systems is stat. In this step, you'll use stat with the --format option to specifically inspect file permissions in a more structured way.
The stat command can display various information about a file, such as size, blocks, access time, modification time, and permissions. The --format option allows you to specify exactly what information you want to see using format sequences.
Let's use stat to look at the permissions of the my_file.txt file you created in the previous step. Make sure you are still in the ~/project directory.
Type the following command and press Enter:
stat --format=%A my_file.txt
You should see output similar to this:
-rw-rw-r--
This output is the same permission string you saw with ls -l. The %A format sequence tells stat to display the file's permissions in a human-readable format, just like ls -l.
Let's try another format sequence. The %a sequence displays permissions in octal notation. Octal notation is a numerical representation of permissions, where each digit represents the permissions for the owner, group, and others.
Type the following command and press Enter:
stat --format=%a my_file.txt
You should see output similar to this:
0664
Let's break down the octal permission 0664:
- The first digit (
0) is usually ignored for basic permissions.
- The second digit (
6) represents the owner's permissions. In binary, 6 is 110. This corresponds to read (1), write (1), and execute (0). So, read and write permissions.
- The third digit (
6) represents the group's permissions. Again, 110 in binary, meaning read and write permissions.
- The fourth digit (
4) represents others' permissions. In binary, 4 is 100. This corresponds to read (1), write (0), and execute (0). So, only read permission.
This matches the -rw-rw-r-- output from ls -l. Octal notation is often used when changing permissions with the chmod command, which you might learn about in a future lab.
Using stat --format allows you to extract specific pieces of information about a file, which can be very useful in scripting or for detailed analysis.
Click Continue to move on.