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:
1 | huo@myserver:/opt/files/>dos2unix -o file1.sh file1.sh |
2 | dos2unix: converting file file1.sh to UNIX format ... |
4 | huo@myserver:/opt/files/>dos2unix -o * |
5 | 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:
1 | huo@myserver:/opt/files/>find ./ -name huo* |
2 | ./lib/huo-common-1.3.1.jar |
3 | ./lib/huo-batch-1.3.1.jar |
Searching of files via regular expression:
1 | huo@myserver:/opt/files/>find ./ -name h[uU][oOijTt]* |
2 | ./lib/huo-common-1.3.1.jar |
3 | ./lib/huo-batch-1.3.1.jar |
Searching of files via their last modification date time:
1 | huo@myserver:/opt/files/>find ./ -ctime -1 |
4 | ./lib/huo-common-1.3.1.jar |
5 | ./lib/huo-batch-1.3.1.jar |
Searching of files via their size:
01 | huo@myserver:/opt/tomcat/webapps/host-manager/>find .-size +5 -exec ls -s {} \; | sort -nr | more |
04 | 8 ./images/asf-logo.gif |
01 | huo@myserver:/opt/tomcat/webapps/host-manager/>find .-size +5 -exec ls -s {} \; |
03 | 4 images 4 index.jsp 4 manager.xml 4 META-INF 4 WEB-INF |
07 | 4 401.jsp 4 403.jsp 4 404.jsp |
Treatment of searching of files
Searching of files via their filename and displaying their size on the disk in kbytes:
1 | huo@myserver:/opt/files/>find ./ -name huo* -exec du -k {} \; |
2 | 112 ./lib/huo-common-1.3.1.jar |
3 | 12 ./lib/huo-batch-1.3.1.jar |
4 | 44 ./lib/huo-db-1.3.1.jar |
Searching of files containing the log4j string:
01 | huo@myserver:/opt/files/>find ./ -type f | xargs grep -il log4j |
03 | ./lib/commons-logging.jar |
07 | huo@myserver:/opt/files/>find ./ -type f | xargs grep -il log4j > find.out |
08 | huo@myserver:/opt/files/>more find.out |
10 | ./lib/commons-logging.jar |
Find text in a file
1 | huo@myserver:/opt/tomcat/webapps>find . -exec grep 'response' {} \; -print |
3 | response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + ./index.jsp |
Process
To find the ID of a process:
1 | huo@myserver:/opt/files/>ps -ef | grep java |
To kill a process whose the ID is 905:
1 | huo@myserver:/opt/files/>kill 905 |
To kill a process whose the ID is 905 with a kill forced termination; cannot be trapped :
1 | huo@myserver:/opt/files/>kill -9 905 |
Check disk space
To check the disk space:
1 | huo@myserver:/opt/files/>df -k |
2 | FileSystem 1K-blocks Used Available Use% Mounted on |
3 | /dev/huo1 10220745 4851245 5369500 54% / |
4 | devtmpfs 1020785 0 1020785 0% /dev |
Check size of folder
1 | huo@myserver:/opt/tomcat/webapps>du -hs * |
Free disk space by deleting
Deleting resoures unchanged for 4 months:
1 | huo@myserver:/opt/tomcat/webapps>find -mtime +120 -exec rm{} \; |
That’s all!!!
Huseyin OZVEREN
Related