Posted: December 6, 2010 in Uncategorized

LINUX BASIC COMMANDS:

COMMAND DESCRIPTION

1. sudo su or su                                    Switch user from user login mode to root login mode.

2. su username                                     Switch user to username login mode.

3. exit                                                      Return to user login mode.

4. cd                                                        return to home directory.

5. cd pathname                                     retun to given directory.

6. cd ..                                                     return to parent directory.

7. pwd                                                     show presently working directory.

8. touch datalab.txt                              Update the access and modification times of each FILE to the

current time.  or A FILE argument that does not exist is created

empty.

9. ls                                                           list the content of present directory.

a. -d                                                          list directory entries instead of contents.

b. -l                                                           list directory entries instead of contents.

c. -r                                                           reverse order while sorting.

10. nano filename.(extension)            to edit a file with an extension(eg: .txt,.cpp,.java etc.)

Ctrl+o                                                to save data.

Ctrl+x                                                 to exit from nano editor.

11.  cat datalab.txt                                 take datalab.txt as statdard input.

12.  cat < datalab.txt                              alternaative command of datalab.txt

a. cat datalab -A                                      equivalent to –vET.

b. cat datalab -b                                      number nonempty output lines.

c. cat datalab -E                                      display $ at end of each line.

d. cat datalab -n                                      number all output lines.

COMMAND DESCRIPTION

e. cat datalab -s                                    suppress repeated empty output lines.

d. cat datalab -T                                   display TAB characters as ^I

f. cat datalab -v                                     use ^ and M- notation, except for LFD and TAB.

e. cat datalab -s                                    suppress repeated empty output lines.

13.  nano datasubject.txt

subject

Operating Sysytem

Networking

Data base management system

Data structure

Ctrl+o : to save data

Ctrl+x : to exit from nano editor

14. cat datasubject.txt

15  touch labsubject.txt

16  cat labsubject.txt

17  cat datalab.txt datasubject.txt

> labsubject.txt

18  cat labsubject.txt

19  tac labsubject.txt                                        print the document in reverse order.

20  which cat                                                      where the cat command is located.

TOUCH &CAT COMMAND:

Linux – Redirection & Pipes:

  • Redirection

Features: Ability to control input and output.

1.Input redirection ‘<’:

Eg: cat < 123.txt

Note: Use input redirection when program does NOT default to file as input.

2.Output redirection ‘>’:

Eg: cat 123.txt > onetwothree.txt

Note: Default nature is to  clobber the target file & populate with information from input stream.

3.Append redirection ‘>>’:

Eg: cat 123.txt >> numbers.txt – creates ‘numbers.txt’ if it doesn’t exist, or appends if it does.

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

REDIRECTION & PIPES , COMMAND CHAINING:

###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.

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

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

cat 1234.txt && ls -l

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

  • more|less – paginators, which display text one-page @ a time

1. more /etc/fstab

2. less 1thousand.txt

  • seq – echoes a sequence of numbers

a. seq 1000 > 1thousand.txt – creates a file with numbers 1-1000

  • su – switches users

a. su – with no options attempts to log in as ‘root’

  • head – displays opening lines of text files

a. head /var/log/messages

  • tail – displays the closing lines of text files

a. tail /var/log/messages

  • wc – counts words and optionally lines of text files

a. wc -l /var/log/messages

b. wc -l 123.txt

LINUX – TAR, GZIP, BZIP2, ZIP

  • file – determines file type

a. file /var/log/messages

  • Linux – Tar, Gzip, Bzip2, Zip

Features:

1. Compression utilities (gzip, bzip2, zip).

2. File rollers (the ability to represent many files as one).

Gzip:

Includes:   1. gzip – compresses/decompresses files

2. gunzip – decompresses gzip files

Tasks: 1. compress ’1million.txt’ file using gzip

Eg: 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

Bzip2:

1. bzip2 -c 1million.txt > 1million.txt.bz2

Note: Bzip2 tends to outperform gzip on larger files

2. bunzip2 1million.txt.bz2

3. bzip2 -d 1million.txt.bz2

4. bzcat 1million.txt.bz2 – dumps contents to STDOUT

5. less 1million.txt.bz2 – also dumps the contents to STDOUT

Zip & unzip:

1. zip filename.zip path/ – general usage

2. zip 1million.txt.zip 1million.txt

Note: zip differs slight from gzip and bzip2 in that the destination file (resultant zip file) is specified before the source.

3. unzip 1million.txt.zip

Tar & Gzip/Bzip2:

1. tar -cvf filename.tar path/ – creates a non-compressed archive.

2. tar -cvf 1million.txt.tar 1million.txt

Note: tar, requires a small overhead for itself in each file.

3. tar -czvf 1million.txt.tar.gz 1million.txt – creates, tar/gzip document.

4. tar -cjvf 1million.txt.tar.bz2 1million.txt – creates, tar/bzip2 document.

5. tar –tzvf

6. tar -cjvf 1million.txt.tar.bz2 1million.txt testRH5/- creates, tar/bzip2 document for the text file and ‘testRH5′ directory tree.

  • Linux- 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.

Commands:

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.

LINUX – SYSTEM UTILITIE

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. rpm -qa | grep grep – searches the package database for programs named ‘grep’.

9. rpm -qa | grep -i xorg | wc -l – returns the number of pacakges with ‘xorg’ in their names.

10. grep sshd messages

11. grep -v sshd messages – performs and inverted search (all but ‘sshd’ entries will be returned)

12. grep -v sshd messages | grep -v gconfd

