Linux Commands

Here are some fundamental and common Linux commands with example usage.  Used for configuring a Raspberry Pi device (like myZWave Controller):

Setting up Linux

https://www.ubuntu.com/download/desktop/create-a-usb-stick-on-windows

https://www.ubuntu.com/download/desktop

 

ADMINISTRATION

sudo startx

Launches the desktop on the Pi.

sudo apt-get install lightdm

Installs desktop on the Pi.

sudo reboot

Reboots the Pi.

sudo shutdown or sudo halt 

From a command line:  "sudo systemctl stop myServer" will also stop the application.

sudo shutdown -h now (or sudo halt)

You can’t use shutdown (or halt) unless you have sudo privileges.

-h means halt the system, now means do it immediately. You could also add number 10 to tell it to shut down in 10 minutes. You can even give a specific time 19:45 (in 24 hour format with a : colon).

Shuts down the Pi.

You should always gracefully shutdown the OS before pulling the power on the Pi.

("sudo" command for “run as admin” as the standard account is not an admin.  You can not shutdown or reboot the raspberry pi with a normal account)

 

Updates

Ensure myServer is not running prior to updating OS.

sudo systemctl stop myServer

sudo apt update (gets latest package list that can be upgraded)

sudo apt list --upgradeable (lists what packages will be upgraded)
sudo apt-get upgrade (upgrades all packages)

sudo apt full-upgrade

 

Sometimes with doing updates you get a package error.  Below is example of how to remove a package before doing the get upgrade:

sudo apt-get remove --auto-remove vlc vlc-bin vlc-nox

and then sudo apt-get install vlc vlc-bin

 

Check Version of OS

cat /etc/os-release

FILESYSTEM

LS

Lists the content of the current directory (or one that is specified). Can be used with the -l flag to display additional information (permissions, owner, group, size, date and timestamp of last edit) about each file and directory in a list format. The -a flag allows you to view files beginning with . (i.e. dotfiles).

CD

Changes the current directory to the one specified. Can use relative (i.e. cd directory A) or absolute (i.e. cd /home/pi/directory A) paths.

PWD

Displays the name of the current working directory, i.e. pwd will output something like /home/pi.

MKDIR

Makes a new directory, e.g. mkdir newDir would create the directory newDir in the present working directory.

RMDIR

Remove empty directories, e.g. rmdir oldDir will remove the directory oldDir only if it is empty.

RM

Removes the specified file (or recursively from a directory when used with -r). Be careful with this! Files deleted in this way are mostly gone for good!

Remove all directories including those with files in the current folder: rm -r *

CP

Makes a copy of a file and places it at the specified location (essentially doing a 'copy-paste'), for example - cp ~/fileA /home/otherUser/ would copy the file fileA from your home directory to that of the user other User (assuming you have permission to copy it there!). This command can either take FILE FILE(cp fileA fileB), FILE DIR (cp fileA /directoryB/) or -r DIR DIR(which recursively copies the contents of directories) as arguments.

MV

Moves a file and places it at the specified location (so where cp performs a 'copy-paste', mv performs a 'cut-paste'). The usage is similar to cp, somv ~/fileA /home/otherUser/ would move the file fileA from your home directory to that of the user otherUser. This command can either take FILE FILE(mv fileA fileB), FILE DIR (mv fileA /directoryB/) or DIR DIR(mv /directoryB /directoryC) as arguments. This command is also useful as a method to rename files and directories after they've been created.

TOUCH

Either sets the last modified time-stamp of the specified file(s) or creates it if it does not already exist.

CAT

Lists the contents of file(s), e.g. cat thisFile will display the contents of thisFile. Can be used to list the contents of multiple files, i.e. cat *.txt will list the contents of all .txt files in the current directory.

HEAD

Displays the beginning of a file. Can be used with -n to specify the number of lines to show (by default 10), or with -c to specify the number of bytes.

TAIL

Displays the end of a file. The starting point in the file can be specified either through -b for 512 byte blocks, -c for bytes, or -n for number of lines.

CHMOD

Normally used to change the permissions for a file. The chmod command can use symbols u (user that owns the file), g (the files group) , o (other users) and the permissions r (read), w (write) and x (execute). Using chmod u+x *filename* will add execute permission for the owner of the file.

CHOWN

Changes the user and/or group that owns a file. It normally needs to be run as root using sudo e.g. sudo chown pi:root *filename* will change the owner to pi and the group to root.

SSH

Secure shell. Connect to another computer using an encrypted network connection. For more details see SSH (secure shell)

SCP

Copies a file from one computer to another using ssh. For more details see SCP (secure copy)

SUDO

Run a command as a superuser, or another user. Use sudo -s for a superuser shell. For more details see Root user / sudo

DD

Copies a file converting the file as specified. It is often used to copy an entire disk to a single file or back again eg. dd if=/dev/sdd of=backup.img will create a backup image from an SD card or USB disk drive at /dev/sdd. Make sure to use the correct drive when copying an image to the SD card as it can overwrite the entire disk.

DF

Display the disk space available and used on the mounted filesystems. Usedf -h to see the output in a human readable format using M for MBs rather than showing number of bytes.

UNZIP

Extracts the files from a compressed zip file.

TAR

Store or extract files from a tape archive file. It can also reduce the space required by compressing the file similar to a zip file.

