3 File System

File System

Objective

  • Perform more operations on the file system.
  • Get familiar with file permission assignment.
  • Get familiar with text editors.

alias chgrp chmod chown emacs find gnome-text-editor grep id nano tree vim wc

Aliases

An alias lets you create a shortcut name for a command, file name, or any shell text. By using aliases, you save a lot of time when performing frequent tasks. An alias is a command in various command-line interpreters (shells), which enables a replacement of a word by another string. It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command, for example:

> alias ll="ls -l"
> ll
total 56
drwxr-xr-x. 1 tux tux     0 Jan 25 00:16 Desktop
drwxr-xr-x. 1 tux tux    30 Jan 29 13:59 Documents
drwxr-xr-x. 1 tux tux    62 Jan 29 14:03 Downloads
...

An alias will last for the life of the shell session. Regularly used aliases can be set from the shell’s rc file, such as ~/.bashrc, so that they will be available upon the start of the corresponding shell session:

echo 'alias l="ls"' >> ~/.bashrc
echo 'alias ll="ls -la"' >> ~/.bashrc
source ~/.bashrc
l
ll

Note: alias, without arguments, prints the list of defined aliases.

Searching Files

grep

Looks for strings in a file. Its name comes from the ed, a text editor, command g/re/p (global / regular expression search / and print), which performs the same function.

Syntax

grep [-options] pattern file [file ...]
OptionDescription
-cCount
-iCase insensitive search
-lList filename only
-vDisplay lines with no pattern match

Example

> grep root /etc/passwd
root:x:0:0:Super User:/root:/bin/bash
operator:x:11:0:operator:/root:/usr/sbin/nologin
 
> grep "FTP User" /etc/passwd
ftp:x:14:50:FTP User:/var/ftp:/usr/sbin/nologin

Exercise 1

  1. Display the contents of /etc/hosts to show all known systems on your network.
  2. Locate all occurrences of the word nologin in the /etc/passwd file.
  3. Make a directory called cse.
  4. Create another directory under cse called cs.
  5. Create a file called cmps405. Move cmps405 into cs. Verify the contents of cs.

Locating Files

find

Used to locate files within the directory structure that meet specified search criteria.

Syntax

find path predicate-list

path is the pathname of the directory to start the search from. All subdirectories are searched recursively by default.

PredicateDescriptionExample
-atime nAccessed n days ago-atime 10
-atime +nAccessed more than n days ago-atime +90
-atime -nAccessed less than n days ago-atime -30
-ctime nCreated n days ago-ctime 1
-ctime +nCreated more than n days ago-ctime +15
See man find for more predicates under tests

Example

> find / -ctime +10 -name include

