Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Saturday, September 19, 2015

Linux Bash Shell Tutorial: How to manage background and foreground jobs in Linux terminal.




In this post, I'm going to share on how to manage background and foreground jobs in Linux terminal. Before I go into details, lets look at some  jobs basic terminology .

Jobs are processes that running in a particular shell .Many  jobs can run in the shell at once.Only one job can be active in the terminal at once .However  many jobs can be executed in the background which known as Background Job.The active job in the current terminal is call Foreground Job

Tutorial Content List

Running jobs in background .
Listing Jobs: Listing your jobs.
Suspending Jobs: Suspending a job to do something else.
Background Jobs: Having jobs work in the background.
Foreground Jobs: Putting jobs in the foreground.
Terminating Jobs: Stopping jobs before they're finished.


1)Running jobs in the background .

To run command in the background run 



Running jobs in the background

Once you run the job in the background, the terminal will assign a job id and a process number respectively. job id is the number in the bracket[] followed with process number


2)Listing Jobs.


run "jobs"  command to check the jobs status.



jobs status


NOTE:  the plus(+) sign shows  which is the latest job executed in the terminal and the minus(-) sign shows the second latest job that was executed in the terminal.you can access the specific job by referring the job  id .


3)Suspending Jobs.

To suspend active job in the terminal, press "CTRL-Z"


CTRL-Z

Once suspended, the job status will show  as "stopped" .  Now you you can choose to run the job either in the foreground or in terminal background.

4)Background Jobs .

To put the recent executed job in the background, run "bg" command. This command will put the last executed job in the background  (in other words the job with the plus(+) sign .


Background jobs
If you want to run specific job id in the background, use "bg" command followed with  "%"  and the job id number .



Running job id 1 in the background
5)Foreground Jobs.

To put the recent executed job in the foreground, run "fg" command. This command will put the last executed job in foreground  (in other words the job with the plus(+) sign .


Foreground jobs


On executing the command, job with id 3 is shown in the terminal as it was the latest job executed in the terminal.

to put a specific job id on the terminal, use "fg" command followed with  "%" and the job id . 



specific job id in the foreground


5)Terminating Jobs.
To stop jobs before they finish executing,  press "CTRL -C"

CTRL-C

Or you can also use "kill" command to terminate the job



kill the job












Read Previous : Linux Bash Shell Tutoria : How to use pipeline and filters in Linux Terminal. 8 Tips and Tricks

Tuesday, June 16, 2015

Linux Bash Shell Tutorial :How to use Pipeline and filters . 8 tips and tricks



  In this post, I will share  8 tips that you can use to manage  terminal data using pipeline and filters.

1) less Command

less will allow you to quickly browse any file section by section in the terminal (standard output).



 In above example,when I piped "lshw" command to "less" command, the output is display section by section. you can use space bar or arrow sign to  move to next section of  the file to exit you can press "q"  

2) Sort command 
sort command will sort the output of the first command.


  In above example, "lsusb" command list the usb devices connected to my machine. once I pipe it to sort command ,the usb devices is sorted  accordingly.

3) grep command

grep command will capture certain file pattern and  give the output to the screen


  In above example,i want to search for hardware with the specific word  "Intel" on it.  "lshw" command will list all the hardware on my computer .By pipe it to "grep" command, it will filter the output to only show  the line with Intel specific word in it.

4) wc command

 wc is a short form of "word count" ."wc" command will display the total line, word and byte count of a file .

  
   In above example, I list usb devices on my computer and filter it through "wc" command. The output of this command will give some numbers referring to line,words and bytes . -l will give the total lines , -m will give the total bytes and -w will give the total words in the file.

5) head command

"head" command will print the first few line that we specified in the terminal.

  
   In above example, I want to display first 4 line of "lsusb" result.to do this I pipe "lsusb" command by using head -(dash) n followed with number 4 .

6) tail command

"tail" command will print the last few line that we specified in the terminal.
 


To display few lines from the bottom of standard output,you can use tail command to do so.  pipe  lsusb command  to tail command followed with -n and followed with number of lines that you want to display. in this example, I display 3 bottom lines from lsusb output.


7) tee command

 "tee" program reads standard input and copies it to both standard output (by allowing the data to continue run down in the pipeline) and to one or more files. This is useful for capturing a pipeline’s contents at an intermediate stage of processing


  In above example, I run "lshw" command to list my computer hardware.
tee command  save lshw output to lshw.txt file And at the same time the output is send to "grep" command to look for intel word pattern, this command work as a T junction in our terminal.



8) uniq command

  "uniq" command is often used in conjunction with sort . "uniq" accepts a
sorted list of data from either standard input or a single filename argument and will remove any duplicate information.



   In above example, I list both files in user binary folder and binary folder.
by pipe it through "uniq" command, it will remove all the duplicate files between these two folder.

 If you want to see the list of duplicates file instead, you can  add -d option to "uniq" command




Read Previous : Linux Bash Shell Tutorial : How to shorten command in Linux Terminal

Friday, June 12, 2015

Linux Bash Shell Tutorial :How to shorten command in Linux terminal

  Using long command  can be a pain especially when it requires us to type every single time and do minor changes in the terminal.We can shorten our Linux command by using a command call "alias" .

This command will let you assign any character combination to a specific command.

orca@Positive-Space:~$ alias chg='cd ..'                                                                   



You can also use numbers to assign the command




Read Next : Linux Bash Shell Tutorial: How to use Pipeline and Filters . 8 Tips and Tricks

Friday, May 8, 2015

