Sign In
Sign In

How to Create a Text File in Linux Terminal

How to Create a Text File in Linux Terminal
JC Brian Refugia
Technical writer
Linux
26.02.2024
Reading time: 9 min

A text file is a type of file in Linux that contains plain text and can be opened and edited with a text editor. There's no particular coding or formatting in it. 

There are several ways to create a file in Linux. The fastest way is to use the Linux Command Line or Terminal. This is a fundamental skill for all users especially server admins, who need to quickly create text files, scripts, or configuration files for their work.  

Here are the four common methods on how to create a text file in the terminal.

Preparatory Steps for Crafting Files in Linux Through the Command Line

Before embarking on the task of generating files in a Linux environment using the command-line interface, ensure the following prerequisites are fulfilled:

  1. Access to a Functional Linux System: You must either have a Linux-based operating system installed on your computer or secure access to a Linux server via SSH (Secure Shell) protocol.

  2. Operational Terminal Interface: Confirm that your terminal application is accessible and fully operational. The terminal serves as your primary gateway to executing commands.

  3. Adequate User Permissions: Verify that you possess the requisite privileges to create files within the chosen directory. For directories with access restrictions, you may need to escalate privileges using administrative rights (sudo).

  4. Proficiency in Fundamental Commands: Acquaint yourself with essential commands, including touch for file creation, echo for printing text, cat for viewing file contents, and text editors like nano, vim, or vi for editing files directly.

  5. Availability of Text Editing Utilities: Ensure your system includes text editing tools, such as nano for command-line simplicity, vim for advanced configurations, or graphical options like gedit for user-friendly navigation.

  6. Directory Management Expertise: Develop familiarity with directory navigation commands like cd for changing the working directory and ls for listing directory contents. This knowledge streamlines your workflow and avoids potential errors.

Using the touch Command

The touch command in Linux is generally used to change the access and modification times of files. If the file doesn't exist, touch creates an empty file. 

To create a text file with the touch command in the Linux terminal, follow these steps:

  1. Open your terminal emulator.

  2. Type the command:

touch filename.txt

Image5

Replace filename.txt with the name for your text file. If the file already exists, touch will update the access and modification times without changing its content. If the file does not exist, touch will create an empty file with the given name. 

  1. Press Enter. The command will return without any output if it is successful.

  2. To verify that the file has been created, use the command ls to list the content of the current directory. 

24a059f9 C535 4422 8f95 16b7dca87d8a

Using the Redirection with echo Command

The echo command is widely used to display text on the terminal. But its capabilities go beyond that; it may also be used to write content to a file or create an empty file. To do this, the echo command is used in conjunction with double redirect symbols (single > can also be used) followed by the desired filename. 

To create a text file using the echo command in Linux, redirect the output of echo to a file. Here's the step-by-step process:

  1. Open your terminal emulator.

  2. Type the command:

echo “Your text content here” > filename.txt

Image8

Replace "Your text content here" with the text you want to add to the file. Make sure the text content is enclosed in double quotations (). 

  1. Press Enter. The echo command will write the specified text to the file filename.txt. If the file already exists, it will be overwritten with the new content. If the file does not exist, it will be created.

  2. To verify that the file has been created and contains the desired content, use cat command to display the content. 

Df931fbe B2fd 408f Ae95 A53318c0426d

Using the Redirection with cat Command

In Linux, the cat command is mostly used to concatenate and show file contents. It can, however, also be used to generate a text document. To create a text file using redirection with the cat command, redirect the standard output of cat to a file. Here's the step-by-step process:

  1. Open your terminal emulator.

  2. Type the following command:

cat > filename.txt

74a3a2f1 976f 44d4 Ba84 Cf082370f6c1

Replace filename.txt with the name for your text file. With the help of this command, cat is instructed to begin receiving input from the terminal and to redirect it into the filename.txt

  1. Press Enter. The terminal will be waiting for input. 

  2. Enter the text you want in the file by typing it and press Enter after each line. 

  3. Press Ctrl + D after entering the text you want in the file. This signals the end of input to the cat and saves the content. 

  4. To verify that the file has been created and contains the desired content, use the cat command to display the content. 

