Sunday, September 04, 2011

Searching for disk space on Linux UNIX

One of the most confusing issues I had moving to Linux was searching for large files. It's somewhat confusing on Windows but there are many graphical applications available to help with the process. On command line, it's a slightly different process but still similar steps taken. Here's an example of searching for the largest files on a host. 


First, run the command "df -h" 


-bash-4.2$ df -h
Filesystem              Size    Used   Avail Capacity  Mounted on
/dev/sda1              3.9G    927M    2.7G    25%    /
/dev/sda2               10G     10G      0G    99%    /home
-bash-4.2$ 


Now you know the largest files are in the /home directory. Run the command "cd /home" In the example below, there's only one user, "rob" so I will go straight to the path.



-bash-4.2$ cd home/rob
-bash-4.2$ sudo du -sh *
9.9G    logs
 17M    dev
4.0K    documents
 12K    tools
-bash-4.2$

We find that in the path /home/rob/logs there is 9.9G of files. One method is to move the files to another directory or you can move the files, compress them then move them back. Just a note, certain files that are already compressed (mp3, jpg, zip) will not compress that well. Others such as text and log files compress extremely well. 


For the same path /home/rob/logs we find there are many log files taking up the space. 




-bash-4.2$ ls -lh
total 5
-rw-r--r--  1 robertf  users   2.5G Aug 31 23:00 log_083111
-rw-r--r--  1 robertf  users   2.5G Sep  1 23:00 log_090111
-rw-r--r--  1 robertf  users   2.5G Sep  2 23:00 log_090211
-rw-r--r--  1 robertf  users   2.5G Sep  3 23:00 log_090311
-rw-r--r--  1 robertf  users     5M Sep  4 07:46 log_090411
-bash-4.2$ 

We do not have space on /home so let's copy to /tmp to run the archive command


First create the folder on your /tmp directory by running the command "mkdir /tmp/rob"

-bash-4.2$ mkdir /tmp/rob

Then let's move the files over using the command "mv * /tmp/rob"

-bash-4.2$ mv * /tmp/rob

Just make sure the files have been copied

-bash-4.2$ ls /tmp/rob
log_083111      log_090111      log_090211      log_090311      log_090411


Now, we can start the compress of the files using the command "tar -zcvf log.tar /tmp/rob"


-bash-4.2$ tar -zcvf log.tar /tmp/rob
tar: Removing leading '/' from member names
a tmp/robertf
a tmp/robertf/logs
a tmp/robertf/logs/log_090411
a tmp/robertf/logs/log_090311
a tmp/robertf/logs/log_090211
a tmp/robertf/logs/log_090111
a tmp/robertf/logs/log_083111

Let's check again if the tar file has been created. 


-bash-4.2$ ls
log.tar

Now, the log.tar was created in the original location /home/rob/logs so we don't need to move that back from the /tmp directory. 


This is just a small example of how to find and clear space on your system. There are many many other examples. 


Nixcraft - How to find large files

Rob












No comments: