Useful Raspberry Pi Commands

Getting starting with the Raspberry Pi command line can be a steep learning curve, with a whole heap of commands at your fingertips it’s hard to remember them all. Here then are a bunch of commands I use quite frequently.

BEING SUPER

A lot of the time you’ll probably need to run commands as a super user, so to do this you’ll want to prefix your command with the keyword “sudo”

sudo mycommand # Executes mycommand with super user permissions

UPDATING/UPGRADING

As with any computer, it’s best to keep the system/packages installed on it up to date. This is done on the Raspberry Pi with 2 commands “update” and “upgrade”, where update syncs the internal database of available packages, and upgrade applies any updates.

sudo apt-get update # Syncronizes the package database
sudo apt-get upgrade # Upgrades installed packages inline with updated package database

MOVING AROUND

Once you are logged in to your Raspberry Pi you are probably going to want to navigate around the files/directories on it.

LS – Listing files/directories

ls # List the contents of the current directory

CD – Changing directory

cd Desktop # Move into a folder called Desktop inside my current location
cd /Desktop #  Move into a folder called Desktop located at the root of my device
cd ~/Desktop # Move into a folder called Desktop located in the home folder of the current user
cd .. # Move into the parent folder of my current location

PWD – Ouput the current working directory

pwd # Prints out the path of the current working directory

WORKING WITH FILES

Now that you can get around, you’ll probably want to be able to actually do something with those files.

MKDIR – Make a directory

mkdir myDir # Created a new directory named myDir inside the current working directory

RMDIR – Remove empty directories

rmdir oldDir # Removes the directory oldDir only if the folder is empty

RM – Remove a file

rm myFile.txt # Removes the file myFile.txt

CP – Copy a file/directory

cp oldFile.txt newFile.txt # Copies oldFile.txt into a new file named newFile.txt
cp oldFile.txt ~/tmp # Copies oldFile.txt into the ~/tmp directory
cp -r ~/tmp1 ~/tmp2 # Recursively copies files from ~/tmp1 into ~/tmp2

MV – Move a file/directory (TIP: Also handy for renaming files)

mv oldFile.txt newFile.txt # Moves oldFile.txt to newFiles.txt, effectively renaming it
mv oldFile.txt ~/tmp # Moves oldFile.txt into the ~/tmp directory
mv ~/tmp1/myFolder ~/tmp2/myFolder # Moves the myFolder directory from ~/tmp1 to ~/tmp2

WGET – Get a file from the internet

sudo wget http://www.example.com/file.txt # Downloads file.txt to the current working directory

EDITING A FILE

There are many way that you can edit a file on your Raspberry Pi, but the easiest way is to probably use the nano command.

sudo nano myFile.txt # Opens myFile.txt in nano for editing

Once you are in nano, you’ll find yourself using the following commands quite frequently.

Ctrl+X # Exits nano (It will ask you if you want to save changes if changes have been made)
Ctrl+K # Cut the current line
Ctrl+U # Un-cut or pastes the previously cut line

RUNNING A PYTHON SCRIPT

If you are creating scripts to run on your Raspberry Pi, the likelihood is that you have written it in Python. To run your script, you’ll want to use the “python” command passing in the python script file name you want to run.

sudo python myScript.py # Runs the myScript.py python script

SHUTTING DOWN/REBOOTING (SAFELY)

When it comes to shutting down or rebooting your Pi (say after making some system changes) the temptation is to just disconnect/reconnect the power, however this can lead to some unexpected side effects (corrupted SD card being the primary one). To shutdown/reboot safely then, you’ll want to use one of the following commands.

sudo reboot # Reboots the Pi immediately
sudo shutdown -r now # Reboots the Pi immediately
sudo shutdown -r 10 # Reboots the Pi in 10 minutes from now
sudo halt # Shuts down the Pi immediately
sudo shutdown -h now # Shuts down the Pi immediately
sudo shutdown -h 10 # Shuts down the Pi in 10 minutes from now

I will be updating this list in future with any other commands I find myself executing on a regular basis, but for now, that should be more than enough to get you started.


Something you'd like to ask or comment on? Connect with me on Twitter.
Tweet Me