Quick! Without using Google, could you tell me what day of the week January 1, 2000 was?
Unless you have super human powers, the likely answer is no.
Let's try another question. What if I asked you to create three new empty text files named text1.txt, text2.txt and text3.txt on your desktop. How long would this take? Would it take longer to create eight new files?
By using the command cal -my 01 2000 in a bash shell we can find out that January 1, 2000 was on a Saturday. At the end of this article I will show you a simple way to create any number of files on your desktop.
What is a Shell?
A shell is a user interface that takes commands from the keyboard and sends them to the operating system. A basic understanding of how to interact with your computer from the command line can open up new possibilities and increase your productivity.
While this article covers interacting with Bash shell on a mac, many of the topics and concepts apply to other operating systems and shells.
Getting Started
A terminal emulator is often used to connect to a shell. The default terminal emulator on a mac is call terminal. Terminal can be started by using Spotlight and typing terminal.
Figure 1
Once terminal is launched, a window similar to the one in figure 2 is displayed. Terminal defaults to log you into a Bash shell, but there are many command shells to choose from.
Figure 2
Now that we have a shell open we can start sending commands to the operating system. First, let's learn how to end a shell session. The exit command will log you out of the shell. Simply type exit and hit enter.
Figure 3
Figure 4
The exit command is a builtin command. For more detail open a new shell window and type man exit (figure 4). The man command, short for manual pages, provides help for shell commands. For more detail type man man (figure 5). The help command can be used for a simple description of a builtin command (figure 6).
Manual pages are open in a program called less. Type man less for the less manual pages.
Three important key commands to know when using less are:
space(goes to the next page)
p(goes to the previous page)
q(exits the pager)
Figure 5
Figure 6
Four more commands
So far we've learned about exit, man and less. Knowing these three commands are the building blocks for getting started.
Next take a look at ls, pwd, cd and touch.
Spend some time to read the manual pages to find out what each of these commands do. If it is a builtin command don’t forget to used the help command.
Here’s a brief summary of the commands:
<strong>pwd</strong> – return working directory name
<strong>ls</strong> – list directory contents
<strong>cd</strong> – cd is a builtin command (change the current directory to DIR)
<strong>touch</strong> – The touch utility sets the modification and access times of files. If any file does not exist, it is created with default permissions.
Putting it all together
Now let’s look at the question we started with: How would you create three new text files on your desktop? While something like this might manually be rather tedious, in your shell you can do this with just two commands:
cd Desktop
touch text1.txt text2.txt text3.txt
Figure 7 shows my terminal, my desktop in finder and my desktop before I run the commands. Figure 8 shows everything after the commands are run.
This command can be shortened to: touch text{1..3}.txt
If you want to create 8 new files you can run: touch text{1..8}.txt
Figure 7
Figure 8
Creating new files is just one simple example of what can be done with a shell. Creating new blank documents may not be overly useful, after all how often do you do that, but what if you wanted to create new folders to organize your documents or photos? Using the mkdir command we can easily create any number of new directories. Using the command below will create directories since 2005.
mkdir {2005..2015}
Finding other commands
Want to find more commands on your system? When you are in your shell, if you hit the tab key twice you will get a prompt asking if you want to see all of the possible commands. If you type a letter and then tab you will see every command that starts with that letter (figure 9).
Figure 9
The best thing to do now is to take some time, read the manual pages and practice the commands we have reviewed. Use the man command to explore other commands on your system. The more time you spend in your shell the more comfortable you will become.