Showing posts with label UNIX Hacks. Show all posts
Showing posts with label UNIX Hacks. Show all posts

Wednesday, November 23, 2011

Alternative of "ls" command

What if your "ls" binary get corrupted ? Is there any alternative of "ls" command. The Answer is "YES"

echo Does ls

Did you know that echo can list out a directory much like the ls command? The shell you use must understand globbing in order for this to work. The formatting of the information that results is really the only difference between the two. An output of ls yields a single or multiple column listing. When echo is used, the files are all spaced one right after the other.

% cd /usr
% ls *
bin  etc  games  include  java  kerberos  lib  libexec  local  man  nsh  openv  sbin  share  src  tmp  X11R6

% echo *
bin etc games include java kerberos lib libexec local man nsh openv sbin share src tmp X11R6


In looking at the examples for the two commands, you can see right away that echo doesn't output any of the file description labels. The real ls command won't display the file description labels either. These labels help to identify the type of files, which are directories (/), soft links (@), or executables (*).

So why do they show up on the ls command? These description labels appear when the -F argument is passed to ls. A lot of vendors and admins like to set an alias entry in the user startup login scripts as a convenience to help identify what the files are. Check the login script that you are using—.login, .profile, .cshrc, or .alias—and you will see an entry similar to

alias ls     ls -CF

Reasons
There might come a time when you will not be able to use the ls command. It might not even be accessible from miniroot. Script writing is easier without having to unalias the ls command all the time. Using echo displays a clean list of files within a given directory.
Real World Experience
Hard system crashes or drives dying can bring systems down to the point where filesystems are so corrupted that they are unable to mount. When this occurs, at times the only way to see the system files is to use echo for displaying the files and directories.

In writing scripts for users or for the system, you never know whether the account that the script runs under has spurious alias definitions. Some users and admins can get creative with their aliases and pass multiple commands or pipe several commands together within an alias entry. To avoid having to set an unalias in your scripts, use the echo command. Here are some examples where echo might be used within scripts.

A variable definition:
list=`echo *`

To pass files through a loop:
for $list in `echo *`
do
  source code
done

Swap on-the-fly

Swap on-the-Fly

If you find that, after you have installed UNIX on your system, you still need more swap space, it is an easy thing to accomplish. Create a swap file and make it active.

Syntax:
mkfile size[m] filename     
The main command for adding swap on the fly is mkfile. This command is simple to use, so adding swap on the fly is easy. After you determine that you need more swap and you have found an underutilized disk that can help balance the disk I/O, you're ready.

# mkfile 200m /disk2/swap_500MB

This mkfile command creates a 500MB swap file in the /disk2 partition. The only step left is to turn the swap file into active swap for the system. Depending on your flavor of UNIX, this is achieved in the following ways:

# swap -a /disk2/swap_500MB
# swapon -a /disk2/swap_500MB

This uses the swap only for the length of time that the system is up. If you reboot or shut down the system for any reason, the swap file is still there, but it is no longer active. The swap or swapon command must be executed again.

To hardcode the swap file into the system so that it is always activated when the system comes up, place an entry in the filesystem table by editing the filesystem table on your system. This file will be called /etc/fstab or /etc/vfstab.

Add the following line to the filesystem table:
/disk2/swap_500M    swap    swap   rw 0   0

Reason
It used to be that the fastest disk I/O was only on the system drive. With the speed of today's SCSI buses and fiber channels, creating the device swap only on the system disk is not really necessary anymore. It is now easy to put swap anywhere on a system, but with that comes the necessity to balance the disk I/O to get the maximum amount of performance out of the system.

Real World Experience
Sometimes a user calls complaining that the applications they are running take up too much of the system's resources. If this happens and you determine that lack of swap space is the problem, let the user know how concerned you are and that you want to fix it on the spot. By adding more swap on-the-fly for the user while you have him on the phone, it looks like you're pulling some kind usable space out of thin air. This simple feat can make you look pretty good in the user's eyes.

fuser Instead of ps

Here is an alternative way to get the process ID (PID) of a particular process. The fuser command is more reliable and can be quicker than ps.



The fuser command outputs the PIDs of all processes that are currently opened under the named file. If a named directory is passed through fuser, the PIDs of all the processes that have a file or files open for reading in that directory are displayed. The files passed must be fully qualified in order for the command to function properly. If they are not, the proper syntax is displayed on standard output.
There is one caveat to using this command. You must have read access to /dev/kmem and /dev/mem. This is because fuser takes an actual snapshot of the system image that is found in these character devices at the time it is executed.
 
# fuser /bin/csh
/bin/csh: 1485t 1106t

The t at the end of the each PID denotes that these processes have their own executable text segment that is open.
The fuser command has an option (-k) that can be passed to send a kill signal to the PID. So, to kill all the csh processes, execute the following simple command:
 
# fuser -k /bin/csh
/bin/csh: 1485t 1106t

This replaces the following set of commands you would use a number of times throughout the day:

# ps -ef | grep ksh

root 1484 1485  1 17:54:02 pts/1   0:00 /bin/ksh
root 1116 1117  1 17:54:16 pts/1   0:00 grep ksh
root 1090 1091  0   Aug 09 pts/2   0:00 /bin/ksh

# kill 1484 1090

If multiple processes are associated with a particular process that you run within your environment, you can easily write a script to kill the application and all the daemons associated with it.

Suppose an application lives in /sbin called bsr. It has several daemons that run independently from bsr, such as bsrqqd, bsrexecd, and bsrojbd. You can write a quick-and-dirty script to kill the entire application by using fuser:

#! /bin/sh

fuser -k /sbin/ls
fuser -k /sbin/bsrqq
Line 1: Define the shell to use.
Lines 3–6: Find the process of the file running and kill its process.
Reason
Using fuser is simple, to the point, and very efficient. It can be time consuming to pick from hundreds of processes on larger servers that might or might not relate to the process you are trying to kill or gather information on. This single command quickly gathers information and kills the PID, if necessary, on request. It is a very useful command for an administrator.
Real World Experience
I have become accustomed to using this command for killing predetermined processes. I have several scripts similar to the one described in place to kill off various user applications, X sessions, and shells, among other things. On a remote system defined as a trusted host, it is nice to be able to execute a remote shell and kill processes quickly without having to log in to the remote machine. To the user it appears as though you have killed processes without even logging in to the system: it's magic to them!



/usr/sbin/fuser /bin/csh