CRUD operations on file in Ubuntu using Terminal

navya , Credit to  volkotech-solutions Nov 21
CRUD operations on file in Ubuntu

This blog targets Ubuntu users looking to understand actions involving files and how to perform CRUD (Create, Read, Update, Delete) operations to manage them.

Pre-requisites:

  1. Laptop/Pc with Ubuntu OS installed
  2. Active Terminal

If you don’t have Ubuntu on your Laptop/Pc and if you feel to install it, then please refer to this blog

https://mycode.blog/naveen-golla/how-install-ubuntu-your-computer

The global way of creating files in Ubuntu is using terminal commands, we can also create files with the help of some code editors like vscode, atom, and sublime. Now, will discuss the file operations using the terminal commands.

Creating file using the Terminal in Ubuntu:

The only most effective way to perform any operation in Ubuntu is using a terminal, now will see all the four basic operations file with examples.
Major dealing of projects is with files only, so it is very important to learn how to create files on your machine. We have multiple types of files with unique extensions, here is the list of file extensions and their usage

.txt - Text file

Any file with a .txt extension is used to create a text file and the purpose of the text file is to store some data in the form of text for further reference of the project

The terminal command that is used to create a text file is touch <fileName>.txt
In the below screenshot, I’m creating one text file on the desktop, you can create any file in any path by just changing the directory.

Creating text file using Ubuntu terminal

.html - HTML file

Any file with a .html extension is used to create an HTML file which can be considered at the heart of any webpage. Simply, without an HTML file, there is no website.

Terminal command that is used to create HTML file is touch <fileName>.html
In the below screenshot, I’m creating one HTML file on the desktop, but all the HTML files could be created in the respective project folder for easy maintenance.

Creating HTML file using Ubuntu terminal

.pdf - PDF file

Any file with extension .pdf is for storing data in the form of pdf.

Creating pdf file using Ubuntu terminal

.doc - Document

Any file with a .doc extension is for storing data in document format. Now we will see the following operations for the index.html file, and the procedure for the remaining file types is the same: just change the file name.

Creating doc file using Ubuntu terminal

Reading file using the Terminal in Ubuntu:

We can read any type of file with the help of the cat command and the syntax of the command is cat <fileName>

Reading HTML file using Ubuntu terminal

As the file is empty, nothing prints here. Now it will update the file with the following procedure.

Updating file using the Terminal in Ubuntu:

We can update the files with comfortable editors, for Ubuntu we have two major editors namely nano and vim. With the help of these editors, we can update the files.
Now it will update the index.html file by using nano editor. The command that is used for opening the nano editor is sudo nano <fileName>

Updating HTML file using Ubuntu terminal

Then give your password and press enter, you can see the nano editor like this

Opening HTML file in Ubuntu nano editor

Then type your Html code here,

Writing HTML code in Ubuntu nano editor

Then press ctrl+x to save the file and then select y, then press enter.

Saving HTML code in nano editor using Ubuntu terminal

After this, it will ask to confirm the file name as shown below,

Modifying HTML file name in nano editor Ubuntu terminal

If you want to change the file name then do it, otherwise, press enter.

After updating the file try to read the file, by using the cat command then you can see the data present in the respective file.

Reading HTML file using Ubuntu terminal

Delete the file using the Terminal in Ubuntu:

Now if you don’t want to keep the file anymore, you can remove it from your project folder. The command that is used to remove the file is

rm -rf <filename>

Deleting HTML file using Ubuntu terminal

The file exists no more now.

Finally, I want to conclude that managing files in a project is very important because major data is present in different file formats only. So guys, go on and get the practice of all the file-related commands.

Feel free to share your comments and suggestions, thank you.

FAQ:

Is it possible to create files without terminal commands?

Yes, we can. The purpose of creating files is either to store data or to perform any action like with the Html files only the web pages are rendering. So to create and add some data in files we need editors like vscode, atom sublime, etc. So with the help of these editors, we can create new files and we have to save them with appropriate extensions.

Is it possible to create a pdf file with the terminal command and update the data?

Absolutely yes, you can create pdf files by using ubuntu commands and update the data with the help of LibreOffice writer, after all your data addition is done then you have to export the current document as pdf and can access it.

Comments

Authors

Read Next