Basic Linux Commands







This Blog will familiarize you with the basic linux commands. The practice section in the bottom of this page will explain how to use it in the simplest way


Explanations for commands
cd  /
Go to root folder 
open /
open root folder
folder Structure
Root —> User —> Current (home directory) bkrishnankutty  {root is MatichoshHd}
ls
ls :- list all the files and folders expect hidden files. ls -a command is used to see all files including hidden files . ls is the command and -a is the option . ‘.’ file names proceeded with dot are hidden
ls -d */ and ls -d */*/  :- List all the directories and subdirectories in a folder
. and ..
. means current directory cd .    And  .. means parent directory cd ..
~
Home directories can also be referred to by the tilde ~ character.  try  ls ~/..  and  ls ~/.
copy file
cp /Users/bkrishnankutty/protractor-design.png .  Don't forget the dot (.) at the end. Remember, in UNIX, the dot means the current directory


Listing files and directories
ls
list files and directories
ls -a
list all files and directories
mkdir
make a directory
cd directory
change to named directory
cd
change to home-directory
cd ~
change to home-directory
cd ..
change to parent directory
pwd
display the path of the current directory

Vim
vim filename
 Open a text file with Vim editor. When you open file, it will be in cmd line mode
Insert line mode
 i 
Last line mode  
 :   {v -visual mode <vj combination for select>}
exit insert mode
esc   {Command line —>Insert mode —> Last line —> Command mode}
New Tab in Vim
  • :tabnew filename or :tabn filename - open a file in a new tab
Close the Current Tab
  • :tabclose or :tabc - close the current tab and all its windows
 undo &  redo
  • u - undo   / Ctrl + r - redo
Cursor movement 
h -left , l - right , j- up and k - down
Existing
  • :w - write (save) the file, but don't exit
  • :wq or :x or ZZ - write (save) and quit
  • :q - quit (fails if there are unsaved changes)
  • :q! or ZQ - quit and throw away unsaved changes

  • :ls - list all open buffers
  • :bd - delete a buffer (close a file)
  • :e filename - edit a file in a new buffer
  • :bnext or :bn - go to the next buffer
  • :bp - go to the previous buffer
copy :-yy, 2yy
delete/cut :-dd,2dd
paste :p, 2p or put




Files and Directories
cp file1 file2
copy file1 and call it file2 .cp /vol/examples/tutorial/science.txt .
mv file1 file2
move or rename file1 to file2  mv science.bak backups/.
rm file
remove a file rm tempfile.txt 
rmdir directory
remove a directory rm -r tempdir
cat file
display a file  cat science.txt
more file
display a file a page at a time less science.txt   <space bar>
head file
display the first few lines of a file head -5 science.txt  <default 10>
tail file
display the last few lines of a file tail -5 science.txt <default 15>
grep 'keyword' file
search a file for keywords   grep Science science.txt
-v display those lines that do NOT match 
-n precede each maching line with the line number 
-c print only the total count of matched lines 
-i ignore case 
grep -ivc ‘science top' science.txt
wc file
count number of lines/words/characters in file

 

Redirection   or Outputs
command > file
redirect standard output to a file  cat > list1 
command >> file
append standard output to a file  cat >> list1
command < file
redirect standard input from a file  sort < biglist > slist
command1 | command2
pipe the output of command1 to the input of command2
cat file1 file2 > file0
concatenate file1 and file2 to file0  cat list1 list2 > biglist
sort
sort data sort < biglist > slist
who
list users currently logged in
a2ps -Pprinter textfile
print text file to named printer
lpr -Pprinter psfile
print postscript file to named printer


Little Theory

1) What is a Linux kernel
2) What is a shell  :-  http://prntscr.com/bt2kue
3) Everything in UNIX is either a file or a process.
4) What is a file and Process
5) Directory structure of unix :- All the files are grouped together in the directory structure. The file-system is arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called root.


Practical

*Problem Statement 


  1. Make to directory "Google" 
  2. Make 2 sub directory 'TestA' and 'TestB'
  3. Go to TestA and create files 'FileA.txt' and 'FileB.txt'
  4. use Vim and copy paste Just data into file 'FileA.txt'
  5. Copy contents of 'FileA.txt' to directory 'TestB'
  6. Rename File in 'FileA.txt' to 'TestOnly.txt'
  7. search for String say"Object" in  file 'TestOnly' and output to a new file 'searchResult'
  8. Display contents of a file 'TestOnly'
  9. Display contents of a file 'TestOnly' and view as pages
  10. Display first 2 pages of a file 'TestOnly'
  11. Display last 2 pages of a file 'TestOnly'
  12. get the count of characters in the file   and the line number 'TestOnly'
  13. Create a file with 5 students and sort it using linux commands
  14. Remove file 'TestOnly'
  15. Remove folder 'TestB'
  16. Find a directory or file in your machine
  17. Find a Applications Process ID
  18. Kill an Application 
  19. To get a more complete picture of the processes on this system, run the following
  20. Easiest way to find out what processes are running on your server is to run the command
Solutions

#Make to directory "Google"  and Go to "Google"

LM-ip-19906554:~ Myroot mkdir GoogleLM-ip-19906554:~ Myroot cd Google

#Make 2 sub directory 'TestA' and 'TestB'

LM-ip-19906554:Google Myroot mkdir TestA TestBLM-ip-19906554:Google Myroot lsTestA TestB

#Go to TestA and create files 'FileA.txt' and 'FileB.txt

LM-ip-19906554:Google Myroot cd TestALM-ip-19906554:TestA Myroot touch FileA.txt fileB.txt

LM-ip-19906554:TestA Myroot lsFileA.txt  fileB.txt

#use Vim and copy paste Just data into file 'FileA.txt'

LM-ip-19906554:TestA Myroot vim FileA1 files to edit

#Copy contents of 'FileA.txt' to directory 'TestB'

LM-ip-19906554:TestA Myroot cp FileA.txt /Users/balkrishnankutty/Google/TestBLM-ip-19906554:TestA Myroot cd ..LM-ip-19906554:Google Myroot cd TestB

#Rename File in 'FileA.txt' to 'TestOnly.txt'

LM-ip-19906554:TestB Myroot mv FileA.txt TestOnly.txtLM-ip-19906554:TestB Myroot lsTestOnly.txt

#Search for String say"firebase" in  file 'TestOnly' and output to a new file 'searchResult'

LM-ip-19906554:TestB Myroot grep 'firebase' TestOnly.txt >> SearchResultLM-ip-19906554:TestB Myroot lsSearchResult TestOnly.txt

#Get the count of characters in the file   and the line number 'TestOnly'

LM-ip-19906554:TestB Myroot wc SearchResult LM-ip-19906554:TestB Myroot sort SearchResult >> Sorted.txt

#Display contents of a file 'Sorted.txt'

LM-ip-19906554:TestB Myroot cat Sorted.txt 

#Display contents of a file 'TestOnly' and view as pages

LM-ip-19906554:TestB Myroot more Sorted.txt 

#Display first 2 pages of a file 'TestOnly'

LM-ip-19906554:TestB Myroot head -2 Sorted.txt 

#Display last 2 pages of a file 'TestOnly'

LM-ip-19906554:TestB Myroot tail -2 Sorted.txt 

#Difference between 2 files

LM-ip-19906554:TestB Myroot vim -d SearchResult Sorted.txt 

#Create a file with 5 students and sort it using linux commands
LM-ip-19906554:TestB Myroot touch student.txtLM-ip-19906554:TestB Myroot vim student.txt LM-ip-19906554:TestB Myroot sort student.txt >> sortedStudentLM-ip-19906554:TestB Myroot vim -d student.txt sortedStudent 

#Remove file 'TestOnly'
LM-ip-19906554:TestB Myroot rm -f Sorted.txt LM-ip-19906554:TestB Myroot cd ..

#Remove folder 'TestB'
LM-ip-19906554:Google Myroot rm -r TestALM-ip-19906554:Google Myroot lsTestB

#Find a directory or file in your machine :-  find . -iname "*.mp3"| sort

#Find a Applications Process ID :-  pgrep firefox

#Kill an Application :- Kill Pid

#To get a more complete picture of the processes on this system, run the following :- ps aux

#Easiest way to find out what processes are running on your server is to run the command:-top


No comments:

Post a Comment