Shell Script to Remove Temporary Files

27 Apr, 2021
Temporary files in a linux environment are world-writable and world-readable which means that any user in the system can read and write to the temporary directory.
In most of the Linux systems “/tmp” directory is used as a temporary directory and any user or process in the system can use this directory to store any temporary data. But any program/user should not assume that data stored in “/tmp” directory to be persisted over time, it should only be used for a temporary purpose. This shell script demonstrates the number of temporary files in “/tmp” directory and clearing the “/tmp” directory
Approach:
We will count the number of temporary files present in the temporary directory i.e “/tmp” directory and display the count to the user before and after deleting the temporary files. We calculate the no. of files by ‘ls’ command. “ls” command gives a list of all the files present in the directory, and we use “wc” command to count how many lines are printed by “ls” command. ls /tmp | wc -l Finally, the removal of temporary files is completed by running “rm” command this command takes the argument “-rf” that tells “rm” command to remove all files recursively and forcefully. We check the return code of the remove command to check if the command is successfully executed. In bash return code of the previous command can be checked b “$?” variable, if the value of this variable is equal to zero then the previous command is executed successfully else the previous command failed with some other return code. The remove command may fail if there is some file that is being currently open or some process has acquired lock on that file at the time we are running the remove command.
We will count the number of temporary files present in the temporary directory i.e “/tmp” directory and display the count to the user before and after deleting the temporary files. 
We calculate the no. of files by ‘ls’ command. “ls” command gives a list of all the files present in the directory, and we use “wc” command to count how many lines are printed by “ls” command. ls /tmp | wc -l 
Finally, the removal of temporary files is completed by running “rm” command this command takes the argument “-rf” that tells “rm” command to remove all files recursively and forcefully. 
We check the return code of the remove command to check if the command is successfully executed. In bash return code of the previous command can be checked b “$?” variable, if the value of this variable is equal to zero then the previous command is executed successfully else the previous command failed with some other return code. 
The remove command may fail if there is some file that is being currently open or some process has acquired lock on that file at the time we are running the remove command.
Code:
#!/bin/bash

# Script name script.sh
# Script for removing all temporary files from temporary directory

TMP_DIR="/tmp"
echo "Removing all temporary files from $TMP_DIR"
  
# Counting the number of temporary files
files=`ls -l $TMP_DIR | wc -l` 

echo "There are total $files temporary files/directory in $TMP_DIR"

rm -rf $TMP_DIR/*

if [[ "$?" == "0" ]];then
    echo "All temporary files successfully deleted"
else
    echo "Failed to delete temporary files"
fi

# Counting the number of temporary files
files=`ls -l $TMP_DIR | wc -l` 

echo "There are total $files temporary files/directory in $TMP_DIR directory"
Output:
Executing the script
Before execution assigns the permission of these scripts:
chmod +x script.sh 
Picked
Shell Script
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.