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.
- Laptop/Pc with Ubuntu OS installed
- Active Terminal
In Ubuntu, we can do CRUD operations in two methods, they are
- Using terminal
- 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
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.
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.
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 -
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:
- Using copy command and
- 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
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.
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.
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.
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
Now select a new folder and click create,
Here you can see your new folder.
Read directory/folder using UI in Ubuntu:
Now to read the directory, do right-click on the directory , then select open.
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.
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.
Remove directory/folder using UI in Ubuntu:
You can remove the directory by right-clicking on the folder and selecting move to trash option.
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.
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.