Bash and Node commands (Basics)

  1. PWD

>> pwd

shows/displays the current working directory/base root directory(pwd-print working directory) (GUI-Graphical user interface)

  1. Navigating through the directory
  • >> cd

Change Directory(navigate (next) to the directory asked for)

  • >> cd ..

go to the previous directory(takes you back to the previous dir)

  • >> cd ../..

go to the 2 previous directories

  1. Folders Files
  • >> ls

Listing all the files/Folders(it basically shows all the files in the directory you are in)

  • >> mkdir

Make directory(creates a folder)

  • >> touch index.js

creates a new file with the file-type provided

  • >> cat index.txt

concatenate
to show & accessing the contents of the file/prints the contents

  1. Vi Command(if you understand this You are going great)
  • >> vi index.txt

editor is opened. To edit files directly from terminal
Press i -> INSERT mode is ON (You can edit now_
Press ESC+:+w+q+! -> To save the contents edited and exiting in vi
Press ESC -> to come out of INSERT mode
Press :q! -> to escape out of vi (just to get out the vi)

To come out of vi directly -> press esc+:+q+! altogether get back to terminal

  1. Moving Files
  • >> mv filename folder

    Moves file to the provided folder

  • >> mv folder1 folder2

    Move folder1 to folder2

  1. Copy Files
  • >> cp index.js folder1

    can copy file to folder simply

  • >> cp -r folder1 folder2

    Need to raise a flag(-r) to copy folders from 1 to 2

  1. Clear & Exit
  • >> clear

    Clears your terminal

  • >> exit

    Exit from the whole terminal

  1. More fun with ls command

    Just for fun to check, look and see files subfiles, directories

     #1. displays all files, folders in folder1(directory-folder)
     ls folder1
    
     #2. to check the last edited time by whom and file folder owner details
     ls -l
     #gives you
     #permissions no.-of-references owner-name group-owner size modified-day modified-time file/folder-that-modified
    
     #3. what exactly there in my dir
     ls -l folder1
     #gives you all the modified details of the files folders in the folder1
    
     #4. what r in sub-dir(files/fol inside the folders)
     ls -r folder1
    
     #5. last modified in the ls
     ls -t newdir
    
     #6. last modiefied in the ls with time
     ls -lt newdir
    
     #7. to get the modified details with the hidden files(denoted by .fName eg: .gitignore etc) sub-dir
     ls -la newdir
    
     #8. to get the modified details with the hidden files(denoted by .fName eg: .gitignore etc) sub-dir recursively
     ls -lRa newdir
    
     #9 you wanna check or see the reverse f what you did using timestamp based ordering
     ls -lr newdir
    
     #10. with size
     ls -s newdir
    

    to do more fun with ls ..,

     #1. recursively giveme all the look modifiys of the directoy
     ls -lR | grep .js
    
     #2 sub-dir
     ls -l newdir/folder1
    
     #3. ls command to search for a file by entering only 3-4 letter of it
     ls ind*
     #gives you the index.js index.html files as ind is searched in the present working direcory
    
     #4. to see dir from the prev directories
     ls ..
     #shows the files/folders from the parent dir
    
     #5. navigates to previous 3 dir
     cd ../../..
    
     #6.
    
  2. More fun with cat command

     #1. to enter into the editor and add some words chars etc
     cat > file.txt
     iuytrdfghj
     #file. ctrl + D to save and exit. ctrl + C to exit.
     #press ctrl+D twice to exit from edit contents of cat
     #prints iuytrdfghj
     # this is just tempporarily now if you again do cat > file.txt it opens as new
    
  3. More Fun with mv command

    #1. move and change filename taht is moved consider style.css is initial name but in moved folder i want it to be as index.css
    mv style.css folder1/index.css
    
  4. More fun with cp command

    #1. TO copy a folder from fol1 to fol2 (it cannot copy it displays not copied bcoz its dir hence in that case put -r
    cp -r fol1/fol fol2
    #now you can see fol in fol1 & fol2 its copied
    
  5. To wipe/delete file or folder

rm -r fol2
#deletes fol2 folder
rm -r file.txt
#deletes file
  1. ls -l Permissions details

generally look as r--rw-xr in ls -l command above.
they are nothing but read write denoted by r, w, rw.
0. chmod
1. setting permissions for whom u-users, g-group, o-other.
2. adding or removing
3. read-r, write-w, readwrite-rw,x-execute
4. for folder do - before read write but for file nothing

#1. setting the permissions
chmod u+x index.sh
#it means we are adding(+) execute(x) permission to use(u) of file(index.sh) it means setting permission

31. chmod ugo-rwx ---------------------------To add permission to a file.
31. chmod -R ugo-rwx ------------------------To add permission to a folder.
32. chmod u+x filename
33. chmod g+wx filename
34. chmod u-x filename
1-x, 2-w, 4-r
35. chmod 664 foldername

now numeric permissions in this: only permitting group(not noted)

  1. Echo command

    $ echo "12345kjhgf"
    12345kjhgf
    
    $ echo "hello miss "hi" yeah"
    hello miss hi yeah
    
    $ echo 'hiiii'
    hiiii
    
  2. Head Tail commands

    $ head file.txt
    #displays first 10 rows of the file
    
    $ tail file.txt
    #displays last 10 rows of the file
    
    $ head -20 file.txt
    #specific set of rows
    #first 20 rows displayed
    
    $ tail -n +25 filename | head -n +5
    #from 15 to 19 view rows
    #pipe operator
    #command 1 | command 2
    # uses output of command 1 and goestail
    
  3. word count & history

    $ wc file.txt
    $ history 
    #to view all the command that i've used.
    
  4. grep command

$ grep "one" filename 
#where "one" has been used in the file.
$ grep "one" filename | wc -l 
#how many times "one" has been used in the file.
$ grep -c "one" filename 
#how many times "one" has been used in the file.
$ grep -h "one" filename 
#where "one" has been used in the file. (case sensitive)
$ grep -hi "one" filename 
#where "one" has been used in the file. (not case sensitive)
$ grep -hir "one" directoryname 
#where "one" has been used in the folder.
$ grep -hin "one" filename 
#where "one" has been used in the file inc line numbers. (not case sensitive)
$ grep -hinw "one" filename 
#where "one" has been used inside a word also individually. {colone, one, One} (case sensitive)
$ grep -o "one" filename 
#only gives us the matched part.
$ grep -w "one" filename 
#where "one" has been used in the file.
$ grep "ERROR" filename 
#will view all the error messages in that file.
$ grep -v "INFO" filename
$ grep -A 5 ERROR filename 
#to view rows after the occurance of ERROR text in a file
$ grep -B 5 ERROR filename 
#to view rows before the occurance of ERROR text in a file
$ grep -C 5 ERROR filename 
#to view rows before and after the occurance of ERROR text in a file.
  1. Sed Command

    $ sed -n '/ERROR/ p' filename 
    #to print lines with ERROR text.
    $ sed 's/ERROR/CRITICAL' filename 
    #Replace ERROR with CRITICAL in the file.
    $ sed -ibackup 's/ERROR/CRITICAL/' filename 
    #Create a backup of the file.
    $ sed '3 s/CRITICAL/VERYCRITICAL/' filename 
    #Replace CRITICAL with VERYCRITICAL in line number 3.
    $ sed '3,5 s/ERROR/CRITICAL/' filename 
    #Replace CRITICAL with VERYCRITICAL in line number 3 to line number 5.
    $ sed -n '3,/ERROR/ p' filename
    
  2. awk command

    $ awk '/ERROR/{print $0}' filename 
    #to print lines with ERROR text.
    $ awk '{gsub(/ERROR/, "CRITICAL")}{print}' filename
    #Replace ERROR with CRITICAL in the file.
    $ awk 'BEGIN {print "LOG SUMMARY\n--------------"} {print} END {print "--------------\nEND OF LOG SUMMARY"}' filename 
    #add text in the beginning and ending of a file.
    $ awk '{print $1, $2}' filename 
    #Print 1st and the 2nd column of the data (file).
    $ awk -F "," '{print $1, $2}' filename
    $ awk '{count[$2]++} END {print count["ERROR]}' filename 
    #Count the occurance of ERROR in second column of the file.
    $ awk '{ if ($1 > 1598863888 ) {print $0} }' log.txt 
    #view the rows after 1598863888 in first column.
    
  1. >> node

    if you see welcome!---
    Your Node got Installed
    This command opens Node shell and to exit node shell press ctrl+c twice then you will get back to bash/git Terminal

     $ which npm
     /usr/bin/npm
     $ npm --version
     6.13.7
    
  2. >> node index.js

    compiles & Runs the JS file and prints the contents

  3. >> nvm

    Node Version Manager

  4. >> npm
    this npm command gives the list of commands you can have with it
    Node Package Manager - manages packages

    NPM Registry - collection of all external packages

  5. >> npm install express

    Installs express package from external to your local machine and you may see package.json, nodeModule files

    for all the packages and names guide, see on: https:/npmjs.com/package

To run the packages from that URL to on your local machine >> npm install name-of-lib

  1. >> node package m

    same often