Files are created and saved in the directories of Linux, they can be text files, image files, music files, or any other format. These files occupy some space and many files present in our system are purposeless, they should be deleted in order to make space for new files.

In Linux, we can either delete/remove a single file or delete all the files from a directory using a single command in a terminal. In this write-up, we will discuss the ways to remove all the files in a directory of Linux.

How to remove all files in a directory of Linux

We have directories in a path /home/hammad/ with a name, new_directory, new_directory1, new_directory2, and new_directory3 that contain files, to view it, and list down the components of the path:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-all-files-of-a-directory-in-Linux-1.png" data-lazy- height="118" src="data:image/svg xml,” width=”1067″>

To view the files of “new_directory”, use the command:

$ ls /home/hammad/new_directory

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-all-files-of-a-directory-in-Linux-2.png" data-lazy- height="97" src="data:image/svg xml,” width=”1087″>

To remove these files from the directory “new_directory”, we can simply use the command of “rm”, the general syntax of using the rm command is:

$ rm [option] [path]/[filename]

The explanation to the syntax is simple:

  • Use the command of rm to remove the files from the directory
  • Use any options like to display the progress, to remove it forcibly
  • Type the pathname where the directory is located
  • Mention the filenames which you want to remove or simply use “*” to remove all the files from the directory

Suppose we want to remove all the files from the “/home/hammad/new_directory/*” path, we will use the command:

$ rm /home/hammad/new_directory/*

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-all-files-of-a-directory-in-Linux-3.png" data-lazy- height="69" src="data:image/svg xml,” width=”918″>

We can also display the progress of the removed files by using the flag “-v”, we will execute the command:

$ rm -v /home/hammad/new_directory1/*

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-all-files-of-a-directory-in-Linux-4.png" data-lazy- height="120" src="data:image/svg xml,” width=”1076″>

In the above command, the “-v” flag is used to display the progress of the command where “*” is used to delete all the files of the directory and for verification that all the files are deleted, we will again list down the content of the directory using the command:

$ ls -l /home/hammad/new_directory

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-all-files-of-a-directory-in-Linux-5.png" data-lazy- height="102" src="data:image/svg xml,” width=”1103″>

The above command verified that the directory contains no files and there are some other useful options as well; the “-f” which is used to delete the files forcibly and “-r” deletes the directory and its files, we will use all these flags altogether to delete the files of “new_directory2”

$ rm -vfr /home/hammad/new_directory2/*

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-all-files-of-a-directory-in-Linux-6.png" data-lazy- height="218" src="data:image/svg xml,” width=”1008″>

In the above output, we can see that the “-r” flag removes the sub directory, “officedirectory”, from the new_directory2, to list the files of new_directory2, we use the ls command:

$ ls -l /home/hammad/new_directory2

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-all-files-of-a-directory-in-Linux-7.png" data-lazy- height="94" src="data:image/svg xml,” width=”747″>

We have another option that is “-i”, if we use this option, it will ask for permission before deleting each file should we continue to delete it or not, if we still want to delete it, type “y” or “n” to cancel it. We will use “-i”, to delete files of new_directory3:

$ rm -i /home/hammad/new_directory3/*

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-all-files-of-a-directory-in-Linux-8.png" data-lazy- height="94" src="data:image/svg xml,” width=”902″>

In the above command, it can be seen that we have confirmed to delete “myfile” and canceled to delete “myscript.sh”, to view the files in the folder:

$ ls /home/hammad/new_directory3

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-all-files-of-a-directory-in-Linux-9.png" data-lazy- height="96" src="data:image/svg xml,” width=”767″>

We can see the “myscript.sh” file has not been removed from the directory.

To delete all the empty directories we use the “rmdir” command:

$ rmdir new_directory new_directory1 new_directory2 new_directory3

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-all-files-of-a-directory-in-Linux-10.png" data-lazy- height="72" src="data:image/svg xml,” width=”1168″>

All the empty directories are removed, use ls command to verify:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/How-to-remove-all-files-of-a-directory-in-Linux-11.png" data-lazy- height="95" src="data:image/svg xml,” width=”1054″>

Conclusion

Removing the files from the directory will create free space for the new files and applications. Therefore, files of no use should be removed from the system. To remove all the files from the directory the convenient way is to use the rm command which is discussed in this write-up. Different options can also be used along with the rm command which is also discussed with the help of examples.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/12/echo/hammad–150×150.jpg61c81f1b6969e.jpg" height="112" src="data:image/svg xml,” width=”112″>

Hammad Zahid

I’m an Engineering graduate and my passion for IT has brought me to Linux. Now here I’m learning and sharing my knowledge with the world.