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.
NotesYou 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 pagesTo 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.
Change directoryUse 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.
SSHTo SSH into another Raspberry Pi from the first oneIf 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.
Speed testA 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.
Disk imagerThis 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.
Disk usageTo find out the usage of all connected drives, the -h turns the data into a human readable format.
Directory listingsA directory listing with permissions, owner and size details displayed
As above but includes hidden files etc.
CPU load averagesReturns 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.
Free memoryReturns 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.
Updating RaspianTo 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.
RestartRebooting the system.
ShutdownShutting down the system. This actually turns off the system.
Copying and moving filesCopying files, this just copies files, it supports wildcards *
Renaming and moving files, use the same command for both operations, supports wildcards as above.
Creating an empty fileUse touch to create an empty file
Deleting files and foldersUse 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
Text file editingNano 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
Changing ownership and permissions on file and foldersChown to change ownership, you may require sudo if it's not your file, use this format user:group, I normally set them the sameChmod 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
Clear screenSometimes you just want to clear the screen before working on something
View processesA bit like Windows task manager, press CTRL+C to exit
Controlling servicesStarting, 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
Clearing out old configuration filesWhen 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