3 Apr 2016

12 Basic terminal commands and usage - Part 2

Part 2 of 12 Basic terminal commands and usage, lets learn some more commands!

This post is part 2, part 1 can be be found here

This post will cover the following commands
  • 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 useful extras at the end, lets begin!

cat - Concatenate file and print out

This command is used to print out the contents of a file to the terminal, it can be very useful to look at what is inside a file without opening the file up with nano or some other editor.
Usage of this command is simply cat followed by the file name or the full path to the file and the file name.
In the screenshot below I have typed

cat file_2.txt


file_2.txt being the file we created in the previous post using nano, we can see the contents of the file is then printed out on the screen. If we wanted to view a file that is not in our current working directory we would have to add the path to the file in the command. Note here the Tab key can be used to auto complete where possible! The commands I have entered to get the output in the screenshot below are

clear
 to clear my screen

pwd
To print my current working directory

cat /etc/h
I have then pressed Tab twice and I have been shown a list of my possible options

cat /etc/hostname
This prints out on screen the information in the file hostname located in the directory etc/


As with all these command across both of these posts there are many options available to use with this command, try typing 

cat --help
or
man cat
Both of these commands will bring up additional information about this command, --help and man can be used for most if not all commands we are using here if you need more detail.


mkdir - Make Directory

Again this is quite a simple command but a useful one, we are going to need some where to store all our files.

In the screenshot below I have typed

clear
 To clear my screen

pwd
To print my current working directory

ls
To list all files and directory's currently in my working directory

mkdir Blog
I have then create a new directory named Blog!

ls
To list everything, which shows our newly created directory Blog!



Simple stuff, if you want to create a directory within a directory you are not currently in you would do it like so.

mkdir Blog/Delete
This has then created a new directory within our Blog directory named Delete


We can then show our new directory using the ls command

ls Blog/
There we can see our new directory named Delete


Lets use our new Blog directory in the next section by moving a file in to it.


mv - Move or copy a File/Directory

The mv command is used to move files and directory's around, it can also overwrite files so be careful with this one.

In the screenshot below I have moved file_1.txt in to the Directory Blog/Delete/ we created earlier, this was done using the following commands

clear
 To clear my screen

pwd
To print my current working directory

ls
To list all files and directory's currently in my working directory

mv file_1.txt Blog/Delete
This moves file_1.txt in to the directory Blog/Delete/

ls Blog/Delete
Using this command we can now see our file_1.txt has moved where wanted it go!




If we then use ls again we can see our file is no longer in our working directory

ls


The mv command can also be used to move directory's, lets move the Delete directory from within our Blog directory to our current working directory home/pi/

mv Blog/Delete /home/pi

ls


Great now we can see the Delete directory has been moved in to our current working directory of /home/pi. 

