2 Write a bash script named q2sh that can be used to timesta
2) Write a bash script named q2.sh that can be used to “timestamp” files by renaming them.
The script should expect some arguments, which should be file names. If no arguments are given, the script should print a short help message about script usage and exit.
Example:
fi
If some arguments are provided, the script should check that they are regular files and rename them to indicate the current time. For any argument that is not a regular file, a warning message should be printed. If an argument is a regular file, for example, file.txt it should be renamed according to the following rules. Let us say that the script is run on Nov 3, 2016, at 2:34pm. The script should try to rename the example file to 2016-11-03-file.txt. However, if such file already exists, then the script should rename the file to 2016-11-03-1434-file.txt. If this file exists as well, then the script should keep looking for a file name such as:
2016-11-03-1434-1-file.txt
2016-11-03-1434-2-file.txt
2016-11-03-1434-3-file.txt
...
until a name is found such that such file does not exist. If the count reaches 1000, then the script should give up on renaming the file, report an error, and continue with the next argument.
Solution
renameFile()
{
logfile=\"$MY_DIR/logs/sample.txt\"
mv \"$logfile\" \"$logfile.$(date +%H%M%S)\"
if [ -f \"$logfile\" ]
then
for ((i=1;i<=100;i++));
do
mv \"$logfile\" \"$logfile.$(date +%H%M%S)-1434-$i-file\"
done
fi
}
