You are here: ApiroTech > UNIX > UNIX

 
 
 

UNIX

PREVIOUS     NEXT

Install a Solaris package

 If a package file (i.e., from sunfreeware.com) exists in the current directory, this recipe will allow you to install it on the system.If the package file is called newpackage: pkgadd -d newpackage Some packages will interactively ask you questions and for confirmation.... Read More

View a man page manually

 Sometimes a man page for a package does not exist in the MANPATH and cannot be seen my the man program. Using nroff to format the text the way that man does is an option to altering your MANPATH variable.To view a man page file called recipe.1 in the current directory, use: nroff -man recipe.1 | more... Read More

Keep watch on the number of instances of a process

 During troubleshooting, it may be helpful to monitor the number of process instances to see if it is stable or increasing.The following command will display the number of occurrences of sendmail processes every 5 seconds in Bourne shell derivatives (i.e., sh, ksh, bash...): while true do ps -ef | grep [s]endmail | wc -l sleep 5 done Using a loop from the command line is a powerful tool. When you press ENTER at the end of each line, you will be presented with a different prompt based on your shell indicating that it is continuing the command and waiting for more. The [s] in sendmail represents a regular expression trick that means match any single character within the square brackets. This means that [s]endmail will match only sendmail and will prevent counting the grep command in the ps -ef listing because it will show up as [s]endmail. You can vary the parameter to sleep to increase or decrease the number of seconds between each counting.... Read More

Copy files and directories recursively with tar

 Copying a directory tree and its contents to another filesystem using tar will preserve ownership, permissions, and timestamps. A neat trick allows using tar to perform a recursive copy without creating an intermediate tar file.To copy all of the files and subdirectories in the current working directory to the directory /target, use: tar cf - * | ( cd /target; tar xfp -) The first part of the command before the pipe instruct tar to create an archive of everything in the current directory and write it to standard output (the - in place of a filename frequently indicates stdout). The commands within parentheses cause the shell to change directory to the target directory and untar data from standard input. Since the cd and tar commands are contained within parentheses, their actions are performed together. The -p option in the tar extraction command directs tar to preserve permission and ownership information, if possible given the user executing the command. If you are running th... Read More

Find help with man pages

 The UNIX command line interface is wonderfully, wickedly powerful. With that power comes complexity if only in the sheer number of commands. Even seasoned UNIX gurus need help remembering the syntax of less frequently used commands. Fortunately, UNIX has a great built-in manual called man.To get information for a command such as grep, use: man grep If you are not sure what the name of a command is, try using a keyword search. The keywords are matched against the Name line in each known man page which contains the name of the command and a brief description of its function. To search for man pages relating to SQL databases, commands, or functions, use: man -k sql... Read More

Update the man page indexes

 New man pages are often included when you add software to your UNIX system. If you want to be able to search for these using the man keyword (-k) option, or if you get an error when trying to use this option, you need to update the windex files with one easy command.To update the windex files for all man pages in your MANPATH, run this as superuser: catman -w... Read More

Control which man pages are available in sh with MANPATH

 Using the MANPATH environment variable, you can select which man page directories are searched when using man or apropos. This recipe describes changing your MANPATH variable when using the Bourne shell.If you have added software with its own man page directory, such as /usr/local/software/man, you can add this to your personal MANPATH variable so that those man pages will be available to man. The technique for changing this variable depends on your shell. If you are using the Bourne shell (sh), you must edit the file .profile in your home directory (you can use the shortcut ~/.profile to refer to this). If this file already contains a MANPATH declaration, you can add the new path by adding: :/usr/local/software/man to the end of the line so that it looks something like: MANPATH=/usr/man:/usr/local/man:/usr/local/software/man If your Bourne shell .profile doesn't have a MANPATH directive, you can create one by adding these lines to .profile: MANPATH=/usr/local/software/man ... Read More

Recursively delete certain files

 Using the find command, you can locate and delete files based on many attributes such as filetype. This recipe will demonstrate how to find and delete files based on part of their filename.To find and delete all files on the system that end in .log, run this as superuser: find / -name \*.log -exec rm {} \; The backslashes (\) are important in this command. Using the -exec option allows an arbitrary command, in this case rm, to be used with the filename substituted in place of the {} characters. This would be a dangerous command to execute, so a safer alternative uses the -ok command instead of the -exec. When used this way, find will prompt you before each execution of the command following -ok. To optionally delete all files ending with .tmp in the /var filesystem, use: find /var -name \*.tmp -ok rm {} \;... Read More

Basic FTP commands

 Listing of standard FTP commands.close disconnect from the current host open hostname connect to a different host ls list the contents of a directory dir list the contents of a directory with expanded information cd directory change remote working directory lcd directory change local working directory !ls or !dir list contents of local directory get filename copy a single file from the remote host mget copy multiple files from the remote host put copy a single file to the remote host mput filename copy multiple files to the remote host binary set binary file type (set before transferring binary files) ascii set ascii file type (set before transferring text files) mkdir newdirectory create directory del filename delete remote file help command displays help information on the command quit exit the ftp program... Read More

Backup a UNIX/Solaris UFS filesystem with ufsdump

 Many techniques exist for backing up data on UNIX systems. The ufsdump command is a powerful choice because it is free, already installed on most systems, has a wide array of options such as incremental backups, and is fast. This recipe will describe the simplest use of ufsdump: backing up a single filesystem to a file on disk.The ufsdump command can be intimidating (particularly if you just read the man page). For simple backups, most options are not relevant. For instance, to backup the /usr filesystem to a file called /data/usr.ufs, use this command: ufsdump 0f /data/usr.ufs /usr The 0 (zero) option determines the dump level of the backup. 0 indicates a full backup while 1-9 are used for incremental backups. The f option, like with tar, tells ufsdump which file to use to dump the data to (just like the tar command). The last argument (/usr) specifies the target to be backed up. It can be specified as a filesystem path (such as /usr) or the raw device path (like /dev/rdsk/c1t0d0s4). ... Read More

