\begin{exe} {Defragmentation}

   Newcomers to \textbf{Linux} are often surprised at the
   lack of mention of filesystem \textbf{defragmentation}
   tools, since such programs are routinely used in the
   \textbf{Windows} world.

   However, native filesystems in \textbf{UNIX}-type
   operating systems, including \textbf{Linux}, tend not
   to suffer serious problems with filesystem
   fragmentation.

   This is primarily because they do not try to cram
   files onto the innermost disk regions where access times
   are faster.  Instead, they spread free space out
   throughout the disk, so that when a file has to be
   created there is a much better chance that a region of
   free blocks big enough can be found to contain the
   entire file in either just one or a small number of
   pieces.

   For modern hardware, the concept of innermost disk
   regions is obscured by the hardware anyway.

   \begin{dontdothis}
      For \textbf{SSDs} defragmentation can actually shorten
      the lifespan of the storage media due to finite
      read/erase/write cycles.  On smart operating systems
      defragmentation is turned off by default on \textbf{SSD}
      drives.
   \end{dontdothis}

   Furthermore, the newer \textbf{journalling} filesystems
   (including \textbf{ext4}) work with \textbf{extents}
   (large contiguous regions) by design.

   However, there does exist a tool for de-fragmenting
   \textbf{ext4} filesystems:

   \begin{cmd}
$ sudo e4defrag
   \end{cmd}
   \begin{response}
Usage   : e4defrag [-v] file...| directory...| device...
        : e4defrag  -c  file...| directory...| device...
   \end{response}

   \textbf{e4defrag} is part of the \textbf{e2fsprogs}
   package and should be on all modern \textbf{Linux}
   distributions.

   The only two options are:
   \begin{itemize}
      \item \verb?-v?: Be verbose.
      \item \verb?-c?: Don't actually do anything, just analyze and report.
   \end{itemize}

   The argument can be:
   \begin{itemize}
      \item A file
      \item A directory
      \item An entire device
   \end{itemize}

   Examples:
   \begin{cmd}
$ sudo e4defrag -c /var/log
   \end{cmd}
   \begin{response}
<Fragmented files>                             now/best       size/ext
1. /var/log/lastlog                              5/1              9 KB
2. /var/log/sa/sa24                              3/1             80 KB
3. /var/log/rhsm/rhsm.log                        2/1            142 KB
4. /var/log/messages                             2/1           4590 KB
5. /var/log/Xorg.1.log.old                       1/1             36 KB

 Total/best extents                             120/112
 Average size per extent                        220 KB
 Fragmentation score                            1
 [0-30 no problem: 31-55 a little bit fragmented: 56- needs defrag]
 This directory (/var/log) does not need defragmentation.
 Done.
   \end{response}
   \begin{cmd}
$ sudo e4defrag  /var/log
   \end{cmd}
   \begin{response}
ext4 defragmentation for directory(/var/log)
[2/152]/var/log/Xorg.2.log:     100%    [ OK ]
[3/152]/var/log/Xorg.0.log.old: 100%    [ OK ]
[4/152]/var/log/messages-20141019.gz:   100%    [ OK ]
[5/152]/var/log/boot.log:       100%    [ OK ]
[7/152]/var/log/cups/page_log-20140924.gz:      100%    [ OK ]
[8/152]/var/log/cups/access_log-20141019.gz:    100%    [ OK ]
[9/152]/var/log/cups/access_log:        100%    [ OK ]
[10/152]/var/log/cups/error_log-20141018.gz:    100%    [ OK ]
[11/152]/var/log/cups/error_log-20141019.gz:    100%    [ OK ]
[12/152]/var/log/cups/access_log-20141018.gz:   100%    [ OK ]
[14/152]/var/log/cups/page_log-20141018.gz:     100%    [ OK ]
...
[152/152]/var/log/Xorg.1.log.old:       100%    [ OK ]

        Success:                        [ 112/152 ]
        Failure:                        [ 40/152 ]
   \end{response}
   Try running \textbf{e4defrag} on various files, directories,
   and entire devices, always trying with \verb?-c? first.

   You will generally find that \textbf{Linux} filesystems only
   tend to need defragmentation when they get very full,
   over 90 percent or so, or when they are small and have
   relatively large files, like when a \textbf{boot} partition
   is used.
\end{exe}


