Linux: Which directory has the most files?

An inode is used for every file in the filesystem. If the system is running out of inodes that means there are some directories with a lot of files in them.

The following command will find the directories with most files.

find / -xdev -printf '%h\n' | sort | uniq -c | sort -k 1 -n

The following command list free inodes on all filesystems

df -hi

 

Linux: Clear contents of a file

Empty an in-use nohup.out file

cat /dev/null >nohup.out

 

If the file is not in use, the following works in bash

> foo.log

The command above empties out the file foo.log

If sudo is needed, truncate can be used as well

sudo truncate -s 0 foo.log

-s is the size option. 0 in this example sets the size to 0 bytes

Managing services on Linux

This article applies to Red Hat, Fedora, CentOS, AWS Linux distros.

For enabling, disabling and listing system services, use the script called chkconfig.

List all services

chkconfig --list

List a specific service

chkconfig --list httpd

Turn a service on for specific levels

chkconfig httpd on --level 2,3,5

Disable a service

chkconfig httpd off

For stopping, starting and restarting a service, use the script called service.

service httpd stop
service httpd start
service httpd restart

AWS EC2 Linux swap file creation

The Amazon EC2 Linux instances come without a swap file. The steps below show how to add a swap file to an EC2 Linux server.

dd if=/dev/zero of=/mnt/swapfile bs=1M count=4096
chown root:root /mnt/swapfile
chmod 600 /mnt/swapfile
mkswap /mnt/swapfile
swapon /mnt/swapfile
swapon -a

To make these changes permanent (reboot proof). Add the following line to /etc/fstab

vi /etc/fstab
/mnt/swapfile swap swap defaults 0 0