CRUD operations on the Directory(folder) in Ubuntu Terminal

navya , Credit to  volkotech-solutions Nov 16
CRUD operations on directory/folder in Ubuntu

Find a detailed explanation to perform CRUD (Create, Read, Update, Delete) operations on a directory or folder & learn how to manage them using Ubuntu terminal.

Pre-requisites:

  • Laptop/Pc with Ubuntu OS installed
  • Active Terminal

In Ubuntu, we can do CRUD operations in two methods, they are

  1. Using terminal
  2. With UI

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

1. Using Terminal

The only effective way to perform any operation in Ubuntu is to use a terminal; now you will see all the four basic operations directory with examples.

Creating directory/folder using the Terminal in Ubuntu:

A directory is similar to a container, which holds ‘n’ no. of files, images, and required data of a project. The terminal command that is used to create a directory/folder is mkdir <filename>. In the below screenshot, I created a directory called Demo on the desktop; you may create a  directory in any path by just altering the current working directory.


Creating folder using terminal

Reading directory/folder using the Terminal in Ubuntu:

Now, if you read the directory, we will not get anything why because the directory is empty now. We can read the directory with the following command ls <directoryName>

In the previous example, we have created a directory on the desktop by the name Demo, now we will read that particular directory with the help of ls command.

listing the directory using terminal

We have another command to get complete details of the folder and the command is ls -al <directory name>. This will print the information of the directory along with the permissions.


In the below screenshot, you can see the default permissions of the directory, if you want you can change the permissions as required. To get detailed information about file permissions please refer to the following blog - 
 

listing the directory along with state of permissions using terminal

Updating directory/folder using the Terminal in Ubuntu:

So we have to update the directory, we can update the directory using two commands. They are:

  1. Using copy command and 
  2. using the move command

Using copy command:

Copy command just keeps the original folder and gives the copy of original to the destination folder, and the command is cp -R <sourceFolder> <destinatationFolder>

In the below screenshot, you can see that without recursive command we are unable to move any file that has data, but with the recursive command we did that


Updating the directory using terminal

Using move command:

Move command will delete the original file and move the content of the original file to a new file. The syntax of the move command is 
mv <SourceFolderName> <DestinatationFoldername>. 

In the below screenshot, you can see the folder name as Demo without the move command.

Copying data from one directory to another using terminal

In the below screenshot, you can see the same folder name is changed to New. So move command will delete the original folder and change the folder name along with the data.

Updating folder name

Removing directory/folder using the Terminal in Ubuntu:

We can remove any unwanted directory/folder with this command, rm -rf <directoryName> Refer to the below screenshot, for more clarifications.

Removing the folder using terminal

2. Using UI

We have another method of creating, reading, updating, and deleting the directory. That is using UI. UI in the sense in your computer just creating folder/directory by just right-clicking and selecting the option create a new folder, as shown below.

Creating directory/folder using UI in Ubuntu:

Right-click on your desktop or any path you want to create the folder

Creating folder using Ubuntu GUI

Now select a new folder and click create,

Writing folder name using Ubuntu GUI

Here you can see your new folder.

Folder is created with the name newFolder

Read directory/folder using UI in Ubuntu:

Now to read the directory, do right-click on the directory , then select open.

Reading the directory using Ubuntu GUI

Now click open, after that you can see all the available data in the directory. As it is a newly created directory it is empty now.

Empty folder is appeared in Ubuntu file explorer


Update directory/folder using UI in Ubuntu:

Now if you want to store any files in this directory, you can copy-paste the data like images, text files, project files, and whatever you want.

In the below screenshot, I have collected one folder and some random images, so if you open the directory, you can see the data.

Updating folder with some date using Ubuntu GUI

Remove directory/folder using UI in Ubuntu:

You can remove the directory by right-clicking on the folder and selecting move to trash option.

Deleting folder using Ubuntu GUI

Conclusion

With the help of these simple commands, you can do operations on the directory like create, read, update and delete. Maintaining your data in an appropriate folder can assist you in keeping track of your data without any confusion.

FAQ:

Which method is more preferable for CRUD operations?

Using the terminal method is more preferable, as it is very simple and takes very little time to do all the operations, whereas the UI method takes more time to track the folder and perform operations.

Where can we retrieve the deleted folders in Ubuntu?

You can find all your deleted files in the Trash folder and can retrieve them back within a particular duration of time.
 

Comments

Authors

Read Next