I have a folder called statistics in an Ubuntu server in which data files are regularly stored. How can I rename statistics folder to backup-xx while re-creating statistics folder to be available for storing new files?The files in statistics folder is created by PHP file_put_contents.I prefer renaming the folder, as there are many files in the statistics folder.
How to move and recreate a folder at the same time?
bash;shell script;files;rename
mv statistics backup-xx && mkdir statisticsThis would rename the existing statistics directory to backup-xx, and if that succeeds it would carry on to create a new statistics directory.For a more atomic operation, consider creating a directory statistics-001 (or similar, maybe by replacing 001 with today's date in a suitable format), and a symbolic link to it called statistics:mkdir statistics-001ln -s statistics-001 statisticsWhen you want to rotate this so that new data goes into a clean directory, create the directory first, then recreate the statistics link to it:mkdir statistics-002ln -sf statistics-002 statisticsmv statistics-001 backup-001This way, any program writing to the statistics directory (i.e. the directory that this symbolic link points to) will never1 fail to find it.If you need special permissions or ownership set on the directory that statistics points to, set these before (re-)creating the link.1Or rather, this way, the time that a program would be without a valid target directory is minimized as much as practically possible using standard Unix tools.