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:

huo@myserver:/opt/files/>dos2unix -o file1.sh file1.sh
dos2unix: converting file file1.sh to UNIX format ...

huo@myserver:/opt/files/>dos2unix -o *
dos2unix: 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:

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

Searching of files via regular expression:

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

Searching of files via their last modification date time:

huo@myserver:/opt/files/>find ./ -ctime -1
./
./lib
./lib/huo-common-1.3.1.jar
./lib/huo-batch-1.3.1.jar
./lib/huo-db-1.3.1.jar

Searching of files via their size:

huo@myserver:/opt/tomcat/webapps/host-manager/>find .-size +5 -exec ls -s {} \; | sort -nr | more
8 web.xml
8 ./WEB-INF/web.xml
8 ./images/asf-logo.gif
8 asf-logo.gif
...
4 jsp
4 index.jsp
4 images
4 fix.gif
4 docs.gif
4 design.gif
4 context.xml
...
4 404.jsp
4 403.jsp
4 401.jsp
total 40
total 4
total 20
total 12
total 12
huo@myserver:/opt/tomcat/webapps/host-manager/>find .-size +5 -exec ls -s {} \; 
total 20
4 images   4 index.jsp   4 manager.xml   4 META-INF   4 WEB-INF
total 12 
4 jsp   8 web.xml
total 12
4 401.jsp  4 403.jsp   4 404.jsp
...
total 4
4 context.xml


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

huo@myserver:/opt/files/>find ./ -name huo* -exec du -k {} \;
112 ./lib/huo-common-1.3.1.jar
12  ./lib/huo-batch-1.3.1.jar
44  ./lib/huo-db-1.3.1.jar

Searching of files containing the log4j string:

huo@myserver:/opt/files/>find ./ -type f | xargs grep -il log4j
./lib/log4j-1.2.16.jar
./lib/commons-logging.jar
./lib/log4j.properties
./lib/startBatch.sh
...
huo@myserver:/opt/files/>find ./ -type f | xargs grep -il log4j > find.out
huo@myserver:/opt/files/>more find.out
./lib/log4j-1.2.16.jar
./lib/commons-logging.jar
./lib/log4j.properties
./lib/startBatch.sh


Find text in a file

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


Process
To find the ID of a process:

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

To kill a process whose the ID is 905:

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

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

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


Check disk space
To check the disk space:

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


Check size of folder

huo@myserver:/opt/tomcat/webapps>du -hs *
15M    myApp
21M    myApp.war
92K    host-manager
124K   manager
212K   ROOT


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

huo@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