Linux - DesktopThis forum is for the discussion of all Linux Software used in a desktop context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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
Last edited by H_TeXMeX_H; 12-02-2012 at 05:40 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.
Last edited by DavidMcCann; 12-03-2012 at 11:00 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.