Swap error or disk usage error
Published:
Swap error or disk usage error.
If you ever get an error that your disk usage is full and you are unable to create a new file, the first place to check is the diskusage using the command:
du -sh
This gives you the total disk usage for a directory. If you want to check individual folders and files within the directory, add the wildcard
du -sh *
The second place is to check your editor settings, chances are that you have created a swap file with same name and this needs to be deleted. But if your disk usage is not full and you are still unable to create a new file, and it still gives you swap error, chances are your inode files are full. This can happen when you have a program running that is creating a lot of temporary files which are 0 bytes (This happened to me, hence the blog).
Inode
Inode is a index node in Unix system that keeps track of files and directory in your system. Each file ever created in a unix based system has a unique inode number. This is how the file descriptors, file tables and inode numbers are laid out in unix.
Please click here to learn more about inode. I personally think the wiki page is a great resource for beginners.
Checking inode number
To check for inode number for a file, in the terminal type:
ls -i
This will list all the files with their respective inode numbers. This is how the output will look like, with the first column as inode number and the second column is either the file name or folder name within that directory. Notice how the folder 01-sequence and the file cleanUp.sh both have a unique inode number. All the files within the folder 01-sequence also have an inode number.
162135661699203487 01-sequence
162135760433126919 cleanUp.sh
162135729009392318 findGenes.sh
162135661699216256 make-treeUltrametric.R
162135725721059734 Gene10.fasta
162135661699215338 nexus2fasta_forSupermatrix.pl
The solution
The obvious solution is to delete those temporary files, but these can be a bit hard to find. The command to use it is as follows:
find FOLDERPATH -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n
Make sure you replace FOLDERPATH by the folder for which you want to find the inode count. For example, if you want to find it for home folder replace FOLDERPATH by /
find / -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n
Another example of folder path (my folder path actually) is as follows:
find /blue/burleigh/nhans -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n
The output would be multiple lines and each line has two columns: column 1: the first should be the inode count column 2: the second should be the path
6757 /blue/burleigh/nhans/BAMMcloned/bamm-master/build
Understanding the output
The files are sorted in ascending order of inode count. This means that the first column of the last line should have the highest number of inode count. If this number looks ridiculously large, go to the file path and check if there are any unusual files and be careful in deleting those.
Breaking down the lines
Let me further break down the piped commands. find command looks for files in the path “FOLDERPATH” provided by the user
-xdev
-xdev is used to prevent the find command from decending to directories on other file systems.
-printf '%h\n'
-printf followed by ‘%h\n’ format is used to print each horrizontal line and add a new line to it.
sort
sorts the output
uniq -c
uniq -c filters out the repeated lines in the sorted output
sort -k 1 -n
sorts the output further by first column
Leave a Comment