loading...
Cover image for Getting Started With the Terminal

Getting Started With the Terminal

dailydevtips1 profile image Daily Dev Tips Originally published at daily-dev-tips.com ・3 min read

The other day Carl made a useful comment:

Terminals and command lines scare me. Do you have any dev tips on getting started with these or know any good resources?

And it made me realize, I also was scared to use the Terminal at one point. It is one of these things wherein the beginning, you just don't know what's happening.

So let's walk through some basic commands today, which will make us more comfortable in using the Terminal.

Which Terminal to Use?

Perhaps a good starting point is which Terminal to use, to be honest, it's much of a preferred choice than actually making a difference. But I use iTerm2 which works beautiful!

If you rather stick to another choice or the default Terminal, be my guest, it won't make a difference in what we are going to do today.

Basic Terminal Commands

Oke, let's get cracking on some commands.

Bash Change Directory

cd command means change-directory, and it is the same as clicking on a folder on your regular desktop.

For instance:

cd Desktop // move into the desktop "folder"

We can also go up one level by using ..

cd ..
// Or even multiple levels
cd ../../

We can always go back to the starting point by using cd without arguments.

cd

Bash Where are We?

Every now and then, you forgot where you are, and you want to know what the current folder is.

You can use the pwd command Print Working Directory

pwd // Return something like: /Users/chrisbongers/Desktop

Bash List

Another handy command is ls it means list and can we used to show folders inside the directory we are in.

ls // Show current directory
ls .. // Show parent directory
ls Desktop // Show specific directory

Bash Creating Folders

Sometimes it's easier to create a folder in the Terminal because you are already there.

We can use mkdir make directory for this.

mkdir NewApp

Bash Removing

Be careful when using remove commands. The Terminal is strong and can remove system files, so use these with care.

We can use rmdir Remove Directory to remove a folder

rmdir NewApp

Or we can use rm Remove in general

rm testfile.txt

For the rm command we can give it the -r parameter which stands for recursive it will delete everything inside the folder you pass

rm -r NewApp

Bash Copy

We can also copy folders and files with the Terminal by using the cp Copy command

cp testfile.txt test2.csv

Where the first argument is the source and the second the destination file.

We can also copy a complete folder and contents:

cp -r NewApp TestApp

Bash Move

Another excellent command is mv Move. It works the same as the cp one but will move the elements instead of copying them.

mv testfile.txt Desktop/testfile2.txt

As you can see, we can even move and rename.

Bash Creating Files

Perhaps the most interesting one is the ability to create files.
There are multiple ways of creating files, the most common is touch, but my personal favorite is nano.

Nano works great because it's generic, it can create but also edit a file at the same time.

nano testfile.txt // Will create the file and open it!

Once you opened a file in nano you can type whatever you want, and once your done, use CTRL+X to close and save the file.

Resume

I hope these Terminal commands were helpful, and I challenge you to have a play around with these.

Let me know in the comments if there are any really good ones I might have missed.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Posted on by:

dailydevtips1 profile

Daily Dev Tips

@dailydevtips1

I write daily development tips to contribute to the development community!

Discussion

markdown guide
 

Hi there.

I'd like to suggest removing the prefix "Bash" on your headings, because these commands aren't specific to bash. You're using MacOS (as evidenced by iterm, Terminal-with-a-capital-T and your cover picture, and MacOS moved away from bash as a default a while ago. It uses zsh these days, so I wouldn't want new users to get confused over that. It doesn't matter, but if you're new, you don't know it doesn't matter. You know?

 

Yes actually very good and valid point, will have a look at refixing that.
Thanks! 😊

 

Being able to navigate the terminal is very important, never know when you'll have to remote into a server. This article is definitely a good starter. My only nitpick is that nano isn't really a command for creating files, its a terminal based text editor. Its more synonymous to vim than to touch. While you can use nano in that way, it should be clear that it isn't the alternative to touch.

 

You are completely right, I use nano because it's more versatile for me to use, then vim.
I find vim a bit more work for some reason.

 

Nice write-up! A few things I'd like to share that seem to go amiss when introducing someone and even experienced programmers to the terminal and a shell...

  1. how to get help without defaulting to Google all the time -- built-in Unix utilities like all the ones that you've listed usually have options. The easiest way to know what's available is to resort to the manual aka (man pages) that can simply be invoked with $ man <some util>, (e.g. $ man ls). It is very useful to use ls -l (long format) and ls -la (long with directories)

  2. man pages typically adopt vi movements, but without diving into that, they can simply be searched by first hitting the / (forward slash) followed by some keyword or option (e.g. /foobar) will search for foobar in the man pages

  3. using slash will take you directly to the first match, we can use n (lowercase) to go to the "next" instance and <shift>-n (capital) to go back

  4. most of the time when we make a new directory, we'd also like to change into it after, but we can do it in a single command: $ mkdir foo && cd $_. The last command is captured in the _ (underscore) var and to access variables, we use the $ prefix

  5. if we're moving back and forth between two directories, we can also use - to go back and forth instead of trying to remember or pushing up/down to find the command that took us into that directory:

$ pwd 
/Users/me/Desktop

$ cd foo
foo $

foo $ pwd
/Users/me/Desktop/foo

foo $ cd -
/Users/me/Desktop
  1. readline commands - allow you to navigate within line and edit. This is a huge efficiency gain. See: readline.kablamo.org/emacs.html (it's the first result that came up, so disregard the emacs in the URL). Surprisingly most of our tools are built with readline, so these typically work in any place that allows you to type text (i.e. Chrome, Apple Notes, Google Doc, etc) so they are worth learning even outside of a shell

  2. previous/next commands (stop using the up/down arrow) - if you're a touch-typer that does not like to remove your hands away from the "home row", this one's for you! <shift>-p (mnemonic for "previous") is the same as the up-arrow and if you scrolled past it, then <shift>-n (mnemonic for "next") is the same as the down-arrow. But what's even better? reverse-i-search activated with <ctrl>-r followed by keyword(s) and it will find the first command that matches what you type. You may hit <ctrl>-r repeatedly to go to the next previous match and to move forward, <ctrl>-s

 

Yes! Very nice additions indeed, will look into incorporating some of these for sure!