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 directorytouch: Create file. For example,touch index.htmlcreates an empty file namedindex.html. If the file already exists, it updates the file's last modified date.rm: Remove file. For example,rm index.htmlremoves the file namedindex.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-roption to remove a directory and all its contents recursively. For example,rm -r directoryremoves the directory nameddirectoryand all its contents.cp: Copy file. For example,cp index.html about.htmlcopies the file namedindex.htmlto a new file namedabout.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.htmlprints the contents of the file namedindex.html. If the file doesn't exist, it does nothing.man <command>: Show manual for command. For example,man lsshows the manual for thelscommand.clear: Clear terminal screen.exit: Exit terminalhistory: Show command historyecho: Print text to terminal. It simply prints the text you provide to the terminal. For example,echo "Hello World"printsHello Worldto the terminal.tail: Print last lines of file. For example,tail index.htmlprints the last 10 lines of the file namedindex.html. You can specify the number of lines to print using the-noption. For example,tail -n 20 index.htmlprints the last 20 lines of the file namedindex.html. Ortail -n1prints the last line of the file.date: Print current date and time.grep: Search for text in file. For example,grep "Hello" index.htmlsearches for the textHelloin the file namedindex.html. You can use the-ioption 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 .htmllists 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:
Understanding Unix File System: Permissions and Hard Links
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. Admeans it's a directory. Other types include-for regular file andlfor symbolic link. - Permissions (
rwxrwxr-x): The next nine characters show file permissions, divided into three groups of three:- User (Owner) Permissions (
rwx):rfor read - file is readable.wfor write - file is writable.xmeans the file is executable.
- Group Permissions (
rwx):rfor read - file is readable by the group.wfor write - file is writable by the group.xmeans the file is executable by the group.
- Others Permissions (
r-x):rfor read - file is readable by others.-means the file is not writable or executable by others.
- User (Owner) Permissions (
- 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 isroot. - Group (
admin): The next field shows the file's group. In this case, the group isadmin. - 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, theetcfile is a symbolic link to theprivate/etcdirectory.
(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. Almeans 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):rfor read - file is readable.wfor write - file is writable.xmeans the file is executable.
- Group Permissions (
r-x):rfor read - file is readable by the group.-means the file is not writable or executable by the group.
- Others Permissions (
r-x):rfor 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 isroot. - Group (
wheel): The next field shows the file's group. In this case, the group iswheel. Wheel is a special group that allows users to use thesucommand 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 namedetc.
- User (Owner) Permissions (
(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-):rfor read - file is readable.wfor write - file is writable.-means the file is not executable.
- Group Permissions (
r--):rfor read - file is readable by the group.-means the file is not writable or executable by the group.
- Others Permissions (
r--):rfor 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 isMiles. - Group (
staff): The next field shows the file's group. In this case, the group isstaff. Staff is a special group that allows users to use thesudocommand 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 namedGoogleService-Info.plist.
- User (Owner) Permissions (