The Shell Command Line and File System Basics

1. Common Commands

  • pwd: Print working directory.
  • ls: List directory contents.
  • cd: Change directory. In Unix-like operating systems, the root directory is represented by a forward slash (/). . represents the current directory, and .. represents the parent directory. For example, cd .. changes the current directory to the parent directory. cd / changes the current directory to the root directory. cd . does nothing because it changes the current directory to the current directory. cd ../.. changes the current directory to the grandparent directory. cd ~ changes the current directory to the home directory. cd - changes the current directory to the previous directory.
  • mkdir: Make directory
  • touch: Create file. For example, touch index.html creates an empty file named index.html. If the file already exists, it updates the file’s last modified date.
  • rm: Remove file. For example, rm index.html removes the file named index.html. If the file doesn’t exist, it does nothing. If the file is a directory, it removes the directory and all its contents. You can use the -r option to remove a directory and all its contents recursively. For example, rm -r directory removes the directory named directory and all its contents.
  • cp: Copy file. For example, cp index.html about.html copies the file named index.html to a new file named about.html. If the file already exists, it overwrites the file. If the file doesn’t exist, it creates a new file. If the file is a directory, it copies the directory and all its contents.
  • mv: Move file.
  • cat: Print file contents. For example, cat index.html prints the contents of the file named index.html. If the file doesn’t exist, it does nothing.
  • man <command>: Show manual for command. For example, man ls shows the manual for the ls command.
  • clear: Clear terminal screen.
  • exit: Exit terminal
  • history: Show command history
  • echo: Print text to terminal. It simply prints the text you provide to the terminal. For example, echo "Hello World" prints Hello World to the terminal.
  • tail: Print last lines of file. For example, tail index.html prints the last 10 lines of the file named index.html. You can specify the number of lines to print using the -n option. For example, tail -n 20 index.html prints the last 20 lines of the file named index.html. Or tail -n1 prints the last line of the file.
  • date: Print current date and time.
  • grep: Search for text in file. For example, grep "Hello" index.html searches for the text Hello in the file named index.html. You can use the -i option to make the search case-insensitive.
  • |: Pipe. It takes the output of one command and uses it as input for another command. For example, ls | grep .html lists all files in the current directory that end with .html.

2. The ls -l Command and File Permissions

The ls -l command lists files in a directory in long format. It shows the following information for each file:

When working with Unix-like operating systems such as Linux and macOS, understanding file permissions and hard links is essential. Here’s a guide to help you navigate these concepts.

File System Basics

A file system is a method for storing and organizing files on a storage device. It defines how data is stored, retrieved, and updated. The Unix file system is hierarchical, meaning it uses directories to organize files into a tree structure. The root directory is the top-level directory, and all other directories are subdirectories of the root directory.

In Unix-like operating systems, the root directory is represented by a forward slash (/). For example, the path /Users/username/Documents refers to the Documents directory in the username directory in the Users directory in the root directory.

Inodes

An inode is a data structure that stores metadata about a file. It contains information like the file’s size, owner, permissions, and timestamps. Each file has an inode, and the inode number uniquely identifies the file. When you create a file, the operating system allocates an inode and assigns it a number. The inode number is not visible to the user, but you can see it using the ls -i command.

(base) ~/MyWorkSpace/Sep769_project> ls -i
97787252 GoogleService-Info.plist    95700643 Sep769_project/
98715063 Podfile                     95700641 Sep769_project.xcodeproj/
95987916 Podfile.lock                95987913 Sep769_project.xcworkspace/
95987893 Pods/

File Permissions

When you create a file, the operating system assigns permissions to the file. File permissions determine who can read, write, or execute a file. When you list files in a terminal using the ls -l command, you’ll see an output like this:

(base) /> ls -l
total 10
drwxrwxr-x  63 root  admin  2016 Jan 12 19:14 Applications/
drwxr-xr-x  75 root  wheel  2400 Oct  1 18:35 Library/
drwxr-xr-x@ 10 root  wheel   320 Sep 16 09:28 System/
drwxr-xr-x   5 root  admin   160 Oct  1 18:32 Users/
drwxr-xr-x   4 root  wheel   128 Jan 12 19:14 Volumes/
drwxr-xr-x@ 39 root  wheel  1248 Sep 16 09:28 bin/
lrwxr-xr-x@  1 root  wheel    11 Sep 16 09:28 etc@ -> private/etc
lrwxr-xr-x   1 root  wheel    25 Nov 21 23:22 home@ -> /System/Volumes/Data/home

(base) ~/MyWorkSpace/Sep769_project> ls -l
-rw-r--r--@  1 Miles  staff   1128 Oct 12 22:11 GoogleService-Info.plist
-rw-r--r--@  1 Miles  staff    792 Oct 17 19:06 Podfile
-rw-r--r--   1 Miles  staff  33117 Oct 17 18:46 Podfile.lock
drwxr-xr-x  39 Miles  staff   1248 Oct 17 18:46 Pods/
drwxr-xr-x  27 Miles  staff    864 Oct 18 23:16 Sep769_project/
drwxr-xr-x@  6 Miles  staff    192 Oct 18 22:17 Sep769_project.xcodeproj/
drwxr-xr-x@  5 Miles  staff    160 Oct  7 22:33 Sep769_project.xcworkspace/

