Delete Files and Folders Using Linux Terminal
How to Delete Files and Folders Using Ubuntu Linux Terminal - To become a Linuxer, we must learn how to manipulate the data in the hard drive. One way to manipulate data is to delete files or folders.
In this article, the linux that I use is a derivative of Debian > Ubuntu, Elementary OS. This can also use on linux with other Debian or Ubuntu variants.
There are at least 3 commands that are used to delete data, both files and folders on Linux using the terminal, namely rm, unlink and rmdir.
- rm or unlink is used to delete files
- rm or rmdir is used to delete a folder
A. How to Delete Files
How to delete 1 file?
To delete a file in linux use the rm and unlink commands followed by the file name:
$ rm fileName.extension or $ unlink fileName.extensionFor example we want to delete the zoom application with the file name zoom_amd64.deb. So to delete the file is to use the command:
$ rm zoom_amd64.deb or $ unlink zoom_amd64.deb
How to delete multiple file?
What kind of delete method do you want?
If you want to delete only some files use the command:
$ rm fileName1.extension fileName2.extension fileName3.extension or $ unlink fileName1.extension fileName2.extension fileName3.extensionDon't forget every filename and other files are separated by a space ' '.
For example, we want to delete the files zoom_amd64.deb, learn_linux.jpg and guide_linux.pdf. So the command we use is:
$ rm zoom_amd64.deb learn_linux.jpg guide_linux.pdf or $ unlink zoom_amd64.deb learn_linux.jpg guide_linux.pdf
To delete multiple files, use to add a star character (*).
If you want to delete the entire file in the same file extension type use the command:
$ rm *.extensionFor example we want to delete all pdf files, then the command is:
$ rm *.pdfIf you want to delete all files, all of them use the command:
$ rm *Warning!! using the $rm * command will delete any files!
B. How to Delete Folders
To delete a folder that does not have a file in it, the
command used is:
$ rm -d folderName or $ rmdir folderName
To delete a folder that contains files, the command used
$ rm -r folderName
To delete multiple folders, use a command like the sequence
number 2 above, but by adding the folder name and separated by a space
' ', as follows:
$ rm -r folderName1 folderName2 folderName3
To delete all folders entirely, use the asterisk character (*) in
the following command:
$ rm -r *Warning!! using the $rm -r * command will remove whatever it is!