Saturday, July 5, 2014

useful linux commands


Shell:

The shell is an interactive command interpreter environment within which commands may be typed at a prompt or entered into a file in the form of a script and executed.

A variety of shell environments have been developed over the years (Bourne shell, C shell, Korn shell, Bash shell). The default shell on CentOS 6 is the Bash shell (shorthand for Bourne again shell) and is based on features provided by both the Bourne Shell and the C Shell.

Bash provides a wide range of command line editing options as outlined in the following table:

Ctrl-b //Move cursor back one position
Ctrl-f  //Move cursor forward on position
Delete  //Delete character currently beneath the cursor
Backspace //Delete character to the left of the cursor
Ctrl-_  //Undo previous change (can be repeated to undo all previous changes)
Ctrl-a //Move cursor to the start of the line
Ctrl-e  //Move cursor to the end of the line
Esc then f  //Move cursor forward one word
Esc then b //Move cursor back on word
Ctrl-l  //Clear the screen of everything except current command
Ctrl-k  //Delete to end of line from current cursor position
Esc then d  //Delete to end of current word
Esc then DEL  //Delete beginning to current word
Ctrl-w  //Delete from current cursor position to previous white space 

File Name and Path Completion 

press the Esc key twice //complete the filename for you with the first file or path name in the directory that matches the characters you entered.

press Esc =  //Displays a list of possible matches 

Shell commands:

history //Displays a list of previously executed commands. In addition, Ctrl-p (or up arrow) and Ctrl-n (or down arrow) may be used to scroll back and forth through previously entered commands. Another option is to enter the ‘!’ character followed by the first few characters of the command to be repeated followed by the Enter key.

which ls //The available shell commands are either built into the shell itself, or reside on the physical file system. The location on the file system of a command may be identified using the which command.

cat list?.txt //Single character matches may be specified using the ‘?’ character. ‘*’ wildcard character to can be used to represent any number of characters.

ls *.txt > files.txt //redirect the output from an ls command to a file named files.txt 

wc –l < files.txt //The contents of a file may be fed into a command in place of stdin. This displays the number of lines contained in files.txt file.

df -h //Displays available partitions with their free space. -h allows human readable format

dd if=/home/srinivas/downloads/CentOS-6.5-x86_64-bin-DVD1.iso of=/dev/sdb //we can create bootable linux image usb device

sudo [options] [command]  //If you are allowed, execute command as the superuser. Authorized users of sudo and the commands they are permitted to execute are listed in the sudo configuration file, /etc/sudoers. If an unauthorized
user attempts to run a command, sudo informs an administrator via email. By default, it sends the message to the root account. Users attempting to run commands are prompted for their password. Once authenticated, sudo sets a timestamp for the user. For five minutes from the timestamp, the user may execute further commands without being prompted for her password. This grace period may be overridden by settings in the /etc/sudoers file. Also see /etc/sudoers for configuration examples. The sudoedit form of sudo is equivalent to running sudo -e.

su [option] [user] [shell_args]  //Create a shell with the effective user ID user. If no user is specified, create a shell for a privileged user (i.e., become a superuser). Enter EOF to terminate. You can run the shell with particular options by passing them as shell_args (e.g., if the shell runs bash, you can specify -c command to execute command via bash, or -r to create a restricted shell).

env //Shell environment variables provide temporary storage of data and configuration settings. Displays list of currently defined environment variables

echo $PATH //Displays the value of PATH environment variable. PATH defines the directories in which the shell will search for commands entered at the command prompt prompt, and the order in which it will do so.

export PATH=$PATH:$HOME/scripts //you wanted the shell to also look for commands in the scripts directory located in your home directory, you would modify the PATH variable like this.

export DATAPATH=/data/files //You can create your own environment variables using the export command. If there are environment variable or alias settings that you need to be configured each time you enter the shell environment, they may be added to a file in your home directory named .bashrc.

export NOW=`date` //Assign the output from a command to an environment variable involves the use of back quotes (`) around the command.

vmstat

top

rpm -Uvh package_file //updates existing installation

rpm -i package_file //installs package  

rpm -e package_name --noscripts //if normal erase didn't work because of any installation problems, the --noscripts option forcefully erases the package.

