zoola.jpg

header.png


Home
IWH Software
My projects
Retro stuff
Random stuff
About me
Contact me
Admin login




Raspberry Pi useful terminal commands

Using the command line is scary to some users, they'd prefer to use a GUI, however to get the most from your Raspberry Pi you will need to use it.
These are the terminal commands I find useful, but not easy to remember. So I put them here for myself and everyone to refer to.

This page is still work in progress.

Notes
You can recall previous commands by pressing the up cursor key.
If any filenames have spaces in, then use an escape character (backslash) before them, ie, /home/ian/Download/Hello\ there.txt.
Pressing the tab button will auto complete filenames, which will add escape characters for you, if there is a choice of filenames then a list will be displayed to help you.
You can combine commands with a double-ampersand &&, all the commands will be executed one at a time in order.

Man pages
To get a detailed (but confusing) description of each command, type man (short for manual) followed by the command you want more detail on, although these pages are usually replicated somewhere on the internet where it's easier to read.

ian@Home:/$ man wget

Change directory
Use the cd command to change the current directory. Use cd / to go back to root, cd .. to go to the parent directory and cd ~ to go to your home directory.

ian@Home:/$ cd /home/ian/Download
ian@Home:/$ cd /
ian@Home:/$ cd ..
ian@Home:/$ cd ~

SSH
To SSH into another Raspberry Pi from the first one
If you don't specify the username (pi@), it will try to log you in under the same user that you're logged in as on the first Pi, which may not exist on the second.
Type exit to exit.

ian@Home:/$ ssh pi@192.168.0.30

Speed test
A command line internet speed test.
You can replace the test10.zip with test100.zip and test500.zip for a longer test, the /dev/null part ensures the file is not saved anywhere, so it doesn't use up room on your disk.

ian@Home:/$ wget -O /dev/null http://speedtest.wdc01.softlayer.com/downloads/test10.zip

Disk imager
This is a handy command if you need to create a disk image of a large, but empty drive, like you've created an application for the Pi and want to allow users to be able to download the disk image, but you don't want to host a 8GB+ image size.
On another Linux machine (or live disc) use Gparted to shrink the partition size down to just slightly larger than the data and note the size of the new partition in Megabytes (and the device name, sda?). Gparted does display a graph of disk usage so this part is easy. If data has to be moved around then this operation may take a while.
Then run the below command, if= is the input device, of= is the output file, make sure the destination directory exists, bs= is block size, leave this at 1M, it'll make the size calculation easier, but the process slower, and count= is the amount of blocks you want copied (in Megabytes, bs=1M remember?), this will need to be slightly larger than the size you noted down earlier.
So on a 32GB card with 4.2GB used, I would shrink the main partition to around 4.5GB, a swap file may exist on the end, you should include this in the total size as well. Gparted does tell you the exact size of partitions when resizing so it's easy.
Then I would set the count value to 5000.
The imaging process will take a long time with no output on the screen, so find something else to do for a bit. When the process is complete, you will have a 5GB image of the SD card which you can write to an 8GB or larger card with the same command, just swap the if= and of= values around and omit the bs= and count= options.
However be careful to triple check you have the correct destination drive as the dd command will overwrite the system drive without warning.

You can then use either Gparted to grow the partition back to it's original size (on both cards), or use the option in raspi-config.

ian@Home:/$ sudo dd if=/dev/sdx of=/home/xxx/Downloads/image.img bs=1M count=1000

Disk usage
To find out the usage of all connected drives, the -h turns the data into a human readable format.

ian@Home:/$ df -h

Directory listings
A directory listing with permissions, owner and size details displayed

ian@Home:/$ ls -l

As above but includes hidden files etc.

ian@Home:/$ ls -al

