👋🏽 Hello everyone! As always, I hope you are having an excellent day/night and I expect you are ready to learn something new today. 😉 In the following days I will be posting a series of 2 entries in which I’ll try my best to explain everything you need to know about our all-day partner: the terminal 💻.
In this particular entry I will show you:
- The basics (definition, basic commands) 🐣
- How does the file management system works? 👀
- The shell and its magic-like functionality 🦀
- Intro to file types and permissions 🗂
Without further ado, let’s begin!
〉The basics 🐣
The terminal is more than the program we use to look as cool hackers in front of others. Formally, we can define it as:
💡 A UI that allows us to interact directly with our operating system via input commands.
Before we continue, I must emphasize the fact that this UI emulates a shell interface or, said with more common words, a command-line interface. But, what is a command? Since its usability varies depending on the context, it can and has different definitions:
💡 A command is:
- An executable compiled program
- A native shell utility
- A downloaded utility (eg.
- A custom alias.
Thus, we can conclude that a command is, regardless of its origin, a particular action that will have an effect in our OS thanks to its introduction in the terminal 🤯. Now that we have defined some basic concepts, let’s see what we can do in practice.
🚀 Commands that should and must be learnt
- To make a directory:
$ mkdir test $ ls test
- To print the working directory:
$ pwd /Users/francisco
- To list every directory in current working space:
$ ls test
- To change to another directory:
$ cd test ~/test $
- To remove a directory:
rm -r directory_name
$ rm -r test $ ls
- To create a new file:
$ touch test_file $ ls test_file
- To copy a file:
cp file new_destination
$ cp test_file copy_test_file $ ls copy_test_file test_file
- To move a file to a new location or rename a file:
mv file new_destination
# RENAMING $ mv test_file copy $ ls copy_test_file copy ---------------------- # MOVING $ mv copy .. $ ls copy_test_file $ cd .. $ ls copy
- To delete a file:
$ rm copy_test_file $ ls
- To see in an interactive manner a file:
$ less DEV.txt
- To concatenate files or see the content of a single file:
cat file file2
$ cat copy This is the content of a test file
- To see the first 10 elements within a file:
$ head copy This is the content of a test file 1 2 3 4 5 6 7 8 9
- To see the last 10 elements within a file:
$ tail copy 10 11 12 13 14 15 16 17 18 19
- To see the type of a command:
$ type ls ls is an alias for ls -G
- To consult how a command works via the manual:
$ man ls
- To print something in the terminal:
$ echo ls ls
In addition to what we have seen, every shell or terminal emulator has something called wildcards. A wildcard can be perceive as:
💡 A special or reserved character that enables a more sophisticated search within our file management system by looking up for a particular pattern.
- The symbol
*returns all the files and/or directories that coincide with the preceded or proceded pattern:
# PRECEDED $ ls D* Desktop Documents Downloads --------------------------- # PROCEDED $ ls *.txt text.txt
- The symbol
?returns all the files and/or directories that fulfill the exact number of characters represented by the mentioned symbol:
$ ls image? image1 -------------------------- $ ls image??? image123
- The symbol
[[:upper:]]*returns all the files and/or directories that begin with an upper letter:
$ ls -d [[:upper:]]* Documents Downloads Music Pictures Desktop
- As before but with the expected twist. The symbol
[[:lower:]]*returns all the files and/or directories that begin with an lower-case letter:
$ ls -d [[:lower:]]* test
As you can see, the topic is extensive on its own. Thus, if you want to know more this link may be useful 😉.
〉The file management system 👀
Alright, now that you know a bunch of commands to type in your terminal, its time to go deeply in the rabbit hole of the command-line and explain why files and directories are more important than we once thought.
In a UNIX-like operating system (eg. Linux or MacOS) not only core functionalities of it live within a file management system, but the whole OS lives within a directory. This system -or The Linux File Hierarchy Structure to formally named it- defines the hierarchy, content and the whole structure of our OS. This is the reason why our interactions within the terminal are limited to edit, view and navigate across our system’s files and folders.
As everything in life, the hierarchy of our OS’s directory structure begins with a root 🌱:
- The root directory
/is the folder in which every existent directory and file is and will be saved.
To follow with the structure of the File System is kinda’ complicated since every operating system may have exclusive directories. However, here you have a list of some of which I consider essential to be aware of. In addition, you can visit geeks for geeks for more information regarding this topic:
/etcdirectory stores system’s configuration information as well as startup and shutdown scripts for individual programs.
/devdirectory stores files of attached media such as a USB.
/usrdirectory stores applications and utilities that are shared among users.
/bindirectory stores the needed executable binaries for basic commands (eg.
cp) to properly work.
/libdirectory stores shared libraries.
/tmpdirectory stores temporary files.
/homedirectory stores the information (like proprietary directories) of a unique user. In the terminal is represented with the
〉The shell functionality 🦀
Our shell unlocks great power if you know how to use it and how it works. Any command-line is able to interpret a command, display an output or an error message if we have done something wrong. This process is handled by a file descriptor, which is nothing but a number that serves as a unique identifier for three main standard events:
- input 👉🏽 A.K.A stdin, whose ID’s 0 and handles any entry from our keyboard or files.
- output 👉🏽 A.K.A stdout, whose ID’s 1 and prints the correct output of a command.
- error 👉🏽 A.K.A stderr, whose ID’s 2 and prints an error message whenever the command we introduced do not works.
This knowledge is useful not only to understand why our shell works the way it does, but because it allows us to use three powerful concepts:
💡 Taking the shell’s stdin (
<), stdout or stderr (
>) and save it into a file.
Saving the stdout in a file
$ ls Pictures > images.txt $ cat images.txt image1.png image0.png image2.png
Saving the stderr in a file
$ ls Picture > images.txt $ cat images.txt ls: Picture: No such file or directory
Saving the stdin in a file
$ sort < images.txt $ cat images.txt image0.png image1.png image2.png
NOTE: Be aware that any time you use the individual symbols of
< you will rewrite whatever is in your result file. To concatenate information use:
💡 Concatenating commands
|. Use the stdout as the stdin of another command.
Creating a file
$ ls Pictures | tee result.txt # tee creates a file within pipes $ cat result.txt image1.png image0.png image2.png
🕹 Control operators
💡 Concatenating and/or executing various commands in an asynchronous, synchronous or conditional manner.
Asynchronous (multithread execution using the
$ ls & mkdir test  1669. # --> thread 1  1670. # --> thread 2  + 1670 done mkdir test # --> thread 2
Synchronous (Ordered execution using the
$ ls ; mkdir test Blog # --> command 1 # --> command 2 $ ls Blog test
Conditional (Logic-based execution using the
# executed if both of the commands do not have a stderr (fd: 2) $ mkdir test1 && ls Blog test test1 ------------------- # executed if one of the commands do not have a stderr (fd: 2) $ cd test2 || ls Blog test test1
These examples were simple. Thus, I will leave you with this link if you are interested in knowing more about it.
〉File types and permissions 🗂
To conclude with this blog entry, let’s introduce ourselves to permissions and file types and owners. A file in UNIX-like operating systems can be classified as:
- A normal file (represented in the shell with
- A directory (represented in the shell with
- A symbolic link (represented in the shell with
- A special block (represented in the shell with
For now I will not explain in detail what does these types mean. Yet, be aware that these characteristics allow us to interact in specific ways with our files. However, despite its type, every file has the same permissions options:
Reading (represented in the shell with
Writing (represented in the shell with
Executing (represented in the shell with
These modes affect or can be configured to three different actors:
- The owner of the file 🤓
- A group of users 👯♀️
- The rest of the world (someone who is not part in one the previous actors) 🌎
This is important since each permission can be activated or deactivated (represented in the shell with
-) to a specific actor using the octal numeric system:
To change the permissions use the command
chmod octal_number file:
$ ls -l drwxr-xr-x 5 francisco staff 160 Feb 1 19:28 Developer $ chmod 777 Developer $ ls -l drwxrwxrwx 5 francisco staff 160 Feb 1 19:28 Developer
You did it to the end. Thank you so much ❤️! Remember that this guide is not complete yet. Nonetheless, I hope you found it useful 🤓. I would like to give special thanks to the Platzi community. Specially to professor Enrique Devars, who taught me everything you saw and will see in these terminal-related posts 💚😉.
Programen bonito 💻!
Top comments (0)