Lets say I've a directory /etc/php5/conf.d/
, with the following hypothetical files in it:
mysql.ini
mysqli.ini
20-mysql.ini
20-mysqli.ini
20-pdo_mysql.ini
I would like to delete all these files except the last one (pdo
), this is what I have at the moment:
for phpIni in mysql mysqli; do
if [[ -f /etc/php5/conf.d/$phpIni.ini ]]; then
rm /etc/php5/conf.d/$phpIni.ini
if [[ -f /etc/php5/conf.d/20-$phpIni.ini ]]; then
rm /etc/php5/conf.d/20-$phpIni.ini
fi
done
It works, but I can't help noticing that the above could be simplified with glob patterns, such as:
if [[ ! -z /etc/php5/conf.d/{,20-}mysql*ini ]]; then
rm /etc/php5/conf.d/{,20-}mysql*ini
fi
There's a problem though, if the any of the expansions doesn't exists, rm
will complain about it:
$ if [[ ! -z /etc/php5/conf.d/{,20-}mysql*ini ]]; then rm /etc/php5/conf.d/{,20-}mysql*ini; fi
rm: cannot remove `/etc/php5/conf.d/20-mysql*ini': No such file or directory
How can I make rm only remove existing files? Or at least prevent it from throwing all these errors?
You can use rm -f
, it will not complain if files don't exist: as seen in the man page,
"ignore nonexistent files, never prompt".