The mv command will over write files of the same name if you try and move them to the same place. For this example we are going to create a new file using the cat command (didn't cover that before!)

If we type

cat > file_1.txt

we will then have to enter some text for our new file, I have typed "This is file one again"



Once you have entered your text for the file press enter and then Ctrl+d to save it. Right if we clear the screen and now take a look at our files using ls we can see we have created a new file_1.txt in our current directory.

clear

ls



If we use the command cat on this file we will be returned the text we entered.

cat file_1.txt

If we use the command cat on our other file_1.txt within our Delete directory we can see the difference

cat Delete/file_1.txt


Now what will happen if we move our file_1.txt from our current directory in to the directory Delete that has a file of the same name?!

mv file_1.txt Delete/file_1.txt

ls



Well we can see the file has gone from our working directory, what about the Delete directory?


ls Delete/
Shows us there is a file named file_1.txt in that directory

cat Delete/file_1.txt
This shows us that the original file_1.txt has been replaced with the file we just moved in there with the same name! See the text? it shows "This is file one again" instead of the original "this is file one".



We can be shown warnings about this by adding an option on to the use of the mv command, so very quickly I am going to clear my screen and create another file by the same name but this time move it with the -i option, lets see what happens!

clear
 To clear my screen

cat > file_1.txt
to create another file named file_1.txt in my working directory

ls
To show the new file has been created

cat file_1.txt
Print out the file we just created


Now this time when we try and move file_1.txt in to the Delete directory we will use the -i option or interactive mode by just adding -i after the mv command like below.

mv -i file_1.txt Delete/file_1.txt


We can see this time we are asked if we want to overwrite the file, No point in over writing the file again so enter n and press enter. We can then see our file has not moved! and if we cat the file in the Delete directory it has also remained unchanged.


There is not really any reason not to use the -i option whenever using the mv command as it could stop you from doing something you really didn't mean to do.


rmdir - Remove directory


Another simple command which is remove directory, though you may come across issues if you have files within the directory you are trying to remove. First of all lets remove the empty directory Blog/
we will use this command below

rmdir Blog/

If we then use ls we can see that the empty directory Blog/ has now been removed, great!


But what about if we want to remove a directory that has files in? Lets try,

rmdir Delete/


We can see we get given an error message - rmdir: failed to remove 'Delete/': Directory not empty.

Back to the rm command...

Well I want to delete the directory and its files. Even though there is only one file in that specific directory, I wouldn't want to be forced to delete every single file within a directory one by one just so I can remove the directory.

Using the rm command again just this time with an extra option -r we can remove the directory and all files within it!

In the screenshot below I have typed


clear
 To clear my screen

ls
To list all files and directory's in my current working directory

ls Delete/
To show there is a file in the directory we are going to delete named "Delete"

rm -r Delete/
Adding the option -r deletes the directory and also the files within it all at once.

ls
Listing the files and directory's once more to show the directory Delete is now gone!



Remember there is no recycle bin so only delete directory and files you no longer need.

sudo/su - Super user access!

The command sudo stands for Super User Do. It temporarily gives you full access to your system, allowing you to do, what you may not be able to using your pi account. You maybe wondering why if your the only user of this little Raspberry Pi your pi account does not have these access rights by default - I know I was when I first started using Linux! 

Basically you can break your system irreparably if you do the wrong thing while you have super user access so it is best to use sudo; only when you need to do something that requires permission your pi account does not have. Lets try an example.
Type in to your terminal

apt-get update

You will see you get a whole bunch of messages saying you do not have permission and are you root?
This is for your systems safety, you do not want to be doing things as root or super user without explicitly stating you want these powers. The old saying with great power comes great responsibility comes to mind.


If we type the same command but with sudo first we are allowed to proceed.

sudo apt-get update


Saying all that this is Linux we are working with not Windows, so we are not restricted. We can use the command su to just become the root user and have no restrictions - again this is not ideal if you do not know what you are doing - you can break stuff!

But anyway in the terminal type

su


You will be asked for your root account password which we set up in the first post, enter your password and you will see a change.


Instead of pi@raspberrypi you are now logged is as root@raspberrypi in your home directory, try typing

apt-get update

This would now work without the need for sudo like before, as you are now using the root account and have access to everything. To go back to using the pi user just type

exit


Last warning be very careful using this! Just use sudo when it is needed, it takes no time to type it in and may save a big mistake.


shutdown - Shut the system down


One of the first things I noticed when I got my first Raspberry Pi was, No Power button! So it seemed the only way to turn it off was to pull the power cable, which seems quite violent to me and perhaps not a good idea in some cases it may corrupt data.

We can shutdown or restart our Raspberry pi 3 using the shutdown command.

We can choose whether we want to fully shutdown using the 
-h option; h being for halt.
Or we can use the 
-r option; r being restart. 

Lets go through some examples below.

To get the Raspberry Pi to restart straight away we would type

sudo shutdown -r now

To get it to restart in 1 minute we would type
sudo shutdown -r 1
This will broadcast a message to all users that the system is going down in one minute and subsequently restart the system in 1 minute


Doing this will kill our SSH connection the Raspberry Pi so we will need to connect again using PuTTY.

If at the end of the day your finished with your Raspberry Pi and you want to shut it down completely not just restart we can type

sudo shutdown -h now

This will shut the Raspberry down completely straight away and to turn it back on, you will need to disconnect your power cable and reconnect it.

That being said there are not many times you will need to turn your Raspberry Pi off completely. Once we have all of our sensors and a web server running you are only ever likely to use the restart command as we want the Raspberry Pi running at all times if possible!

Useful Bonus commands

Here are a bunch of random, but useful commands for you to try out!

ifconfig
Get details of all your network interfaces, ie your wireless and Ethernet

hostname -I
Get Local IP address

curl -s http://whatismyip.akamai.com/
Get your external IP address

whoami
Displays the currents user

w
Displays all users currently connected and how long system has been up

top
Displays all processes, think windows task manger. Press Ctrl+c to exit out of this

history
Displays a historical list of all the commands you have used in the terminal

wget 
wget followed by the web address of a file will download that file to your current directory

man
man followed by the name, for example man apt will return the manual for apt


Hopefully these two posts have improved your confidence using the command line on your Raspberry Pi, see anything important I have missed out? Leave a comment below!

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