13. grep -C 2 sshd messages – returns 2 lines, above and below matching line.

Note: Most, if not all, Linux programs log linearly, which means one line after another, from the earliest to the current.

Note: Use single or double quotes to specify RegExes. Also, execute ‘grep’ using ‘egrep’ when RegExes are being used.

  • Linux – System Utilities:

Features:

1. Process listing.

2. Free/available memory.

3. Disk utilization.

Commands:

1. ps – process status/listing.

Eg: a. ps -ef or ps -aux

2. top – combines, ps, uptime, free and updates regulary.

3. uptime – returns useful system utilization information:

LINUX – SYSTEM UTILITIES

Eg: a. current time

b. uptime – days, hours and minutes

c. connected users

d. load averaged – 1,5,15 minute values

4. free – returns memory utilization.

a. RAM

b. SWAP

free -m – for human readable format.

5. df – returns disk partition/mount point information.

a. df – returns info. using kilobytes.

b. df -h – returns info. using megabytes/human readable (gigs/teray/etc.)

6. vmstat – reports on: processes, memory, paging, block I/O, traps, CPU activity.

a. vmstat

b. vmstat -p /dev/hda1 – returns partitions stats for /dev/hda1 (/boot)

7. gnome-system-monitor – GUI, combining most system utilities.

8. ls -ltr /proc

a. cat /proc/cpuinfo

9. kill PID – kills the process with a given PID.

10. runlevel – returns runlevel information using 2 fields:

a. represents previous runlevel.

b. represents current runlevel.

  • Linux – User/Group Management:

Features:

1. The ability to control users and groups.

Primary tools:

LINUX – SYSTEM UTILITIES:

1. useradd – used to add users and modify group membership.

2. system-config-users.

Task:

1. Create a user named ‘student1 using ‘useradd’.

Note: Default user settings derive from: /etc/login.defs.

a. useradd student1

b. set password for user ‘student1′: passwd student1

Default User Accounts DB: /etc/passwd

student1:x:501:501::/home/student1:/bin/bash

username:shadow_reference:uid:gid:Description(GECOS):$HOME:$SHELL

Note: /etc/passwd is a world-readable file.

Note: /etc/shadow now stores passwords in encrypted form.

Note: /etc/shadow is NOT world-readable.

Fields in /etc/shadow:

student1:$1$XSFMv2ru$lfTACjN.XxaxbHA0EkB4U0:13891:0:99999:7:::

1. username:

2. encrypted_password:

3. Days_since_Unix_epoch_password_was_changed (01/01/1970)

4. Days before password may be changed

5. Days after which the password MUST be changed

6. Days before password is to expire that user is warned

7. Days after password expires, that account is disabled

8. Days since Unix epoch, that account is disabled

9. Reserved field (currently unused)

2. Modify user ‘student1 to have password expire after 45 days:

GNOME-SYSTEM-MONITOR:

a. usermod

Groups:

1. groupadd – adds new group

2. groups – lists groups on the system: /etc/group

/etc/group – maintains group membership information

Task: Create a ‘sales’ group and add ‘linuxcbt’ and ‘student1′ as members

1. groupadd sales

2. usermod -G sales linuxcbt

3. usermod -G sales student1

Note: 2 types of groups exist:

1. Primary – used by default for a user’s permissions.

2. Supplemental – used to determine effective permissions.

Note: use ‘id’ to determine the group information of user.

Note: Create a new shell session to realize new group membership information.

userdel/groupdel are used to delete users and groups, respectively.

  • Linux – File Types – Permissions – Symlinks:

Features:

1. The ability to restrict/control access to files.

Note: 10 bits represent permissions for files (including directories). Use ‘ls -l’ to examine permissions or GUI application like ‘Nautilus’.

-rwxrwxr-x 1 linuxcbt linuxcbt  681 Jan 13 11:31 regextest.pl

1st bit = file type. ‘-’ = file, ‘d’ = directory.

2nd – 4th bits = owner’s permissions.

r = read = 4

w = write = 2

CPU INFORMATION

x = execute = 1

– = none = 0

5th – 7th bits = group owner’s permissions

r = read = 4

w = write = 2

x = execute = 1

– = none = 0

8th – 10th bits = everyone (world)

r = read = 4

w = write = 2

x = execute = 1

– = none = 0

Task:

1. Manipulate file permissions using ‘chmod’.

a. chmod -x regextest.pl

-rw-rw-r– 1 linuxcbt linuxcbt 681 Jan 13 11:31 regextest.pl

rw = 6 or 4+2 for owner

rw = 6 or 4+2 for group owner

r = 4 for everyone else (world)

Octal notation: 664 for file ‘regexetest.pl’

chmod 664 regextest.pl – removes execution for ALL users

chmod 775 regextest.pl – enables execution for ALL users

2. Ensure that ‘regextest.pl’ is rw by owner and noone else

a. chmod 600 regextest.pl

Note: File will now be rw by owner (linuxcbt) and ‘root’

LINUX – USER/GROUP MANAGEMENT:

3. Ensure that ‘regextest.pl’ is r by owner and noone else

a. chmod 400 regextest.pl && ls -l regextest.pl

Note: chmod supports string values, which represent octal values.

chmod +/- x file

chmod +/- w file

chmod +/- r file

chmod +/- u+x file – updates owner’s execute permissions on the file.

chmod +/- o+x file – updates other’s execute permissions on the file.

chmod +/- g+x file – updates group’s execute permissions on the file.

chmod a+rwx = chmod 777

chown – permits changing of ownership of files

This slideshow requires JavaScript.

 

Leave a comment