JavaBlog.fr / Java.lu Unix Unix : Some useful commands (find, process, disk space…)

Unix : Some useful commands (find, process, disk space…)

Hi,

In this article, I propose you a range of useful Unix commands.

Use an ASCII file in DOS/MAC format in Unix format
The line endings Windows are different of Unix line endings, and this point could create problem for the plain text files like CSH scripts, SQL scripts…etc. To convert the plain text files in DOS/MAC format to UNIX format, there is dos2unix:

1huo@myserver:/opt/files/>dos2unix -o file1.sh file1.sh
2dos2unix: converting file file1.sh to UNIX format ...
3 
4huo@myserver:/opt/files/>dos2unix -o *
5dos2unix: converting file file1.sh to UNIX format ...

Note: The DOS line endings in Unix are visible via vi like ^M.


Find a file
The find command allows the searching of files.
Searching of files via their filename:

1huo@myserver:/opt/files/>find ./ -name huo*
2./lib/huo-common-1.3.1.jar
3./lib/huo-batch-1.3.1.jar
4./lib/huo-db-1.3.1.jar

Searching of files via regular expression:

1huo@myserver:/opt/files/>find ./ -name h[uU][oOijTt]*
2./lib/huo-common-1.3.1.jar
3./lib/huo-batch-1.3.1.jar
4./lib/huo-db-1.3.1.jar

Searching of files via their last modification date time:

1huo@myserver:/opt/files/>find ./ -ctime -1
2./
3./lib
4./lib/huo-common-1.3.1.jar
5./lib/huo-batch-1.3.1.jar
6./lib/huo-db-1.3.1.jar

Searching of files via their size:

01huo@myserver:/opt/tomcat/webapps/host-manager/>find .-size +5 -exec ls -s {} \; | sort -nr | more
028 web.xml
038 ./WEB-INF/web.xml
048 ./images/asf-logo.gif
058 asf-logo.gif
06...
074 jsp
084 index.jsp
094 images
104 fix.gif
114 docs.gif
124 design.gif
134 context.xml
14...
154 404.jsp
164 403.jsp
174 401.jsp
18total 40
19total 4
20total 20
21total 12
22total 12
01huo@myserver:/opt/tomcat/webapps/host-manager/>find .-size +5 -exec ls -s {} \;
02total 20
034 images   4 index.jsp   4 manager.xml   4 META-INF   4 WEB-INF
04total 12
054 jsp   8 web.xml
06total 12
074 401.jsp  4 403.jsp   4 404.jsp
08...
09total 4
104 context.xml


Treatment of searching of files
Searching of files via their filename and displaying their size on the disk in kbytes:

1huo@myserver:/opt/files/>find ./ -name huo* -exec du -k {} \;
2112 ./lib/huo-common-1.3.1.jar
312  ./lib/huo-batch-1.3.1.jar
444  ./lib/huo-db-1.3.1.jar

Searching of files containing the log4j string:

01huo@myserver:/opt/files/>find ./ -type f | xargs grep -il log4j
02./lib/log4j-1.2.16.jar
03./lib/commons-logging.jar
04./lib/log4j.properties
05./lib/startBatch.sh
06...
07huo@myserver:/opt/files/>find ./ -type f | xargs grep -il log4j > find.out
08huo@myserver:/opt/files/>more find.out
09./lib/log4j-1.2.16.jar
10./lib/commons-logging.jar
11./lib/log4j.properties
12./lib/startBatch.sh


Find text in a file

1huo@myserver:/opt/tomcat/webapps>find . -exec grep 'response' {} \; -print
2<%
3response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + ./index.jsp


Process
To find the ID of a process:

1huo@myserver:/opt/files/>ps -ef | grep java

To kill a process whose the ID is 905:

1huo@myserver:/opt/files/>kill 905

To kill a process whose the ID is 905 with a kill forced termination; cannot be trapped :

1huo@myserver:/opt/files/>kill -9 905


Check disk space
To check the disk space:

1huo@myserver:/opt/files/>df -k
2FileSystem            1K-blocks       Used    Available      Use%       Mounted on
3/dev/huo1             10220745     4851245      5369500       54%       /
4devtmpfs               1020785           0      1020785       0%        /dev


Check size of folder

1huo@myserver:/opt/tomcat/webapps>du -hs *
215M    myApp
321M    myApp.war
492K    host-manager
5124K   manager
6212K   ROOT


Free disk space by deleting
Deleting resoures unchanged for 4 months:

1huo@myserver:/opt/tomcat/webapps>find -mtime +120 -exec rm{} \;

That’s all!!!

Huseyin OZVEREN

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

Related Post