How to remove file and directory in Linux / Unix Operating system?
If you have ever encountered the a Linux and UNIX system while working with any database like I did, So you must remember some of the quick commands to perform short tasks. One of them is ‘rm’ to perform delete or remove operation.
“rm” is the short form of remove that frequently utilizes in Unix to delete file. Whereas ‘rmdir’ is the remove directory Linux syntax.You can not remove objects until your OS user has appropriate rights for that particular file or directory.You can not remove objects until your OS user has appropriate rights for that particular file or directory.
rm -r object.txt
Here ,
Object.txt is indicating any OS file.
-r = Remove the file (objct.txt) from this directory or mount point recursively.
You can still have an option to remove a file forcefully without getting any prompt. You can do it by adding “-f” with Unix delete file command.
Example:
rm –f –r Object.txt
In order to remove an empty directory from any mount point you can use ‘rmdir ’ instead of ‘rm’ command. For Example:
rmdir empty_dir
where rmdir is the syntax to delete null directory.
empty_dir is the name of that directory.
If it is not empty, then Linux shell command prompt you with an error.
Error Message:
rmdir: empty_dir: Directory not empty
Note: Please use next tip to remove such directory.
If you need to delete all directories and archives in any particular directory at once, then you can use following Linux command.
rm –rf {directoryName}
Let say your directory name is ‘mydir’ and you wanted to delete all files within this directory. So, we can use shell operator “*” with remove syntax. For example:
rm -r mydir/*
Unlike deleting dozens of files one by one in a single directory, you can remove all files at once that have the same extension.
Like I have five objects in mydir_1 such as:
1- Abcd.avi 2- Abcd2.avi 3- Abcd3.txt 4- Abcd4.avi 5- Abcd5.ora
From them I just want to remove files that have ‘.avi’ extension. This activity will save my time and effort.
Cd mydir_1
rm –rf *.avi
Caution:
Beware while executing some of the following delete operations with command prompt as this will lead to a disastrous impact.
Do not try to execute mentioned below command from the root or any user, because these commands will delete all your data.
rm –rf / rm –rf *
What is the Main Cause of a Heart Attack? What is its Solution? A heart attack is the blockage of… Read More
In the vast economic arena, one term that often takes center stage, inciting extensive debates and discussions, is the "debt… Read More
De-Dollarization: The Changing Face of Global Finance The financial landscape is in a state of flux, with an intriguing economic… Read More
The curtains closed on a dramatic Bundesliga season with Bayern Munich standing tall once again, clinching their 11th straight title.… Read More
The Unfolding Story of Celine Dion's Health In recent news that has left fans across the globe stunned, iconic singer… Read More
As the echoes of the recent NBA season start to fade, the attention of enthusiasts is firmly glued to one… Read More