738f5fb6 Fed2 4c4a 8ffd 5760919a9f5c

Using printf for Advanced File Creation

The printf utility is a powerful alternative to echo, offering enhanced formatting options for structuring text. It allows users to create files with precisely formatted content.

  1. Launch the Terminal Application: Open the terminal to gain access to the command-line environment.

  2. Execute the printf Command: Use printf to define the text layout, incorporating formatting elements like newlines (\n) or tabs (\t). Redirect the output to a file using the > operator.

Example:

printf "First Line\nSecond Line\nIndented\tThird Line\n" >  formatted_file.txt
  1. Verify File Creation and Content: Employ the cat command to inspect the file's content and ensure the formatting matches expectations.

Append Without Overwriting: To add content to an existing file without overwriting its current data, replace > with the append operator >>:

printf "Additional content here.\n" >> formatted_file.txt

4. Using a Text Dditor

There is always at least one integrated command-line text editor in Linux distributions. You can also install many command-line text editors to benefit from their distinct features and advantages. Vim, Nano, and Emacs are the three terminal-based text editors that are most widely used in Linux. 

Vim

vim, which stands for "Vi IMproved," is a very flexible and adaptable text editor. It is well-known for its modal editing, which allows for distinct modes for various functions like text entry, navigation, and editing. It allows split windows, multiple buffers, syntax highlighting, and a large selection of plugins for extra features. To create a text file using vim, follow the steps below: 

  1. Open vim, with the desired filename as an argument with a new buffer for editing the file filename.txt.

Image12

  1. Press i to switch to Insert mode.

  2. Start typing and editing the filename.txt

  3. To save and exit, press Esc to ensure that command mode is running. Type: wq (write and quit) and press Enter.

Image2

Nano

nano is ideal for short adjustments and straightforward text files because it is lightweight and requires little setup. It provides support for basic text manipulation functions, search and replace, and syntax highlighting. To create a text file using nano, follow the steps below: 

  1. Open nano, with the desired filename as an argument with a new buffer for editing the file filename.txt.

0c16ba2d 440f 4824 Be3a A36b2ea47a6e

  1. Start typing and editing the filename.txt

  2. To save and exit, press Ctrl + O to write the file, confirm the filename, and then press Ctrl + X to exit Nano.

Sqdwefrthyuy

Emacs

emacs is a powerful and flexible text editor that supports syntax highlighting, multiple buffers, split windows, and integration with external tools and programming languages. To create a text file using emacs, follow the steps below: 

  1. Open emacs, with the desired filename as an argument with a new buffer for editing the file filename.txt.

  2. Start typing and editing the filename.txt

Image7

  1. To save and exit, press Ctrl + X, followed by Ctrl + S to save the file, and then Ctrl + X, followed by Ctrl + C to exit Emacs.

Note: If a message states that "VIM command not found", "nano command not found" or "emacs command not found" in Linux, it typically means that the vim, nano or emacs text editor is not installed on the system, or it's not included in the PATH environment variable, which is a list of directories where the operating system looks for executable files.

5239213a 9fb4 42e4 8806 Abda7f2d4a81

To resolve this, install the text editor first using the command: 

apt-get install vim
apt-get install nano 
apt-get install emacs

Gedit

Gedit is an intuitive graphical text editor that caters to both novice and experienced users. It supports plain text creation and syntax highlighting for programming languages.

  1. Open the Gedit Application: Launch Gedit either through the applications menu or by executing the following command in the terminal:

gedit example.txt

If the specified file does not exist, Gedit will automatically create a new one.

  1. Input Your Text: Type or paste your desired content into the editor. Gedit’s straightforward interface simplifies tasks ranging from quick edits to complex document preparation.

  2. Save the File: Save your work by pressing Ctrl + S or selecting File > Save. When creating a new file, specify a filename and choose a storage location.

  3. Verify the File: Return to the terminal and use commands like ls to confirm the file’s presence or cat to review its content.

