
Everything is a File in Linux
Beginner: Hello everyone! Are we going to learn some new knowledge about Linux files today? I heard that “everything is a file” in Linux, but what does that really mean?
Expert: That’s right! The phrase “everything is a file” is a core concept in Linux, meaning that hardware, directories, and regular data are all abstracted as files for processing. However, even though they are all files, they need to be distinguished by type, just like we humans have different identities. Before we delve into file types, let’s first look at the “attributes” of files, which will help us better understand file types.
Beginner: File attributes? What are those? How can I see these attributes?
Expert: File attributes are like a file’s “ID card” or “resume”. The most commonly used command to view file attributes is <span>ls -l</span>. Let’s execute it and see what it displays:
ls -l
Expert: You will see output similar to this (depending on your actual system output):
-rw-r--r--. 1 root root 1234 May 11 11:43 access.log
drwxr-xr-x. 2 root root 4096 May 10 10:00 my_directory
lrwxrwxrwx. 1 root root 7 May 09 09:00 link_to_file -> file.txt
Expert: The output of <span>ls -l</span> typically contains 9 columns of information. Let’s quickly go through them:
- First column: Permissions and file type.
- Second column: Number of hard links.
- Third column: File owner.
- Fourth column: File group.
- Fifth column: File size (in bytes).
- Sixth, seventh, and eighth columns: Last modification time (month, day, time).
- Ninth column: File name.
Beginner: Wow, that’s a lot of information! Some I can guess, like file name, size, and modification time. But what do the permissions in the first column, the number of hard links in the second column, and the owner and group mean?
Expert: You are very observant! Permissions, the number of hard links, and the owner and group are all very important concepts that we will explain in detail in future lessons. For now, let’s just get a preliminary understanding of them.
Expert: For example, the third and fourth columns represent the file’s Owner and Group. Just like your computer is yours, you are its owner. Linux files also have owners, usually the user who created the file.
Beginner: So if I create a file, I am its owner?
Expert: Exactly! Let’s demonstrate this. First, we switch to a regular user <span>osuser</span> (if your system doesn’t have this user, you can create one with <span>sudo adduser osuser</span>):
# Assuming you are currently the root user or have sudo privileges
sudo su - osuser
Expert: Now you are logged in as <span>osuser</span>. Let’s create a file:
touch my_test_file.txt
ls -l my_test_file.txt
Expert: You will see output similar to this:
-rw-rw-r--. 1 osuser osuser 0 May 11 12:00 my_test_file.txt
Expert: See? The third and fourth columns both show <span>osuser</span>, indicating that <span>my_test_file.txt</span> is owned by <span>osuser</span> and belongs to the group <span>osuser</span>.
Beginner: I understand! What about the file size in the fifth column and the modification time in the sixth, seventh, and eighth columns?
Expert: These two columns are quite straightforward. The fifth column is the file size in bytes. The sixth, seventh, and eighth columns show the last modification time of the file. We can also quickly demonstrate how the modification time changes:
# Switch back to your familiar user (if you switched earlier)
exit # If you are still in osuser
date # Check the current time
echo "Hello, Linux!" > test_time.txt
ls -l test_time.txt
Expert: Note the modification time of <span>test_time.txt</span>. Now let’s modify this file:
sleep 5 # Wait for 5 seconds to ensure a noticeable change in time
echo "More content." >> test_time.txt
ls -l test_time.txt
Expert: You will find that the modification time of <span>test_time.txt</span> has been updated. This indicates that when the file content is modified, the time attribute is also updated accordingly.
Beginner: It really changed! Now the ninth column is the file name, which I understand. Now let’s go back to the first column. Besides permissions, you mentioned it also includes file types. What does that mean? I see some files start with a dash <span>-</span>, some with <span>d</span>, and others with <span>l</span>. What are they distinguishing?
Expert: Good question! The first character of the first column is used to indicate the file type. Although in Linux “everything is a file”, these files have different roles and behaviors in the system, so types are needed to distinguish them.
Expert: Common file types in Linux include:
<span>-</span>(Regular file): This is the most common file type, such as text files, images, videos, executable programs, etc.<span>d</span>(Directory file): This is what we commonly refer to as a folder.<span>l</span>(Symbolic link file): Similar to shortcuts in Windows.<span>b</span>(Block device file): Used to access block devices, such as hard drives, USB drives, etc.<span>c</span>(Character device file): Used to access character devices, such as keyboards, terminals, etc.<span>s</span>(Socket file): Used for inter-process communication.<span>p</span>(Pipe file): Used for inter-process communication.
Beginner: Wow, that’s a lot! Let’s start with the most common regular file <span>-</span>. If they are all regular files, how do I know if it’s a text file, an image, or a video? Do I just look at its file name extension? For example, <span>.mp4</span> is a video, <span>.txt</span> is text?
Expert: This is a very critical question! In Linux, file name extensions (like <span>.mp4</span> or <span>.txt</span>) are merely a human convention for our convenience. The Linux system itself does not rely on file extensions to determine file types. You can rename a <span>.mp4</span> file to <span>.txt</span>, and the system will not consider it a text file.
Beginner: Really? If the system doesn’t look at the extension, how does it know what type a file is? Can’t I just use <span>ls -l</span> to see the first column and determine what kind of file it is?
Expert: Yes, <span>ls -l</span> can only tell you that it is a regular file, but it cannot tell you what kind of content it is. To accurately determine the true type of a regular file, we need to use a specialized command: <span>file</span>.
Expert: Let’s do a few experiments:
-
Create an empty file:
touch empty_file.txt file empty_file.txtThe output might show:
<span>empty_file.txt: empty</span>(empty file) -
Check a system command (executable file):
file /bin/lsThe output might show:
<span>/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=..., stripped</span>indicating it is an executable binary file. -
Check a configuration file (text file):
file /etc/hostnameThe output might show:
<span>/etc/hostname: ASCII text</span>(ASCII text file) -
Check a compressed file: (assuming you have a compressed file named
<span>archive.tar.gz</span>)# If not, you can create a simple compressed file tar -czf test.tar.gz empty_file.txt file test.tar.gzThe output might show:
<span>test.tar.gz: gzip compressed data, last modified: ...</span>(gzip compressed data)
Beginner: Wow, the <span>file</span> command is so powerful! It can see so many details. If I rename a video file <span>.mp4</span> to <span>.txt</span>, can the <span>file</span> command still recognize it?
Expert: Yes, the <span>file</span> command exists to solve this problem. It determines the file type by examining the internal structure and content of the file, rather than relying solely on the file name extension.
Expert: Suppose you have a video file <span>my_video.mp4</span>:
# Assuming this file already exists
ls -l my_video.mp4
The output will show a <span>-</span> at the beginning, indicating it is a regular file.
Expert: Now let’s rename it to a strange extension, or even no extension at all:
mv my_video.mp4 weird_file
ls -l weird_file
<span>ls -l</span> will still show it as a regular file.
Expert: However, using the <span>file</span> command to check:
file weird_file
The output will still show it as <span>ISO Media, MPEG v4 system, ...</span> (ISO media, MPEG v4 system), accurately indicating it is a video file.
Beginner: That’s amazing! So even though we follow conventions and name video files <span>.mp4</span>, script files as <span>.sh</span> or <span>.py</span>, that’s just for our convenience. The system actually determines file types using the <span>file</span> command.
Expert: Absolutely correct! This naming convention is for human readability and understanding, while the <span>file</span> command is the tool for the system to identify the essence of files. So, when you see a regular file <span>-</span> and are unsure of what it is, the <span>file</span> command is your best helper.
Beginner: I understand the case of regular files <span>-</span>. What about other types of files? Like <span>d</span>, <span>l</span>, <span>b</span>, <span>c</span>, <span>s</span>?
Expert: Okay, let’s continue looking at other types:
-
<span>d</span>(Directory file – Directory): This is the simplest, it’s what we commonly refer to as a folder.ls -ld /rootThe output’s first column will start with
<span>d</span>, indicating that<span>/root</span>is a directory. -
<span>l</span>(Symbolic link file – Symbolic Link): This is a special file that points to another file or directory, similar to a shortcut in Windows.ls -ld /bin/shThe output’s first column will start with
<span>l</span>, and it will show the target it links to, such as<span>-> bash</span>. -
<span>b</span>(Block device file – Block Device): Block devices transfer data in fixed-size “blocks”, such as hard drives, USB drives, and SD cards. They are usually located in the<span>/dev</span>directory.ls -l /dev/sdaThe output’s first column will start with
<span>b</span>. Disks are typical block devices, where data is stored and read in blocks. -
<span>c</span>(Character device file – Character Device): Character devices transfer data as a stream of characters (one character at a time), such as keyboards, mice, and terminals. They are also usually located in the<span>/dev</span>directory.ls -l /dev/ttyThe output’s first column will start with
<span>c</span>. Your terminal is a character device, and every character you input is processed by the system. -
<span>s</span>(Socket file / Socket): Socket files are used for communication between different processes on the same machine. For example, if your system is running a MySQL database service, it might create a socket file:ls -l /var/run/mysqld/mysqld.sock # Path may vary by systemThe output’s first column will start with
<span>s</span>. This communication method is faster than inter-process communication over network protocols (like TCP/IP) because it doesn’t need to go through the network stack.
Beginner: I understand! So by looking at the first character of the first column in the <span>ls -l</span> output, I can know the basic type of the file. If it’s a regular file <span>-</span>, I can then use the <span>file</span> command to delve deeper into its specific content type.
Expert: You summarized it very well! This is the main content regarding file types. Today we mainly addressed the issue of identifying file types in the output of <span>ls -l</span>. As for permissions, users and groups, the number of hard links, and other columns, we will explain them in detail in upcoming lessons.
Beginner: Awesome! I learned a lot of practical knowledge today, especially the <span>file</span> command, it’s truly a gem! Thank you, expert!
Expert: You’re welcome! Understanding file attributes and types is fundamental to managing a Linux system. Mastering these will help you better understand and operate files. Keep up the good work!