Creating and Managing Files in Linux Using Command Line Tools, Part 2

Pramod Dutta
3 min readMay 29, 2024

--

In this blog post, we’ll explore various commands and techniques for creating and managing files in a Linux environment. Understanding these commands is crucial for anyone working with Linux systems, whether you’re a beginner or an experienced user. We’ll cover file creation, adding content, viewing file contents, and using text editors in the command line.

1. Creating a File

To create a file in Linux, you can use the touch command. This command is straightforward and doesn't add any content to the file. For example:

“touch pramod.txt”

This command creates an empty file named pramod.txt. To verify its creation, use the ls command:

“ls”

You’ll see pramod.txt listed among the files in your directory.

2. Adding Content to a File

To add content to a file during creation, you can use the redirection operator (>). For example:

echo “Hello” > pramod1.txt

This command creates pramod1.txt with the content "Hello". You can list the files to see both pramod.txt and pramod1.txt:

3. Viewing File Details

To view detailed information about the files in a directory, including hidden files, use the ls -la command:

“ls -la”

This command displays the size, permissions, and other attributes of each file and directory. For example, the size may be shown in kilobytes (KB), and the permissions indicate who can read, write, or execute the file.

4. Viewing File Content

To view the content of a file, use the cat command:

“cat pramod1.txt”

This command displays the content of pramod1.txt in the terminal.

5. Using Text Editors in Command Line

Linux offers several text editors for the command line, including vim and nano. These editors are essential for editing files when no graphical user interface (GUI) is available.

Using Vim

To edit a file with vim:

“vim pramod1.txt”

Inside vim, press i to enter insert mode and start typing your content. For example:

This is a text file.
We are learning Linux commands.
I am using the vim editor.

To save and quit, press Esc, type :wq, and press Enter.

6. File Size and Permissions

To see the size and permissions of a file:

“ls -lh”

This command displays file sizes in a human-readable format (e.g., KB, MB) and shows the permissions, owner, and group associated with each file.

7. Using Cat, More, and Less Commands

To view the content of a file, you can use cat, more, and less:

  • cat: Displays the entire content of a file.
cat pramod1.txt

more: Allows you to view the content page by page.

more pramod1.txt

less: Similar to more, but with additional navigation features.

less pramod1.txt

8. Using Tail and Head Commands

To view the end or the beginning of a file, use the tail and head commands:

  • tail: Shows the last part of a file, often used for monitoring log files.
tail -f application.log
  • head: Shows the beginning part of a file.
head pramod1.txt

9. Deleting Files and Directories

To delete a file, use the rm command:

rm pramod1.txt
  • To delete a directory and its contents, use rm -rf:
rm -rf delete_me

Be cautious with rm -rf, as it forcefully deletes directories and their contents without confirmation.

Conclusion

In this blog post, we’ve covered essential Linux commands for creating and managing files, viewing file content and details, and using command line text editors. Mastering these commands will enhance your productivity and efficiency when working with Linux systems. Experiment with these commands to become more comfortable with the Linux command line interface. Happy learning!

Join Our Live Classes

If you’re serious about becoming an automation tester and want live classes with me, Join our new batch at https://sdet.live/become Use Code “PROMODE” for 10% OFF!” We’re starting a new batch soon, and I’d love to help you on your journey to becoming an automation tester.

Thanks for reading this post. If you found it helpful, please give it a like and share it with others who might benefit from it. See you in the next post!

--

--