Guidelines and Recommendations for File Creation

  • Directory Permissions: Always ensure you have sufficient permissions to create files in your target directory. If permissions are insufficient, consider working in a directory where you have full rights or elevate privileges using sudo.

  • Preventing Data Loss: Before using the > operator, double-check whether a file with the same name already exists, as the command will overwrite existing content. To preserve existing data, opt for the append operator >>.

  • Efficiency in Workflow: Familiarizing yourself with the printf command, the echo command, and text editors like vim or nano will enhance your productivity and reduce errors when working with files in Linux.

  • Structured Formatting: Use printf for creating files requiring structured content, such as configuration files or scripts with precise formatting needs.

Conclusion

Creating a file in Linux using the terminal is a fundamental skill that employs commands and command-line text editors. There are various quick and effective ways to create and manipulate text files using the Linux command line. These methods, such as using the touch command, echo command, cat command, printf command, or text editors such as vim, nano, gedit, or emacs, provide different strategies to fulfill a different demand. Users can select the method that best meets their requirements, such as creating empty files, appending text, or significantly modifying material. In summary, any of these methods enable Linux users to easily and quickly handle text files straight from the command line.

Hostman offers a reliable managed Linux VPS for your projects.

Linux
26.02.2024
Reading time: 9 min

Similar

Linux

How to Download Files with cURL

