You can remove all files in a directory using unlink command. Another option is to use the rm command to delete all files in a directory.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Linux Systems queries.
In this context, we shall look into how remove files and directories using the rm command in Linux system.
Syntax of rm Command
Below is the basic syntax of rm command:
$ rm [OPTIONS] [FILE_NAME]
- OPTIONS: You can specify the options to use with rm command.
- FILE_NAMES: File name(s) which need to be delete.
Different commands to Remove Files from Linux System ?
Following are the multiple examples with different options to delete files using rm command:
1. How to Remove a File ?
To remove or delete a file in Linux you can use rm command without any options.
$ rm test.jpg
This command will remove test.jpg file from the system.
You should make sure before deleting any files because once a file has been deleted will not be restore.
If the file is write protected then you will be prompted for confirmation as given below. You should press Y and hit Enter key to continue to delete, Otherwise if file is not write protected it will be deleted without any prompt.
$ rm: remove write-protected regular empty file 'test.jpg'?
2. How to Delete Multiple Files from Linux system ?
Also you can delete multiple files using rm command without passing any options. You just need to separate file names by space.
$ rm test1.jpg test2.jpg test3.jpg
Once you run above command it will delete all test1.jpg,test2.jpg and test3.jpg from system.
3. How to get Prompt Before Deleting File on Linux ?
Specify -i option with rm command to show prompt before deleting a file in Linux system.
$ rm test1.jpg test2.jpg test3.jpg
When you run above command it will prompt you before deleting each file. If you really want to delete then press Y or N.
4. How to Delete Write Protected File without Prompt ?
If you don't want to see any prompt even if file is write protected or not you can use -f option with rm command.
$ rm -f data.pdf
Here, data.pdf file will be deleted without any confirmation even if that file is write protected.
5. How to Remove Multiple File with Regular Expression ?
When you have requirement to delete multiple files with specific type then you should use regular expression. With rm command you can use regular expression to delete files as give below:
$ rm *.jpg
This command will file all the files with .jpg extension and delete from current directory. Like this, you can specify any regular expression as per your requirements.
Different commands to Remove Directories from Linux System ?
1. How to Remove a Directory or Folder
When the directory is empty then you can remove using -d option with rm command. Run the below command to delete empty directory:
$ rm -d dirname
To delete non-empty directory run below command:
$ rm -r dirname
In the same way, if the file is write protected then it will prompt you for confirmation.
2. Remove Multiple Directories or Folders
You can delete multiple directories simultaneously using -r option with rm command and separate file with space one by one.
$ rm -r dirname1 dirname2 dirname3
Above command will delete all directories dirname1, dirname2 and dirname3.
3. Delete Write Protected Directory without Prompt
When you do not want any type of confirmation at the time of directories deletion you should use -rf along with rm command.
$ rm -rf dirname
This command will delete directory without any confirmation even if file is write protected or not.
[Need assistance in deleting files and directories from Linux System? We can help you. ]