Linux Delete All Files in Current Directory

Written by: Bobbin Zachariah   |   Last updated: February 13, 2023

You may need to delete files from your directory to free up space, clean up a project, or remove malware files. In this tutorial, we will learn how to delete all files in the current directory in Linux.

Delete all files in the directory

To delete all files in the directory use rm command followed by the path to the directory and a wildcard character "*".

Syntax:

rm /path/to/dir/*

This command will delete all files in the directory, but it will not delete any subdirectories or their contents. to delete subdirectories as well, you can add the "-r" flag to the command to make it recursive.

With that note: In Linux, it is best practice to navigate to a desired directory and perform the action from the current directory. Using an absolute path is more prone to accidentally choosing the incorrect path and may end up deleting the wrong files.

Example:

To delete all files in the directory /home/ubuntu/mydata/, type:

rm /home/ubuntu/mydata/*
rm delete all files  in a directory

Here all the non-hidden files in the directory are deleted. However, it returns an error for sub-directories. On a side note, you can exclude a specific file from deletion by rm -v !(filename.txt).

To remove all files and sub-directories from the directory /home/ubuntu/mydata, type:

rm -r /home/ubuntu/mydata/*
rm delete all files and subdirectories in a directory

You can verify using ls -al /home/ubuntu/mydata to confirm all files and subdirectories in it are deleted.

Delete all files in the current directory

As mentioned before safest way would be to navigate to the directory and delete files from the current directory.

1. Navigate to the Directory

Use cd command to change the directory. For example, we are using /home/ubuntu/mydata.

Change to /home/ubuntu/mydata:

cd /home/ubuntu/mydata
change to directory where the content needs to be deleted

Use pwd command to confirm the current path:

pwd
confirm current path

This will print out the absolute path of the directory.

2. List directory contents

List the directory content to make sure you going to delete the right files. For listing all files in the current directory use ls -al command.

ls -al
list the directory contents

This command will list all the contents of the directory /home/ubuntu/mydata such as sub-directories and files ( including hidden files).

3. Delete all files in the Directory

Once we confirm we are in the right directory, we can delete all files in the current directory using rm -rv *.

Example:

rm -rv *
from current directory remove all files and sub directories

This command will delete all the files and subdirectories in the current directory. The -v option gives a verbose output to show removed files and the directory.

Note: If you have any write-protected files and directories rm will prompt.

You can verify by typing ls -al

ls -al

If you have any manually created hidden files, to remove them all use rm -rv .* command. This is because the asterisk (*) does not match files that begin with a dot, which are hidden files.

rm -rv .*
remove all files and subdirectories including manually created hidden files from a directory.

The individual directories . (indicates current directory) and .. (indicates parent directory ) will be still there - which cannot be deleted. No harm in keeping it.

Delete all files in directory without prompt

Use rm -rf * to delete all files in the directory without any prompt. Prompt occurs when rm encounters any errors such as write access protection. This option helps to avoid it.

For example, to delete all files in the directory /home/ubuntu/mydata without any prompt, type:

rm -rf *
delete all files in a directory without prompt

We verify that the files have been deleted by running:

ls -al

Conclusion

In this tutorial, we learned how to delete all files in a directory in Linux. Be cautious when using rm with asterisk command, as it can potentially delete important files. Always double-check the files you want to delete before executing the command.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin Zachariah is an experienced Linux engineer who has been supporting infrastructure for many companies. He specializes in Shell scripting, AWS Cloud, JavaScript, and Nodejs. He has qualified Master's degree in computer science. He holds Red Hat Certified Engineer (RHCE) certification.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment