You are on page 1of 44

CpE001L Final Lab Lecture 2

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

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.

PART I
Working with Linux Files

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

Linux File Types

Figure 1. Linux File Types

Linux File Types


Special Characters for Different File Types

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 a special 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 a device one character at a time Block special files - also provide a mechanism for communicating with device drivers via the file system

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

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 users familiar with Windows or Mac OS, Linux/UNIX directories are 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. Other are similar to aliases or shortcuts and enables to access a single file using different names. e.g. .profile, /dev, /dev/sda

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

Linux Pathnames
Full Pathnames
A pathnames that starts from / or /root (the root directory)

Relative Pathnames
A pathnames that starts from the present working directory

Linux Pathnames

Figure 2. Example Linux Directory Structure Types

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)

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)

Listing Files
ls - list the files and directories stored in the current directory.
With the ls command: $ ls dir/file $ ls /home tux1 tux2 tux3 Important options
-l -a -t -R -F lists - long listing (more information) - lists all files (including hidden) - lists files sorted by change date - lists contents recursively - appends a character indicating the file type of each of the items it

Knowing the Current Directory


pwd (print working directory) command used to find out what your current working directory is.
$ pwd /home/tux1

Changing Current Directory


With the cd (change directory) command: $ cd dirname
$ $ $ $ $ $ cd cd cd cd cd cd doc (relative) /home/tux1/doc (full) ~tux1/doc (home) (Go to your home directory) .. (Go one directory up) (Go to previous directory)

Creating Directories
With the mkdir (make directory) command: $ mkdir dirname
$ mkdir /home/tux1/doc (full pathname) $ cd /home/tux1 $ mkdir doc (relative pathname)

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

Working with Multiple Directories


Create and remove multiple directories simultaneously 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

Modifying File Time


touch command creates an empty file, or updates the modification time of an existing 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

Copying Files
cp command copies files: cp source/s target
Copying one file to another:
$ cp .bashrc bashrc.old

Copying multiple files into a target directory:


$ cp /tmp doc/mon_report doc/walrus

Copying Files
cp can recursively copy directories with the -R flag $ cp -R /home/tux1/doc /tmp To prevent cp from overwriting existing files, use:
$ cp -R -i /home/tux1/doc /tmp cp: overwrite `/tmp/doc/walrus?

Removing and Renaming Files


With the mv 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 existing files

Removing and Renaming Files


Moving and renaming files can be combined by mv $ cd $ pwd /home/tux1 $ mv /tmp/walrus ./test/rob To move a directory: $ mv ./test /tmp Note: mv is recursive by default

Listing the File Contents


With the cat (concatenate) command: $ cat file1 file2 ...
$ 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."

Creating File Content with cat


With the cat (concatenate) command: $ cat > file1
$ cat > walrus2 Welcome to Linux Basic Linux Shell End of the test text <ctrl+d> $cat walrus2 Welcome to Linux Basic Linux Shell End of the test text

Displaying Page by Page


With the more or less commands: $ less/more filename
"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)

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

Removing Files
With the rm command: $ rm filename $ ls test/rob ls: rob: No such file or directory If unsure, use -i option $ rm -i test/rob rm: remove `test/rob? To remove files and directories recursively: $ rm -ir test

Splitting Files
You can split a file into smaller files with the split command $ split -b <bytes> 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--rw-r--r--rw-r--r--rw-r--r--rw-r--r--

1 1 1 1 1

root root root root root

root root root root root

4194304 1048576 1048576 1048576 1048576

Feb Feb Feb Feb Feb

21 21 21 21 21

13:31 13:33 13:33 13:33 13:33

large large.aa large.ab large.ac large.ad

PART II
Linux File Permissions

File Permission
Linux files are setup so access to them is controlled. There are three types 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 called other(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 file belongs to.

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

File Names and Permission Characters


File names can be up to 256 characters long with "-", "_", and "." characters along with letters and numbers. When a long file listing is done, there are 10 characters that are shown on the left that indicate type and permissions of the file. File permissions are shown according to the following
Syntax: d rwx rwx rwx

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 and root

File Names and Permission Characters


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

File Names and Permission Characters


Permissions Notation

File Names and Permission Characters


Required Permission

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/tux1 drwx------ 4 tux1 users 512 Jan 5 12:43 /home/tux1

Mode can be octal:


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

Changing File Permissions


Table 1. who Table 2. Permission

Table 3. Action

Changing File Permissions


Calculating numeric (octal) mode:

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

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 have access 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 group ID bit is set and the user running the program is given access based on access permission for the group the file belongs to.

Changing File Permissions


The following command:
$ chmod +s myfile $ chmod g+s myfile - sets the user ID bit on the file "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 ID bit set.
-rws--x--x -rwxr-sr-x 1 root 1 root root mail 14024 Sep 9 1999 chfn 12072 Aug 16 1999 lockfile

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

You might also like