I'm currently trying to get an image working for a network boot using somebody else's file contents. The previous images I made keeping all original files present were way too big, so I need to shrink down the size of the necessary directories. Looking through the contents of these directories though, like /bin, /dev, /etc, etc., I really can't tell what's crucial to the system versus what is extra stuff specific to certain programs that were installed (which are not needed on the image). I don't want to delete anything important, though.

The biggest directories by far are /etc, /lib, and /usr, all hundreds of MB larger than the equivalent directories of an image I previously got to work in the past. Because of this, I know that there's a lot of extra stuff in these directories. At the same time, I'm using a different operating system version (SL5 instead of SL4) so I'm not sure about comparing and contrasting those, especially since the filesystems had different things installed on them anyway.

Is there a quicker way to either sort what's needed versus what isn't, or to delete a lot of the extra "crap" files? (E.g. one recommendation was to delete everything labelled as documentation, but that's still not enough.)

  • For a long-term project to give you a depth of understanding on this subject, you might like to check out "Linux From Scratch." – Wildcard May 25 '16 at 19:02
  • Do you have an idea of what functionality you need? DHCP, ssh server, your locally-developed software, compression utilities, X Windows, etc.? – Mark Plotnick May 26 '16 at 2:33
  • Your're going about this in precisely the wrong way. Don't start with a large image and remove files, instead start from a bare-minimum base install (of whatever OS you want to use) and then add only the things you need. – cas May 26 '16 at 4:35

You should not delete files directly from system directories. Instead you should remove unneeded packages. In this manner, the system will remove unnecessary files and its dependences.

Note: you can remove every file (but not directories) in /var/cache. Additionally old logs in /var/log/ could be removed. Check about unread system mails (/var/mail/ or /var/spool).

| improve this answer | |
  • How would I remove the packages? I'm working with these files before they've been mounted as root and the system/any sort of relevant kernel has actually been booted. I.e. if I uninstall a package, it would be on the OS that's actually running. Would chroot work here? Also, I just checked /var/cache and the only contents in there are directories (coolkey, fontconfig, foomatic, man, mod_proxy, samba, yum--that's all of them). Can I remove their contents at all? (Also directories from what I can see.) – Ted Desmond May 25 '16 at 18:36
  • I take it "SL5" is Scientific Linux 5, in which case you'd be using rpm -e to remove packages. rpm supports a --root option to handle use-cases similar to yours; it handles everything properly (chroot where appropriate etc.). – Stephen Kitt May 25 '16 at 20:41

Keeping in mind that you should be removing packages (rather than individual files), I would first ensure that the filesystem supports the update of access-time when a program is executed, and then check which packages have not been used since the last boot-time. Assuming normal use of your system, that's as good a first guess at unused packages as you'll need.

I've done this before (a moderately complicated script). That can be done by

  • using find to look for files in your path (e.g., /bin, /usr/bin, etc), which have not been accessed "recently" (since the last reboot)
  • for each file in that list, keep track of the corresponding package and the most recently-used file belonging to the package (you can use rpm -ql packagename to get the list of files).
  • review the list of packages versus access dates and present a report of potentially unused packages (and their sizes).
| improve this answer | |
  • I don't have packagename installed and not sure if I can install it right now. Is there another way of checking for the second part? I'm also still unsure how I would uninstall packages in general since I haven't actually booted up this system yet... See the comment on the other answer. – Ted Desmond May 25 '16 at 20:35
  • I meant packagename for the given package you are examining, and pointed to rpm because it provides a way to list the files belonging to the package. – Thomas Dickey May 25 '16 at 21:27

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.