CPU load averages
Returns the CPU usage. The values are averages over one minute, five minutes and fifteen minutes. a value of 1.0 on a single core processor means 100% CPU usage, a value of 1.0 on a dual-core processor means 50% CPU usage, a value of 1.0 on a quad-core processor means 25% CPU usage etc. A quad-core processor maxed out at 100% will show 4.0. To work out CPU loads disregard the one minute value, all systems have load spikes, you're looking at the five and fifteen minute values, if these are high, like 0.7 on a single core processor, then you may need to think about upgrading.
BTW the values can go over 1.0, this means the processor is too slow for what you're asking it to do and programmes are having to wait for the processor to catch up.

ian@Home:/$ uptime  | grep -oP '(?<=average: ).*'

Free memory
Returns the memory usage, Linux systems will use free memory as a cache for applications you may want to use, so the memory always looks fully used. Windows systems do the same with Pre/Superfetch, This cached memory can be instantly brought back into use by whatever needs it, so don't worry too much.

ian@Home:/$ free

Updating Raspian
To fully update the system in one go.
apt-get update updates the repositorys, apt-get upgrade performs the upgrade, the -y tells it not to ask for permission, and apt-get autoremove removes any unnecessary dependances.

ian@Home:/$ sudo apt-get update && apt-get upgrade -y && apt-get autoremove

Restart
Rebooting the system.

ian@Home:/$ sudo reboot

Shutdown
Shutting down the system. This actually turns off the system.

ian@Home:/$ sudo shutdown now

Copying and moving files
Copying files, this just copies files, it supports wildcards *

ian@Home:/$ cp /home/ian/Download/hello.txt /media/external/Temp
ian@Home:/$ cp /home/ian/Download/* /media/external/Temp

Renaming and moving files, use the same command for both operations, supports wildcards as above.

ian@Home:/$ mv /media/external/Transmission/* /media/external/Music/Music\ Albums
ian@Home:/$ mv /home/ian/Download/hello.txt /home/ian/Download/there.txt

Creating an empty file
Use touch to create an empty file

ian@Home:/$ touch hello.txt

Deleting files and folders
Use the rm command to delete files, it also supports wildcards, to delete an EMPTY folder use rmdir, or rm -R {folder}.
You may have to use sudo to get around permission problems

ian@Home:/$ rm hello.txt
ian@Home:/$ rmdir temp
ian@Home:/$ rm -R temp

Text file editing
Nano is a simple text editor, use the cursor keys to move around, press CTRL+X to exit, you will be asked if you want to save the file, or press CTRL+O then enter to save without exiting

ian@Home:/$ nano hello.txt

Changing ownership and permissions on file and folders
Chown to change ownership, you may require sudo if it's not your file, use this format user:group, I normally set them the same
Chmod to change permissions, again you may require sudo. 777 allows all users to read, write and execute the file, 755 allows the owner to read, write and execute, and other users to read only.
Both commands support wildcards, and you can use ls - l to check ownership and permissions

ian@Home:/$ sudo chown ian:ian hello.txt
ian@Home:/$ sudo chmod 777 hello.txt
ian@Home:/$ ls -l hello.txt

Clear screen
Sometimes you just want to clear the screen before working on something

ian@Home:/$ clear

View processes
A bit like Windows task manager, press CTRL+C to exit

ian@Home:/$ top

Controlling services
Starting, stopping, restarting and checking running services. Start and stop are obvious, restart is useful if you've edited a configuration file and want to apply the new settings without rebooting the whole system, and status tells you if it's running or not

ian@Home:/$ sudo service transmission-daemon start
ian@Home:/$ sudo service transmission-daemon stop
ian@Home:/$ sudo service transmission-daemon restart
ian@Home:/$ sudo service transmission-daemon status

Clearing out old configuration files
When you uninstall a programme using apt-get remove or the GUI equivalent, some configuration files are left behind just in case you reinstall it. The following command clears out all the configuration files from all uninstalled programmes, while reclaiming some disk space

ian@Home:/$ sudo dpkg --purge `dpkg --get-selections | grep deinstall | cut -f1`

© 2019 IWH Software (Ian Hill)