rpm -qa | grep java   //finds all the installed java rpm's on the machine

rpm -qf /user/bin/vim //displays which package is responsible for the file installation

rpm -ql package-name //list all files installed by that package


yum search package_file // * can be used at either end of the package file name

yum install package_file   //yum searches the repositories listed under /etc/yum.repos.d/ for specified package file. yum does dependency resolution also.

yum localinstall jdk-7u60-linux-i586.rpm  //package-file is on the local machine. This takes the help of yum for installation along with dependency resolution.

yum erase package_name //uninstall the package 

 yum update //apply any available updates to the installed packages

yum info package_name //displays information about the package 

yum repolist //display your current yum repositories

yum repolist all //to display all the repositories (both enabled and disabled) 

yum --enablerepo=fedora-source install vim-X11.x86_64 //install from a disabled repositories

yum list | less //list all the packages available in the yum database
yum list installed | less //view all the packages that are installed on your system
yum provides /etc/sysconfig/nfs //know which package a particular file belongs to

chmod +x filename.bin //well bin files you typicallly have to change the file permissions to be able to execute it by typing

./filename.bin //and then run. If instead you want to only extract the RPM file but not install it, you can run the .bin file with the -x argument.

ls /bin /usr/bin /usr/sbin | sort | uniq | grep -i Postfix //this command returned no entries. This means Postfix binaries are not installed on this machine.

groupadd groupname  //adds a new group. The /etc/group file contains all the grops
usermod -a -G groupname username //Add an Existing User to a Group
usermod -g groupname username //Change a User’s Primary Group
id username  //to see what groups the user is assigned to
groups username  //to see what groups the user is assigned to
groups //To view all the groups on the system

service mysqld start //runs mysqld available under /etc/rc.d/init.d. To automatically start and stop a service create symbolic links for /etc/rc.d/init.d/mysqld under /etc/rc.d/rc3.d/S99mysqld and /etc/rc.d/rc0.d/K01mysqld

service mysqld stop //runs mysqld available under /etc/rc.d/init.d

chkconfig --list //displays whether a service is enabled at various run levels.

nautilus . //opens the current directory in the file browser




Shell scripting:

Shell scripts are essentially text files containing sequences of statements that can be executed within the shell environment to perform tasks. In addition to the ability to execute commands, the shell provides many of the programming constructs such as for and do loops and if statements that you might reasonably expect to find in a scripting language.

The first step in creating a shell script is to create a file (for the purposes of this example we name it simple.sh) and add the following as the first line:

#!/bin/sh

The #! is called the shebang and is a special sequence of characters indicating that the path to the interpreter needed to execute the script is the next item on the line (in this case the sh executable located in /bin). This could equally be, for example, /bin/csh or /bin/ksh if either were the interpreter you wanted to use.

The next step is to write a simple script:

#!/bin/sh
for i in *
do
echo $i
done

All this script does is iterate through all the files in the current directory and display the name of each file. This may be executed by passing the name of the script through as an argument to sh:

sh simple.sh

In order to make the file executable (thereby negating the need to pass through to the sh command) the chmod command can be used:

chmod +x simple.sh

Once the execute bit has been set on the file’s permissions, it may be executed directly. For example:

./simple.sh




