43
Using Linux Commands Lab 4

Using Linux Commands

Embed Size (px)

DESCRIPTION

Using Linux Commands. Lab 4. Using the Shell in Linux Commands Syntax. Options: could be added to the commands to change their behavior (-a , -la , --help) Argument: is an extra piece of information (ex.: a file name) Environment variables : - PowerPoint PPT Presentation

Citation preview

Page 1: Using Linux Commands

Using Linux Commands

Lab 4

Page 2: Using Linux Commands

Using the Shell in LinuxCommands SyntaxOptions:

could be added to the commands to change their behavior (-a , -la , --help)

Argument: is an extra piece of information (ex.: a file name)

Environment variables : where the shell stores information that may be useful to the user’s shell session.

Examples of environment variables include $SHELL (which identifies the shell you are using )and$PS1 (which defines your shell prompt)

Meta characters: that have special meaning to the shell

• (>) used to direct the output of a command to a file.• ( | ) pipe the output to another command.

Page 3: Using Linux Commands

Environment variables :

Page 4: Using Linux Commands

Getting Help with Using the ShellUse --help with the command (date --help)

Page 5: Using Linux Commands
Page 6: Using Linux Commands

Use the man commandman = manualTo reading a manual and return to the shell prompt within the open terminal press q

Page 7: Using Linux Commands

Understanding file permission Why Keep users from accessing other users’ private files To protect important system files

permissions bits

rwx rwx rwx

Owner’s| Group | Others

r = read w = write x = execute

$ ls –al-rw-rw-r-- 1 chris sales 1024 May 10 01:49 .. drwxr-xr-x 2 chris sales 2204 May 18 21:30 .bash_history

Page 8: Using Linux Commands

Understanding file permission

For Files: "Read" means to be able to open and view the file "Write" means to overwrite or modify the file "eXecute" means to run the file as a binary

files are executable only if they are programs and shell scripts, not useful for data files.

For Directories: "Read" means to be able to view the contents of the directory "Write" means to be able to create new files/directories or delete files/directories

within the directory "eXecute" means to be able to "Change Directory" (cd) into the directory =

permission to access the directory.

How to view the permission for a file or directory? $ ls -al

Page 9: Using Linux Commands

Understanding file permission1 2 3 4 5 6 7 8 9 10

d ≡ directory- ≡ file

Permission for the owner

Permission for the group

Permission for the others

if the permission field is - the permission is not given.

Page 10: Using Linux Commands

Understanding file permission

Only the owner of a file can change its permission. How to set file permission?

Use the command chmod(change file mode bits).

chmod has two notations: Numeric(octal) notation. Symbolic notation.

Page 11: Using Linux Commands

Change permission on a file - numeric the file permissions aren't represented by characters. Instead, they

are represented by a three-digit octal number. 4 = read (r)

2 = write (w)1 = execute (x)0 = no permission (-)

Octal# (421) Binary permission

0 0+0+0 000 ---

1 0+0+1 001 --x

2 0+2+0 010 -w-

3 0+2+1 011 -wx

4 4+0+0 100 r--

5 4+0+1 101 r-x

6 4+2+0 110 rw-

7 4+2+1 111 rwx

725

4+2+1 0+2+0 4+0+1

rwx -w- r-x

If the permission is

Page 12: Using Linux Commands

Change permission on a file – symbolicPermissions are represented by characters rwx

This gives “who” the specified permissions for a given filename. The “who” is a list of letters re going to be giving permissions to.

These may be specified in any order.

+ add the selected permission. - remove the selected permission.

chmod who+

permission filename-

u The user who owns the file (this means “you.”)

g The group the file belongs to.

o The other users not in the file’s group.

a all of the above (an abbreviation for ugo)

Page 13: Using Linux Commands

Change permission on a fileFor r, w, x octal value is 4,2,1 respectively

$ chmod 777 file-name rwxrwxrwx

$ chmod 755 file-name rwxr-xr-x

owner(u) group(g) other(o) all(a) rwxrwxrwx

$ chmod a-w file-name r-xr-xr-x

$ chmod go-rwx file-name rwx------

owner(u) group(g) other(o) all(a) ---------

$ chmod u+rw file-name rw-------

Page 14: Using Linux Commands

Set different permissions of start file:

Page 15: Using Linux Commands

15

Creating files and directories

Use the command mkdir to create a new directory to the current working directory.

$ mkdir directoryName

Page 16: Using Linux Commands
Page 17: Using Linux Commands

17

Creating directories (Exercise)1. Go to your home directory. Type cd.2. Make sure that you got to your home directory, type pwd3. Create a new directory called test in your home directory:

4. Check the permissions of the directory by typing: ((The -d option tells ls not to list the contents of the test directory; just show us the listing for the directory itself)).

5. Suppose that you want to prevent everyone else from using or viewing the files in this directory:

6. Make the test directory your current directory :

$ mkdir test

$ chmod 700 test

$ cd test

$ ls –ld testdrwxr-xr-x 2 chris sales 1024 May 18 21:30 test

Page 18: Using Linux Commands

Answer

Page 19: Using Linux Commands

19

Creating directoriesTo create a directory(test) in the

Desktop, we have two ways:mkdir /home/chris/Desktop/testcd /home/chris/Desktop