Exercise 2

  1. Create the following file structure under the current running user:
      • file-a
      • file-b
      • file-c
      • exercise-1.c
      • hw-1.md
      • prog-1.c
      • project-1.md
      • hello.png
      • hw-1.png
      • hw-1.svg
      1. Add sample C source code to the *.c files.
      2. List all C file names whose content has main using one command.
      3. Copy all C files only from the docs directory to the source directory using one command.
      4. Move the backup directory and put it inside the images directory using one command.
      5. Starting from your home directory, find all files whose name start with the letter D using one command.

      Sorting Lines

      sort

      Sorts lines, lexicographically or numerically, in a file.

      Syntax

      sort [-nr] filename

      Example

      > sort /etc/passwd
      abrt:x:173:173::/etc/abrt:/sbin/nologin
      adm:x:3:4:adm:/var/adm:/usr/sbin/nologin
      apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin
      avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
      bin:x:1:1:bin:/bin:/usr/sbin/nologin
      chrony:x:990:989:chrony system user:/var/lib/chrony:/sbin/nologin
      ...

      Counting Words

      wc

      Counts lines, words, and characters in a file.

      Syntax

      wc [-lwc] filename
      OptionDescription
      -lLines
      -wWords
      -cCharacters
      NoneShow all counts

      Example

      > wc -l /etc/passwd
      46 /etc/passwd
      > wc -w /etc/passwd
      106 /etc/passwd
      > wc /etc/passwd
        46  106 2646 /etc/passwd

      Text Editing

      nano

      To start GNU nano, open a terminal and type nano followed by the name of an existing file or a new file that you want to create. The available shortcuts are displayed at the bottom of the terminal window.

      vim

      • To start the vim text editor, open a terminal and simply type vim followed by the name of an existing file or a new file that you want to create.
      • vim works in two main modes, one for editing text and the other for giving commands. To switch between the two modes you use the i and <escape> keys for editing and commands respectively.
      • Use the command :wq to save (write) changes and quit.
      • Use the command :w to save (write) changes without quitting.
      • Use the command :q! to ignore changes and quit.

      emacs

      • To start the Emacs text editor, open a terminal and simply type emacs followed by the name of an existing file or a new file that you want to create.

      • To save a file, press C-x C-s (<ctrl> + x followed by <ctrl> + s).

      • If you don’t want to save but quit, use the command C-x C-c (<ctrl> + x followed by <ctrl> + c). If there is unsaved data, press y for yes.

      • emacs has the following editing keys: C stands for <ctrl> and A stands for <alt>:

        CommandKey Combination
        Moving to the next lineC-n (n for Next)
        Moving to the previous lineC-p (p for Previous)
        Moving one character forwardC-f (f for Forward)
        Moving one character backwardC-b (b for Backward)
        Moving one word forwardA-f (f for Forward)
        Moving one word backwardA-b (b for Backward)
        Moving to the start of a lineC-a
        Moving to the end of a lineC-e (e for End)
        Moving one page downC-v (or <page-down>)
        Moving one page upA-v (or <page-up>)

      gnome-text-editor

      To start the GNOME text editor, open a terminal and type gnome-text-editor followed by the name of an existing file or a new file that you want to create. Notice that the terminal is occupied until you exit from the editor.

      Linking Files

      Symbolic links are files that point to other files or directories. They are similar to shortcuts and hyperlinks.

      ln

      Creates a symbolic or a hard link.

      Syntax

      ln [-s] original-file symbolic-link

      Example

      > touch file
      > ln -s file link
      > ls -l
      total 4
      -rw-r--r--. 1 tux tux 0 Feb  4 20:49 file
      lrwxrwxrwx. 1 tux tux 4 Feb  4 20:49 link -> file
      > echo "Hello, world!" >> file
      > cat link
      Hello, world!
      > echo "Hello, multiverse!" >> link
      > cat file
      Hello, world!
      Hello, multiverse!
      > ls -l
      total 4
      -rw-r--r--. 1 tux tux 0 Feb  4 20:50 file
      lrwxrwxrwx. 1 tux tux 4 Feb  4 20:49 link -> file
      > rm file
      > ls -l
      total 4
      lrwxrwxrwx. 1 tux tux 4 Feb  4 20:49 link -> file

      File Security

      • File security limits access to the files. The system provides you with the commands to specify who can access a file and, once accessed, what type of operations can be done on the file.
      • The three types of permissions are: r (read), w (write) and x (execute).
      • To deny a certain permission - is used in place of rwx, e.g. r-- r-x
      • Access may be granted to or revoked from the following types of users: the owner or user (u), members of the group (g), other users (o). The a option may be used instead of ugo.
      • Operators used are + (grants a permission), - (denies or revokes a permission), and = (sets all permissions for a specified user).
      • Permissions are listed and applied in the following order: u [rwx], g [rwx], o [rwx].
      • The ls -l command is used to view or list permissions.
      • The chmod command is used to set the file permission (access mode).

      Listing Permissions

      File information may be displayed using ls -l and has the following format for each entry:

      1. Type, permissions, and attributes
        1. Entry type
        2. User permissions
        3. Group permissions
        4. Others permissions
        5. Extended attributes
      2. Hard-link count
      3. Size (bytes)
      4. User (owner)
      5. Group (owner)
      6. Last modification date
      7. Name

      Entry Type

      SymbolType
      -Ordinary file
      dDirectory
      lSymbolic link
      bBlock special file
      cCharacter special file
      sLocal socket
      p, |First-in/first-out pipe special file

      Users

      SymbolTypeRemarks
      uUserOwner of the file which is usually the file creator
      gGroupMembers of the group with which the file is associated; usually the primary group of the file creator
      oOthersEveryone else, that is, anyone who is not the file owner or a member of the group.

      To display your user id and group information, use the id command:

      > id -a
      uid=1000(tux) gid=1000(tux) groups=1000(tux),10(wheel) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
      > id tux
      uid=1000(tux) gid=1000(tux) groups=1000(tux),10(wheel)
      > id root
      uid=0(root) gid=0(root) groups=0(root)

      Permissions

      SymbolPermissionEffect on FilesEffect on Directories
      rReadExamine or copy file contentsList filenames (ls)
      wWriteModify file contentsAdd and remove (cp, rm, touch, mv)
      xExecuteRun or execute a script or a programObtain files’ details and search the directory (cd, ls -l, find)
      -RevokeUsed in place of a permission to revoke it (deny access)Similar to the effect on files

      Changing Permissions

      chmod

      • Permissions may be changed for several reasons:

        • to hide private files from other users
        • to share files with other users
        • to make files modifiable
        • to make files executable
        • to prevent or allow modification of directory contents
      • File and directory permissions are changed using the chmod command.

      • Only the owner of the file or the superuser (root) can change the permissions.

      • Permissions can be specified using either the symbolic method or the octal method.

      • You can change the owner and group using the chown and chgrp commands, respectively, for example:

        sudo chown root file
        sudo chgrp wheel file
        ls -l file

      Symbolic Method

      chmod [who][action][access] file [file ...]
      1. who: u, g, o, a.
      2. action: +, -, =.
      3. access: r, w, x.

      Example

      > mkdir directory
      > cd directory
      > touch tigers bears elephants
      > ls -l
      total 0
      -rw-r--r--. 1 tux tux 0 Feb  4 20:39 bears
      -rw-r--r--. 1 tux tux 0 Feb  4 20:39 tigers
      -rw-r--r--. 1 tux tux 0 Feb  4 20:39 elephants
      > chmod u-w tigers
      > ls -l tigers
      -r--r--r--. 1 tux tux 0 Feb  4 20:39 tigers
      > chmod go-r bears
      > ls -l bears
      -rw-------. 1 tux tux 0 Feb  4 20:39 bears
      > chmod u=rx,g=r elephants
      > ls -l elephants
      -r-xr--r--. 1 tux tux 0 Feb  4 20:39 elephants
      > chmod o= elephants
      > ls -l elephants
      -r-xr-----. 1 tux tux 0 Feb  4 20:39 elephants

      Exercise 3

      1. Use touch to create a file called file_modes:
      touch file_modes
      1. List the file information by typing:
      ls -l file_modes
      1. Remove all permissions from this file using:
      chmod ugo= file_modes
      1. List the file information and confirm the change.
      2. Verify that you can neither read from nor write to this file.
      cat file_modes
      echo "date" >> file_modes
      1. Add write permission for yourself:
      chmod u+w file_modes
      1. Verify that you can write now to the file file_modes:
      echo "date" >> file_modes
      1. Now add write permission for everyone else. Also add read permission for yourself:
      chmod go+w,u+r file_modes
      1. Verify that you can now read from the file file_modes:
      cat file_modes
      1. Assign everyone read and write permission to your file:
      chmod ugo=rw file_modes
      1. Now deny everyone write permission, but grant everyone execute permission:
      chmod a-w,a+x file_modes
      1. Finally, verify that your file can now be executed:
      ./file_modes

      As file_modes contains the command date, the output should be the current system date.

      Octal Method

      The permission categories rwx can be represented numerically: A granted permission is represented by 1 and a revoked permission by 0. The rwx combination for any user category may now be represented as a 3-digit binary number which is equivalent to one octal digit:

      rwxBinary
      4 2 1
      Octal DigitPrivilege
      ---0 0 00No privilege
      --x0 0 11Execute only
      -w-0 1 02Write only
      -wx0 1 13Write and execute
      r--1 0 04Read only
      r-x1 0 15Read and execute
      rw-1 1 06Read and write
      rwx1 1 17Read, write, and execute

      The effective access permission for the 3 user categories (ugo) is represented by just one 3-digit octal number, as shown in the following table:

      Access PermissionsOctal Equivalent
      rwxrwxrwx777
      r--r--r--444
      rw-rw-r--664
      rwxr-xr-x755
      r--------400
      ---------000

      Example

      > ls -l
      total 0
      -r--------. 1 tux tux 0 Feb  4 20:39 bears
      -r--r--r--. 1 tux tux 0 Sep 15 21:22 tigers
      -r-xr-----. 1 tux tux 0 Sep 15 21:22 elephants
      > chmod 664 *
      > chmod 000 tigers
      > ls -l
      total 0
      -rw-rw-r--. 1 tux tux 0 Feb  4 20:39 bears
      ----------. 1 tux tux 0 Feb  4 20:39 tigers
      -rw-rw-r--. 1 tux tux 0 Feb  4 20:39 elephants

      Exercise 4

      1. Create a file called practice using touch.
      2. List the files in your directory along with their permissions.
      3. Change the permissions on the file practice so that only you can read and write it.
      4. Change the permissions so that nobody can access the practice file, not even yourself.
      5. Verify that you have no access to the file.
      6. Try to create a file in another user’s directory.

      Resources