Delete All Files in the Directory in Linux Ubuntu Command Line

To delete all files in a directory or folder and its subdirectories via Linux Ubuntu command line, you need to type command sudo rm -rf * on command line and hit enter. Additionally, You can also use the Find and Delete command for this.

Linux Ubuntu Command Line to Remove All Files in a Directory

Here are two simple solutions on how to remove all files in directory or folder on Linux Ubuntu using command line or terminal using rm and find command:

  • Solution 1: rm-f Command To Delete All Files in Directory Linux Ubuntu
  • Solution 2: find Command To Remove All Files in Directory Linux Ubuntu

Solution 1: rm-f Command To Delete All Files in Directory Linux Ubuntu

Let’s use the rm command with the -rf option to delete all the files from the folder in the Linux Ubuntu command line, see here:

Step 1: Open a Terminal

Press Ctrl + Alt + T to open a terminal window.

Step 2: Remove All Files in Directory Linux Ubuntu

Now you can type the command rm -rf* in the linux ubuntu terminal window or command line and hit enter, which will delete all the files in the folder:

rm -rf /path/to/dir/*

Let’s see some examples of deleting files in a folder using the rm command with its options:

Example 1 – Command to remove multiple files at once:

If you want to delete multiple files at once on Linux Ubuntu command line, you can use this:

rm filename1 filename2 filename3

Example 2 – Delete All Specific File Type from Folder Linux Ubuntu

If you want to delete a file of a specific type or extension such as .txt, .pdf, .png, .zip, etc. from a folder, you can use this command:

rm *.pdf

Example 3 – Remove file with confirmation Linux Ubuntu

If you want confirmation when deleting a file from the linux ubuntu command line, you have to use this command:

rm -i filename(s)

Example 4 – Deleting files without confirmation in linux

Linux Ubuntu command line, to delete a file without confirmation, you can use this command:

rm -f filename(s)

Example 5 – Delete all files in subdirectories linux

To delete all files from a subdirectory in a directory, you can use this command at the command line:

rm -rf /path/to/subdirectory/*

Example 6 – Remove all Files in a Directory and Subdirectories Linux

To delete all files from the directory and its subdirectories, you can use this command at the Linux Ubuntu command line:

rm -rf /path/to/directory/*

Solution 2: find Command To Remove All Files in Directory Linux Ubuntu

Let’s use the find command with the -delete option to delete all the files from the folder in the Linux Ubuntu command line, Here are two steps for that:

Step 1: Open a Terminal

Open a terminal window as explained earlier.

Step 2: Delete All Files in Directory Linux Ubuntu

To remove all files in the directory and its subdirectories, you can use this:

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.

Here is the video guide on Linux Ubuntu Delete All Files In Directory Using Command Line:

Conclusion

That’s it; You have learned how to remove all files in a directory on Linux Ubuntu command line using rm command and the find command..

Recommended Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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