mkdir test

NOTE:

The -d option tells ls not to list the contents of the test directory; just show us the listing for the directory itself.

$ ls –ld test provide a long listing of the test directory, without showing the contents of the test directory.

Page 20: Using Linux Commands

20

Moving, copying, and deleting files• The command mv moves or renames files.

- The simplest form of use is:

mv oldfilename newfilename.

it will rename the file a.kwd to a new name b.kwd

$ mv file1 file2 Renaming

- It will also move a file to a directory. If you use it in the form:

mv filename directoryname

it will move the file into the named directory keeping its old name

$ mv file2 \test or mv file2 test

Page 21: Using Linux Commands

Rename file start start1

Page 22: Using Linux Commands

After rename the file

Page 23: Using Linux Commands

23

Moving, copying, and deleting filesTo copy files, you use the cp command. The following will copy file to file2. Note that if file2 doesn't exist, it'll be created, but if it exists, it'll

be overwritten:

$ cp file file2If you want to copy file into directory dir1:

$ cp file dir1

To remove a file from the current directory

rm filename

$ rm file

$ rm *

* remove all files in the current directory

Page 24: Using Linux Commands

Before remove application file.

Page 25: Using Linux Commands

After remove application file.

Page 26: Using Linux Commands

Winows file before and after (cp command )

Page 27: Using Linux Commands

27

Create empty files

To create an empty file in the current directory use the command touch

$ touch file1 file2 file3

$ touch apple banana grape grapefruit watermelon

$ ls -l

Using file-matching metacharacters Metacharacters help to match one or more files without typing each

filename completely. * This matches any number of characters(zero or more characters). ? This matches any one(single) character. […] This matches any one of the characters between the brackets,

which can include a dash-separated rang of letters or numbers.

Page 28: Using Linux Commands
Page 29: Using Linux Commands

29

$ ls a* The first example matches any file that begins with a

apple

$ ls g* The second example matches any file that begins with g

grape grapefruit

$ ls g*t This example matches any file beginning with g and ending in t

grapefruit

$ ls *e* matches any file that contains e in the name

apple grape grapefruit watermelon

$ ls *n* matches any file that contains n in the name

banana watermelon

Using file-matching metacharacters

This matches any number of characters *

Page 30: Using Linux Commands

30

Using file-matching metacharacters

This matches any one(single) character ?$ ls ????e The first example matches any five-character file that ends in e

Apple grape

$ ls g???e* The second matches any file that begins with g and has e as its fifth character 

grape grapefruit

Page 31: Using Linux Commands

31

Using file-matching metacharacters

This matches any one of the characters between the brackets […]$ ls [abw]* any file beginning with a,b or w is matched

apple banana watermelon

$ ls [agw] * [ne] any file that begins with a ,g or w and also ends with either n or e is matched

apple grape watermelon

$ ls [a-g] * any filenames beginning with letter from a through g are matched

apple banana grape grapefruit

Page 32: Using Linux Commands
Page 33: Using Linux Commands

33

Using file-redirection metacharacters

$ mail root < ~/.bashrcthe contents of the .bashrc file in the home directory are sent in a mail message to the computer's root user.

$ echo “I finished the project on $(date)” > ~/projects

• < Direct the contents of a file to the command

• > Direct the output of a command to a file, overwriting any existing file

• >> Direct the output of a command to a file, adding the output to the end of existing file

$ echo “I finished the project on $(date)” >> ~/projects

Page 34: Using Linux Commands
Page 35: Using Linux Commands
Page 36: Using Linux Commands
Page 37: Using Linux Commands

37

echo Command

Use echo command to display text or value of variable.

echo [options] [string, variables...]

Page 38: Using Linux Commands

38

Quotes

"Double Quotes“variables and command substitution(except \ and $).

'Single quotes‘protects everything enclosed between two single quote

marks. It is used to turn off the special meaning of all characters

( NO substitution of variables and commands).

`Back quote`Used with commands only.To execute command.

Page 39: Using Linux Commands

39

command variableDouble “ “Back ` `

$echo “My working directory is `pwd`”

The output:My working directory is /home/nora/Desktop

-

Single ‘ ‘Back ` `

$echo ‘My working directory is `pwd`’

The output:My working directory is `pwd`

-

Double “ ” $echo “My working directory is pwd”

The output:My working directory is pwd

$echo “the home is $HOME”The output:the home is /home/nora

Single ‘ ‘ $echo ‘My working directory is pwd’The output:My working directory is pwd

$echo ‘the home is $HOME’The output:the home is $HOME

Page 40: Using Linux Commands

40

echo Command

Options-n Do not output the trailing new line.-e Enable interpretation of the following backslash escaped characters in the strings:

\a alert (bell)\b backspace\n new line\t horizontal tab

Page 41: Using Linux Commands

41

Examples of quoting special characters

ubuntu@ubuntu:~$ echo –n “operating system”

operating system ubuntu@ubuntu:~$

$ echo –e “operating \t system”

Operating system

$ echo -e "An apple a day keeps away \a\t\tdoctor\n"

An apple a day keeps away doctor

Page 42: Using Linux Commands
Page 43: Using Linux Commands

43

References

http://www.podgrid.org/linux-bible/linux-bible-page109.html