How to Undelete Folder in Ubuntu? [With Solutions]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

Ubuntu is a versatile tool to work with multiple files and folders at a time. Sometimes you might delete any file or file or folder unwantedly. In such cases, you need to undelete that file or folder. So deleting and undeleting files and folders in Ubuntu is a very common task done by a Ubuntu user. In this article, I will show the process to undelete a folder in Ubuntu.

Process Flow Chart to Undelete Folder in UbuntuProcess flowchart of Ubuntu undelete folder.Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS

Steps to Undelete Folder on Ubuntu Using GUI

Here I will show you the process of moving a folder into the trash and then restoring that folder from the trash in Ubuntu.

Undelete a Folder from Trash

You can easily delete a folder from Graphical User Interface(GUI) and then undelete or restore that folder from the trash. Here I have a folder named Project on my desktop. At first, I will move the Project folder to the trash and then undelete or restore it from the trash. Here is the step-by-step guide:

  1. At first, Right click on the Project folder. Then click on the “Move to Trash” from the appeared option.Right-click on the Project folder then click on "move to trash".
  2. Now, click on “Trash”.Clink on the "trash".
  3. Then Right click on the Project folder from the Trash window and click on “Restore From Trash” to restore the Project folder from the Trash window.Click on the "Restore From Trash" to undelete folder in Ubuntu.
  4. Now, close the Trash window.Close the trash window.The following image shows I have successfully undeleted or restored the Project folder from the Trash.The Project folder has been undeleted successfully in Ubuntu.

3 Tips to Undelete Folder in Ubuntu

If you delete any folder in Ubuntu using the rm command, you can not undelete that folder in Ubuntu if your Ubuntu is installed in any virtual machine like VMWare. So here, I am sharing 3 preventive measures to easily undelete folders in Ubuntu.

1. Move a Folder to Trash then Undelete it in Ubuntu

If you remove any file or folder using the rm command you can never undelete or restore that file or folder in Ubuntu. A possible solution in such a case is to move that file or folder to the trash and then restore it from the trash. Here, I will move folder1 to the trash and then restore it from the trash. To achieve so, follow the below procedures:

  1. At first, open the Ubuntu terminal.
  2. Then, type the following command into the terminal to list the contents of the current directory:
    ls -l
    EXPLANATION
    • ls: Lists all contents of the current directory.
    • -l: Prints all the contents of the current directory with size, permission information, owner information, group information, last modification time, etc.
    Showing the current existence of the folder1 in Ubuntu.
  3. Now, execute the following command to move folder1 to the trash:
    trash -d folder1
    EXPLANATION
    • trash: Moves the following file or folder to the trash.
    • -d: Necessary for moving any folder to the trash.
    • folder1: Concern directory to be moved.
    Moving folder1 to trash.
  4. Then, run the following command to list the deleted files and folders:
    trash-list
    EXPLANATION
    • trash-list: Lists the deleted files and folders.
    Printing current contents of the trash to find which folder I have to undelete in Ubuntu..Here, /home/susmit/Desktop/folder1 is the removed folder.
  5. Now, execute the following command to restore the deleted folder:
    trash-restore /home/susmit/Desktop/folder1
    EXPLANATION
    • trash-restore: Restores the following folder.
    • /home/susmit/Desktop/folder1: The concerned folder is to be restored.
    Undeleting the folder1 folder in Ubuntu.The below image shows that I have successfully undeleted folder1 in Ubuntu.The folder1 folder has been successfully undeleted in Ubuntu.


2. Sort of Hack to Protect the Files to Undelete Folder in Ubuntu

