s

Working with Directories"

  • Essentially, everything on your RHEL serveris stored in a text or ASCII file. Therefore, working with files is a very important task when administering Linux. .
  • A Directory is a special kind of file, but it is still a (case sensitive) file. . .
  • Each terminal window. . .any hard disk or partition and any processes are all represented somewhere in the file system as a file. . .It will become clear throughout this program, that everything in Linux is a file
  • All files on Linux (or Unix) are case sensitive: Meaning that FILE1 is different from file1 and /etc/hosts is different from /etc/Hosts
  • The “file” command determines the file type. . .Linux does not use extensions to determine the file type. . .

The command line does not care whether a file ends in .txt or .pdf

As a system administrator, you should use the “file” command to determine the file type.

For Example: # file /etc/passwd

Pathnames [Two types: Absolute and Relative]

Absolute Pathnames:

  • begins with the root directory and follows the tree branch by branch until the path to the desired directory or file is completed;
  • always starts with the forward slash(/) character. . .

For Example: # cd /var

Relative Pathnames:

  • starts from the current working directory. To do this, it uses a couple of special symbols to represent relative positions in the file-system tree.

--- These special symbols are

. (dot) and .. (dot dot)

-- The. symbolrefers to the working directory

-- and the.. symbolrefers to the working directory’s parent directory

show example: cd ..takes you to the working directory’s parent directory

cd ~shortcut to get back into your home directory

cd –shortcut to go to the previous directory (one directory up)

cd command:

  • use this command to change the current working directory. Example: cd /bin(pathname)

pwd command:

  • stands for Print Working Directory, which shows the current working directory you are in
  • Example: type “pwd”

Which command:

  • show the path to the command that will execute if it is run without using the absolute path
  • Example:# which cat
  • results: /usr/bin/cat

mkdir command: use command to create a new directory.

--- Example: creating directory in root: mkdir /test

--- Example: create directory in apps directory:

  • first: # cd /opt/apps
  • then mkdir test: # mkdir test

It is possible to create a complete directory structure in one command:

--- use “-p option”with mkdir to make this possible

--- when -p option is used, no error is reported if a specified DIRECTORY already exist

  • --- The -p option allows you to create parent directories as needed (if parent do not already exits).
  • For example, you can create the following directory structure:
    Example: #mkdir -p ~/public_html/images/trip

Example: # mkdir –p /test/test2/test3

-- -m, --mode option: Set file mode /

--- Example: mkdir -m a=rwxmydir (Create the mydir directory, and set its permissions such that all users may read, write, and execute the contents.)

rmdir command:

  • This command is used to remove directories from file system.
  • This command works with only directories that are empty
  • Example: # rmdir /test

--- In order to remove both a parent directory and a subdirectory of that parent, use -p option)

  • Example Below:

Using -p option:

  • Each directory argument is treated as a pathname of which all components will be removed, if they are empty, starting with the last most component.
  • Example:
  • # cd ~/
  • # mkdir -p bar/foo/data
  • # rmdir -p bar/foo/data

Exercise:

working with directories