loader

Removing files or directories

“rm” stands for remove, so as Its name shows, this command deletes files, directories, and even symbolic links- which we will cover in later lessons-. “rm” command works silently, so users must be careful using it because this command deletes files permanently and users can not restore them after deleting. Although we mentioned that the “rm” command deletes directories, deleting directories is not its default functionality.

Syntax

rm [OPTIONS]…..FILE

Examples

rm” without option will delete the file.
-i: As we mentioned before, “rm” works silently, so that this option will ask for delete confirmation.
-v: verbose the “rm” execution.
-f: Forces to delete the file. it means that never prompts the user and ignores nonexistent files.
-r: “rm” command by default can’t delete a directory to do so we should use this option to delete a directory with its content recursively.
-d: To delete an empty directory, use this option. Note that if the directory is not empty, this option won’t delete it.

Practice

Consider the image below and do the practice.

				
					[root@Lpic1-CentOs9 ~]# pwd
/root
[root@Lpic1-CentOs9 ~]# tree
├── dir1
│   ├── dir2
│   │   ├── txt3
│   │   └── txt4
│   ├── dir3
│   ├── dir4
│   ├── txt1
│   └── txt2

				
			

1- Delete the “txt1” file using the relative address, and then delete the “txt2” file using the absolute address.

Note: We use Centos 9 in the Lpic1 course. So as you have noticed in the answers “rm” command asks for confirmation without the “-i” option. It might be different in prior versions and other distros, and the “rm” without the “-i” option silently deletes the file.

2- Use the “-i” option to confirm deleting “txt4” in the “dir2” directory and use the “-v” to verbose the execution.

3-Force “rm” command to delete “txt3” file under “dir2” directory.

4-Create “txt3” and “txt4” files under “dir2” directory again, then delete “dir2” directory using “-r” option.

5-Remove “dir3” and “dir4” directories using, the “-d” option.

				
					**********************Answers****************************
************************p1*******************************
[root@Lpic1-CentOs9 ~]# rm  dir1/txt1
rm: remove regular file 'dir1/txt1'? y
[root@Lpic1-CentOs9 ~]# rm  /root/dir1/txt2
rm: remove regular file '/root/dir1/txt2'? y
************************p2*******************************
[root@Lpic1-CentOs9 ~]# rm -i -v dir1/dir2/txt4
rm: remove regular file 'dir1/dir2/txt4'? y
removed 'dir1/dir2/txt4'
************************p3*******************************
[root@Lpic1-CentOs9 ~]# rm -f ./dir1/dir2/txt3
[root@Lpic1-CentOs9 ~]# tree
├── dir1
│   ├── dir2
│   ├── dir3
│   └── dir4
************************p4*******************************
[root@Lpic1-CentOs9 ~]# touch ./dir1/dir2/txt{3,4}
[root@Lpic1-CentOs9 ~]# tree
├── dir1
│   ├── dir2
│   │   ├── txt3
│   │   └── txt4
│   ├── dir3
│   └── dir4
[root@Lpic1-CentOs9 ~]# rm -d ./dir1/dir2
rm: cannot remove './dir1/dir2': Directory not empty
rm -r ./dir1/dir2
rm: descend into directory './dir1/dir2'? y
rm: remove regular file './dir1/dir2/txt3'? y
rm: remove regular file './dir1/dir2/txt4'? y
rm: remove directory './dir1/dir2'? y
[root@Lpic1-CentOs9 ~]# tree
├── dir1
│   ├── dir3
│   └── dir4
************************p5*******************************
[root@Lpic1-CentOs9 ~]# rm -d dir1/dir{3,4}
rm: remove directory 'dir1/dir3'? y
rm: remove directory 'dir1/dir4'? y
[root@Lpic1-CentOs9 ~]# tree
├── dir1

				
			

Leave a Reply

Your email address will not be published. Required fields are marked *