(1). drwxrwxr-x 63 root admin 2016 Jan 12 19:14 Applications/

  • Type of File (d): The first character indicates the file type. A d means it’s a directory. Other types include - for regular file and l for symbolic link.
  • Permissions (rwxrwxr-x): The next nine characters show file permissions, divided into three groups of three:
    • User (Owner) Permissions (rwx):
      • r for read - file is readable.
      • w for write - file is writable.
      • x means the file is executable.
    • Group Permissions (rwx):
      • r for read - file is readable by the group.
      • w for write - file is writable by the group.
      • x means the file is executable by the group.
    • Others Permissions (r-x):
      • r for read - file is readable by others.
      • - means the file is not writable or executable by others.
  • Number of Links (63): The first number indicates the number of links to the file. This number is the number of directory entries plus one. For example, if a file is in two directories, it has two links. If a file is in one directory, it has one link. If a file is in no directories, it has zero links.
  • Owner (root): The next field shows the file’s owner. In this case, the owner is root.
  • Group (admin): The next field shows the file’s group. In this case, the group is admin.
  • Size (2016): The next field shows the file’s size in bytes. In this case, the file is 2016 bytes.
  • Last Modified (Jan 12 19:14): The next field shows the date and time the file was last modified. In this case, the file was last modified on January 12 at 19:14.
  • Name (Applications/): The last field shows the file’s name. In this case, the file is named `Applications
  • Symbolic Link (->): If the file is a symbolic link, the name is followed by an arrow (->) and the name of the file it points to. For example, the etc file is a symbolic link to the private/etc directory.

(2). lrwxr-xr-x@ 1 root wheel 11 Sep 16 09:28 etc@ -> private/etc

  • Type of File (l): The first character indicates the file type. A l means it’s a symbolic link.
  • Permissions (rwxr-xr-x): The next nine characters show file permissions, divided into three groups of three:
    • User (Owner) Permissions (rwx):
      • r for read - file is readable.
      • w for write - file is writable.
      • x means the file is executable.
    • Group Permissions (r-x):
      • r for read - file is readable by the group.
      • - means the file is not writable or executable by the group.
    • Others Permissions (r-x):
      • r for read - file is readable by others.
      • - means the file is not writable or executable by others.
    • Number of Links (1): The first number indicates the number of links to the file. This number is the number of directory entries plus one. For example, if a file is in two directories, it has two links. If a file is in one directory, it has one link. If a file is in no directories, it has zero links.
    • Owner (root): The next field shows the file’s owner. In this case, the owner is root.
    • Group (wheel): The next field shows the file’s group. In this case, the group is wheel. Wheel is a special group that allows users to use the su command to switch to the root user.
    • Size (11): The next field shows the file’s size in bytes. In this case, the file is 11 bytes.
    • Last Modified (Sep 16 09:28): The next field shows the date and time the file was last modified. In this case, the file was last modified on September 16 at 09:28.
    • Name (etc@): The last field shows the file’s name. In this case, the file is named etc.

(3). -rw-r--r--@ 1 Miles staff 1128 Oct 12 22:11 GoogleService-Info.plist

  • Type of File (-): The first character indicates the file type. A - means it’s a regular file.
  • Permissions (rw-r--r--): The next nine characters show file permissions, divided into three groups of three:
    • User (Owner) Permissions (rw-):
      • r for read - file is readable.
      • w for write - file is writable.
      • - means the file is not executable.
    • Group Permissions (r--):
      • r for read - file is readable by the group.
      • - means the file is not writable or executable by the group.
    • Others Permissions (r--):
      • r for read - file is readable by others.
      • - means the file is not writable or executable by others.
    • Number of Links (1): The first number indicates the number of links to the file. This number is the number of directory entries plus one. For example, if a file is in two directories, it has two links. If a file is in one directory, it has one link. If a file is in no directories, it has zero links.
    • Owner (Miles): The next field shows the file’s owner. In this case, the owner is Miles.
    • Group (staff): The next field shows the file’s group. In this case, the group is staff. Staff is a special group that allows users to use the sudo command to run commands as the root user.
    • Size (1128): The next field shows the file’s size in bytes. In this case, the file is 1128 bytes.
    • Last Modified (Oct 12 22:11): The next field shows the date and time the file was last modified. In this case, the file was last modified on October 12 at 22:11.
    • Name (GoogleService-Info.plist): The last field shows the file’s name. In this case, the file is named GoogleService-Info.plist.



Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • Python Review Notes
  • IAM and IAM Identity Center
  • Heap vs. Stack Memory in Java