While you are working on Unix environment, sometimes you might need to troubleshoot the errors/issues that caused the abnormal behavior of scripting and commands that related to Unix.
In troubleshooting of Unix, For example, one shell script is trying to move file from one location to another location and if script is throwing some error while moving file to target location. For this, you can say possible root causes are:
- Script has no write permission for the target directory
- Target directory does not have enough memory to store new file
To troubleshoot the above possible causes, you need to check the permissions and available space of target directory.
To check permissions, you can use ‘ls -ltr’ command.
As you see directory does not have write permission for Others, I modified it using chmod and now we can write to that directory without any issue.
To check the available space, you can use ‘du’ command.
du command can be used to determine the disk usage of particular directory. And to find the free disk space you could use ‘ df ‘.
In this way, we can troubleshoot the shell scripts or Unix environment using some useful commands, and those commands are listed below one by one with their usage.
Troubleshooting of Unix – File System
When you debug the files or handling any file systems, if you want to find or search the files based on their names then you can use find command.
It comes with many options to customize your search for ‘find files modified 10 days ago’, ’find files which have more than 1GB data’, ‘find files which have file extension .java’, ‘find files and remove them which have 1KB in size’.
Following command finds the file name ‘nr.txt’ and displays it with the path.
$ find -type f -name “nr.txt”
Troubleshooting of Unix – Determine File Sizes
Search for the three largest directories on your Unix System.
$ du –sk * | sort –nr | head -3
Find the largest files on a Unix System
$ du –k | sort –nr
Troubleshooting of Unix – File Operations
Count the number of files in a directory
ls –l | wc –l
Remove the Control-M (^M) characters from a file.
dos2unix filename
Let’s have one scenario like you have several hundred PDFs under a directory in UNIX. The names of the PDFs are really long (assume approx. 80 chars).
If you try to delete all PDFs together using the following command:
rm -f *.pdf
You get the following error:
/bin/rm: cannot execute [Argument list too long]
In order to avoid above error you can use below one of the methods.
for i in *.pdf; do rm “$i”; done
OR
find . -name “*.pdf” -print0 | xargs -0 rm
Warning: this is a recursive search and will find (and delete) files in subdirectories as well. Tack on -f to the rm command only if you sure you don’t want confirmation.
Troubleshooting of Unix – Unix Processes
If you want to return only process IDs
ps –ef |awk ‘{print $2}’
You can check for more details about the particular process ID using prtstat command.
This gives more details like page faults incurred by that process, CPU times and Memory utilized by that process.
The top command provides a dynamic real-time view of a running system. It can display system summary information, as well as a list of processes or threads currently being managed by the kernel.
The types of system summary information shown and the types, order and size of information displayed for tasks are all user-configurable.
These are the some of the steps to be taken when you want to troubleshoot the Unix.