Robotica con Raspberry - The Older's Team

 

 

Riporto un elenco dei comandi per l'uso di Rspberry PI attraverso terminale. le informzioni riportate sono state estrapolate dai seguenti siti:

Di seguito i comandi:

You should be able to see the following prompt:

pi@raspberrypi ~ $

This shows your username and the hostname of the Pi. Here the username is pi and the hostname is raspberrypi.

Now, let's try running a command. Type pwd (present working directory) followed by the Enter key. This should display something like /home/pi.

Navigating and browsing your Pi

One of the key aspects of using a terminal is being able to navigate your file system. Firstly, run the following command: ls -la.

The ls command lists the contents of the directory that you are currently in (your present working directory). The -la component of the command is what's known as a 'flag'. Flags modify the command that's being run. In this case the l displays the contents of the directory in a list, showing data such as their sizes and when they were last edited, and the a displays all files, including those beginning with a ., known as 'dotfiles'. Dotfiles usually act as configuration files for software and as they are written in text, they can be modified by simply editing them.

In order to navigate to other directories the change directory command, cd, can be used. You can specify the directory that you want to go to by either the 'absolute' or the 'relative' path. So if you wanted to navigate to the python_games directory, you could either do cd /home/pi/python_games or just cd python_games (if you are currently in /home/pi). There are some special cases that may be useful: ~ acts as an alias for your home directory, so ~/python_games is the same as /home/pi/python_games; . and .. are aliases for the current directory and the parent directory respectively, e.g. if you were in /home/pi/python_games, cd .. would take you to /home/pi.

La tilde rappresenta la home ed equivale a /home/pi/   La Tilde viene creata con la combinazione di tasti Alt Gr+126 (dal tastierino numerico)

History and auto-complete

Rather than type every command, the terminal allows you to scroll through previous commands that you've run by pressing the up or down keys on your keyboard. If you are writing the name of a file or directory as part of a command then pressing tab will attempt to auto-complete the name of what you are typing. For example, if you have a file in a directory called aLongFileName then pressing tab after typing a will allow you to choose from all file and directory names beginning with a in the current directory, allowing you to choose aLongFileName.

Sudo

Some commands that make permanent changes to the state of your system require you to have root privileges to run. The command sudo temporarily gives your account (if you're not already logged in as root) the ability to run these commands, provided your user name is in a list of users ('sudoers'). When you append sudo to the start of a command and press enter you will be asked for your password, if that is entered correctly then the command you want to run will be run using root privileges. Be careful though, some commands that require sudo to run can irreparably damage your system so be careful!

Further information on sudo and the root user can be found on the linux root page.

Installing software through apt-get

Rather than using the Pi Store to download new software you can use the command apt-get, this is the 'package manager' that is included with any Debian based Linux distributions (including Raspbian). It allows you to install and manage new software packages on your Pi. In order to install a new package you would type sudo apt-get install <package-name> (where <package-name> is the package that you want to install). Running sudo apt-get update updates a list of software packages that are available on your system. If a new version of a package is available then sudo apt-get upgrade will update any old packages to the new version. Finally, sudo apt-get remove <package-name> removes or uninstalls a package from your system.

More information about this can be found in the linux usage section on apt.

Other useful commands

There are a few other commands that you may find useful, these are listed below:

  • cp makes a copy of a file and places it at the specified location (essentially doing a 'copy-paste'), for example - cp file_a /home/other_user/ would copy the file file_a from your home directory to that of the user other_user (assuming you have permission to copy it there). Note that if the target is a folder, the filename will remain the same, but if the target is a filename, it will give the file the new name.
  • 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, so mv file_a /home/other_user/ would move the file file_a from your home directory to that of the specified user. mv is also used to rename a file, i.e. move it to a new location, e.g. mv hello.txt story.txt.
  • rm removes the specified file (or directory when used with -r). Warning: Files deleted in this way are generally not restorable.
  • mkdir: This makes a new directory, e.g. mkdir new_dir would create the directory new_dir in the present working directory.
  • cat lists the contents of files, e.g. cat some_file will display the contents of some_file.

Other commands you may find useful can be found in the commands page.

Finding out about a command

To find out more information about a particular command then you can run the man followed by the command you want to know more about (e.g. man ls). The man-page (or manual page) for that command will be displayed, including information about the flags for that program and what effect they have. Some man-pages will give example usage.

 

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 directoryA) or absolute (i.e. cd /home/pi/directoryA) 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!

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 otherUser (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, so mv ~/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. Use df -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 command ls | 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.md will 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.

whereis

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

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).

 

 

