How to copy a file into all subdirectories

I was left with a conundrum, I had needed to add a default index.html file into each subdirectory on my web server to ensure that the appropriate response was given if someone browsed to any directory on the web server. The command I came up with to copy the file into all subdirectories was:

ls -d */ | xargs -n 1 cp -i index.html

Where index.html is the file to be copied. The ls -d */ command gets a list of directories in the current directory that can be piped into xargs to execute the copying command, but it wasn’t as robust as I was hoping. This command doesn’t work with directories that have spaces in the name. And it only copies the file into the immediate subdirectories. So I started playing around with the command and came up with the following modified command that will copy a file into all subdirectories recursively.

ls -R | grep ":" | sed "s/^.://" | sed "s/://" | xargs -n 1 cp -n index.html 

Again this command is copying the index.html file, ls -R gets a list of all files and directories recursively from the current folder. grep “:” locates all the directories since they each end with a colon “:”. sed “s/^.://” removes the reference to the current directory “.” in the returned directory list. sed “s/^.://” then cleans off the trailing colons “:” from each directory entry and then the cleaned directories are piped into the xargs command to copy the file into each one.

So give it a go, it could save you a bit of time and hassle now that you can copy a file into all subdirectories.

Reinstall Grub after using CloneZilla

I had made a partition backup of a machine using Clonezilla and wanted to restore it. The restore was successful but because I had only restored the partitions rather than the full disk Grub was not installed in the MBR. Without Grub in the MBR the system failed to boot.

I mounted the new filesystem to /mnt while still using the live Clonezilla disk that I had used for the backup. Then I chrooted using the following command

chroot /mnt

then while in the chroot I attempted to reinstall grub, and since this was a Cloudlinux/Centos install I performed

grub-install /dev/sda

But grub-install complained that it couldn’t find /dev/sda or that /dev/sda was not a valid block device. So then hunting around on the internet for a little bit I came across this article which showed basically how to ensure that your current live

  • /dev
  • /sys
  • /proc

filesystems are accessible inside of your current chroot.

So I ran the following commands outside of the chroot before entering it again.

mount --bind /proc /mnt/proc
mount --bind /dev /mnt/dev
mount --bind /sys /mnt/sys

Then I chrooted to /mnt again and ran my grub-install command and all was well. The machine booted perfectly after that.