1

Given a mountpoint such as /dev/sda1 how can I list the contents of this file system using shell script. My objective is to delete the oldest file in this drive.

4

/dev/sda1 is a block device. It may contain a file system. When mounted, that file system may be available at some mount point like /home, /, and within that file system, some directories may in turn be some mount points for other file systems (may they be in other block devices, or virtual ones like /proc, or network ones...).

If /dev/sda1 is mounted on /, to remove the oldest (in terms of last modification time) regular file, on a recent GNU system, you can do:

find / -xdev -type f -printf '%T@:%p\0' |
   sort -zn |
   sed -z 's/[^:]*://;q' |
   xargs -r0p rm -f

The -xdev flag tells find to stick to one file system, that is, not to descend into other file systems mounted within / in this case.

Note that other file systems may hide files on the file system of their mount point. For instance, if /dev/sda1 is mounted on / but contains a /home/some-old-file and /dev/sda2 is mounted on /home, /home/some-old-file will not be accessible.

On Linux at least, you can work around that by bind-mounting / in another directory:

mount --bind / /mnt/side-access-to-root

Then all the files in the file system mounted at / will be available through /mnt/side-access-to-root.

Then, you can ommit the -xdev and you could use zsh globbing to remove the oldest file:

rm -i /mnt/side-access-to-root/**/*(D.Om[1])
1

/dev/sda1 is not a mount point, it points to your partition on your drive.

To mount your disk use

mount -t vfat(OR THE TYPE OF YOUR DRIVE) /dev/sda1 /path/to/mount/to

Then, to list all files in a path, you can use

ls

However, to delete all files older then X days, you can use:

find /path/to/files* -mtime +x -exec rm {} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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