Showing posts with label commands. Show all posts
Showing posts with label commands. Show all posts

Tracking Processes In UNIX/LINUX System

In Linux (and UNIX in general), each program which is running comprises a process.From the operating system’s point of view, each process is independent of the others. Unless it specifically asks to share resources with other processes, a process is confined to the memory  and CPU allocation assigned to it. If a Process oversteps its memory allocation and could corrupt another running program and make the system unstable are immediately killed by Linux. This method of handling processes has resulted as a major contributor to the stability of UNIX/LINUX systems: User applications cannot corrupt other user programs or the operating system.
                    There are tools provided by which you can manually list and manipulate processes. These are very important elements of a system administrator’s daily work.
        
Command used for Listing Processes: ps
               The ps command lists all the processes in a system, their state, size, name, owner, CPU time, wall clock time, and much more. There are many command-line parameters available; the majoy one are





                     Options for ps                        Description
                   –aShows all the processes with a controlling terminal and not just the current user’s processes.
                  –rShow only running processes
                  –xShow processes that do not have a controlling terminal.
                  –uShow the process owners.
                  –fDisplay parent/child relationships among processes.
                  –lProduce a list in long format.
                 –wShows a process command-line parameters (up to half a line).
                 –wwShow all of a process’s command-line parameters.

        The most commonly used parameters used with ps command is –auxww. These parameters show all the processes (regardless of whether they have a controlling terminal),each process’s owners, and all the processes’ command-line parameters.

Basic VI commands

The most commonly used commands in vi Editor are as follows:


             COMMAND
            DESCRIPTION
              a
starts insert mode after current position of cursor.
              A
starts insert mode at end of the current line.
              cw
Deletes the current word and than enters the insert mode to replace that word.
              dw
Deletes the current word without entering the insert mode.
              dd
Deletes the current line.
              G
Moves cursor at the end of the line.
              15G
Moves cursor to fifteenth line.
               h
Moves cursor left one space.
               I
Enters insert mode.
               o
enters insert mode opening line below current cursor.
               O
enters insert mode opening line above current cursor.
               :q
Exits from vi.
               r
Replace; replaces current character with next character.
               :set nu
shows line number for the current line
               u
Undoes the last change
               :w
Writes the current file.
               Esc
Escapes from insert mode
               /system
Searches the word System in current file

Using the vi Editor On Linux

Linux relies on large number of text files for configuration. So, you need a text editor to configure Linux. vi is one of the most commonly used text editor.

It is easy to open a file with vi. For eg:- If you need to open /etc/inittab file, use

# vi /etc/inittab

There are three basic ways to work in vi

  1. Command mode is default
  2. Insert mode is to insert text.
  3. Execute mode to run regular shell commands

Command Mode:-
When you open a file in vi, first or default mode is command mode. It can be used to scroll through text, search for different text strings delete specific characters, words or lines. One aid in vi is line numbers, which can be activated as

:set nu

1. open terminal and type  
# vi /etc/inittab

2. vi opens inittab file as follows

3.Now to see the line number type :set nu

4.you get



vi allow you to use the directional keys on keyboard (arrows, page up, page down). If you know the line number you want, the G command can help you. When used with line number, such as 20G, it takes you to desired line.

DELETING TEXT:-
There are three commands associated with the current location of the cursor:

     :       Deletes the current character
dw   :       Deletes the current word
dd    :       Deletes the current line

If you accidentally deletes something, than U command reverses the last command entered.

SEARCHING FOR TEXT:
Just start with a forward slash ( / ) followed by the word. For example;

/proceed
Cursor highlights the first place where the "proceed" is found in the file.

INSERT MODE:-
It is used to insert the text into a file. These are several ways to do this as follows:

      COMMAND
     ACTION
       COMMENT
           i
     Insert
 Everything type is inserted, at current position of cursor
           a
      Append
Everything type is inserted, starting one character after current cursor position.
           A
      Same as a
Closely related to a, where everything is inserted, starting at the end of the line with cursor.
           o
       Open
Everything type is inserted, starting one line below current position of the cursor. If "O" is used it insert , it starts one line above current position of cursor.

Execute Mode:-
Regular shell commands can be run from inside the vi editor.  Just type " :! " followed by the command.

For example;
If you are creating a cript, you will need to know the directory location of a certain file. You list the files in /etc/cron.daily directory as:-

:!ls /etc/cron.daily

Regular execute mode starts with colon ( : )
File management are associated with execute mode, including :q(to exit a file) and :w(to write current text to file)
If you think there is some problem in text you inserted, exit from vi without saving any changes, use the :q command.

Related posts:-

Command Combinations

Sometimes it is required to use more than one command in a line or simultaneously. For example:-

If you are using find command and result has a large number of files you can use grep command to search your desired file.

Let"s say we want to find .html files on our system. So we use

# find / -name *.html


It may show you hundreds of file flashing past you on your terminal screen. An alternative is


# find / -name *.html | grep bookmark


This command searches through result for text string "bookmark". Only those files with both strings are outpuut to the screen.

Similarly we can have


# who" grep mj
# ps aux " grep mozilla


The first command who, lists all users currently logged on Linux system. when you pipe(") the result to the grep mj command, you"ll find no. of times that user mj is currently logged on the system.

The second command, ps , lists the process currently running on Linux system. Three switches, aux(a dash is not required ps command switches) , leads very long lists of processes run by all users (a), each associated with username(u) , independent of virtual terminal(x). you need grep to search through these processes. This combined command returns all processes with the word mozilla.


Related Posts:-

Manipulating Files

Several commands are available that allow you to learn about and search for and through different files.

1. wc
The wc command is used to count the lines, words, and character in any text file.

# wc filename
 1914  9298  76066

These numbers corresponds to the number of lines, words and character in the text file.

      COMMAND       RESULT
 wc -l filename Number of lines in the file
 wc -w filename Number of words in the file
wc -c filename Number of characters in the file


2.find
The find command is used to find the file through directories and subdirectories. For eg; if you want the file named fig06.tif, you use the following command:

# find / -name fig06.tif

This command searches in the root directory and all the subdirectories for the fig06.tif file.

With the find command, you can also use the wildcards such as the asterisk(*) and question mark(?), in your search term.

3. locate and slocate
An alternative to the find command is the locate command. This command searches through the database of files. In Red Hat Linux, the locate command is actually soft-linked to more secure slocate command. The database is update per the /etc/cron.daily/slocate.cron script.
take a look at the default command at that script:-

 /usr/bin/updatedb -f "nfs,smbfs,ncpfs,proc,devpts" \
 -e "/tmp,/var/tmp,/usr/tmp,/aft,/net"


as we can see from the updatedb man page, the -f switch excludes a number of filesystem types, and -e switch excludes a number of directories that should be accessible to the root user.

Once database is located; it is more flexible; for example, if you use the following command it returns all the files that include the text string fig0:

# locate fig0

The locate command works as if the asterisks are assumed before and after the search terms.


4. grep
grep command is a easy way to search through a file. As a system administrator, you may have long lists of the users. If you want to search through your /etc/passwd file for a user named michael jang, try following command:

# grep "michael jang" /etc/passwd
mj:x:500:500:michael jang:/home/mj:/bin/bash

The response tells that there is a user named michael jang. It also includes the home directory and default shell for that user.
you can even use grep to search through a series of files with commands like:

# grep mj *
# grep  -c  bash  /etc/passwd

The first command looks for string mj in all files in current directory.
The second command, counts the number of lines that include the word bash.

Related Posts:-

Managing Files

 Linux includes a number of commands to help you read files in different ways. You can read files from top to bottom or vice-versa, you can be limited to few lines, you can count the lines, words, alphanumeric characters within a file, In addition linux lets you search through the files.

1. file
Although some distribution differentiate between file types by colour, but there are no standard extensions in Linux. Executable files don"t end in .exe similarly for documents file may not end in .doc . The file command lets you view type of each file.


2. cat
The concatenate(cat) command is used to read a file in linux. It sends the text of a file to standard output.

# cat file

It can even be used to read the multiple files, in sequence, with the
cat file1 file2 command


3. head and tail
The head file provides you with the view of the first few lines of the file; while the tail command provides you with the view of the last few lines of the same file. You can regulate the amount of the file that you see with switches. For eg:-

# head -n15 abc.txt
It provides you with the first 15 lines of abc.txt file similarly if you substitute head with tail it provides you with the last 15 lines of abc.txt file.


           COMMAND               RESULT
   head 400b abc.txtit shows the first 400 bytes of the file abc.txt
   tail 4k abc.txtit shows final 4kb of the file known as abc.txt
   head 3m abc.txtit shows first 3mb of the file abc.txt
   tail -n22it shows last 22lines of abc.txt

Note:- head command is generally used when you want to know what actually the file is about which can be interpreted by knowing first few lines but what the tail command is used you guess it and provide it in comments section than i will let you know.


4. more and less
The more and less commands used to view the contents of a text file.
The more command is to review the contents of the file one page at a time.
The less command is more versatile; unlike more, it allows you to scroll up and down any large file by using page up and down keys.

Because they can read text a little bit at a time, these commands can open file more quickly than text editor like vi.

# less -N bigfile



Related Posts:-

Setting up Files And Directories

Linux includes specific commands for creating and deleting directories, file management and so on:-

1. touch:-
touch command is simply used for creating a new file in Linux. It creates an empty file under the current directory. For eg:-

# touch filename 


2. cp
the cp command is used to copy the contents from source to destination. For eg:-

# cp file1 file2
It simply copies the content of file1 to file2. cp command can be used as shown in table


                  Command                      Result
              cp file1 file2copies the content of file1 to file2. The destination file has new creation date and inode number.
              cp file* Dir1copies multiple files to directory
              cp -f file1 file2if file named with file2 already exists, than it overwrites its content without prompting.
              cp -i file1 file2if file named with file2 already exists, this command prompts confirmation before it overwrites its contents.
              cp -p file1 file2copies content of file1 to file2. The destination file has the same creation date and inode number as the source file
              cp -r Dir1 Dir2copies contents of directory1 to directory2. The effect is recursive; i.e if there are subdirectories under the Dir1, there files and directories are also copied.
              cp -u file1 file2If you already have file named file2 and file1 is newer, this command overwrites its contents without prompting.

Note:- An inode is the identifier used on each Linux partition for a file. Every file gets its own inode. The inode includes metadata about the file, which includes the permissions, size, last access time, and its disk block were it is located. If inode is misaligned or corrupted it won"t be able to find the associated file. In addition, identical files have the same inode number.

3. mv
mv stands for move and if you want to rename a file in the linux you move it. The mv command changes the name of the file.


            Command               Result
          mv file1 file2changes the name of a file from file1 to file2.
          mv file* Dir1Moves multiple files to the directory.
          mv -i file1 file2if file named file2 already exists, this command prompts you for confirmation before overwriting this file.
         mv -f file1 file2if a file named file2 already exists, this command overwrites its contents without prompting.


4. rm
rm is used to remove files and directories. This is one reason that Linux Administrators are not advised to run Linux in root or superuser mode, because small mistakes in this command can easily remove all your Linux files.lets take an example

suppose you want to remove a group of temporary directories in your root(/) directory: a.tmp, b.tmp, c.tmp. You want to use rm -r *.tmp command, but instead you type the following:

#rm -r * .tmp

Because there is space between the * and .tmp, the shell assumes that you want to recursively delete all directories and then delete the file name .tmp. And surely you don"t want to delete  all your directories and corresponding files and data.

For this reason, Red Hat configures the following as an allias for the root user:

alias rm=" rm -i "

the alias ensures that whenever you want to use the rm command (even rm -r), the shell prompts you for confirmation before you delete any other file.

TIP:- you can find default aliases with the alias command
             rm file 1Deletes file1 without prompting for confirmation.
            rm -d Dir1Deletes Dir1 without prompting for confirmation.
            rm -i file1Deletes file1 after prompting for confirmation from the user
            rm -f file2If a file name file2 already exists, this command overwrites its contents without prompting.
            rm -r *Removes file recursively, if there are any subdirectories in the current directory, this commands deletes them(and all their files) as well

5. ln
In Linux instead of just copying or moving a file, you can link it. Links are common, especially for those programs which run at different runlevels. While you link a file, you are actually creating another path to a currently existing file. Lets take an example

Suppose you and your friend or colleague are working on a file named project, you can create a linked file in your home directory. assume project file is in /home/jm directory. To create link use following command:

#ln  /home/jm/project /home/mj/project


When you work on either file, the change and results are visible and accessible to those who access both the directories. This sometimes known as hard link.

Note:- The ln file1 file2 commands produces the same result as cp -p file1 file2

Another useful option for links is symbolic mode which allows you to see the linked file.

# ln -s  /home/jm/project  /home/mj/projectyou will see the linked file when you run a long listing (ls -l) of that file. This is known as a soft link.

6.mkdir
mkdir command is used to create directories. The directory that you create does not have to be based in your current directory for this you have to specify the path. You can also assign permission to the directory.

# mkdir -p Dir1/Dir2
Creates a directory named Dir2. Id Dir1 does not exist, the -p switch tells Linux to create that directory as well. Since path is not specified so both are created as subdirectories of the current directory.

# mkdir -m 755 /usr/Dir3
Creates a directory named Dir3 as a subdirectory in the /usr directory(here the path is specified). The permissions(755) are rwx for the owner and r-x for other members of the group of group and everyone else.

7. rmdir
This commands allows you to delete empty directories.

# rmdir -p Dir1/Dir3
This command deletes Dir3 if it is empty. If the only "file" in directory Dir1 is Dir3, this command also deletes directory Dir1.

Starting at the command line

Although the linux is provided with number of  integrated GUI tools, still the best way to control the linux is command line interface. Command line tools have more options than the GUI tools. Theyv are even faster than GUI.

Presently we are considering the commands working on Bourne Again Shell(bash). Commands are capable of creating, editing, deleting, navigating to directories and files and almost any part of the system can controlled with the help of the commmand line.

Considering the various commands available are:-

* pwd
It stands for "present working directory" and it tells the absolute path of your current directory.
for example
# pwd
/etc/httpd/conf the output tells you are currently in the  /etc/httpd/conf


*cd
cd is used for change directory command.

cd..
moves up one directory level
cd../..
moves up two directory level
cd /home/mj
navigates to the home directory of user mj
cd ~Navigates to your home directory. works for any user


Note:- Linux is case sensitive so remember all the small letters in commands represents the lower case letters.


ls:-
ls is a versatile command which is used to list all the directories and files under your current directory. It can be used to find permissions, size of a file, check ownership, differentiate b/w file types.


      Command
      Result
         ls
lists in alphabetical order all the non-hidden files in current directory
         ls -a
list all the files including the hidden files
         ls -r
lists in reverse alphabetical order all non-hidden files in current directory
         ls -F
Lists all files by type. The character at the end of each file indicates the file type.
         ls -i
Lists the files by inode numbers. An inode number represents the location of the file on a volume.
         ls -l
Lists all the files in current directory, including the current directory(.) and the parent directory(..). Also lists the size, owner, and permissions associated with each file in what is known as long listing format.
         ls -t
lists the files by the last time they were changed
         ls -u
lists the files by the last time they were accessed



Path Management:-
While describing the location of a file, either specify the absolute path or relative path. An absolute path describes the location of a file relative to the root(/) directory. For example, following command get to the scripts that start a number of Linux daemons

# cd /etc/rc.d/init.d
forward slash in front makes it a absolute path. this command can be started from anywhere in the Linux to get to this directory.

absolute and  relative paths apply to the commands as well. eg:-
# ls /etc/rc.d/init.d

However if you use the relative path, your current directory matters. eg:- if the output from the pwd command is /home/mj, the following command won"t work unless you have an /home/mj/etc/rc.d/init.d directory:-

# ls /etc/rc.d/init.d






 

Managing Shell And its Secrets

Subscribe To Get Update On Mobile

Subscribe
Get updates of latest post on your mobile free