Thursday, June 23, 2011

Linux tips.

This blog post I will keep posting regular updates as and when I learn something new in linux environment. If it turns out big, may be i will start a separate blog! :)

1) How to append a date to a file on command line:
Consider the scenario where you want to append a date to a file. This might be useful when you want to keep a file with different date versions. I encountered a similar scenario when I needed to check log file with multiple run and my program wrote logs to one file. In my case, I could have modified the java program to consider the date while writing to file/ or simply appending the new logs to the same file. But what if I do not have the flexibility of modifying the code, in this case following command can be useful.

ant war > ~/logs/web.war.`date +"%F:%H-%M-%S"`.log

All this command does is appends date to the log file when war is created. If I ran this command on 23rd June, 2011 at 11.47 pm, output file would have the name: web.war.2011-06-23:11-47-00.log

You can change the output date options by changing the %<options>, do a "man date" to see all the other options one can use!


2) How to delete files when directory contains a lot of files (pick a huge number!).
Issue: If the number of files is too large, rm command cries with "bash: /bin/rm: Argument list too long", a way to delete all the files is using this little trick: find . -name "*" | xargs /bin/rm . Be wary careful in using this command, as it will delete all the files in the current directory!

No comments:

Post a Comment