Which of the following commands will delete all the files in a directory?

Despite people constantly downvoting me for this, those methods have a downside: it doesn't seem like they're extensible enough to allow -pruneing a subdirectory (without introducing more problems). By contrast with this method, you could insert

Learning how to use the Linux command line is essential for all Linux users and system administrators. Simple maintenance tasks like creating a file, navigating to a folder, or moving a file can all be done through it.

Since Linux is a popular operating system for virtual server hosting, understanding fundamental Linux commands is crucial for server management.

This article will show you how to remove files and directories using the Linux command line.

Which of the following commands will delete all the files in a directory?

 

How to Remove a Directory in Linux

To permanently remove a directory in Linux, use either the rmdir or rm command:

  • Use the rmdir or rm -dcommand to remove empty directories.
  • Use the rm -rcommand to remove non-empty directories.

Before you remove a directory, you need to know the name of it. To discover files and directories, use the ls command, and to know the current directory you are in, use the pwd command.

The options you use together with these commands are also important to determine how they work. Here’s a quick recap of rm command options:

Command and OptionDescriptionrm -dRemove an empty directory using the rm command.rm -rRemove a non-empty directory and its content.rm -fIgnore any prompt when deleting a write-protected file.rm -rfIgnore any prompt when deleting a write-protected non-empty folder.rm -iOutput a prompt before deleting every file.rm -IOutput a prompt only once before deleting more than three files.rm *Wildcard that represents multiple characters.rm ?Wildcard that represents a single character.rmdir -pRemove an empty subdirectory and its parent directory.rmdir -vPrint the information that the specified directory was deleted.

If you want to use these commands for VPS management, don’t forget to connect to the server using an SSH client beforehand. Then, execute the commands from there.

Your virtual hosting server, your rules. Get all the resources you need for your next project. Get VPS Hosting

How to Remove an Empty Directory (rmdir Command)

In the Linux environment, files and directories will be permanently deleted as there’s no recycle bin or trash folder where you can retrieve them. If you delete a file or directory in Linux by mistake, the only way to recover it is from a backup.

Using the rmdir command prevents such unwanted actions as it only works for empty directories. It will return the following error message if the directory contains files:

rmdir: failed to remove ‘Directory’: Directory not empty

The syntax for the rmdir command is as follows:

rmdir [option] DirectoryName

If you don’t want to use any command line option, remove it from the syntax. Remember that the command line is case-sensitive. Therefore, if the folder name contains uppercase letters, type it accordingly.

For example, here’s a command for deleting the Simple-Directory folder:

rmdir Simple-Directory

The rmdir command also works for multiple empty directories. Add the directories in the command as additional arguments to delete multiple directories in Linux.

rmdir Directory_1 Directory_2 Directory_3

Now let’s see options you can use with the rmdir command. The first one is -p, which works for deleting a subdirectory and its parent directory. For example, if your directory path is /Directory/SubDirectory, use the following command:

rmdir -p /Directory/SubDirectory

This command will delete the SubDirectory folder in the Directory path first. Then, if the Directory folder is empty after the SubDirectory was removed, the folder will also get deleted.

The next option is verbose, represented with -v in the command line. This option will print a text as a confirmation that the specified directory has been deleted. Here’s an example of this command:

rmdir -v Simple-Directory

The output message will be like this:

rmdir: removing directory, ‘Simple-Directory’

How to Remove a Non-Empty Directory (rm Command)

Now let’s see how to remove non-empty directories using the rm command. This command is originally used to remove files, but we can use it to remove directories using options like -r, -rf, and -d. The basic syntax for the rm command is as follows:

rm -d0

Use a recursive option, represented by -r, to remove the directory and its content. The following command shows how to delete Simple-Directory and its content:

rm -d1

Warning! If you delete a non-empty directory using the rm -r command, all the files inside it will be permanently deleted, and you can only recover them from a backup. Execute this command with caution.

If the directory is write-protected, you will be prompted to confirm before proceeding with the directory removal. To delete a directory without being prompted, use the -rf option.

rm -d2

The rm command also works for removing empty directories in Linux. However, unlike the rmdir command, you have to use the -d option.

rm -d3

Just as with the rmdir command, you can use the rm command to remove multiple directories. Add directory names as new arguments in the command line:

rm -d4

How to Delete a File With the rm Command