Downloading content from remote servers is a regular task for both administrators and developers. Although there are numerous tools for this job, cURL stands out for its adaptability and simplicity. It’s a command-line utility that supports protocols such as HTTP, HTTPS, FTP, and SFTP, making it crucial for automation, scripting, and efficient file transfers. You can run cURL directly on your computer to fetch files. You can also include it in scripts to streamline data handling, thereby minimizing manual effort and mistakes. This guide demonstrates various ways to download files with cURL. By following these examples, you’ll learn how to deal with redirects, rename files, and monitor download progress. By the end, you should be able to use cURL confidently for tasks on servers or in cloud setups. Basic cURL Command for File Download The curl command works with multiple protocols, but it’s primarily used with HTTP and HTTPS to connect to web servers. It can also interact with FTP or SFTP servers when needed. By default, cURL retrieves a resource from a specified URL and displays it on your terminal (standard output). This is often useful for previewing file contents without saving them, particularly if you’re checking a small text file. Example: To view the content of a text file hosted at https://example.com/file.txt, run: curl https://example.com/file.txt For short text documents, this approach is fine. However, large or binary files can flood the screen with unreadable data, so you’ll usually want to save them instead. Saving Remote Files Often, the main goal is to store the downloaded file on your local machine rather than see it in the terminal. cURL simplifies this with the -O (capital O) option, which preserves the file’s original remote name. curl -O https://example.com/file.txt This retrieves file.txt and saves it in the current directory under the same name. This approach is quick and retains the existing filename, which might be helpful if the file name is significant. Choosing a Different File Name Sometimes, renaming the downloaded file is important to avoid collisions or to create a clear naming scheme. In this case, use the -o (lowercase o) option: curl -o myfile.txt https://example.com/file.txt Here, cURL downloads the remote file file.txt but stores it locally as myfile.txt. This helps keep files organized or prevents accidental overwriting. It’s particularly valuable in scripts that need descriptive file names. Following Redirects When requesting a file, servers might instruct your client to go to a different URL. Understanding and handling redirects is critical for successful downloads. Why Redirects Matter Redirects are commonly used for reorganized websites, relocated files, or mirror links. Without redirect support, cURL stops after receiving an initial “moved” response, and you won’t get the file. Using -L or --location To tell cURL to follow a redirect chain until it reaches the final target, use -L (or --location): curl -L -O https://example.com/redirected-file.jpg This allows cURL to fetch the correct file even if its original URL points elsewhere. If you omit -L, cURL will simply print the redirect message and end, which is problematic for sites with multiple redirects. Downloading Multiple Files cURL can also handle multiple file downloads at once, saving you from running the command repeatedly. Using Curly Braces and Patterns If filenames share a pattern, curly braces {} let you specify each name succinctly: curl -O https://example.com/files/{file1.jpg,file2.jpg,file3.jpg} cURL grabs each file in sequence, making it handy for scripted workflows. Using Ranges For a series of numbered or alphabetically labeled files, specify a range in brackets: curl -O https://example.com/files/file[1-5].jpg cURL automatically iterates through files file1.jpg to file5.jpg. This is great for consistently named sequences of files. Chaining Multiple Downloads If you have different URLs for each file, you can chain them together: curl -O https://example1.com/file1.jpg -O https://example2.com/file2.jpg This approach downloads file1.jpg from the first site and file2.jpg from the second without needing multiple commands. Rate Limiting and Timeouts In certain situations, you may want to control the speed of downloads or prevent cURL from waiting too long for an unresponsive server. Bandwidth Control To keep your network from being overwhelmed or to simulate slow conditions, limit the download rate with --limit-rate: curl --limit-rate 2M -O https://example.com/bigfile.zip 2M stands for 2 megabytes per second. You can also use K for kilobytes or G for gigabytes. Timeouts If a server is too slow, you may want cURL to stop after a set time. The --max-time flag does exactly that: curl --max-time 60 -O https://example.com/file.iso Here, cURL quits after 60 seconds, which is beneficial for scripts that need prompt failures. Silent and Verbose Modes cURL can adjust its output to show minimal information or extensive details. Silent Downloads For batch tasks or cron jobs where you don’t need progress bars, include -s (or --silent): curl -s -O https://example.com/file.jpg This hides progress and errors, which is useful for cleaner logs. However, troubleshooting is harder if there’s a silent failure. Verbose Mode In contrast, -v (or --verbose) prints out detailed request and response information: curl -v https://example.com Verbose output is invaluable when debugging issues like invalid SSL certificates or incorrect redirects. Authentication and Security Some downloads require credentials, or you might need a secure connection. HTTP/FTP Authentication When a server requires a username and password, use -u: curl -u username:password -O https://example.com/protected/file.jpg Directly embedding credentials can be risky, as they might appear in logs or process lists. Consider environment variables or .netrc files for more secure handling. HTTPS and Certificates By default, cURL verifies SSL certificates. If the certificate is invalid, cURL blocks the transfer. You can bypass this check with -k or --insecure, though it introduces security risks. Whenever possible, use a trusted certificate authority so that connections remain authenticated. Using a Proxy In some environments, traffic must route through a proxy server before reaching the target. Downloading Through a Proxy Use the -x or --proxy option to specify the proxy: curl -x http://proxy_host:proxy_port -O https://example.com/file.jpg Replace proxy_host and proxy_port with the relevant details. cURL forwards the request to the proxy, which then retrieves the file on your behalf. Proxy Authentication If your proxy requires credentials, embed them in the URL: curl -x https://proxy.example.com:8080 -U myuser:mypassword -O https://example.com/file.jpg Again, storing sensitive data in plain text can be dangerous, so environment variables or configuration files offer more secure solutions. Monitoring Download Progress Tracking download progress is crucial for large files or slower links. Default Progress Meter By default, cURL shows a progress meter, including total size, transfer speed, and estimated finish time. For example: % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current                                 Dload  Upload   Total   Spent    Left  Speed100  1256  100  1256    0     0   2243      0 --:--:-- --:--:-- --:--:--  2246 This readout helps you gauge how much remains and if the transfer rate is acceptable. Compact Progress Bar If you want fewer details, add -#: curl -# -O https://example.com/largefile.iso A simpler bar shows the overall progress as a percentage. It’s easier on the eyes but lacks deeper stats like current speed. Capturing Progress in Scripts When using cURL within scripts, you might want to record progress data. cURL typically sends progress info to stderr, so you can redirect it: curl -# -O https://example.com/largefile.iso 2>progress.log Here, progress.log contains the status updates, which you can parse or store for later review. Conclusion cURL shines as a flexible command-line tool for downloading files in multiple protocols and environments. Whether you need to handle complex redirects, rename files on the fly, or throttle bandwidth, cURL has you covered. By mastering its core flags and modes, you’ll be able to integrate cURL seamlessly into your daily workflow for scripting, automation, and more efficient file transfers.
29 January 2025 · 7 min to read
Linux

How to Extract or Unzip .tar.gz Files in Linux

