Tuesday, 28 February 2017

BASIC UNIX COMMANDS

1. tty - reveals the current terminal
2. whoami - reveals the currently logged-in user
3. which - reveals where in the search path a program is located
4. echo - prints to the screen
 a. echo $PATH - dumps the current path to STDOUT
 b. echo $PWD - dumps ths contents of the $PWD variable
 c. echo $OLDPWD - dumps the most recently visited directory

5. clear - clears the screen or terminal
6. history - reveals your command history
 a. command history is maintained on a per-user basis via:
  ~/.sh_history
~ = users's $HOME directory
7. pwd - prints the working directory
8. cd - changes directory to desired directory
 a. 'cd ' with no options changes to the $HOME directory
 b. 'cd ~' changes to the $HOME directory
 c. 'cd /' changes to the root of the file system
 d. 'cd ..' changes us one-level up in the directory tree
 e. 'cd ../..' changes us two-levels up in the directory tree

9. ls - lists files and directories
 a. ls / - lists the contents of the '/' mount point
 b. ls -l - lists the contents of a directory in long format:
 Includes: permissions, links, ownership, size, date, name
 c. ls -ld /etc - lists properties of the directory '/etc', NOT the contents of '/etc'
 d. ls -ltr - sorts chronologically from older to newer (bottom)
 e. ls -a - reveals hidden files. e.g. '.sh_history'
Note: files/directories prefixed with '.' are hidden. e.g. '.sh_history'

10. cat - catenates files
 a. cat 123.txt - dumps the contents of '123.txt' to STDOUT
 b. cat 123.txt 456.txt dumps both files to STDOUT
 c. cat 123.txt 456.txt > 123456.txt - creates new catenated file

11. mkdir - creates a new directory
 a. mkdir test - creates a 'test' directory

12. cp - copies files
 a. cp 123.txt test/

13. mv - moves files
 a. mv 123456.txt test/ - moves the file, preserving timestamp

14. rm - removes files/directories
 a. rm 123.txt
 b. rm -rf 456.txt - removes recursively and enforces

15. touch - creates blank file/updates timestamp
 a. touch test.txt - will create a zero-byte file, if it doesn't exist
 b. touch 123456.txt - will update the timestamp
 c. touch -t 201003221530 123456.txt - changes timestamp

16. stat - reveals statistics of files
 a. stat 123456.txt - reveals full attributes of the file

17. find - finds files using search patterns
 a. find / -name 'fstab'
Note: 'find' can search for fields returned by the 'stat' command

18. alias - returns/sets aliases for commands
 a. alias - dumps current aliases
 b. alias copy='cp'


###Unix Redirection & Pipes###
Features:
 1. Ability to control input and output

Input redirection '<':
 1. cat < 123.txt
Note: Use input redirection when program does NOT default to file as input

Output redirection '>':
 1. cat 123.txt > onetwothree.txt
Note: Default nature is to:
 1. Clobber the target file
 2. Populate with information from input stream


Append redirection '>>':
 1. cat 123.txt >> numbers.txt - creates 'numbers.txt' if it doesn't exist, or appends if it does

 2. cat 456.txt >> numbers.txt


Pipes '|':
Features: Connects the output stream of one command to the input stream of a subsequent command

 1. cat 123.txt | sort
 2. cat 456.txt 123.txt | sort
 3. cat 456.txt 123.txt | sort | grep 3


###Command Chaining###
Features:
 1. Permits the execution of multiple commands in sequence
 2. Also permits execution based on the success or failure of a previous command

 1. cat 123.txt ; ls -l - this runs first command, then second command without regards for exit status of the first command

 2. cat 123.txt && ls -l - this runs second command, if first command is successful
 3. cat 1234.txt && ls -l

 4. cat 123.txt || ls -l - this runs second command, if first command fails


20. more|less - paginators, which display text one-page @ a time
 1. more /etc/fstab
 2. less 1thousand.txt

21. seq - echoes a sequence of numbers
 a. seq 1000 > 1thousand.txt - creates a file with numbers 1-1000

22. su - switches users
 a. su - with no options attempts to log in as 'root'

23. head - displays opening lines of text files
 a. head /var/log/messages

24. tail - displays the closing lines of text files
 a. tail /var/log/messages

25. wc - counts words and optionally lines of text files
 a. wc -l /var/log/messages
 b. wc -l 123.txt

26. file - determines file type
 a. file /var/log/messages


###Tar, Gzip###
Features:
 1. Compression utility (gzip)
 2. File roller (the ability to represent many files as one - tar)


Gzip:
Includes:
 1. gzip - compresses/decompresses files
 2. gunzip - decompresses gzip files

Tasks:
 1. compress '1million.txt' file using gzip
  a. gzip -c 1million.txt > 1million.txt.gz

Note: gzip auto-dumps to STDOUT, by default

  b. gzip -l 1million.txt.gz - returns status information
  c. gunzip 1million.txt.gz - dumps to file, and removes compressed version
  d. gzip -d 1million.txt.gz
  e. zcat 1million.txt.gz - dumps the contents to STDOUT
  f. less 1million.txt.gzip - dumps the contents of gzip files to STDOUT


Tar :

 1. tar -cvf filename.tar path/ - creates a non-compressed archive
 2. tar -cvf 1million.txt.tar 1million.txt

###GREP###
Features:
 1. The ability to parse lines based on text and/or RegExes
 2. Post-processor
 3. Searches case-sensitively, by default
 4. Searches for the text anywhere on the line


1. grep 'linux' grep1.txt
2. grep -i 'linux' grep1.txt - case-insensitive search
3. grep '^linux' grep1.txt - uses '^' anchor to anchor searches at the beginning of lines
4. grep -i '^linux' grep1.txt
5. grep -i 'linux$' grep1.txt - uses '$' anchor to anchor searches at the end of lines

Note: Anchors are RegEx characters (meta-characters). They're used to match at the beginning and end of lines

6. grep '[0-9]' grep1.txt - returns lines containing at least 1 number
7. grep '[a-z]' grep1.txt


8. grep -v sshd messages - performs and inverted search (all but 'sshd' entries will be returned)
9. grep -v sshd messages | grep -v gconfd

No comments:

Post a Comment