To avoid deleting any file using the rm command we can use the mv command in Linux as an alias for the rm command. The mv command will move the file to a directory which is an alternative to the trash. Here, I will move the project directory using the rm alias, which is a bypass of deleting any file. To do so, follow the below procedures:

  1. At first, press CTRL+ALT+T to open the Ubuntu terminal.
  2. Then, type the following command into the terminal and press ENTER to create a directory named .trash on the user’s home directory:
    mkdir ~/.trash
    EXPLANATION
    • mkdir: Makes directory.
    • ~/.trash: Location and name of the directory to be made.
    Creating the .trash directory.
  3. Now, execute the following command into the terminal to open the ~/.bashrc file to which I will define the alias:
    nano ~/.bashrc
    EXPLANATION
    • nano: Opens a file.
    • ~/.bashrc: File name to be opened.
    Opening the .bashrc file.
  4. Then, insert the following command into the ~/.bashrc file to define the rm alias:
    alias rm='mv --target-directory="$HOME/.trash"'
    EXPLANATION
    • alias: Create a user-defined command with the desired name.
    • rm: Name of the alias.
    • mv: Command of the alias which will move the concerned file or folder to the destination location.
    • – -target-directory: Sets the destination directory of the moving file or folder.
    • $HOME/.trash: Destination directory of the moving file or folder.

    After inserting that line, press Ctrl+S then Ctrl+X to save then close the ~/.bashrc file.

  5. Afterward, run the following command into the terminal to communicate this change to the user:
    source ~/.bashrc
    EXPLANATION
    • source ~/.bashrc: Communicates the changes of ~/.bashrc to the user.
    Communicating the change of the .bashrc file with the user.
  6. Now, execute the following command into the terminal to list the current contents on the current directory:
    ls -l

    Initial existence of the project folder.

  7. Then, type the following command into the terminal and press the ENTER button to use the rm alias to move the project directory to ~/.trash:
    rm project
    EXPLANATION
    • rm: Here rm is the alias.
    • project: Directory name.
    Passing project folder to the rm alias previously defined.
  8. Now, execute the following command to list the contents of ~/.trash:
    ls -l ~/.trash
    EXPLANATION
    • ~/.trash: Directory name.
    The project folder has been moved to the .trash directory.The above image shows that the project directory has been successfully moved to ~/.trash which you can easily restore at any time.

3. Remove Folder Interactively to Undelete it in Ubuntu Later

To avoid the unwanted deletion of a folder in Ubuntu you can use an interactive prompt for removing any file. Here, I have a file named linux.txt. At first, I will define an alias named rm which will open an interactive prompt when I use the rm command. To achieve so, follow the below procedures:

  1. At first, open the Ubuntu terminal.
  2. Then, type the following command into the terminal and press ENTER to open ~/.bashrc:
    nano ~/.bashrc
    EXPLANATION
    • nano: Opens a file.
    • ~/.bashrc: Filename to open.
    Opening the .bashrc file.
  3. Then, insert the following command into the ~/.bashrc file to define the rm alias:
    alias rm="rm -i"
    EXPLANATION
    • alias: Create a user-defined command with the desired name.
    • rm: Name of the alias.
    • rm: Command of the rm alias which will remove the concerned file or folder.
    • -i: Opens interactive prompt.

    After inserting that line, press Ctrl+S then Ctrl+X to save then close the ~/.bashrc file.

  4. Afterward, run the following command into the terminal to communicate this change of the ~/.bashrc file to the user:
    source ~/.bashrc
    EXPLANATION
    • source ~/.bashrc: Communicates the changes of the ~/.bashrc file to the user.
    Communicating the change of .bashrc with user.
  5. Now, execute the following command into the terminal to list the current contents on the current directory:
    ls -l

    Initial existence of linux.txt file.

  6. Then, type the following command into the terminal to pass the linux.txt file to the rm alias:
    rm linux.txt
    EXPLANATION
    • rm: Calling the rm alias.
    • linux.txt: The filename.
    Passing the linux.txt file to the rm alias previously defined.
  7. Now, execute the following command into the terminal to list the current contents on the current directory:ls -l
    The linux.txt file does not exist here means this has been deleted.The above image shows that I have successfully deleted the linux.txt file interactively.

Conclusion

In this article, I have shown the process of how to undelete any file or folder in Ubuntu. After going through this article, I believe you will be confident enough to undelete any file or folder in Ubuntu.

People Also Ask

How do I undelete a folder in Ubuntu?

To undelete folder in Ubuntu: go to the Trash, then right-click on the mouse and select Restore From Trash. After that click Close to exit trash.

Are there any tools to recover deleted files in Ubuntu?

Yes. Several tools are available for the Ubuntu system to recover deleted files. For example, recovery utilities such as testdisk, photorec, extundelete, etc. can be installed via the terminal or software center to attempt recovery.

Can I undelete files in Ubuntu from backup?

Yes, you can. Open the terminal and run the command sudo -o rw,remount and enter the user account password. After that run the command sudo cp <filename> /media/<backup_location> and enter the user password again to restore files to their original location.

How to delete an empty folder in Ubuntu?

To delete an empty directory in Ubuntu type the rm -d FOLDER_NAME command into the terminal then press the ENTER button.

How to recursively remove a non-empty directory in Ubuntu?

To delete a non-empty directory recursively in Ubuntu, type the  “rm -r FOLDER_NAME” command into the terminal then press the ENTER button.

What is extundelete in Linux?

The extundelete is a utility that can undelete files and folders from an ext3 or ext4 partition in Linux.

Related Articles

Rate this post
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment