Unix/Linux uses a hierarchical file system structure, relative path and absolute path are used to specify the location of files and directories within the file system.

File path

  • Absolute path An absolute path specifies the complete and exact location of a file or directory from the root directory, it always starts with a forward slash (”/”), which represents the root directory, for example /home/user/documents/file.txt is an absolute path.
  • Relative path A relative path specifies the location of a file or directory in relation to the current working directory. It does not start with a forward slash, instead it provides a path that built on your current directory, for example you are currently at /home/user, then documents.file.txt is a relative path.

Basic commands

CommandDescriptionRef
pwdShow the current working directory
touch <file_name>Create an empty file
mkdir <dir_name>Create the specified directory
lsList contents of the current directory
cd <dir_name>Move to the identified directory
rm <file_name>Remove a file
rmdir <\directory>Remove a directory
cp <\source> <\destination>Copy source to destination

Change directory (cd)

This command you can move into your desired subdirectory:

cd <directory>

cd options

  • Root directorycd /: a ’/’ option brings you to root directory instantly
  • Home directorycd ~: using a ’~’ option to move to home directory
  • Current directorycd .: this option tells shell to stay in the current directory
  • Parent directorycd ..: this option moves you to the parent directory
  • Previous directorycd -: go back to where you came from
  • Upper level directoriescd ../..: the first ../ moves up one level from current directory, the next ../ moves to the even upper directory, you can concatenate more to move to previous directories you desired.

Make directory (mkdir)

This command creates a new directory in the current working directory.

mkdir <directory_name>

mkdir options

  • Create singe directory mkdir <dir_name>: create single directory with no option
  • Create multiple new directorymkdir -p <parent_dir_name>/<child_dir_name>: -p or --parent option creates multiple directories in hierarchy, this will create parent directories that not exists.
  • Set directory permissionmkdir -m <permission> <dir_name>: this sets the permission of the directory upon creation, fine more on File Permission.

List directories (ls)

The ls command lists all files and directories (excluding hidden) in the current working directory.

ls

ls options

  • List all ls -a or --all: displays all files and directories including those are hidden(start with a dot ’.’).
  • List in long format ls -l OR ll: this option displays all information in detail, such as permissions, file size, etc…
  • List recursively ls -R or --recursive: lists files and directories within subdirectories as well.

Open command in MacOS (open)

The open command in MacOS is used to open files, applications, URL and more.

open <option> <file or directory or URL>

Open the current directory

The command will open the directory in Finder, the period (.) specifies the current directory. Similarly you can open the parent directory with double period (..).

open .

Open with specified application

To open a file or directory with a specified application, use the -a option. The application name should be warped with quotation marks.

open -a "Application Name" <filename>

BashBash_scriptingINFO1112Bash_utilitiesFile_manipulation