1 Apr 2016

12 Basic terminal commands and usage - Part 1

Carrying on from the last post where we successfully logged in to our Raspberry Pi using PuTTY on Windows, we will use this new SSH connection to explain some basic terminal commands!

Its alot easier than it looks and can be much quicker and simpler than using your mouse for everything.

Its quite difficult to cover each and every command we may ever use in two posts, this will get updated regularly with new and useful additions to the basic useful commands in part 2.
I have only included 12 (ish) commands, the ones I think you will need to know, even this small amount is to be spread over two posts. But if you are following along with these guides then I want you to be comfortable using this set-up.

Before we start if you are using Ctrl+c to copy commands here in and expecting to paste that in to your terminal you my have trouble, the Windows key command of Ctrl+p does not work.
We will need to use the Key combination of  Right Shift+Insert to paste in to the terminal or if you want you can right click and choose the option to paste each time, dependant on your settings just right clicking your mouse while in terminal may also paste easily.

Also note that you cannot use your mouse to place the cursor in the terminal you must use the arrow keys or alike to move the cursor around.






Starting from where we left off in the last post here we are SSH`ed in to our Raspberry Pi at the terminal command line.

This is where you will spend most if not close to all of your time using any Unix/Linux based system, unless you have specific requirements. At least for this project we will be using the command line for everything, within reason, as its by far the easiest method.

This will span over two posts and we will cover these specific topics below.

Part 1 - this post covers
  • ls - List directory contents
  • pwd - Print working directory
  • Tab Key - Got thrown in there is not a command
  • clear - Clear the screen
  • cd - Change directory
  • rm - Remove
  • nano - Not strictly a command, its a text editor - but we will use it often!
Part 2 - will cover
  • cat - Concatenate file and print out on the terminal
  • mkdir - Make Directory
  • mv - Move or copy a File/Directory
  • rmdir - Remove directory
  • sudo/su - Super user access!
  • shutdown - Shut the system down

And some extras to show you some details of what is happening on your system.



Lets begin with

ls - List directory contents


If we type in to the terminal

ls


Then we will be returned a list of all files of the directory our user currently is.


The blue text that is returned denotes folders and the white text here denotes .txt files which we will look at later. Different file types and directory's have different colours if you are using the default settings and following along with these guides. The ls command can be used to search different directory's than the one you are currently in. 
Lets say for example we want to list the files in the folder we can see in the screen shot above "Pimoroni", we would type in 

ls Pimoroni


This returns the result below, showing we have one folder in the folder or directory Pimoroni named Pianohat. 


The folder Pimoroni will not exist on your Raspberry Pi if you are following the guides, as it is something I have installed myself for a bit of fun. 

You can try this by typing  

ls /home

You will be returned results showing the directory pi



From this we can see the command ls will list files or directory's in a directory, but how do we know what directory we are currently in, lets move on to the next command pwd!


pwd - Print working directory

This is a really simple one, literally this command will just return the directory you are currently in. Type in to the terminal

pwd


You will be returned with directory you currently are in... Simple no?! Add ls in to that and you can see where you are and what files or directory's you have available for use there.


Tab key

Its worth just mentioning this, even though it is not a command - it is one of the most useful features of a Unix/Linux command line and that is the Tab key

That is the key 2 keys under your Escape key on your keyboard. See it?

This key can be used as an auto complete of sorts when using the terminal for example if you type in to the terminal 

ls pyt

and then press the tab key you will see that the tab key auto completes the rest of the name for you.



Pressing enter on this will return a long list of all the files with in that directory python_games/


The Tab key can be used to auto complete pretty much everything, everywhere using the terminal, if you try and auto complete something that is not unique and press tab twice you will be shown all the possible options available to you. Below I have typed

clear

followed by

ls

and then 


ls D

I have then pressed the Tab key twice, this has returned me the options available shown below


You may not have the Downloads directory show up if you type in these commands, you may only see the directory's Desktop/ and Documents/ available. 

If I now complete a bit more of the file name by adding some extra characters on as below and press the Tab key I will be auto completed and away I go! 


I have no files on my desktop so I will not be returned anything for this command, this time.


The Tab key can be used for auto completing pretty much anything from the command line so if your trying to get to a specific directory and do not know the full name just press Tab, not sure the full name of the file you want to look at just press Tab. It is ever so useful and makes working with command line so much easier.

clear - Clear the screen

We actually already used this above and it is what it says, it clears the screen of text so you have more room to see what you are doing. See screen shots below for an example.

clear




Not much else to say about this command right now on to the next one!


cd - Change directory

This is a command you will be using alot, it changes your current working directory.
Lets see this in action, first type

pwd

You will be returned this


Now again type 

ls


From here it is your choice but for my example I am going to change directory to Documents/ so I will type in

cd Documents

I could of just typed 


cd Docu

and then just pressed the Tab key to auto complete where I want to go.



You should see the difference now that instead of just
pi@raspberrypi: ~ $ 
you get
pi@raspberrypi: ~/Documents $

If we now type


ls

We will be shown all files within this directory, again your files maybe different to what is shown on my screen shots.



We could also type

pwd

To been shown our current directory


If you type in

cd /


you will be taken to your root directory


rm - Remove

The remove command is essentially the same as deleting something using Windows except there is no recycle bin here so only remove what you really do not want.

Below I have change back to the home directory for my user name pi listed the contents and then removed the file named file_2.txt. 

You will not have these files but you will get the chance to create them in the next section so for now just know rm is the command you will need to delete stuff.

The commands in the screen shot below are as follows

cd /home/pi

ls


rm file_2.txt


ls


We can see using the ls command that our file has been deleted! We can go further and delete multiple files at once using a wild card the character * but we will not touch on that during this post.

nano - Text editor


Nano is a text editor, there are other choices available a different editor that is vastly popular is vi and both come installed on your operating system. Any sort of text editing will be done using nano for this project.

To open nano just type

nano




To exit nano press Ctrl+x together and you will be returned to the command line



Now to check the directory I am currently working in I have typed

pwd

followed by 


nano file_2.txt




This opens a new file with the name we have just entered, if the file already existed it would have opened that if possible, I am just going to enter the simple text - this is file two



To save my files I can either press Ctrl+x and press y to confirm changes or press Ctrl+o to write out with out closing nano. If I didn't want to save anything at all I could press Ctrl+z.

In the screenshot below I have pressed Ctrl+o to write out the file and am asked to confirm the file name to write out to, I could change it here if required but I will just press enter to confirm.


It then shows in the bottom centre - wrote out 1 line, which means our file has been saved.


If we then press the Ctrl+x keys nano will exit out to the command line. If we then type

ls

We will see that our new file we just made shows up!


If we wanted to edit that file we would type

nano file_2.txt


And we would have the file opened for us to edit in nano again


There is alot more to nano, but for now this is enough we will be using this alot during this project so we will become very comfortable using it.

The next post which can be found here will continue this topic about basic commands within a Unix/Linux environment, it turned out to be a bit much for one post!


Show support for aquaponicpi and like the Facebook page! 
Click on the icon below to be taken to aquaponicpi on Facebook!
follow me on facebook


No comments:

Post a Comment