Basic Unix Commands - File System

The File

  • UNIX treats everything as a file... Directories and devices like the hard disk, DVD-ROM, and printer are files to UNIX.

  • Three types of files

    1. Ordinary file
      • Also known as a regular file, contains only data as a stream of characters.
    2. Directory file
      • A folder containing the names of other files and subdirectories.
    3. Device file
      • Represents a device or peripheral.

The File System Hierarchy

UNIX files are organized in a hierarchical (an inverted tree) structure, starting with root (/)

In [5]:
import IPython.display
IPython.display.Image(filename='images/tree.png', embed=True, width=500)
Out[5]:

Image Source: S. Das. Your UNIX/Linux: The Ultimate Guide. Third. McGraw-Hill, Inc.,

Relative Pathnames

Relative path shortcuts

  • . (a single dot) represents the current directory
  • .. (two dots) represents the parent directory

Example

> pwd /home
> ls .
  john george
> cd john $ pwd
  /home/john
> cd ..
> pwd /home

Making and removing directories

Making directories

>mkdir myDir

(UNIX is case sensitive by-the-way) creates a directory myDir in the current directory

>mkdir myDir1 myDir2

Creates multiple directories in one command

>mkdir myDir1/myDir2

creates myDir2 inside of myDir1 (must exist)

Removing directories

rmdir myDir

Only works for empty directories. (No other files/directories inside)

rm -R myDir

Use will caution! You CANNOT recover from rm

Copy and Move

cp -- copy

>cp file1 file2

Copies file1 to file2

>cp -R myDir1 myDir2

-R option copies recursively, meaning all subdirectories will be copied as well

mv - move

>mv file1 file2

When used this way it’s basically a rename utility

>mv file1 file2 myDir

Moves file1 and file2 into the directory myDir

Deleting files

>rm file1 file2
>rm file*

* is a wildcard, meaning anything, the command will remove all patterns that match file with anything following.

Can be dangerous. With the right permissions rm -Rf /* would remove most of the files on your hard drive without warning.

Protect yourself rm -i