Sunday, 19 June 2016

Commands

Navigating Linux

Use these commands to help navigate through the console.

Firstly we will look at how to view the contents of a current directory, using the following command:



ls

Shows the contents of the current directory. This command can be modified to be more useful by adding any of the following onto the end of it:


-l = [long] - shows extended file information 

-a = [all] - shows all files including hidden
-la = [long and all]


You can also use the ls command to show you the contents of a specific directory, other than the current directory, using the following:

ls /directoryname



Moving Directories

In order to move directories within the shell we can use any of the following commands 

cd = [Change directory] Used to change directories, usually followed by one of the below:

.. = Moves to/uses the parent directory (up one)
. = Move to/use the current directory 
~ = Move to/use the home directory
- = Move to/use the previous directory 
directoryname = Move to/use the specified directory

Finally in order to locate where you are currently within the directories use the following command:

pwd = show current location 


Files Within Linux


cat filename = dump whole file content onto screen 


cat > filename = create file 


cat >> filename = append (add onto current file) 


more filename = show file page by page 


man = manual 


mkdir dirname= make a directory with this filename 


file filename = show type of file 


who = find who is on the system 


date = system date/time 


sort = sort 


sort > filename = redirect stdout to a particular file 


sort < filename = sort a stdout 


Ctrl - D = EOF 


cp - copy 


-r - recursively 

/dir/* - all files in given dir 
~ - to current directory 
cp currentfilename nameofcopy = create a copy of a file under a given name 


Vi 


vi filename - open file in Vi 

i - insert at current 
I - at beginning of line 
a - insert at next 
A - at end of line 
o - new line after 
O - new line before 
s - overwrite 
S - overwrite entire line 
cw - overwrite word from point 
c$ - overwrite rest of line from point 

u - undo 

x - backspace 
dw - delete word 
dd - delete line 
D - delete line after point 
Y - yank (copy line) 
p - insert pasted line under current line 

5x - backspaces 

3dd - delete three lines 
5Y - yank 5 lines 
2p - paste twice 

0 - paragraph 

$ - end of line 
w - next word 
G - end of paragraph 

/word - first occurrence of word 

?word - last occurrence of word 
n - to next occurrence 

:s/word1/word2/ - search and replace on line 

:%s/word1/word2/ - search and replace all lines 

:w - save without leaving 

:w filename - save as new name 
:wq - save and exit 
:q! - exit without saving 


:1,4w file - write lines 1-4 to a file of a set name 


:r file - read a file to current 


:e! - revert to previous version 


chmod +x filename = make file executable 


rm filename – remove file 


#!/bin/bash - indicate interpreter 


echo "The current year is `(date +"%Y")`" 

echo "The current day is `(date +"%d")`" 
echo "The current month is `(date +"%m")`" 
echo "The current date is `(date +"%d")`" 
echo "The current hour is `(date +"%H")`" 
echo "The current minute is `(date +"%M")`" 
echo "The current second is `(date +"%S")`" 


`date` - displays the current date and time. Note the backwards apostrophes.


cp ./originalfile newfile - makes a copy of a file





Shell Scripting:

  1. Create a script using a text editor, named name.sh
  2. Begin the script with "Shebang"
    1. #!/bin/bash
  3. Consecutive lines can be used to "echo" strings
    1. echo "stringblahblah"
    2. echo "something $1" - will echo first variable when executed as name.sh variable
  4. Make the script executable
    1. chmod +x name.sh
  5. Run the script, ensuring to direct the PATH to the current directory.
    1. ./script.sh
    2. ~/script.sh
    3. /home/UICT/yourid/script.sh