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
, thendocuments.file.txt
is a relative path.
Basic commands
Change directory (cd)
This command you can move into your desired subdirectory:
cd <directory>
cd options
- Root directory
cd /
: a ’/’ option brings you to root directory instantly - Home directory
cd ~
: using a ’~’ option to move to home directory - Current directory
cd .
: this option tells shell to stay in the current directory - Parent directory
cd ..
: this option moves you to the parent directory - Previous directory
cd -
: go back to where you came from - Upper level directories
cd ../..
: 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 directory
mkdir -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 permission
mkdir -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
ORll
: 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>
Bash Bash_scripting INFO1112 Bash_utilities File_manipulation