Friday, December 23, 2011

Getting the help for commands on Linux

Most GNU utilities have a ––help option that displays information about the utility.

 

$ cat --help

Usage: cat [OPTION] [FILE]...
Concatenate FILE(s), or standard input, to standard output.
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonblank output lines
-e equivalent to -vE
-E, --show-ends display $ at end of each line

...

If the information that ––help displays runs off the screen, send the output through the less pager (page 31) using a pipe:
$ ls --help | less

Aborting Execution of jobs in Linux / Unix

To terminate a program from a character-based display, press the interrupt key (CONTROL-C or sometimes DELETE or DEL). When you press this key, the Linux operating system sends a terminal interrupt signal both to the program you are running and to the shell. Exactly what effect this signal has depends on the program. Some programs stop execution immediately; others ignore the signal. Some programs take other actions. When it receives a terminal interrupt signal, the shell displays a prompt and waits for another command.

If these methods do not terminate the program, try stopping the program with the suspend key (typically CONTROL-Z), giving the jobs command to verify the job number of the program, and using kill to abort the program. The job number is the number within the brackets at the left end of the line that jobs displays ([1]). The kill command sends a signal to the job specified as its argument. You must precede the job number with a percent sign (%1):

$ bigjob
^Z
[1]+ Stopped bigjob

$ jobs
[1]+ Stopped bigjob

$ kill %1
[1]+ Stopped bigjob

$ RETURN
[1]+ Killed bigjob



By default kill sends a software termination signal (–TERM). When this signal does not work, try using a kill (–KILL) signal:
$ kill -KILL %1

A running program cannot ignore a kill signal—it is sure to abort the program. The kill command returns a prompt; press RETURN again to see the confirmation message.


What are emulators for Linux?

Linux supports programs, called emulators, that run code intended for other operating systems. By using emulators you can run some DOS, Windows, and Macintosh programs under Linux. Wine (www.winehq.com) is an open-source implementation of the Windows API on top of X and UNIX/Linux; QEMU (fabrice.bellard.free.fr/qemu) is a CPU-only emulator that executes x86 Linux binaries on non-x86 Linux systems.

What do GNU stands for?

GNU, which stands for Gnu's Not UNIX, is the name for the complete UNIX-compatible software system which I am writing so that I can give it away free to everyone who can use it.

List of Environment variables in Linux


Variable name Stored information
DISPLAY used by the X Window system to identify the display server
DOMAIN domain name
EDITOR stores your favorite line editor
HISTSIZE size of the shell history file in number of lines
HOME path to your home directory
HOSTNAME local host name
INPUTRC location of definition file for input devices such as keyboard
LANG preferred language
LD_LIBRARY_PATH paths to search for libraries
LOGNAME login name
MAIL location of your incoming mail folder
MANPATH paths to search for man pages
OS string describing the operating system
OSTYPE more information about version etc.
PAGER used by programs like man which need to know what to do in case output is more than one terminal window.
PATH search paths for commands
PS1 primary prompt
PS2 secondary prompt
PWD present working directory
SHELL current shell
TERM terminal type
UID user ID
USER(NAME) user name
VISUAL your favorite full-screen editor
XENVIRONMENT location of your personal settings for X behavior
XFILESEARCHPATH paths to search for graphical libraries

Which shell am I using?

 

To find out which shell you are currently using, type the following command:

echo $SHELL

The result of that command will tell you what your current shell is and may look something like:

/bin/bash

That would mean that you are using the bash shell. On a side note, you would also realise that this shell resides in the bin directory.

Preserve permissions and ownership when copying files using cp

We use the -p flag to tell cp to preserve the permissions and ownership of the files involved. So if we wanted to copy a folder recursively preserving permissions and ownership of the folder and the files contained in it, we would use the following command (preferably as root):

cp -R -p /source/folder/ /destination/folder/

All the files in the source folder would be copied to the destination folder and the permissions and owners would be preserved.

If we had a single file it would be similar:

cp -p /path/to/file-a /path/to/file-b

To check that the owner and permissions have been preserved, we can simply use:

ls -altp /destination/folder/

The result of the above command would show us the permissions that the files in the destination folder have.