LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (http://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   how to "defrag" with slow working Ubuntu Linux? (http://www.linuxquestions.org/questions/linux-desktop-74/how-to-defrag-with-slow-working-ubuntu-linux-4175439627/)

Gloria 12-01-2012 04:02 PM

how to "defrag" with slow working Ubuntu Linux?
 
I've never learned how to "defrag" my computer since I installed Ubuntu Linux. Apparently Ubuntu uses different terminology. Any suggestions?

John VV 12-01-2012 04:35 PM

that is a Microsoft concept and problem
the file systems used in linux OS's do not need to be "defraged"

frankbell 12-01-2012 08:52 PM

To expand on what John_VV said, most Linux file systems, such as ext3 and ext4, handle defragging automatically without user intervention and keep file fragmentation under control.

This article gives a pretty good explanation of how this works:

http://www.howtogeek.com/115229/htg-...defragmenting/

k3lt01 12-01-2012 09:19 PM

This question was asked recently by another member. Here is the particular thread.

H_TeXMeX_H 12-02-2012 02:53 AM

Quote:

Originally Posted by John VV (Post 4840922)
that is a Microsoft concept and problem
the file systems used in linux OS's do not need to be "defraged"

You know I used to think that, but I have found it not to be true.

Try using 'filefrag' to determine the number of extents of files on the filesystem. The fragmentation can get into the thousands of extents and can really slow down performance. There exist a number of scripts and programs to defragment files on Linux (I'm not sure which ones are maintained) and I have also written my own script:

defrag:
Code:

#!/bin/sh
# finds fragmented files

error() # error
{
        echo "ERROR: $1"
        exit 1
}

# make sure we are root
if test ~ != /root
then
        error 'you must be root in order to run this script'
fi

# make sure we have less than 1 argument
if test $# -gt 2
then
  echo "Usage: $(basename $0) option"
  echo 'option can be:'
  echo 'find'
  echo 'defrag [threshold]'
  # fail
  exit 1
fi

# places
dir=/root/.defrag
output="$dir"/output
sorted="$dir"/sorted
defrag="$dir"/defrag
tempdir=/tmp/defrag

# vars
thres=5

if test "$1" = "find"
then
        # generate cleanly
        if test ! -d "$dir"
        then
                rm -f "$dir"
                mkdir "$dir"
        fi
        > "$output"

        # find files in /
        find /bin /etc /home /lib /lib64 /opt /root /sbin /usr /var -type f -print0 | xargs -0 filefrag > "$output"

        # sort
        awk '{printf("%s|%s\n",$(NF - 2),substr($0,1,match($0,/: [0-9]* extents? found/)-1))}' "$output" | sort -n > "$sorted"

        # info
        echo "Ouput written to $output"
        echo "Sorted output written to $sorted"
        echo
        echo "Most fragmented files"
        tail "$sorted"
        echo
elif test "$1" = "defrag"
then
        # $2 must be defined and a number
        if test "$2" != ''
        then
                if echo "$2" | grep '^[0-9]*$'
                then
                        thres="$2"
                fi
        fi
        # generate defrag list
        awk -F'|' -v thres="$thres" '{ if ($1>thres) print $2}' "$sorted" > "$defrag"
       
        # defrag
        echo
        echo "Files to be defragmented:"
        cat "$defrag"
        echo
        echo 'How do you want to defrag these files ?'
        echo '[i]n place (copy to .defrag and move back)'
        echo '[t]emporary directory (default is /tmp/defrag)'
        echo '[n]o defrag, just exit'
        read answer
        case "$answer" in
                i|I)
                        while read line
                        do
                                cp --preserve=all "$line" "$line.defrag" && mv "$line.defrag" "$line"
                        done < "$defrag"
                ;;
                t|T)
                        mkdir "$tempdir" || error "$tempdir exists"
                        while read line
                        do
                                cp --preserve=all "$line" "$tempdir" && mv "$tempdir/$(basename "$line")" "$line"
                        done < "$defrag"
                        rmdir "$tempdir" || error "$tempdir is not empty, check it for files"
                ;;
                n|N)
                        error 'exiting on user input'
                ;;
                *)
                        error 'bad input'
                ;;
        esac
else
        error 'input is not sane'
fi

exit 0

Use at your own risk ! It is provided as-is, no warranties implied.

Typical usage, as root:
Code:

defrag find
defrag defrag


DavidMcCann 12-02-2012 11:48 AM

If Ubuntu seems slow, the problem may be that it's not the best choice for your computer. What's your CPU speed, RAM size, and video chip? What's the CPU load shown in the system monitor when you're watching a Youtube video?

I'm skeptical about defragging. This computer's /home partition has just 13% non-contiguous files after 8 years' heavy use.


All times are GMT -5. The time now is 10:58 PM.