Categories
Server

Unix: Count number of files in a folder

You have run out of inodes on your server and you have no idea where all those files are?

The following command will tell you the number of files of each directory so you can spot where the hell they are.

for i in /home/*; do echo $i; find $i | wc -l; done

When you see a directory with lots files, you can continue examining its subdirectories by calling again the command on it.

for i in /home/thisone/*; do echo $i; find $i | wc -l; done

Categories
Server

Unix: Fast Find and Replace in VIM

I configure a lot of files, and most of the time it’s all about copying an old config file, edit a few variables and save it.
Take for example a NGINX virtualhost file. You already have a website running and you want to run another site with similar configuration.

So you copy the existing virtualhost and give it a name, then opens it and want to change everyting that says “my-first-website” with “my-second-website”.

The following command is doing just that in one shot!

:%s/my-first-website/my-second-website/g

BOOM!

You have notice it’s using some regular expression quantifier “g” to tell VIM to replace all occurence of “my-first-website” with “my-second-website”.

Categories
Server

Unix: Remove tons of files

You may not be able to delete lot of files with the command “rm”.
The error could be “Argument list too long”.

One possible solution is to use the “find” command and the “-delete” option.

The following command is deleting all files in the /deleteme directory.
It deletes also all files in subdirectories but leaves subdirectories structure intact.

find /deleteme -type f -print -delete