Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
In this article, we will be discussing about file management in Linux. You usually work with files, whether you are working on Linux, Windows, Mac, or any other operating system. So, for convenient access to save our time, a proper management system is required to manage these files. The file management in Linux does this work for us.
So, let's discuss the different types of files, the command to list visible and hidden files, and various other operations like creating, editing, deleting, printing, copying, and renaming files. Also, a proper file system requires a managed structure, so let's start with discussing the file structure and its features.
File Structure
The operating system contains a built-in layer for file management in Linux, which handles the data and manages file name, size, and many other properties. These file systems have an oriented structure, naming convention, and organization methods. The data structure, like trees, supports this hierarchical file structure and contains information about the free and used disk space and other file properties.
Linux Foundation maintains this File system Hierarchical Standard(FHS). According to FHS, each file and directory is stored in the root directory (top-most directory), symbolized by a forward slash (/). Like a tree, the root directory is the node, and all other directories are its branches. And so, to access all other directories, you can give the command:
/<directory_name>/<file_name>
Example: /bin = takes the user to the bin directory inside the root directory.
Three layers follow the file system architecture, and these are logical, virtual, and physical.
The logical layer interacts with the application via API(Application Programming Interface) to perform various file operations and pass the request to the virtual layer.
The virtual layer creates multiple objects of the physical layer for the concurrent execution of the applications.
The physical layer handles aspects like managing memory blocks and storing data.
Features of File Management System
Some of the supported features of File management in Linux include:
Unlike windows, Linux uses a forward slash to specify the directory path.
All the files, directories, and commands in Linux are case-sensitive.
File management in Linux automatically organizes the files and directories if the user does not specify a file extension. It helps beginners as it may be confusing to differentiate among different file extensions.
Most of the configuration files in Linux are hidden. All these file names are prefixed by dot(.) and don't need to be accessed or read.
Type of File Systems
File management in Linux offers different file systems.
Ext (Extended FileSystem): It was introduced in 1992. It was developed by Minix OS. It was the first file system that was designed to support kernels. But soon become outdated with innovations in file systems.
Ext2 (Second Extended Filesystem): It was introduced in 1993. It was based on the Minix filesystem. The file size in these systems can be between 16GB - 2TB. These filesystems were good but soon became limited because of popularity and usage.
Ext3 (Third extended Filesystem): It was introduced in 2001. Although the file sizes and total system size remained the same as with ext2, a directory can have up to 32,000 subdirectories in this file system.
Ext4(Fourth extended Filesystem): It was introduced in 2008. It was considered a temporary file system until a new development. Also, the directory size gets increased to 64,000 subdirectories.
Reiser File System: It was introduced in 2001 by a team led by Hans Reiser at Namesys. It is preferred for handling small files in large numbers. The maximum file size and the file system are 8TB and 16TB, respectively for this file system.
Journaled File System: It was introduced in 1990 as the first UNIX commercial file system. It uses a circular log to keep a buffered record of the changes that have not yet been committed. If the system crashes, the files can be brought back.
Btrfs (B Tree File System): It was introduced in March 2009. It is an open-source file management system. It can handle a maximum file size and system size of 50TB. The biggest drawback of this system is that the big files become fragmented quickly.
Swap File System: This file system creates temporary storage to simulate extra space. It is beneficial when the system has a shortage of memory. As the name suggests, this file system swaps idle programs with active ones.
X File System: A unique file system contains information about processors and other systems in a hierarchical structure. It also enables dynamic access to kernel memory.
Getting Files
To display the names of all the files and directories present in the current directory, the command used is:
ls
Output:
And to see a detailed view of all these files, the command will be modified as:
ls -l
Output:
The above image shows that the information is separated into seven columns. Each column represents different properties.
The first column lists the permissions given to the user and the type of our file or directory.
The memory occupied in the number of blocks will be displayed in the second column.
The name of the user by whom that file or directory is created will be listed in the next column.
Every user has an associated group whose name is represented in the fourth column.
The file or directory size in bytes is mentioned in the fifth column.
The date and time of either file creation or modification are listed in the proceeding column.
The last column represents the name of the file or directory itself.
For example, the columns in 1st row are:
Column1 = -rw-r–r– (Permission and file type)
Column2 = 1 (Memory blocks)
Column3 = ayushi (user name)
Column4 = ayushi (group name_
Column5 = 0 (size in bytes)
Column6 = Oct 30 04:13 (date and time)
Column7 = file.txt (file name)
About Hidden Files
Most of the configuration files in Linux are hidden and will not be visible to you. All these file names are prefixed by dot(.). To view these files, you can give the command:
ls -a
Output:
The first two files, i.e., single dot(.) and double dot(..), represented the current directory and parent directory, respectively.
Create Files
To create a new file in Vi Editor, you need to give the following command:
vi <file_name>.<extension(optional)>
A new file with the given name is now in your current directory. This command will direct you to the file and to write and save the file do following the steps:
Press the 'i' key to jump to edit mode.
Write the file content.
Press the escape (esc) key to jump back to read-only mode.
After this, you can change the cursor's position using different keys.
Capital I = to move right
Small h = to move left
Small k = to move up
Small j = to move down
Type 'wq' (write and quit) to save, quit and go back.
We will see examples of this while performing file operations in the below section.
File Operation
For file management in Linux, along with file creation, we need to perform different operations. Let's discuss some of the most commonly used operations.
Copy the File
If you want to create a copy of your file, then you can use copy functionality of file management in Linux. The 'cp' command is used to copy the file content to the new file in the same directory. You need to mention the old and new file names as follows:
cp <old_file_name> <new_file_name>
Example:
Create a file having the name "text". Add some text to the file. Copy this file to another file. This will create a file with the name "newcopy" and copy the content of the " text " file. You can verify by opening the new file. The commands will be:
vi text
cp text newcopy
vi newcopy
Output:
Content written in the file “text”:
After these commands, the same content will be displayed to you in “newcopy”.
Accessing File Content
To print the file content, we can use the 'cat' command followed by the file name. If you want to view the content as a numbered list, you can use the '-b' option as:
cat -b <file_name>
Example: Let's print the file content added in the "text" file created above in both formats.
cat text
cat -b text
Output:
Rename Files
We can use the 'mv' command to rename our files. We have to mention the old name and the new name. The syntax of the command is as follows
mv <old_name> <new_name>
Example: Let's rename the file created in the above section to "newtext". The 'mv' command will create a new file with the new name and move the old name file to it. You can view this by listing all the files using the 'ls' command. To do so, the commands given will be:
ls
mv newcopy newtext
ls
Output:
Counting Number of Words
To get the word count of a file in Linux, we have the 'wc' command, followed by the file name. For example, let's count the number of words in the "text" file created in the copy file section.
Example:
wc newtext
Output:
The three numbers are confusing😕😕, right!!
So, the first number tells the number of lines in the file.
The second number represents the total word count.
The third number gives the actual file size in bytes.
Deleting Files from the Directory
To delete a file from your directory, file management in Linux offers the 'rm' command. The syntax for the command is:
rm <file_to_be_deleted>
Example: Let's delete all the above-created files and verify the result by listing the files.
ls
rm text
rm newtext
Output:
Metacharacters
These were some commonly performed operations. Sometimes, users create files with relatable names, and for searching these types of files, we can use metacharacters. Characters with special meanings are known as metacharacters. Two metacharacters are used while file management in Linux. These are:
Asterisk (*)
This is used for matching any characters.
Example: Let's create three files. We have already discussed how to create new files.
vi text1
vi text2
vi text3
Now, to display all the files that start with the character 't', the command will be
ls t*
Output:
Question Mark (?)
To search for only a single character, we use this metacharacter. Let's see an example using the above-created files.
Example: Search for the file followed by only one character, starting with the word 'text'
ls text?
Output:
Frequently Asked Questions
What are the three types of file permissions in Linux?
The file system in Linux provides three file permissions in Linux. These are Read, Write and Execute.
What are the different layers in the File system?
The file system in Linux consists of three layers used from top to bottom. These are Logical, virtual, and physical layers.
What are the different commands available for performing file operations?
The different commands are ls(to get file list), rm(to delete), cat(to print file content), vi(to create a file), wc(for word count), mv(rename), and cp(copy).
What are hidden files, and how to view them?
Most of the configuration files in Linux are considered hidden files. These file names start with a dot(.). To view the list of hidden files, use the '-a' option of the 'ls' command as “ls -a”.
Who introduced Linux, and in which year?
The initial release of Linux was on September 17, 1991, by Linus Torvalds.
Conclusion
In this blog, we have learned about file management in Linux. The articles contain a brief introduction to the file structure and features offered by file management in Linux. Further article introduces different file types, commands to create files, and perform different file operations.
If you found this blog has helped you enhance your knowledge, and if you want to learn articles like file management in Linux, check out our articles below: