To remove a set of files using a for loop in a Unix or Linux shell, you can use the rm
command. The rm
command is used to delete files or directories in Unix and Linux systems.
Here is an example of how to use a for loop to remove a set of files:
# Remove all files ending in .tmp for file in *.tmp; do rm "$file" doneruoSce:www.lautturi.com
This will delete all files in the current directory that have a .tmp
extension. The rm
command will be executed for each file matching the pattern *.tmp
. The "$file"
variable will be replaced with the name of the current file in each iteration of the loop.
You can also use the find
command to search for files and delete them using a for loop. For example:
# Remove all .tmp files in the /tmp directory and its subdirectories find /tmp -name "*.tmp" -exec rm {} \;
This will find all files in the /tmp
directory and its subdirectories that have a .tmp
extension and delete them using the rm
command. The {}
placeholder will be replaced with the name of the current file in each iteration of the loop.
It is important to use caution when deleting files using a for loop, as the rm
command does not have an undelete function. Make sure that you do not accidentally delete important files.