Exploring the Linux landscape often means dealing with several file formats, especially compressed ones like .tar.gz. This format is popular because it combines multiple documents and folders into one compressed archive. Whether you're obtaining software packages, organizing project backups, or overseeing data storage, mastering this format usage is essential.  Throughout this guide, we will examine various strategies for unpacking .gz archives in Linux. From the versatile tar command to the more straightforward gzip and gunzip commands, we'll cover everything. We'll also dive into combining commands like unzip and tar, and using graphical interfaces for those who prefer a more visual approach. Why Choose .tar.gz? Listed below are few key reasons why you might opt to utilize this format: Space Efficiency: The combination of tar and gzip allows for the streamlined compression of large data amounts, enhancing disk space usage. Simplified Data Management: Merging several documents and directories into a single archive enhances data management and organizes storage. Easy Distribution: This widely-adopted format ensures seamless transfers between systems without any compatibility hurdles. Preservation of Metadata: The tar utility maintains file permissions and timestamps, making it perfect for backups and migrating systems. Creating a .tar.gz File Before jumping into extraction, it's helpful to know how to create an archive. This makes it easier to combine and compress many documents into one neat, smaller package. Here is the standard syntax for creation: tar -czf archive-name.tar.gz file1 file2 directory1 Where: c: Creates an entirely new archive. z: Perform compression. f: Assigns a specific name to the archive. For instance, to compress report1, report2, and the directory projects into a file called backup, apply: tar -czf backup.tar.gz report1.txt report2.txt projects For verification, list the directory items via: ls Examining .tar.gz Content To examine the items without extracting them, use a command that lists every compressed item. This is particularly handy for verifying items before unpacking. To list .gz content: tar -ztvf archive-name.tar.gz For instance, to list the items of backup: tar -ztvf backup.tar.gz Extracting .tar.gz in Linux Linux offers a variety of extraction methods for these archives, each bringing its own advantages. Here are comprehensive instructions for utilizing various commands and tools. Method 1: Via tar Utility The tar command is a powerful and flexible utility designed to manage compressed documents, offering functions to create, extract, and display the items of archives. This command is your ultimate tool for handling .gz resources efficiently. Basic Extraction To unpack .gz items directly into the current directory, apply: tar -xvzf archive-name.tar.gz Where: x: Unpacks the archive's items. v: Verbose mode actively displays each file being unpacked. z: Decompresses the data. f: Gives the archive a unique name. For unpacking the backup, apply: tar -xvzf backup.tar.gz Extracting to a Specific Directory For placing the unpacked files in a different location, use the -C option to indicate your chosen directory. This is handy when you need to ensure your retrieved file are neatly arranged in a designated location. To unpack the items into a chosen directory, apply: tar -xvzf archive-name.tar.gz -C /path/to/destination For instance, to unpack the backup into the Documents folder, utilize: tar -xvzf backup.tar.gz -C /home/user/Documents Extracting Specific Content For retrieving certain items from the archive, simply provide their names. This enables you to pinpoint and retrieve just the necessary data.  Here’s the format: tar -xvzf archive-name.tar.gz file1 file2 For example, to retrieve report1 and report2 from backup, apply: tar -xvzf backup.tar.gz report1.txt report2.txt Extracting Contents with a Specific Extension For retrieving items with a particular extension, the --wildcards option proves to be quite useful. This option lets you filter and retrieve data based on their names or extensions. Here's the syntax: tar -xvzf archive-name.tar.gz --wildcards '*.txt' For instance, to retrieve all .txt docs from backup: tar -xvzf backup.tar.gz --wildcards '*.txt' Method 2: Via gzip Utility The gzip is a tool primarily used for compressing data, but it can also decompress them with the -d option. This method is straightforward and effective for handling .gz resources. To unzip a .gz file, apply the subsequent command: gzip -d archive-name.tar.gz For instance, to unpack backup, apply: gzip -d backup.tar.gz After decompressing, retrieve the items via: tar -xf archive-name.tar For instance: tar -xf backup.tar Method 3: Via gunzip Utility The gunzip is a specifically designed tool for decompressing .gz documents, functioning as an alias for gzip -d. This command is simple to use and directly addresses the need to decompress .gz files. To decompress, apply: gunzip archive-name.tar.gz For example: gunzip backup.tar.gz After decompressing, unpack the items through: tar -xf archive-name.tar For example: tar -xf backup.tar Method 4: Via GUI For users who favor a GUI, various Linux desktop environments include file managers equipped with extraction tools. This method is user-friendly and ideal for beginners. Extracting Contents to the Current Directory Find the .gz file within your file manager. Right-click on it and choose "Extract." Extracting Contents to a Specific Directory Spot the .gz file within your file explorer. Right-click on it and select "Extract to…". Choose the destination directory. Handling Large Archives with Parallel Decompression When handling massive archives, pigz (Parallel Implementation of gzip) can significantly enhance decompression speed by using several CPU cores. Here's how to use it: Install pigz on Linux via: sudo apt install pigz To uncompress a .gz file via pigz, apply: pigz -d archive-name.tar.gz After decompression, retrieve the resulting .tar doc with: tar -xf archive-name.tar Utilizing Compression with Encryption For added security, you can encrypt your .gz doc. GPG (GNU Privacy Guard) can be used to encrypt documents, ensuring that sensitive information remains protected during storage and transfer. Encrypting an Archive For encryption, use GPG with the following command: gpg -c archive-name.tar.gz Decrypting an Archive To decrypt an encrypted archive, apply: gpg -d archive-name.tar.gz.gpg > archive-name.tar.gz Tips for Content Extraction in Linux Backup Important Docs: Always create backups before unpacking multiple docs to avoid data loss. Check Permissions: Ensure you possess the required permissions to retrieve documents in the designated directory. Utilize Wildcards Carefully: Be cautious when using wildcards to avoid unintentional extraction. Troubleshooting Frequent Issues with Extraction Here are a few common extraction difficulties and the ways to address them: Corrupted Archives In case an archive is corrupted, try using the --ignore-zeros option to retrieve it: tar -xvzf archive-name.tar.gz --ignore-zeros Insufficient Permissions Confirm that you have the proper permissions to access and modify files. Utilize sudo if required: sudo tar -xvzf archive-name.tar.gz -C /path/to/destination Disk Space Issues Check that you have enough disk space to unzip the documents. Verify disk usage with: df -h Conclusion Unpacking .tar.gz documents in Linux is a simple task, with multiple methods to cater to different user preferences. Whether you're using the tar, gzip, gunzip commands, or a GUI, Linux equips you with efficient tools to handle compressed data seamlessly. This guide empowers you with the know-how to confidently retrieve .gz docs. Whether it's handling software packages, arranging backups, or managing data storage, mastering the creation and extraction of such files keeps your workflow streamlined and efficient.  By mastering the creation and extraction of these files, you streamline your workflow and enhance your overall efficiency, making data management a breeze.
28 January 2025 · 7 min to read
Linux

