UNIX / Linux: Delete All Files Using rm Command

The rm command is used to remove or delete files and directories in Unix and Linux operating systems. To delete all files in a directory using the rm command, you can use the following command:

rm /path/to/directory/*

This command deletes all files in the specified directory (but not subdirectories) without prompting for confirmation. If you want to delete files in subdirectories as well, you can use the -r option:

rm -r /path/to/directory/*

This command deletes all files in the specified directory and its subdirectories recursively without prompting for confirmation. Be careful when using the -r option, as it can delete a large number of files and directories.

If you want to delete files based on a specific pattern, you can use wildcards with the rm command. For example, to delete all files with a .txt extension in the current directory, you can use the following command:

rm *.txt

This command deletes all files with a .txt extension in the current directory without prompting for confirmation. Be careful when using wildcards, as they can match unintended files if not used correctly.

Note that the rm command is a powerful tool and can permanently delete files, so it is important to double-check which files you are deleting before executing the command.

Leave a Comment