Extract a bz2 or bzip2 file

 You've downloaded a file that ends in .bz2. Now what do you do? This recipe describes how to work with these files.A file with an extension of bz2 has been compressed with the bzip2 program. Programs like bzip2 and gzip work on a single file so they are not themselves an archive of multiple files like a zip archive can be. To extract the file file.bz2, use bzip2 -d file.bz2 This will an uncompressed file in the current directory called 'file' and will delete the original bz2 archive. If you want to keep the original file, add the -k option like bzip2 -dk file.bz2 A useful option for bzip2 is the -c switch which causes bzip2 to write the uncompressed output to stdout which can easily be redirected to another option. For instance, to search the compressed file file.bz2 for the string tech-recipes, use: bzip2 -dc file2.bz2 | grep tech-recipes... Read More

Extract a tar archive or file

 The tar file is the long-time champion of archiving files and even directories of information into a single archive file. However, the first time you face a tar file, it will completely clam up and will not tell you what to do with it. Extracting the contents of a tar file is simple (once you know how).A tar file is extracted (and created) with the easy-to-remember application tar. While the name is simple, the syntax is a little different than normal UNIX commands. To extract the contents of the file archive.tar, use: tar xf archive.tar The x in xf means extract the contents of the archive. The f in xf tells tar to use the next parameter as the filename of the archive. This comes from tar's history as a tape archiving application (the name tar comes from tape archive). By default, tar wants to read or write to a tape drive, thus the f parameter. Another couple of nice parameters to know are t and v. The t parameter is used in place of x to list a table of contents of the archive. tar ... Read More

Remove ^M characters at end of lines in vi

 UNIX treats the end of line differently than other operating systems. Sometimes when editing files in both Windows and UNIX environments, a CTRL-M character is visibly displayed at the end of each line as ^M in vi.To remove the ^M characters at the end of all lines in vi, use: :%s/^V^M//g The ^v is a CONTROL-V character and ^m is a CONTROL-M. When you type this, it will look like this: :%s/^M//g In UNIX, you can escape a control character by preceeding it with a CONTROL-V. The :%s is a basic search and replace command in vi. It tells vi to replace the regular expression between the first and second slashes (^M) with the text between the second and third slashes (nothing in this case). The g at the end directs vi to search and replace globally (all occurrences).... Read More

Select an arbitrary column of text in UNIX

 UNIX has wonderfully powerful text processing capabilities. There are numerous ways to solve the same problem. Frequently, for example, it is necessary to extract a single column of data from a text file or output stream. This recipe will present several solutions to this problem.Many data files have data fields delimited by a single character like a tab or colon. To extract the full name field out of /etc/passwd, the fifth colon-delimited field, use: cut -d : -f 5 /etc/passwd The cut command allows a great deal of flexibility in cutting data. In this case, the -d : directs cut to use a colon character as the delimiter. The -f 5 parameter directs cut to extract only the fifth field. The field parameter makes cut extrememly flexible. Other examples are -f 2-5 to extract fields 2 through 5, -f 1,3,7 to extract the first, third, and seventh fields. To extract a fixed set of columns, for example the column numbers 44 through 49 from a long directory listing (ls -l), use the following c... Read More

Access remote TCP ports with OpenSSH tunnels

 Using OpenSSH, it is possible to access TCP ports on remote systems, even those not on the host you are ssh'ing to, using tunnels.If the remote system you are connecting to with shh is called 'sadie' and the target host with the desirable TCP port of 1433 is named 'molly' the command to build the tunnel between them is: ssh -L 1433:molly:1433 sadie Once you authenticate with ssh, you can access port 1433 on molly as though it were on your system. Make sure that the remote system (sadie) can resolve the name of the target system (molly). You can use IP addresses instead of names.... Read More

Create a hard link in UNIX

 A hard link is a reference to a file or directory that appears just like a file or directory, not a link. Hard links only work within a filesystem. In other words, don't use hard links between mounted filesystems. A hard link is only a reference to the original file, not a copy of the file. If the original file is deleted, the information will be lost.To create a hard link of the file /export/home/fred/stuff to /var/tmp/thing, use: ln /export/home/fred/stuff /var/tmp/thing The syntax for creating a hard link of a directory is the same. To create a hard link of /var/www/html to /var/www/webroot, use: ln /var/www/html /var/www/webroot See also: Create a symbolic link in UNIX... Read More

Identify LDAP naming contexts using ldapsearch

 Using ldapsearch from the command line, it is possible to find all of the naming contexts of the directory server (lowest level distinguished names like o=tech-recipes).The simplest version of this command (running ldapsearch locally on the LDAP server itself): ldapsearch -s base -b "" objectclass=top The resulting text will display the naming contexts, one per line, prefixed by namingcontext: If you are running ldapsearch on a remote system, you can specify the directory server host (say, dir-serv) by adding -h dir-serv before the objectclass=top argument.... Read More

Strip comments from a file

 This one liner will remove all text after the # character in a file cat file-with-comments | sed -e 's;#.*;;' -e '/^[       ]*$/d'... Read More

Convert Macintosh line breaks to Unix line breaks

 This one liner will convert Macintosh line breaks into Unix line breaks tr '\015' '\012' < macintosh-format-file > unix-friendly-file... Read More

Convert DOS line breaks to Unix line breaks

 This one liner will convert a file with DOS line breaks to a file with Unix line breaks tr -d '\15\32' < dos-format-file > unix-friendly-file... Read More

 

 

Pages : 1 2 3