Linux Permissions Explained

In Linux, permissions are extremely valuable in dealing with access to folders as well as files. It makes sure proper authority over which one can deal with them. Effectively handling these privileges is fundamental for enhancing system file management and security. These privileges give groups or users the ability for reading, executing, or modifying, directories and their content. These rules safeguard data and restrict access, particularly in environments with more than one user. Each folder or file comes with particular rights that represent what users can accomplish. This article will demonstrate the basis of permissions, point out access, understanding, and changing privileges for them, and manage folders and their content. Basis of Permissions For beginners, the directories or file privileges can be challenging. It involves the concepts of types and groups as below:  Types  In Linux, each folder or file holds three kinds of permissions, each serving a particular purpose: Read (r): It indicates the permission to view the file’s content or enlist the items inside the folder. Write (w): It allows modifications to the particular file or addition and deletion of files inside the directory. Execute (x): It permits the file execution as the program or getting the folder content. Groups They are classified into three groups, each serving a particular role: User (Owner): It indicates the user who has the ownership rights of the folder or file. Group: It indicates a bunch of users having shared access rights. Others: It includes those who are not owners or participants of the desired group. Permission Formats Privileges are visualized in two formats: symbolic and octal. The symbolic employs symbols to mean rights, r indicates reading, w refers to writing, and x is utilized for the execution purpose. In contrast, the octal utilizes numbers, where 4 means reading, 2 stands for writing, and 1 signifies execution. Linux Display Permissions  Linux offers several methods to examine privileges. Individuals can employ a terminal for detailed information or go through the file manager's properties option for a graphical visualization. Using GUI This approach is the most straightforward for evaluating rights of permissions. It permits individuals to display them through the file manager's properties. To employ this method, hit the right-click on the desired folder and click Properties: Next, navigate to Permissions for viewing the permissions given to the particular directory and its content: In the figure, readers can see and adjust privileges for directories and their content, defining what the group is permitted to do, such as modifying, accessing, or deleting them. Additionally, it provides security context info and offers the choice to implement these privileges to all enclosed files: Through the ls Command You can employ the ls command along with -l, followed by the specified folder or file, to analyze its stats, including privileges: ls -l <file_or_directory_name> It retrieves thorough entries, including file privileges and a variety of properties. For instance, the below one retrieves the privilege attributes of the Downloads: ls -l Downloads In the output, the starting part indicates the permissions for all files or folders. For instance, -rw-rw-r-- describes the file as having reading and writing rights for the group as well as the owner. Also, reading-only privileges for others. drwxrwxr-x demonstrates the particular folder possessing the privileges of reading, writing, and executing for the group and owner. Also, reading and executing privileges for others. The next section describes the number of hard links to a particular file or folder. The next section shows (e.g., linuxuser) the owner. The next part shows the group which is corresponding with the directory or file. The fifth part describes the file's size in bytes. Next you see the most recent modification date and time, and finally the seventh section shows the file or folder’s name. Through the namei Utility In Linux, namei is an effective utility that shows the individual sections of a file or folder path along with their rights: namei -l /path/to/file Now, employ the namei -l to visualize comprehensive details about the Downloads folder: namei -l Downloads In this outcome, f: Downloads relates to the last entry in the folder, e.g. Downloads. The d signifies that it is a directory. The rwxr-xr-x means that the linuxuser has the right to read, write, and execute. However, both the linuxuser owner and the group have the capacity to read and execute privileges. It confirms that the linuxuser group as well as a user have the owners' rights of the particular folder. Through the stat Command This utility retrieves comprehensive info about the particular folder and its content, e.g. files: stat fileName  Let’s employ it to retrieve the comprehensive statistics of the Downloads: stat Downloads It retrieves the size of the file, rights, and a lot more: Modifying Permissions Editing file and folder rights are effective for system privacy purposes. Linux provides two main methods to revise privileges: symbolic and absolute mode. Symbolic Mode In this mode, individuals adjust permissions by adding (+), deleting (-), or setting (=) specific rights for the owner, group, or others. For making these modifications, the chmod is utilized.  Let's check out the permissions for the hostmanData file: ls -l hostmanData For adding execution access for the file’s owner, utilize the chmod utility as below: chmod u+x hostmanData Next, verify the updated privileges by running: ls -l hostmanData Absolute Mode In this method, rights are given through octal synonyms. There, every digit is related to reading, writing, and executing access for the user, group, and others. For instance, the code line allows full privileges to the owner and gives reading and executing access to the group and others: chmod 755 hostmanData Modifying Owner Rights The chown utility permits individuals to alter the folder's ownership and content. It allocates the new group or owner to maintain access control. Modifying Ownership We can alter the owner status of a particular directory or file via the chown. For instance, to alter the privileges of the hostmanData file to anees, employ the below code line: sudo chown anees hostmanData Next, confirm the changes via the following code line: ls -l hostmanData Modifying Group Ownership For updating the owner of a group of files, you can employ the below syntax: sudo chown :users hostmanData The above line updates the group of the hostmanData from linuxuser to users: Other Permissions  Linux permits individuals the appropriate way to handle advanced or complex operations via the below utilities: setuid: It allowed the file to execute with the authority of the owner compared to the user when implemented to the particular executable file. setgid: It permits the specific file for execution with the particular authority of the group that corresponds with the given file. Sticky Bit: It makes sure that the file’s owner has the capacity for renaming or deleting particular files inside a particular folder. Final Words In Linux, permissions are significantly important for handling access to particular folders or files. It plays an essential impact in system management or security. In this article, we covered the basis of permissions, their authority and modification, and editing ownership. We also demonstrated special rights to deal with complicated tasks. With a solid comprehension of these concepts, users can effectively secure Linux and manage access with ease.
27 January 2025 · 6 min to read

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start.
Email us
Hostman's Support