This article is going to show you that how to Remove Files and Directories in Linux. If you want to learn Remove Files and Directories in Linux Command Line then this article is best for you.
Linux is an operating system, like Windows OS, iOS, and Mac OS. Linux is one of the most popular operating system on the planet, Android is also powered by Linux OS(operating system). An operating system is software that manages all hardware resources associated with desktop or laptop. In simple words, the OS(operating system) manages communication between software and hardware.
Remove Files in Linux terminal
To remove a file from Linux system using the command line, use either the rm
or unlink
command.
The unlink
command remove only a single file and the rm
command remove multiple files at once.
- To delete a file, use the
rm
orunlink
command with the file name that you want remove:
$ unlink filename
$ rm filename
If your file is write-protected then you will be prompted for confirmation to delete file, as shown below output. To remove the file simply type y
and hit Enter
Key. And if the file is not write-protected then it will be deleted without prompting for confirmation.
Output
rm: remove write-protected regular empty file 'filename'?
- To delete multiple files, use the
rm
command as shown below.
$ rm filename1 filename2 filename3
You can also use a wildcard(*) and Regular Expressions to select multiple files. for example, if you want to remove all .txt files in the your current directory then use the below command:
$ rm *.txt
- Use
rm
command with the-i
option to confirm before deleting each file:
$ rm -i filename(s)
- Use
rm
command with the-f
(force) option to remove files without prompting for confirmation even if the files are write-protected :
$ rm -f filename(s)
Remove Directories (Folders) in Linux terminal
Using rmdir and rm commands, you can remove Directories (Folders) in Linux.
rmdir
is a command for deleting empty directories and rm
command can remove both empty and non-empty Directories (Folders).
- To remove an empty directory, use the
rmdir
orrm -d
commands with the directory name:
$ rm -d dirname
$ rmdir dirname
- To remove a non-empty directory and all its content, use the
rm
command with the-r
(recursive) option:
$ rm -r dirname
If the directory or a file within the directory is write-protected then you will be prompted to confirmation to delete the directory.
- Use
rm
with the-r
(recursive) and-f
(force) options to remove a non-empty directory and its all the files without prompting for confirmation to delete even if the files are write-protected:
$ rm -rf dirname
- To remove multiple directories at once:
$ rm -r dirname1 dirname2 dirname3
You can also use a wildcard(*) and Regular Expressions to select multiple directories to remove|delete.
That’s all
If you find any error and issue in the above steps , please use below comment box to report.