Remove All Files in Directory in Linux Ubuntu

Remove All Files in Directory in Linux Ubuntu

In Linux Ubuntu, removing all files from a directory is a common task that can be accomplished using various command-line utilities. In this tutorial will show you some methods to safely remove all files from a directory in linux ubuntu using the terminal without prompt.

Linux Command to Remove/Delete All Files in a Directory

Using the following methods, you can easily remove all files from directory or folder in linux ubuntu:

  • Method 1: Using the rm Command
  • Method 2: Using find Command with rm

Method 1: Using the rm Command

The rm command is a standard Unix/Linux command used to remove files and directories. To remove all files within a directory, follow these steps:

Step 1: Open a Terminal: Press Ctrl + Alt + T to open a terminal window.

Step 2: Navigate to the Directory: Use the cd command to navigate to the directory from which you want to remove the files. For example:

cd /path/to/your/directory

Step 3: Remove Files: Run the following command to remove all files within the directory:

rm *

Let’s look of rm command with multiple options to remove all files in directory without prompt in linux ubunt, as shown below:

Example 1 – To delete multiple files at once using rm command:

rm filename1 filename2 filename3

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

Example 3 – Delete file with confirmation

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

rm -i filename(s)

Example 4 – Delete files in linux without confirmation

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

rm -f filename(s)

Method 2: Using find Command with rm

The find command is powerful for searching and processing files. This method provides more flexibility, especially when dealing with nested directories.

Step 1: Open a Terminal: Open a terminal window as explained earlier.

Step 2: Navigate to the Directory: Use the cd command to navigate to the directory you want to clean up.

Step 3: Remove Files: Execute the following command to remove all files within the directory and its subdirectories:

find . -type f -delete

This command uses find to locate all files (-type f) within the current directory (.) and its subdirectories, then the -delete flag is used to remove them.

Conclusion

That’s it; You have learned how to remove all files from a directory in Linux Ubuntu using rm command and the find command with rm are two effective options.

Recommended Tutorials

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 *