How to Remove All Files and Directories Using Linux Terminal

In this remove or delete directories and files Linux tutorial guide, you will learn how to remove empty directories and non empty directories linux using the command line. And as well as how to remove/file files linux using the command line.

How to Remove All Files and Directories Using Linux Terminal

use the rmunlink, and rmdir commands to remove or delete all files and directories in Linux and without confirmation.

  • To Remove All Files From the Directory in Linux
    • remove a single file in linux
    • remove multiple files at once in linux
  • How to Delete Directory in Linux
    • Remove empty directory linux
    • Remove non-empty directory Linux
    • Remove multiple directories linux

To Remove All Files From the Directory in Linux

If you want to remove (or delete) a file in Linux from the command line. So, you can use the rm (remove) or unlink command.

Note that, The unlink command allows you to delete only a single file, while with rm you can delete multiple files at once.

Remove a single file in linux

You can use the rm and unlink command to delete single file in linux. See the uses of rm and unlink command given below:

unlink filename
rm filename

If the file is write-protected, you will be prompted for confirmation on your terminal or command prompt, as shown below.

rm: remove write-protected regular empty file 'filename'?

Note that, Otherwise, if the file is not write-protected, it will be deleted without prompting.

Remove multiple files at once in linux

You can use the rm command to remove multiple file in linux. See the uses of rm command with multiple options, as shown below:

  • To delete multiple files at once using rm command:
rm filename1 filename2 filename3
  • Wildcard (*) and regular expansions to delete files

If you want to remove all .txt files in the current directory, use the following command:

rm *.pdf
  • Delete file with confirmation

You can use the rm with the -i option to confirm each file before deleting it:

rm -i filename(s)
  • Delete files in linux without confirmation

You can use the rm with the -f option to without prompting even:

rm -f filename(s)

How to Delete Directory in Linux

If you want to remove (or delete) a direcotories in Linux from the command line. So, you can use the rmdir and rm command.

Remove empty directory linux

If you want to delete an empty directory in linux. So, you can use rmdir or rm -d command, as shown below:

rm -d dirname

rmdir dirname

Remove non empty directory linux

If you want to remove non empty directory in linux. So, you can use with the-r (recursive) option, as shown below:

rm -r dirname
  • Delete non empty directory linux without prompt

To delete non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:

rm -rf dirname
  • Delete non empty directory linux without prompt

To delete non-empty directories and all the files without being prompted, use rm with the -r (recursive) and -f options:

rm -rf dirname

Remove multiple directories linux

If you want to delete multiple directories at once in linux. So, you can use the rm -r command, as shown below:

rm -r dirname1 dirname2 dirname3

Conclusion

In this tutorial guide, you have learned how to use the Linux rmrmdir and unlink commands to remove or delete files and directories in linux. And as well as how to remove or delete empty and non empty directories in linux.

Recommended Posts

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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