How to take away all recordsdata of a listing in Linux

2

Files are created and stored within the directories of Linux, they are able to be textual content recordsdata, symbol recordsdata, track recordsdata, or some other structure. These recordsdata occupy some house and plenty of recordsdata found in our gadget are purposeless, they must be deleted so as to make house for brand new recordsdata.

In Linux, we will be able to both delete/take away a unmarried document or delete the entire recordsdata from a listing the use of a unmarried command in a terminal. In this write-up, we can speak about the techniques to take away the entire recordsdata in a listing of Linux.

How to take away all recordsdata in a listing of Linux

We have directories in a trail /house/hammad/ with a reputation, new_directory, new_directory1, new_directory2, and new_directory3 that include recordsdata, to view it, and listing down the elements of the trail:

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

$ ls /house/hammad/new_directory

how to take away all recordsdata of a listing in linux How to take away all recordsdata of a listing in Linux 1640299700 387 How to remove all files of a directory in Linux

To take away those recordsdata from the listing “new_directory”, we will be able to merely use the command of “rm”, the overall syntax of the use of the rm command is:

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

The clarification to the syntax is understated:

  • Use the command of rm to take away the recordsdata from the listing
  • Use any choices love to show the development, to take away it forcibly
  • Type the pathname the place the listing is situated
  • Mention the filenames which you need to take away or just use “*” to take away the entire recordsdata from the listing

Suppose we wish to take away the entire recordsdata from the “/home/hammad/new_directory/*” trail, we can use the command:

$ rm /house/hammad/new_directory/*

how to take away all recordsdata of a listing in linux How to take away all recordsdata of a listing in Linux 1640299700 815 How to remove all files of a directory in Linux

We too can show the development of the got rid of recordsdata by means of the use of the flag “-v”, we can execute the command:

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

how to take away all recordsdata of a listing in linux How to take away all recordsdata of a listing in Linux 1640299700 314 How to remove all files of a directory in Linux

In the above command, the “-v” flag is used to show the development of the command the place “*” is used to delete the entire recordsdata of the listing and for verification that the entire recordsdata are deleted, we can once more listing down the content material of the listing the use of the command:

$ ls -l /house/hammad/new_directory

how to take away all recordsdata of a listing in linux How to take away all recordsdata of a listing in Linux 1640299700 5 How to remove all files of a directory in Linux

The above command verified that the listing incorporates no recordsdata and there are every other helpful choices as smartly; the “-f” which is used to delete the recordsdata forcibly and “-r” deletes the listing and its recordsdata, we can use some of these flags altogether to delete the recordsdata of “new_directory2”

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

how to take away all recordsdata of a listing in linux How to take away all recordsdata of a listing in Linux 1640299700 112 How to remove all files of a directory in Linux

In the above output, we will be able to see that the “-r” flag gets rid of the sub listing, “officedirectory”, from the new_directory2, to listing the recordsdata of new_directory2, we use the ls command:

$ ls -l /house/hammad/new_directory2

how to take away all recordsdata of a listing in linux How to take away all recordsdata of a listing in Linux 1640299700 639 How to remove all files of a directory in Linux

We have an alternative choice this is “-i”, if we use this feature, it’ll ask for permission ahead of deleting every document must we proceed to delete it or no longer, if we nonetheless wish to delete it, sort “y” or “n” to cancel it. We will use “-i”, to delete recordsdata of new_directory3:

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

how to take away all recordsdata of a listing in linux How to take away all recordsdata of a listing in Linux 1640299700 958 How to remove all files of a directory in Linux

In the above command, it may be observed that we have got showed to delete “myfile” and canceled to delete “myscript.sh”, to view the recordsdata within the folder:

$ ls /house/hammad/new_directory3

how to take away all recordsdata of a listing in linux How to take away all recordsdata of a listing in Linux 1640299700 199 How to remove all files of a directory in Linux

We can see the “myscript.sh” document has no longer been got rid of from the listing.

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

$ rmdir new_directory new_directory1 new_directory2 new_directory3

how to take away all recordsdata of a listing in linux How to take away all recordsdata of a listing in Linux 1640299700 305 How to remove all files of a directory in Linux

All the empty directories are got rid of, use ls command to ensure:

how to take away all recordsdata of a listing in linux How to take away all recordsdata of a listing in Linux 1640299700 335 How to remove all files of a directory in Linux

Conclusion

Removing the recordsdata from the listing will create loose house for the brand new recordsdata and packages. Therefore, recordsdata of no need must be got rid of from the gadget. To take away the entire recordsdata from the listing the handy means is to make use of the rm command which is mentioned on this write-up. Different choices can be used together with the rm command which could also be mentioned with the assistance of examples.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More