Affichage des articles dont le libellé est bash. Afficher tous les articles
Affichage des articles dont le libellé est bash. Afficher tous les articles

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


~ mardi 16 juin 2015 0 commentaires

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 change 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


~ vendredi 12 juin 2015 0 commentaires

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 .


~ vendredi 8 mai 2015 0 commentaires

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 !! :-)


~ jeudi 7 mai 2015 0 commentaires

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.

~ jeudi 16 avril 2015 0 commentaires

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



~ vendredi 26 septembre 2014 0 commentaires

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)


~ samedi 15 février 2014 0 commentaires

How to use Vox for basic system function,remote VNC Raspberry Pi login and others ( Vox part 2 script)




 This post show How to use Vox for controlling system basic function using voice recognition .It is a continuity from my previous post    .At this moment, The program consist of 3 main script.There will be  a more function added to this program in the future .

Script

1.vox (main script)
2.vrec (recording script. will use more for future development)
3.vsort ( return text integrate with local system script )


Script details

1.Vox script(main script)



1.1.Variable declaration and changing directory working directory to ~/vox-master .

URL="http://www.google.com/speech-api/v1/recognize?lang=en-us&client=chromium&maxresults=10" 


 cd ~/vox-master

If you use different language than english, you need to  change "en-us" in the above script..Check here for language reference. Notice the "&maxresults=10" line . What this line do is, it request for maximum 10 results from Google server.


1.2. Calling the recording program.

./vrec

Currently, the program can only record for 3 seconds  .You can remove this line and just  use          " rec -r 16000 -b 16 voice.flac trim 0 3 " instead .The recorded file will be save as "voice.flac"

1.3 "voice.flac" will then be send over to Google voice search engine and it will feedback a text message to our system . The retrieve result from Google is then pipe into a file call "result" .I use "result" information  to custom  my "vsort"file .

wget -q -U "Ninetailfox" --post-file voice.flac --header "Content-Type: audio/x-flac; rate=16000" -O - "$URL" >result

The detail information of the above command is much more easier to understand with snapshot from Wireshark . Click here to download vox wireshark file .

Vox Wireshark snapshot



1.4 Sorting out the return result and send it over to "vsort" script. After vsort file being execute, The program will wait for 5 second delay before executing .


RETURN="$(cat result | cut -d\" -f12 )"
echo "you said :$RETURN"
./vsort "$RETURN"
sleep 5

2 Vrec script .
At this moment, Vrec only consist of one line .I'll be using this file more in the future development.


3.Vsort script


What this script does is it associate the word that has been retrieve from Google server and use it to execute a command  that is associated with your system. You need to make sure what kind of word is use and what command you want  to execute . You need to edit this file to make it work with your system.



In the existing script ,

case "$1" in
 'ping server') 
 cd /home/shark_attack/Programming/bash
 ./pi -ping 
 ;;

'ping server' is the word I choose and I want it to execute my "./pi" script  in  "/home/shark_attack/Programming/bash" . Click here to check my previous post on my pi script .

I will update more function on vox in the future . At the moment, I'm testing some other stuff and playing around with some code. That's it for now.Please leave comment on the box below .





~ vendredi 31 janvier 2014 0 commentaires

How to use Voice for basic system control function,Raspberry pi remote VNC server login etc (Vox install and use)




   A few days ago I played around with Google voice search and wonder if  Google has this good program that can detect voice and return their search result, I can use this function to make use on my system . Instead of  writing big chunk program with dictionary stuff to do some basic voice recognition function,I can make use Google server voice recognition service to return the text and execute it on my system. Once I start to work around the script, I realize there is heaps of function I can integrate with this script. This can be a good program by itself.

  In this post , I'm going to share how you can use Google voice recognition to do simple task with your system . The idea behind this program is  using your recorded voice,send it to Google server and it will  translate the voice to text . The translated text will be use as a control structure to run basic command for your system .

What do you need for this project?

1. Internet connection
2. Built in or external mic
3. Any Linux Distro (Bash shell)
5. A little bit of bash scripting knowledge

Steps

1. Download the source code zip file. Extract it on your "home" folder . Please click here

Downloading Vox


2.Run the "setup" file
shark_attack@Positive-Space:~$ cd vox-master
shark_attack@Positive-Space:~/vox-master$ sudo ./setup   
#installing Vox dependencies and run it                

3.Set your keyboard shortcut .Open your keyboard setting . Since I'm running on Ubuntu 12.04 , below is snapshot how to do this. If you are running on different Linux Distro, you can try to look around in your system .

3.1. Search for Keyboard setting
Search for Keyboard setting

3.2 Click "Shortuct>Custom Shortcut" and click the '+' sign . Give it a name .You can use your own name if you want to :)

add new shortcut

3.3 In the command box . add below command in the shortcut box

gnome-terminal -e /usr/local/bin/vox

edit the command part

3.4 Click on the right side and give it a shortcut that you want . In my case, I use "CTRL -M"
 
choose your shortcut

4. Edit "vsort" file to use it with your  system setting.The voice recognition is not 100% accurate because of different dictation between people and how Google server feedback the result to our system.For that reason ,I save 10 utterance  that being detected by Google in the "result" file . You can use the result uttterence word by editing in  the "vsort" file .I will share the detail script drill down information in the next post .Stay tune .

5.Next test and play around with it. You can run it from the terminal or use the shortcut key you  have set .
shark_attack@Positive-Space:~$ vox

The program should work with basic file searching using your own voice.This program is still under testing phase and I will update more function to vox.Anyone interested with this project  or have cool idea what to include in are welcome to contribute.Please visit my Github page.



Read Previous :How to convert video file type with robot voice feedback using bash script on Linux
Read Next :How to use Vox for basic system control,remote VNC Raspberry Pi login and others (Vox part 2)

~ 0 commentaires