To create a compressed file use tar -cvzf *filename.tar.gz* *directory/*To extract the contents of a file use tar -xvzf *filename.tar.gz*

PIPES

A pipe allows the output from one command to be used as the input for another command. The pipe symbol is a vertical line |. For example to only show the first 10 entries of the ls command it can be piped through the head commandls | head

TREE

Show a directory and all subdirectories and files indented as a tree structure.

&

Run a command in the background freeing up the shell for future commands.

WGET

Download a file from the web directly to the computer e.g. wget http://www.raspberrypi.org/documentation/linux/usage/commands.mdwill download this file to your computer as commands.md

CURL

Download or upload a file to/from a server. By default it will output the file contents of the file to the screen.

MAN

Show the manual page for a file. To find out more run man man to view the manual page of the man command.

SEARCH

GREP

Search inside files for certain search patterns e.g. grep "search" *.txt will look in all the files in the current directory ending with .txt for the string search.

Supports regular expressions which allows special letter combinations to be included in the search.

AWK

Programming language useful for searching and manipulating text files.

FIND

Searches a directory and subdirectories for files matching certain patterns.

free -o -h

To see how much free system memory is available

WHEREIS

Finds the location of a command. Looks through standard program locations until it finds the requested command.

File Edit

Edit the samba config file.
sudo nano /etc/samba/smb.conf

sudo nano /boot/config.txt

NETWORKING

PING

Utility usually used to check if communication can be made with another host. Can be used with default settings by just specifying a hostname (e.g. ping raspberrypi.org) or an IP address (e.g. ping 8.8.8.8). Can specify the number of packets to send with the -c flag.

NMAP

Network exploration and scanning tool. Can return port and OS information about a host or a range of hosts. Running just nmap will display the options available as well as example usage.

HOSTNAME

Displays the current hostname of the system. A privileged (super) user can set the hostname to a new one by supplying it as an argument (e.g. hostname new-host).

IFCONFIG

Displays the network configuration details for the interfaces on the current system when run without any arguments (i.e. ifconfig). By supplying the command with the name of an interface (e.g. eth0 or lo) you can then alter the configuration (check the man-page for more details).

 

Display

Set Rotation: sudo ./adafruit-pitft.sh

Creating a SDMicro image using Linux

You need to have a linux environment with http://www.w3.org/2000/svg\">"); background-size: 1px 1px; background-position: 0px calc(1em + 1px);">gparted installed. If you are on a Mac or Windows, You will have to quickly download http://www.w3.org/2000/svg\">"); background-size: 1px 1px; background-position: 0px calc(1em + 1px);">virtualbox and run an ubuntu instance.

in order to install gparted in ubuntu,

sudo apt-get install gparted

Note: We used ubuntu 18.04 LTS for this tutorial

Step 1: Clone SD Card

Okay, assuming you are logged into ubuntu and have inserted the SD card containing the custom Raspbian OS which was used to run your pi, you can open up the terminal and list the disks as follows.

sudo fdisk -l

Note: If you are using a virtualBox, Please mount the memory card inside so that you can access it from the terminal.

This was run in a virtual box, depending on your system, the path of your sd card may change. In this example, as shown in the screenshot, the path to my sd card would be /dev/sdb.

Okay, now we need to completely clone the SD Card and we do it using the following command.

sudo dd if=/dev/sdb of=/your/path/to/clone.img

In this command if stands for input file which is my disk path /dev/sdb. This value may change in yours. And of stands for output file, which you can give any path you specify.

This process will take some time as it will copy what’s in your memory card block by block. The bigger the memory card size is, longer it would take. You will see in the command line when the process is finished.

Note: In order for this to complete in a virtualBox, your free disk space should be more than atleast twice the size of your SD Card.

In our case, the size of the memory card was about 14.5GB and it took approximately 20–30 mins to fully clone to our non SSD Hard Disk.

Step 2: Shrinking the Image

Okay, once the image was cloned, you might be wondering about the size of the image. In my case the image was 14.5gb. But my custom os only takes up about 4gb as I could remember. Shrinking the image was the next problem we had to tackle. For this, Linux is your only choice as gparted is only available in linux.

To do this, we are going to use pyShrink script file written by Drewsif which can be found http://www.w3.org/2000/svg\">"); background-size: 1px 1px; background-position: 0px calc(1em + 1px);">here.

Open up your terminal and execute the following commands line by line in your terminal.

wget  http://www.w3.org/2000/svg\">"); background-size: 1px 1px; background-position: 0px calc(1em + 1px);">https://raw.githubusercontent.com/Drewsif/PiShrink/master/pishrink.shchmod +x pishrink.shsudo mv pishrink.sh /usr/local/bin

Okay with that done, the rest is easy. To shrink your image, run the following.

sudo pishrink.sh /your/path/to/clone.img /your/path/to/clone-shrink.img

This will also take some time and finally it will give you an output saying how much your initial image was shrunk in the new file.

That’s pretty much it, and your custom image is now ready for flashing.

Step 3: Flash the image to a new SD Card

This is relatively easy. Download the http://www.w3.org/2000/svg\">"); background-size: 1px 1px; background-position: 0px calc(1em + 1px);">Etcher application for your OS and select your newly shrunken image, then select the new SD card and click the button Flash And you are all good.

Step 4: Compress the image further

Still with an image of 4gb+ it’s a bit too much to be shared with others on the internet. We can further compress this by running gzip.

gzip -9 /your/path/to/clone-shrink.img

This will zip your image file even further so that you can easily upload it and store in a cloud storage bucket or a drive.