DEV Community

Cover image for ⌨️ Get to know your terminal pt.1
Francisco Arenas 🐻
Francisco Arenas 🐻

Posted on

⌨️ Get to know your terminal pt.1

πŸ‘‹πŸ½ 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:

  1. The basics (definition, basic commands) 🐣
  2. How does the file management system works? πŸ‘€
  3. The shell and its magic-like functionality πŸ¦€
  4. 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. tree)
- 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 new_directory_name
$ mkdir test
$ ls
test
Enter fullscreen mode Exit fullscreen mode
  • To print the working directory: pwd
$ pwd
/Users/francisco
Enter fullscreen mode Exit fullscreen mode
  • To list every directory in current working space: ls
$ ls
test
Enter fullscreen mode Exit fullscreen mode
  • To change to another directory: cd directory_name
$ cd test
~/test $ 
Enter fullscreen mode Exit fullscreen mode
  • To remove a directory: rm -r directory_name
$ rm -r test
$ ls

Enter fullscreen mode Exit fullscreen mode
  • To create a new file: touch new_file
$ touch test_file
$ ls
test_file
Enter fullscreen mode Exit fullscreen mode
  • To copy a file: cp file new_destination
$ cp test_file copy_test_file
$ ls
copy_test_file  test_file
Enter fullscreen mode Exit fullscreen mode
  • 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 
Enter fullscreen mode Exit fullscreen mode
  • To delete a file: rm file
$ rm copy_test_file 
$ ls

Enter fullscreen mode Exit fullscreen mode
  • To see in an interactive manner a file: less file
$ less DEV.txt
Enter fullscreen mode Exit fullscreen mode

less command

  • To concatenate files or see the content of a single file: cat file file2
$ cat copy
This is the content of a test file
Enter fullscreen mode Exit fullscreen mode
  • To see the first 10 elements within a file: head file
$ head copy
This is the content of a test file
1
2
3
4
5
6
7
8
9
Enter fullscreen mode Exit fullscreen mode
  • To see the last 10 elements within a file: tail file
$ tail copy
10
11
12
13
14
15
16
17
18
19
Enter fullscreen mode Exit fullscreen mode
  • To see the type of a command: type command
$ type ls
ls is an alias for ls -G
Enter fullscreen mode Exit fullscreen mode
  • To consult how a command works via the manual: man command
$ man ls
Enter fullscreen mode Exit fullscreen mode

Iman command

  • To print something in the terminal: echo text
$ echo ls
ls 
Enter fullscreen mode Exit fullscreen mode

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.

For example:

  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • The symbol [[:upper:]]* returns all the files and/or directories that begin with an upper letter:
$ ls -d [[:upper:]]*
Documents Downloads Music Pictures Desktop
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

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:

  • The /etc directory stores system’s configuration information as well as startup and shutdown scripts for individual programs.
  • The /dev directory stores files of attached media such as a USB.
  • The /usr directory stores applications and utilities that are shared among users.
  • The /bin directory stores the needed executable binaries for basic commands (eg. ls, ping or cp) to properly work.
  • The /lib directory stores shared libraries.
  • The /tmp directory stores temporary files.
  • The /home directory stores the information (like proprietary directories) of a unique user. In the terminal is represented with the ~ symbol.

〉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:

πŸ”„ Redirections

Useful for:

πŸ’‘ Taking the shell’s stdin (<), stdout or stderr (>) and save it into a file.

Examples:

Saving the stdout in a file

$ ls Pictures > images.txt
$ cat images.txt
image1.png
image0.png
image2.png
Enter fullscreen mode Exit fullscreen mode

Saving the stderr in a file

$ ls Picture > images.txt
$ cat images.txt
ls: Picture: No such file or directory
Enter fullscreen mode Exit fullscreen mode

Saving the stdin in a file

$ sort < images.txt
$ cat images.txt
image0.png
image1.png
image2.png
Enter fullscreen mode Exit fullscreen mode

NOTE: Be aware that any time you use the individual symbols of > and < you will rewrite whatever is in your result file. To concatenate information use: >> or << πŸ€“.

🚬 Pipes

Useful for:

πŸ’‘ Concatenating commands |. Use the stdout as the stdin of another command.

Example:

Creating a file

$ ls Pictures | tee result.txt # tee creates a file within pipes
$ cat result.txt
image1.png
image0.png
image2.png
Enter fullscreen mode Exit fullscreen mode

πŸ•Ή Control operators

Useful for:

πŸ’‘ Concatenating and/or executing various commands in an asynchronous, synchronous or conditional manner.

Examples:

Asynchronous (multithread execution using the & operator)

$ ls & mkdir test
[1] 1669.  # --> thread 1 
[2] 1670.  # --> thread 2
[2]  + 1670 done         mkdir test  # --> thread 2
Enter fullscreen mode Exit fullscreen mode

Synchronous (Ordered execution using the ; operator)

$ ls ; mkdir test
Blog   # --> command 1
       # --> command 2
$ ls
Blog   test
Enter fullscreen mode Exit fullscreen mode

Conditional (Logic-based execution using the && and || operators)

# 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
Enter fullscreen mode Exit fullscreen mode

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 d)
  • A symbolic link (represented in the shell with l)
  • A special block (represented in the shell with b)

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 r)
  • Writing (represented in the shell with w)
  • Executing (represented in the shell with x)

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:

octal

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
Enter fullscreen mode Exit fullscreen mode

Conclusion

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)