Overview
Most computer Operating Systems (OS) provide a graphical user interface (GUI or “gooey”) to interact with systems through point-and-click mouse movements. However, using the command-line interface (CLI) provides a more concise and powerful means to control a program or OS. Moreover, using programs with a CLI are usually easier to automate via scripting, which is a necessity for any serious developer, programmer, software engineer, or computer scientist.
Note: the words directory and folder are used synonymously.
pwd
Print the full filename of the current working directory.
$ pwd
ls
List the contents of the current directory.
$ ls
cd ..
The change directory and double period combination will move to the parent directory of the current directory.
$ cd ..
cd ~
The change directory and ~ (tilde) combination will return to the home directory. It is an alternative to using the $HOME command.
$ cd ~
cd /
The change directory and / (forward slash) combination will return to the root directory.
$ cd /
Creating Directories
mkdir
Make directory is used to create a new directory in the current directory or in another directory. It can also create multiple directories by providing a list of file paths, separated by spaces.
# Create a new directory in the current directory.
$ mkdir dev_projects
$ ls
dev_projects/
# Create a new directory in another directory.
$ mkdir dev_projects/test_1
$ ls dev_projects/
test_1/
# Create multiple directories in another directory.
$ mkdir dev_projects/ex_1 dev_projects/ex_2
$ ls dev_projects/
ex_1/ ex_2/
Creating Files
touch
Touch can create a new file in the current directory or another directory. It can also create multiple files by providing a list of file paths, separated by spaces.
# Create new file in current directory.
$ touch test.py
$ ls
test.py
# Create new file in another directory.
$ touch dev_env/hello-world.py
$ ls dev_env/
hello-world.py
# Create multiple files in another directory.
$ touch dev_env/ex1.py dev_env/ex2.py
$ ls dev_env/
ex1.py ex2.py
ls -lSh
Use ls (list directory contents), the -l (long listing format), -S (sort by file size), and -h (human readable) flags to view additional information.
$ ls -lSh test_env/
# Total in Kilobytes.
total 4.0K
# The leading dash (-) means this is a file.
-rw-rw-r-- 1 user user 19 Feb 19 06:41 example.py
Removing Files
rm
The remove command is used to delete a single file or multiple files.
# Remove a single file.
$ ls
example.py
$ rm example.py
# Remove multiple files at once.
$ ls
note_1 note_2 note_3
$ rm note_1 note_2
$ ls
note_3
Removing Directories
rmdir
The remove directory command will only delete a completely empty directory.
$ ls
ex_1/ ex_2/ test_1/
$ rmdir test_1/
$ ls
ex_1/ ex_2/
rm -rf
The rm command in conjunction with the -rf flag will delete the directory and all files and folders inside. This is done with recursive force, using an approach called recursion.
Use this command with extreme caution!
$ ls
ex_1/ ex_2/
$ rm -rf ex_1/
$ ls
ex_2/
Summary
✔️ ls -lSh
- list a directory’s contents, sorted by file size.
✔️ cd
- change to a different directory.
✔️ pwd
- print the current working directory’s location.
✔️ touch
- create a new file or update a file’s timestamp.
✔️ mkdir
- make a new directory.
✔️ rm
- remove a file.
✔️ rmdir
- remove an empty directory.
✔️ rm -rf
- permanently remove a directory and its contents (caution is advised).