Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a NAS appliance that crashed last weekend. The IT guys apparently / maybe didn't recover it in the right way? There's about 50,000 directories in a "lost+found" directory each with a numeric name like "#4289756". It looks as if all the missing files are within these directories, each with the original date and owner information (Linux). I need to organize them so they are findable to the staff.

My goal is to copy / move these files into a structure so that each employee has their own folder with their own files, and each folder is further divided by month.

The appliance itself is running a Busybox, a stripped down Linux version that only supports a subset of the typical Bash commands. I thought I could use a script with the "find" command to sort them by owner and date and then pipe that to "-exec cp" to the appropriate place, but it looks like Busybox doesn't have that ability.

Here's a suggestion someone had, but it assumes the original directories have the correct UIDs, which is not the case.

cd /lost+found ; for i in *; do USER=`ls -l $i | awk '{print $3}'`; mkdir -p /home/$USER/recovered ; cp -a $i /home/$USER/recovered/$i ; done
share|improve this question

migrated from serverfault.com May 25 '12 at 2:14

This question came from our site for professional system and network administrators.

1  
Working with BusyBox systems is quite specific sometimes, I'm going to move this over to our Unix and Linux specialist site as I think it will get better eyeballs over there. –  Mark Henderson May 25 '12 at 2:14

1 Answer 1

Each appliance has its own compilation of BusyBox with different sets of features, so I can't be sure what yours supports. If I understand correctly, you have directories like /lost+found/#123456, and every file in that directory belongs to the same user. Here's an untested script that dispatches these directories into the user's home. I assume that your user names do not contain spaces, and that users' home directories obey the /home/$username convention.

cd /lost+found
[ -d /root/recovered ] || mkdir /root/recovered
for d in *; do
  username=$(ls -l "$d" | sed -e 's/^[^ ]  *[0-9][0-9]*  */' -e 's/ .*//' -e q)
  if [ -d "/home/$username" ]; then
    dest=/home/$username/recovered
  else
    homedir=/root/recovered/$username
  fi
  [ -d "$dest" ] || mkdir "$dest"
  cp -Rp "$x" "$dest"
done

This is almost what you posted, except that your awk command is printing the files' owner multiple times. You can use awk '1 {print $3}' or sed as above. If you're picking up the wrong ownership, either you wrote ls -ld in the command you actually ran, or your description of the situtation (“all the missing files are within these directories, each with the original date and owner information”) is wrong. I also made the script more robust, in particular files owned by system users will be copied under /root/recovered.

If this doesn't work for you, be sure to clearly explain what's wrong. Copy-paste any error message or ls output.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.