Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Having just written an answer about moving /usr to a new partition I was wondering about deleting files once a new partition has been mounted. To use the example from the question, is it possible to mount a new partition on /usr and then delete all the files under /usr on the root partition to free up space on the root partition.

share|improve this question

1 Answer 1

up vote 14 down vote accepted

Not directly, but there is a way around that: mount --bind is your friend:

# Existing directory with a couple files in it
root@nkubuntu1004:~/test# ls testdir
bar  foo

# Mount a filesystem over existing directory
root@nkubuntu1004:~/test# mount -o loop testfs testdir
root@nkubuntu1004:~/test# ls testdir
lost+found

# Bind mount root filesystem to another directory
root@nkubuntu1004:~/test# mount --bind / bindmnt

# Can now get to contents of original directory through the bind mount
root@nkubuntu1004:~/test# ls bindmnt/root/test/testdir/
bar  foo

# Remove a file
root@nkubuntu1004:~/test# rm bindmnt/root/test/testdir/bar
root@nkubuntu1004:~/test# ls bindmnt/root/test/testdir/
foo
root@nkubuntu1004:~/test# ls testdir
lost+found

# Unmount filesystem
root@nkubuntu1004:~/test# umount testdir

# Observe the change having taken effect
root@nkubuntu1004:~/test# ls testdir
foo
root@nkubuntu1004:~/test#

See also man mount -- search for "bind mounts".

share|improve this answer
    
Excellent answer - I'll just add a link to an online version of the mount man page. –  Hamish Downer Aug 3 '10 at 14:28

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.