General Commands

  • apt-get update: Updates your version of Raspbian.
  • apt-get upgrade: Upgrades all of the software packages you have installed.
  • clear: Clears the terminal screen of previously run commands and text.
  • date: Prints the current date.
  • find / -name example.txt: Searches the whole system for the file example.txt and outputs a list of all directories that contain the file.
  • nano example.txt: Opens the file example.txt in “Nano”, the Linux text editor.
  • poweroff: To shutdown immediately.
  • raspi-config: Opens the configuration settings menu.
  • reboot: To reboot immediately.
  • shutdown -h now: To shutdown immediately.
  • shutdown -h 01:22: To shutdown at 1:22 AM.
  • startx: Opens the GUI (Graphical User Interface).

File/Directory Commands

  • cat example.txt: Displays the contents of the file example.txt.
  • cd /abc/xyz: Changes the current directory to the /abc/xyz directory.
  • cp XXX: Copies the file or directory XXX and pastes it to a specified location; i.e. cp examplefile.txt /home/pi/office/ copies examplefile.txt in the current directory and pastes it into the /home/pi/ directory. If the file is not in the current directory, add the path of the file’s location (i.e. cp /home/pi/documents/examplefile.txt /home/pi/office/ copies the file from the documents directory to the office directory).
  • ls -l: Lists files in the current directory, along with file size, date modified, and permissions.
  • mkdir example_directory: Creates a new directory named example_directory inside the current directory.
  • mv XXX: Moves the file or directory named XXX to a specified location. For example, mv examplefile.txt /home/pi/office/ moves examplefile.txt in the current directory to the /home/pi/office directory. If the file is not in the current directory, add the path of the file’s location (i.e. cp /home/pi/documents/examplefile.txt /home/pi/office/ moves the file from the documents directory to the office directory). This command can also be used to rename files (but only within the same directory). For example, mv examplefile.txt newfile.txt renames examplefile.txt to newfile.txt, and keeps it in the same directory.
  • rm example.txt: Deletes the file example.txt.
  • rmdir example_directory: Deletes the directory example_directory (only if it is empty).
  • scp Questo indirizzo email è protetto dagli spambots. È necessario abilitare JavaScript per vederlo.:/some/path/file.txt: Copies a file over SSH. Can be used to download a file from a desktop/laptop to the Raspberry Pi. Questo indirizzo email è protetto dagli spambots. È necessario abilitare JavaScript per vederlo. is the username and local IP address of the desktop/laptop and /some/path/file.txt is the path and file name of the file on the desktop/laptop.
  • touch: Creates a new, empty file in the current directory.

Networking/Internet Commands

  • ifconfig: To check the status of the wireless connection you are using  (to see if wlan0 has acquired an IP address).
  • iwconfig: To check which network the wireless adapter is using.
  • iwlist wlan0 scan: Prints a list of the currently available wireless networks.
  • iwlist wlan0 scan | grep ESSID: Use grep along with the name of a field to list only the fields you need (for example to just list the ESSIDs).
  • nmap: Scans your network and lists connected devices, port number, protocol, state (open or closed) operating system, MAC addresses, and other information.
  • ping: Tests connectivity between two devices connected on a network. For example, ping 10.0.0.32 will send a packet to the device at IP 10.0.0.32 and wait for a response. It also works with website addresses.
  • wget http://www.website.com/example.txt: Downloads the file example.txt from the web and saves it to the current directory.

System Information Commands

  • cat /proc/meminfo: Shows details about your memory.
  • cat /proc/partitions: Shows the size and number of partitions on your SD card or hard drive.
  • cat /proc/version: Shows you which version of the Raspberry Pi you are using.
  • df -h: Shows information about the available disk space.
  • df /: Shows how much free disk space is available.
  • dpkg –get-selections | grep XXX: Shows all of the installed packages that are related to XXX.
  • dpkg –get-selections: Shows all of your installed packages.
  • free: Shows how much free memory is available.
  • hostname -I: Shows the IP address of your Raspberry Pi.
  • lsusb: Lists USB hardware connected to your Raspberry Pi.
  • UP key: Pressing the UP key will enter the last command entered into the command prompt. This is a quick way to correct commands that were made in error.
  • vcgencmd measure_temp: Shows the temperature of the CPU.
  • vcgencmd get_mem arm && vcgencmd get_mem gpu: Shows the memory split between the CPU and GPU.

 

Pagina 3 di 3

weschool

weschool

Cerca nel Sito