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.
Key Takeaways
- Being able to undelete folders in Ubuntu.
- Getting familiar with moving any file or folder to the trash.
- Getting familiar with the process of defining and using an alias.
Process Flow Chart
Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS
Watch How to Undelete Folder in Ubuntu [With Solutions]
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.
Delete a Folder from GUI then Undelete that 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.
Steps to Follow >
➊ At first, Right click on the Project folder. Then click on the “Move to Trash” from the appeared option.➋ Now, click on “Trash”.
➌ 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.
➍ Now, close the Trash window.
The following image shows I have successfully undeleted or restored the Project folder from the Trash.
Complementary Information
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 some preventive measures to easily undelete folders in Ubuntu.
Move a File 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.
Steps to Follow >
➊ At first, open the Ubuntu terminal.
➋ Then, type the following command into the terminal to list the contents of the current directory.
ls -l
- 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.
trash -d folder1
- 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.
trash-list
- trash-list: Lists the deleted files and folders.
➍ Now, execute the following command to restore the deleted folder.
trash-restore /home/susmit/Desktop/folder1
- trash-restore: Restores the following folder.
- /home/susmit/Desktop/folder1: The concerned folder is to be restored.
- Remove All Files in a Directory with the Prompt in Linux
- 3 Ways to Remove Write Protected Regular File Using the rm command
- Remove All Files from Current Directory in Linux [2 Methods]
Sort of Hack to Protect the File 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.
Steps to Follow >
➊ At first, press CTRL+ALT+T to open the Ubuntu terminal.
➋ 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
- mkdir: Makes directory.
- ~/.trash: Location and name of the directory to be made.
nano ~/.bashrc
- nano: Opens a file.
- ~/.bashrc: File name to be opened.
alias rm='mv --target-directory="$HOME/.trash"'
- 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.
- –targe-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.
➎ Afterwards, run the following command into the terminal to communicate this change to the user.
source ~/.bashrc
- source ~/.bashrc: Communicates the changes of ~/.bashrc to the user.
ls -l
- 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.
rm project
- rm: Here rm is the alias.
- project: Directory name.
ls -l ~/.trash
- 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.
- ~/.trash: Directory name.
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 will use the rm command. To achieve so, follow the below procedures.
Steps to Follow >
➊ At first, open the Ubuntu terminal.
➋ Then, type the following command into the terminal and press ENTER to open ~/.bashrc.
nano ~/.bashrc
- nano: Opens a file.
- ~/.bashrc: Filename to open.
alias rm="rm -i"
- 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.
➍ Afterwards, run the following command into the terminal to communicate this change of the ~/.bashrc file to the user.
source ~/.bashrc
- source ~/.bashrc: Communicates the changes of the ~/.bashrc file to the user.
ls -l
- 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.
rm linux.txt
- rm: Calling the rm alias.
- linux.txt: The filename.
ls -l
- 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.
Conclusion
In this article, I have shown the process of undeleting 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
Related Articles
- How to Force Remove Directory in Linux? [Step-by-Step]
- Remove a User and Home Directory in Linux [2 Methods]
- How to Remove a Non-Empty Directory in Linux [2 Methods]
- 2 Cases of Recursive Remove of Directory in Linux
- 3 Ways to Remove All Files in a Linux Directory
- How to Find and Delete Directory in Linux [3 Methods]