CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

download CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

of 44

Transcript of CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    1/44

    CpE001L Final Lab Lecture 2

    by: Engr. Ricrey E. Marquez, CpE, MSCS

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    2/44

    Lecture Objectives

    After this lecture, students should be

    able to:

    identify the Linux file system,

    execute basic file system commands, and

    give permission to Linux file system.

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    3/44

    PART IWorking with Linux Files

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    4/44

    Linux Files

    File

    a collection of data

    a stream of characters or a "byte

    stream"

    no structure is imposed on a file by the

    operating system

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    5/44

    Linux File Types

    Figure 1. Linux File Types

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    6/44

    Linux File Types

    Special Characters for Different File Types

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    7/44

    Linux File Types

    Regular files are the most common type of files store any kind of data(data can be stored as plain text, an application- specific format, or aspecial binary format that the system can execute)

    Symbolic link is a special file that points to another file on the system. Device files are access points to the device within the file systems. two

    main types of device files are:

    Character special files - provide a mechanism for communicating with adevice one character at a time

    Block special files - also provide a mechanism for communicating withdevice drivers via the file system

    Socket files are created when communication to a process on anothermachine located on a network is required.

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    8/44

    Linux File Types

    In Linux/UNIX there are three basic types of files:

    Ordinary Files - a file on the system that contains data, text,or program instructions. e.g. myfile.txt, myscript.sh

    Directories - store both special and ordinary files. For usersfamiliar with Windows or Mac OS, Linux/UNIX directoriesare equivalent to folders. e.g. /myfolder, /yocirem, /home

    Special Files - provide access to hardware such as hard drives,CD/DVD-ROM drives, modems, and Ethernet adapters. Otherare similar to aliases or shortcuts and enables to access a singlefile using different names. e.g. .profile, /dev, /dev/sda

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    9/44

    Linux Filenames

    Some important facts about Linux filenames:

    Should be descriptive of the content (readable)

    Should only use alphanumeric characters (uppercase, lowercase,number, @, _ )

    Should not include embedded blanks

    Should not contain shell metacharacters: * ? > < / ; & ! [ ] | \ ' " () { }

    Should not begin with + or - sign

    Case sensitive (uppercase is different from lowercase letters)

    Filenames starting with a . are hidden files

    Maximum number of characters for a filename is 255

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    10/44

    Linux Pathnames

    Full Pathnames

    A pathnames that starts from / or /root (theroot directory)

    Relative Pathnames

    A pathnames that starts from the presentworking directory

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    11/44

    Linux Pathnames

    Figure 2. Example Linux Directory Structure Types

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    12/44

    Example Linux Pathnames

    Assume that the working directory is at/home/tux1

    /home/tux1/doc/mon_report (full)

    doc/mon_report (relative)

    ../tux3/pgms/suba (relative)

    ./test (a file in the current dir)

    ~/test (same as above)

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    13/44

    Example Hidden Files

    An invisible/hidden file is one whose first

    character is the dot or period character (.).Some common examples of hidden files include

    the files:

    .profile (the Bourne shell ( sh) initialization script)

    .kshrc (the Korn shell ( ksh) initialization script) .cshrc (the C shell ( csh) initialization script)

    .rhosts (the remote shell configuration file)

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    14/44

    Listing Files

    ls - list the files and directories stored in the currentdirectory. With the ls command: $ ls dir/file

    $ ls /hometux1 tux2 tux3

    Important options

    -l - long listing (more information) -a - lists all files (including hidden)

    -t - lists files sorted by change date

    -R - lists contents recursively

    -F - appends a character indicating the file type of each of the items itlists

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    15/44

    Knowing the Current Directory

    pwd (print working directory) -

    command used to find out what yourcurrent working directory is.

    $ pwd/home/tux1

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    16/44

    Changing Current Directory

    With the cd (change directory) command: $cddirname

    $ cd doc (relative)$ cd /home/tux1/doc (full)$ cd ~tux1/doc (home)

    $ cd (Go to your home directory)$ cd .. (Go one directory up)$ cd - (Go to previous directory)

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    17/44

    Creating Directories

    With themkdir (make directory)command:$ mkdir dirname

    $ mkdir /home/tux1/doc (full pathname)$ cd /home/tux1

    $ mkdir doc (relative pathname)

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    18/44

    Removing Directories

    With the rmdir (remove directory) command: $rmdir dirname

    $ pwd/home/tux1$ rmdir doc test

    rmdir: doc: Directory not empty

    Note: directory must be empty

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    19/44

    Working with Multiple

    Directories

    Create and remove multiple directoriessimultaneously with the -p flag

    $ mkdir -p dir1/dir2/dir3 or$ mkdir dir1 dir2 dir3

    $ rmdir -p dir1/dir2/dir3 or$ rmdir -r dir1 dir2 dir3

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    20/44

    Modifying File Time

    touch command creates an empty file, or updates the modification time of anexisting file

    $ ls -l-rw-rw-r-- 1 tux1 penguins 512 Feb 24 11:10 docs

    $ touch docs$ ls -l-rw-rw-r-- 1 tux1 penguins 512 Mar 5 15:37 docs

    $ touch new$ ls -l-rw-rw-r-- 1 tux1 penguins 512 Mar 5 15:37 docs-rw-rw-r-- 1 tux1 penguins 0 Mar 5 15:37 new

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    21/44

    Copying Files

    cp command copies files: cp source/starget

    Copying one file to another:$ cp .bashrc bashrc.old

    Copying multiple files into a target directory:$ cp doc/mon_report doc/walrus/tmp

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    22/44

    Copying Files

    cp can recursively copy directories with the-R flag

    $ cp -R /home/tux1/doc /tmp

    To prevent cp from overwriting existingfiles, use:

    $ cp -R -i /home/tux1/doc /tmp

    cp: overwrite `/tmp/doc/walrus?

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    23/44

    Removing and Renaming Files

    With themv command: $ mv source/s target

    To move a file do another directory:$ mv doc/walrus ../../tmp

    To rename a file:

    $ mv doc documents

    Use the -i option to prevent mv from overwriting existingfiles

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    24/44

    Removing and Renaming Files

    Moving and renaming files can be combined bymv$ cd

    $ pwd/home/tux1$ mv /tmp/walrus ./test/rob

    To move a directory:$ mv ./test /tmp

    Note:mv is recursive by default

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    25/44

    Listing the File Contents

    With the cat (concatenate) command: $ cat file1file2 ...

    $ cat walrus"The time has come", the walrus said,"To talk of many things:Of shoes - and ships - and sealing wax -

    Of cabbage - and kings -And why the sea is boiling hot -And whether pigs have wings."

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    26/44

    Creating File Content with cat

    With the cat (concatenate) command: $ cat > file1

    $ cat > walrus2Welcome to LinuxBasic Linux ShellEnd of the test text

    $cat walrus2Welcome to LinuxBasic Linux ShellEnd of the test text

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    27/44

    Displaying Page by Page

    With themoreor less commands: $ less/morefilename

    "The time has come", the walrus said,"To talk of many things:Of shoes - and ships - and sealing wax -Of cabbage - and kings -

    And why the sea is boiling hot -And whether pigs have wings."/tmp/test/walrus 1-6/6 (END)

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    28/44

    Displaying Binary Files

    With the od command

    With the strings command:

    $ strings /usr/bin/passwd/lib/ld.so.1

    __gmon_start____deregister_frame_info__register_frame_info

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    29/44

    Removing Files

    With the rmcommand: $ rm filename$ ls test/rob

    ls: rob: No such file or directory

    If unsure, use -i option$ rm -i test/robrm: remove `test/rob?

    To remove files and directories recursively:$ rm -ir test

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    30/44

    Splitting Files

    You can split a file into smaller files with the split command $ split -b file [prefix]

    $ ls -l-rw-r--r-- 1 root root 4194304 Feb 21 13:31 large

    $ split -b 1024k large large.

    $ ls -l

    -rw-r--r-- 1 root root 4194304 Feb 21 13:31 large-rw-r--r-- 1 root root 1048576 Feb 21 13:33 large.aa-rw-r--r-- 1 root root 1048576 Feb 21 13:33 large.ab-rw-r--r-- 1 root root 1048576 Feb 21 13:33 large.ac-rw-r--r-- 1 root root 1048576 Feb 21 13:33 large.ad

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    31/44

    PART IILinux File Permissions

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    32/44

    File Permission

    Linux files are setup so access to them is controlled. There are threetypes of access:

    read (r)

    write (w)

    execute (x)

    Each file belongs to a specific user (u) and group (g).

    Access to the files is controlled by user, group, and what is calledother(o).

    The term, other, is used to refer to someone who is not the user(owner) of the file, nor is the person a member of the group the filebelongs to.

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    33/44

    File Permission

    Setting permissions for "other" users to use, it is commonly referredto as setting the world execute (x), read (r), or write (w) bit sinceanyone in the world will be able to perform the operation if thepermission is set in the other category.

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    34/44

    File Names and Permission

    Characters

    File names can be up to 256 characters long with "-","_", and "." characters along with letters andnumbers.

    When a long file listing is done, there are 10 charactersthat are shown on the left that indicate type andpermissions of the file. File permissions are shownaccording to the following

    Syntax: d rwx rwx rwx

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    35/44

    File Names and Permission

    Characters

    Permissions

    File permissions are assigned to:

    owner of a file

    members of the group the file is assigned to

    all other users

    Permissions can only be changed by the owner androot

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    36/44

    File Names and Permission

    Characters

    Viewing Permissions To show the permissions of a file, use the ls command with

    the -l option

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    37/44

    File Names and Permission

    Characters

    Permissions Notation

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    38/44

    File Names and Permission

    Characters

    Required Permission

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    39/44

    Changing File Permissions

    To change the permission of a file use the chmod command

    Syntax: chmod[MODE] [FILE/S]

    Mode can be symbolic

    $ chmod go-rx /home/tux1$ ls -ld /home/tux1drwx------ 4 tux1 users 512 Jan 5 12:43 /home/tux1

    Mode can be octal:

    $ chmod 700 /home/tux1$ ls -ld /home/tux1drwx------ 4 tux1 users 512 Jan 5 12:43 /home/tux1

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    40/44

    Changing File Permissions

    Table 1.

    whoTable 2.

    Permission

    Table 3.

    Action

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    41/44

    Changing File Permissions

    Calculating numeric (octal) mode:

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    42/44

    umask Command

    New files should not be created with 666, to avoid this problem apermission mask exists. Syntax: $ umask 022

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    43/44

    Changing File Permissions

    Set User Identification Attribute

    File permissions bits include an execute permission bit for file

    owner, group, and other. When the execute bit for the owner is set to "s" the set user ID

    bit is set.

    This causes any persons or processes that run the file to haveaccess to system resources as though they are the owner of the

    file.

    When the execute bit for the group is set to "s", the set groupID bit is set and the user running the program is given accessbased on access permission for the group the file belongs to.

  • 7/29/2019 CpE001L Final Lab Lecture 2 - Linux Files and File Permissions

    44/44

    Changing File Permissions

    The following command:

    $ chmod +s myfile- sets the user ID bit on the file "myfile".

    $ chmod g+s myfile - sets the group ID bit on the file "myfile".

    The listing below shows a listing of two files that have the group or user IDbit set.

    -rws--x--x 1 root root 14024 Sep 9 1999 chfn

    -rwxr-sr-x 1 root mail 12072 Aug 16 1999 lockfile

    Note: The files chfn and lockfile are located in the directory "/usr/bin".