How to Remove Directories and Files in Linux using Command Line

How to Remove Directories and Files in Linux using Command Line

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

If you work with Linux then you will need the following:

  • how to remove empty directory in linux,
  • how to remove non empty directory,
  • how to remove directory without confirmation linux
  • how to remove files with and without confirmation in linux.

So, this tutorial guide will show you you how to use the rmunlink, and rmdir commands to remove or delete files and directories in Linux with and without confirmation.

How to Remove Files and Directories Using Linux Command Line

In this tutorial, you will learn the following:

  1. How to Remove Files Linux
  2. How to Delete Directory in Linux
    1. Remove empty directory linux
    2. Remove non empty directory linux
    3. Remove multiple directories linux

1 – How to Remove Files 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.

1 :- How to 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.

2 :- How to 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)

2 – 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.

1 :- 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

2 :- 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

3 :- 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, Vue JS, Angular JS, React Js, WordPress, 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 *