Linux Bash Shell Tutorial : How to use I/O redirection.5 tips and tricks


  Understanding Input Output or I/O redirection works in Linux helps us streamline our job better when working with the terminal. In this tutorial,I will share quick tips and tricks on how to use I/O redirection in our system.

A few terminology that will help you understand this tutorial is as below:



Standard Input -what is being type in terminal from keyboard.

Standard output -text that display in terminal on command execution
Standard  error – text that display in terminal when a command runs and exit with an error

Content List

1)Redirecting standard output to a file.
2)Redirecting a file to standard input.
3) Redirecting standard error to a file.
4)Redirecting Standard output and error to one file
5) Disposing unwanted output -/dev/null (bit bucket)


1)Redirecting standard output to a file.


Use “>” operator sign to redirect and save output file in current directory .




To append/update existing file use “>>”




2)Redirecting a file to standard input.

Use  “<” operator to redirect an input to the terminal


In above example, I write new content to file stdin and  redirect stdin file to the terminal  using "<" sign operator.

3) Redirecting standard error to a file.

Use “2>” operator sign to redirect standard error to a new file in current directory.


To append/update existing file use “2>>”



4)Redirecting Standard output and error to one file

Use “&>” operator sign to redirect standard output and standard error to a  new file in current directory .


To append/update existing file use “&>>”


file stdouterr
5) Disposing unwanted output -/dev/null (bit bucket)

Use “2> /dev/null” operator sign to redirect unwanted  output to bit bucket. Data  that are redirect to bit bucket will be deleted without a trace. As such some people also call bit bucket as a black hole.



*NOTE:If you execute the same command using  ">" ,"<" ,"2>" ,"&>"  to same file name but with different content, the file will be overwritten with new file content .

Thursday, May 7, 2015

Linux Bash Shell Tutorial : How to change user terminal password in Ubuntu using command line and GUI



 Changing terminal password is an important  part in managing a Linux system. There will be a time when you want to change terminal password to something more secure or reliable. This tutorial will  guide you on how to change user terminal password using either the terminal command or Ubuntu GUI.

1)Change password from the Terminal

1.1) Run in terminal "passwd" command
orca@Positive-Space:~$ passwd                                                                                                                                        


On running the command, you  will be prompted to enter your new password. Enter your new password to confirm. Your password will be updated  straight away .


2)Change password from Ubuntu GUI

2.1) Search user accounts from Ubuntu dash panel
 Search for User accounts setting panel .


2.2)Click  password panel to change the password.


2.3)Change your password and click "Change"



Logout from the system and  test your new password !! :-)

Thursday, April 16, 2015

Linux Bash Shell Tutorial :How to save terminal session typescript or logfile in Linux




   Sometime as a beginner, intermediate or expert system admin, we need to keep a record on what we did to the system whenever we faced with system problems. One way  we can do this is by recording the Linux terminal session.

 We can save terminal session in Linux by using the built in "script" command.  To record any  terminal session type in script command and followed with the file name that you want.

orca@Positive-Space:~$ script log.01012015                                               

  I make it a habit to include the date in all my logs file name for future reference. Once you entered the command,  the terminal will save everything that you type in the terminal and will save it in current directory location. In this example, "log.01012015" file will be saved in  "user home" directory.

saving terminal session

To stop the terminal saving,you can use exit command .

orca@Positive-Space:~$ exit                                                                     

stopping terminal saving



The saved file can be viewed using any text file editor.

Friday, September 26, 2014

How to make sudo password display as asterisk in Linux terminal



  
    In this tutorial, I'm going to share on how to make password display as asterisk in the terminal.Normally when we type our password in the terminal, nothing is display. This simple tips will help you not to delete all the characters when you misspell your sudo password. :)

      To do this , you need to edit sudoer file in /etc directory. Please make sure that you  use visudo command to edit this file. visudo command has  the function to edit the file and checking it syntax upon saving. You will not want to mess with this file as if something goes wrong, you will having problem in getting root privilege.

Steps

1)Run Visudo command to edit  /etc/sudoers file

Run visudo command

2)Edit the file and save.

2.1 ) Add "Defaults" and "pwfeedback" to the file 



2.2) Press "Ctrl -X" and press "Y" to save the file .

3)Exit current terminal and open new one for it to take effect.

run any sudo command and see the asterisk for password


Saturday, February 15, 2014

How to use "ls" Bash command . 10 tips and tricks



  
   In this post, I'm going to share practical tips on how to use "ls" command . These command I found useful when dealing with Linux environment . You can mix up the command base on it's function as how I show in the video

Content List 

1. List all entries -a
2. List all details file and directories -l
3. List one file per line -1
4. List file size in human readable form -h
5. List directory details -ld
6. List file base on last modified time -lt
7. List file  base on reverse  last modified time -ltr
8. List file directory recursively -R
9 . List file inode -i
10 . List  file alphabetically by extension


1. List all entries "ls -a"

ls -a

2. List all details file and directories  "ls -l"

ls -l

3. List one file per line "ls -1"
ls -1


4. List file size in human readable form  "ls -h"

ls -lh



5. List directory details  "ls -ld"

ls -ld


6. List file base on last modified time "ls -lt"

ls -lt

7. List file  base on reverse  last modified time  "ls -ltr"

ls -ltr

8. List file directory recursively  "ls -R"

ls -R

9 . List file i-node  "ls -i"

i-node or index node is a data structure  base on Unix style filesystem .Each i-node stores the attributes and disk block locations of the filesystem object's data. For more information please visit here .

ls -i

10 . List  file alphabetically by extension "ls -X"

ls -X




Read Previous :How to use rsync .8 tips and tricks
Read Next:How to resize image file on Ubuntu (easy)