Filesystem layout (http://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-filesystem-fhs.html):

File systems break files down into two logical categories:
  • Shareable vs. unsharable files
  • Variable vs. static files
Shareable files are those that can be accessed locally and by remote hosts; unsharable files are only available locally. Variable files, such as documents, can be changed at any time; static files, such as binaries, do not change without an action from the system administrator.

/boot/ directory contains static files required to boot the system, such as the Linux kernel.

/dev/ directory contains device nodes that either represent devices that are attached to the system or virtual devices that are provided by the kernel. The udev demon takes care of creating and removing all these device nodes in /dev/.
Devices in the /dev directory and subdirectories are either character (providing only a serial stream of input/output) or block (accessible randomly). Character devices include mouse, keyboard, modem while block devices include hard disk, floppy drive etc.

The /etc/ directory is reserved for configuration files that are local to the machine. No binaries are to be placed in /etc/. Any binaries that were once located in /etc/ should be placed into /sbin/ or /bin/.

/lib/ directory should contain only those libraries needed to execute the binaries in /bin/ and /sbin/. These shared library images are particularly important for booting the system and executing commands within the root file system.

/media/ directory contains subdirectories used as mount points for removeable media such as usb storage media, DVDs, CD-ROMs, and Zip disks. 

/mnt/ directory is reserved for temporarily mounted file systems, such as NFS file system mounts. For all removeable media, please use the /media/ directory. Automatically detected removeable media will be mounted in the /media directory.

/opt/ directory provides storage for most application software packages.
A package placing files in the /opt/ directory creates a directory bearing the same name as the package. This directory, in turn, holds files that otherwise would be scattered throughout the file system, giving the system administrator an easy way to determine the role of each file within a particular package.
For example, if sample is the name of a particular software package located within the /opt/ directory, then all of its files are placed in directories inside the /opt/sample/ directory, such as /opt/sample/bin/ for binaries and /opt/sample/man/ for manual pages.
Packages that encompass many different sub-packages, data files, extra fonts, clipart etc are also located in the /opt/ directory, giving that large package a way to organize itself. In this way, our sample package may have different tools that each go in their own sub-directories, such as /opt/sample/tool1/ and /opt/sample/tool2/, each of which can have their own bin/, man/, and other similar directories.

/proc/ directory contains special files that either extract information from or send information to the kernel. Examples include system memory, cpu information, hardware configuration etc.

/sbin/ directory stores executables used by the root user. The executables in /sbin/ are used at boot time, for system administration and to perform system recovery operations. Of this directory, the FHS says:
/sbin contains binaries essential for booting, restoring, recovering, and/or repairing the system in addition to the binaries in /bin. Programs executed after /usr/ is known to be mounted (when there are no problems) are generally placed into /usr/sbin. Locally-installed system administration programs should be placed into /usr/local/sbin.

/usr/ directory is for files that can be shared across multiple machines. The /usr/ directory is often on its own partition and is mounted read-only. At a minimum, the following directories should be subdirectories of /usr/:

/usr
   |- bin/
   |- etc/
   |- games/   
   |- include/   
   |- kerberos/   
   |- lib/   
   |- libexec/        
   |- local/   
   |- sbin/   
   |- share/   
   |- src/   
   |- tmp -> ../var/tmp/ 


/usr/local hierarchy is for use by the system administrator when installing software locally. It needs to be safe from being overwritten when the system software is updated. It may be used for programs and data that are shareable among a group of hosts, but not found in /usr.

/var/. Since the FHS requires Linux to mount /usr/ as read-only, any programs that write log files or need spool/ or lock/ directories should write them to the /var/ directory. The FHS states /var/ is for:
...variable data files. This includes spool directories and files, administrative and logging data, and transient and temporary files.

Below are some of the directories found within the /var/ directory:

/var
   |- account/   
   |- arpwatch/   
   |- cache/   
   |- crash/   
   |- db/   
   |- empty/   
   |- ftp/   
   |- gdm/   
   |- kerberos/   
   |- lib/   
   |- local/   
   |- lock/   
   |- log/   
   |- mail -> spool/mail/   
   |- mailman/   
   |- named/   
   |- nis/   
   |- opt/   
   |- preserve/   
   |- run/   
   +- spool/
       |- at/        
       |- clientmqueue/        
       |- cron/        
       |- cups/        
       |- exim/               
       |- lpd/        
       |- mail/        
       |- mailman/        
       |- mqueue/        
       |- news/        
       |- postfix/         
       |- repackage/        
       |- rwho/        
       |- samba/         
       |- squid/        
       |- squirrelmail/        
       |- up2date/        
       |- uucp         
       |- uucppublic/        
       |- vbox/   
|- tmp/   
|- tux/   
|- www/   
|- yp/
 System log files, such as messages and lastlog, go in the /var/log/ directory. The /var/lib/rpm/ directory contains RPM system databases. Lock files go in the /var/lock/ directory, usually in directories for the program using the file. The /var/spool/ directory has subdirectories for programs in which data files are stored.


No comments:

Post a Comment

Followers