You may want to use the rm command to manually remove files instead of deleting a non-empty folder straight away. This method is safer as it prevents removing important files accidentally.

To delete a single file in the current working directory, use the rm command followed by the file name:

rm -d5

To delete multiple files in the directory, write all the file names you want to delete in the command:

rm -d6

All the commands above work if you are in the same directory as the specified files. However, you can put the file path into the command line argument to delete a file that’s not in your current working directory.

rm -d7

Since the files will be permanently removed, you can add the -i option in the command to make the terminal prompt you to confirm every file deletion. This way, you prevent unpleasant mistakes from happening.

rm -d8

Type Y and press Enter if you want to delete the file, or type N and press Enter if you don’t.

Use -I instead to get prompted only once before deleting more than three files. While this is less safe than the -i option, it still gives extra protection to avoid deleting important files.

rm -d9

However, the system will prompt you anyway before removing files if they’re write-protected. If you don’t want to get prompted when deleting such files, use the -f option.

rm -r0

The commands we’ve discussed so far will delete specific files. However, using wildcards, you can delete multiple files with a single command. There are two types of wildcards – the asterisk (*) and the question mark (?).

The asterisk represents multiple unknown characters, and one of the most common use examples is deleting files with a certain extension. The command below will delete all .txt files in the current working directory:

rm -r1

You can also use the asterisk to delete all files beginning with a specific letter.

rm -r2

In the example above, the asterisk represents all unknown characters that come after the letter a. The command will delete all files beginning with a regardless of their extensions, such as amazon.txt, alligator.png, and aaron.zip.

The question mark wildcard, on the other hand, represents a single character. In conjunction with the asterisk wildcard, you can use it to delete files with a single character extension, such as .S, .O, and .C.

rm -r3

Warning! Be careful when using wildcards, as you may end up deleting important files. First, check all the files in the directory using the ls command. Make sure that there are no important data or system files before using the wildcard command.

Conclusion

Deleting directories and files through the command line in Linux requires understanding the correct use of the rm and rmdir commands. To put it simply, the rm command works for files and non-empty directories, while the rmdir only works for empty folders.

Remember that there is no recycle bin or trash folder in Linux. Once you remove files and directories using the command line, they will be permanently deleted. Therefore, use these commands with caution or create a backup before deleting the files and folders on your VPS.

If you have additional questions, feel free to leave us a comment.

Learn More Linux Commands for File Management

How to Create an Empty File
How to Locate a File
How to Compress a File with Tar Command
How to Change File Ownership with Chown Command
How to Unzip Files in Linux
How to Change FIle Permissions with Chmod Command
How to Rename a File
How to Check File Type
How to Create a Symbolic Link (Symlink)

Linux Remove Directory FAQ

How to Delete All Files and Folders in Linux?

For empty folders, use either the rmdir or rm -d commands followed by the name of the folder:
rm -d dirname
rmdir dirname

The rm command with -r dir can be used for folders that are not empty.
rm -r dirname

Which of the following commands will delete all the files in a directory?

The author

Ignas T.

Ignas is a part of Hostinger's Technical team. He takes care of server administration and monitoring with a particular focus on software virtualization. In his free time, he's always up for a skiing or kayaking adventure or a board game night.

More from Ignas T.

The Co-author

Leonardus N.

Leo is a WordPress fanatic and contributor. He likes keeping up with the latest WordPress news and updates, and sharing his knowledge to help people build successful websites. When he's not working, he contributes to WordPress documentation team and pampers his dogs.

What is the command to delete all files in a directory?

To delete a folder and all the files in it, use these steps:.
Open the command prompt. Press the Windows key and type 'cmd' to open the command prompt. ... .
Change the path of the directory in cmd. ... .
Use the rmdir command..

Which of the following commands will delete a directory?

Use the rmdir command to remove the directory, specified by the Directory parameter, from the system. The directory must be empty (it can contain only .

Which of the following commands will delete all files in a directory except one?

To delete all files in a directory except filename, type the command below: $ rm -v !("filename") Delete All Files Except One File in Linux..
To delete all files with the exception of filename1 and filename2: $ rm -v !("filename1"|"filename2") Delete All Files Except Few Files in Linux..

Which of the following commands will delete all files in your current Windows directory?

In a current directory, all files can be deleted by using rm command. The rm command removes the entries for the specified File parameter from a directory.