What is Unix Shell? More importantly, why would you ever need to use it?
Two words. Automation & efficiency. Of course, there are many other pros of Unix and shell, but for me it comes down to automation of repetitive manual tasks and doing so quickly and efficiently.
The Unix Shell is a command-line interface (CLI), that allows users to interact with the operating system. There are many commands and combinations of these commands that make it a powerful tool, today we will cover some of the basic, and essential, commands.
At one point or another, you will have to create, read, update, and delete files and directories. These four operations are commonly referred to as CRUD operations.
Here are the commands you can use to carry out each of these actions:
**Note: When entering commands do not forget to add space or dashes where needed, as it is case and space sensitive. ☺️
Create a new file: touch
melissa$ touch filename
Create, or “make”, a new directory: mkdir
melissa$ mkdir DirectoryNameHere
melissa$ ls ##this command lists all items within current directory
Project 1 Imporant-Files Repo
Icons learning-commands my-folder
personal-directory index.txt DirectoryNameHere
Read a file: cat
melissa$ cat filename
This would display the content within the file name you named after the command.
Read the beginning of a file: head
melissa$ head filename
Read the end of a file: tail
melissa$ tail filename
Read one page at a time: less
melissa$ less filename
Update: nano
melissa$ nano filename
To add or append text to the end of a file: echo
Practice melissa$ echo "Add your string of text here" >> fileName.txt
#example below, Practice is a directory currently open
Practice melissa$ echo "Hi" >> practice.txt
Practice melissa$ cat practice.txt
Hi
Delete an empty file or directory: rm
$ rm <file or directory name>
Delete a file or directory containing contents: rm -r
$ rm -r directoryname
Other Basic commands:
To “backout” or return to the parent directory of the directory currently open use cd ..
It is important to note that failure to add the space between “cd” and “..” will not change your directory and return a command not found message.
Here is an example when using cd ..
to return to the parent directory.
Practice melissa$ cd ..
Documents melissa$ #notice how it is no longer in the "Practice" directory
To print the pathname of the current working directory”
Practice melissa$ pwd
/Users/melissa/Documents/Practice
To list the contents of the current directory use the ls
command.
melissa$ ls
Project1 Practice Tutorial my-folder my-first-repo index.txt
Icons Super-Important-Stuff hello-world Important Files Another-Folder
#Notice how it is listed and compare with the example below.
Very much like learning a new language, putting together a couple keywords together can make things happen. Here a few command combinations to try:
To list the contents of the current directory in a long list use ls -l
Documents melissa$ ls -l
total 8
drwxr-xr-x 2 melissa staff 64 Feb 7 00:13 Important Files
drwxr-xr-x 12 melissa staff 384 Feb 5 19:20 Icons
drwxr-xr-x 3 melissa staff 96 Mar 8 00:43 Practice
drwxr-xr-x 3 melissa staff 96 Feb 23 23:39 Project1
drwxr-xr-x@ 8 melissa staff 256 Feb 18 18:53 Tutorial
drwxr-xr-x@ 8 melissa staff 256 Feb 17 22:22 hello-world
drwxr-xr-x 6 melissa staff 192 Feb 18 21:54 Super-Important-Stuff
drwxr-xr-x 2 melissa staff 64 Feb 6 23:25 Another-Folder
drwxr-xr-x 5 melissa staff 160 Feb 16 21:43 my-first-repo
drwxr-xr-x 6 melissa staff 192 Feb 11 00:01 my-folder
-rw-r--r-- 1 melissa staff 45 Feb 7 19:45 index.txt
To list the contents of a directory including hidden files use ls -a
melissa$ ls -a
. .DS_Store .. .localized Project1 Practice Tutorial my-folder
my-first-repo index.txt Icons Super-Important-Stuff
hello-world Important Files Another-Folder
#Notice ..DS_Store and .. .localized, these were the hidden files
ls
lists the contents of the current directory but to list files that were recently changed last use ls -l -rt
. The command ls
will list the contants, the -l
will list it in long form and the -rt
will reverse the order of the list and sort it according to its timestamp. This is helpful when you need to quickly identify which file was recently modified.
Documents melissa$ ls -l -rt
total 8
drwxr-xr-x 12 melissa staff 384 Feb 5 19:20 Icons
drwxr-xr-x 2 melissa staff 64 Feb 6 23:25 Another-Folder
drwxr-xr-x 2 melissa staff 64 Feb 7 00:13 Important Files
drwxr-xr-x 5 melissa staff 160 Feb 16 21:43 my-first-repo
drwxr-xr-x@ 8 melissa staff 256 Feb 17 22:22 hello-world
drwxr-xr-x@ 8 melissa staff 256 Feb 18 18:53 Tutorial
drwxr-xr-x 6 melissa staff 192 Feb 18 21:54 Super-Important-Stuff
drwxr-xr-x 3 melissa staff 96 Feb 23 23:39 Project1
drwxr-xr-x 3 melissa staff 96 Mar 8 00:43 Practice
drwxr-xr-x 6 melissa staff 192 Mar 22 00:01 my-folder
-rw-r--r-- 1 melissa staff 45 Mar 23 19:45 index.txt
#Notice the difference between this list and the one listed earlier.
To change to a different directory cd
melissa$ cd ~/DirectoryName
# Or
Documents melissa$ cd Practice
Practice melissa$
# Notice how it changed from Documents to the Practice Directory.
And for this command, I like to think of it as the “CTRL + F” of shell commands. To search for the occurrences or patterns in a text file or files you can use grep
and --color
to highlight the occurrences in color.
grep --color "keyword" name-of-file.txt
Practice melissa$ grep --color "puppies" cute-puppies
This afternoon on my drive back home I passed by an animal shelter.
They had large poster boards advertising puppies up for adoption. I stopped and saw a small play pen filled with small, cute puppies.
I have always wanted to have puppies.That afternoon,
I went home with 7 puppies 🥰.
There are many, many more, but knowing a few of the basics will go a long way.
Happy learning!