You are on page 1of 13

Linux commands1

Here are some fundamental and common Linux commands with example usage:

Filesystem
ls

The ls command lists the content of the current directory (or one that is specified). It 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

Using cd changes the current directory to the one specified. You can use relative (i.e. cd
directoryA) or absolute (i.e. cd /home/pi/directoryA) paths.

pwd

The pwd command displays the name of the present working directory: on a Raspberry Pi,
entering pwd will output something like /home/pi.

mkdir

You can use mkdir to create a new directory, e.g. mkdir newDir would create the directory
newDir in the present working directory.

rmdir

To remove empty directories, use rmdir. So, for example, rmdir oldDir will remove the
directory oldDir only if it is empty.

rm

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

cp

Using cp makes a copy of a file and places it at the specified location (this is similar to copying
and pasting). For example, cp ~/fileA /home/otherUser/ would copy the file fileA from
1
https://www.raspberrypi.org/documentation/linux/usage/commands.md

pág. 1
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

The mv command 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

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

cat

You can use cat to list 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

The head command displays the beginning of a file. Can be used with -n to specify the number
of lines to show (by default ten), or with -c to specify the number of bytes.

tail

The opposite of head, 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

You would normally use chmod to change the permissions for a file. The chmod command can
use symbols u (user that owns the file), g (the files group) , and 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

pág. 2
The chown command 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

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

scp

The scp command copies a file from one computer to another using ssh. For more details see
SCP (secure copy)

sudo

The sudo command enables you to run a command as a superuser, or another user. Use sudo -s
for a superuser shell. For more details see Root user / sudo

dd

The dd command copies a file converting the file as specified. It is often used to copy an entire
disk to a single file or back again. So, for example, 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

Use df to 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

The unzip command extracts the files from a compressed zip file.

tar

Use tar to 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
pág. 3
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 ten entries of the ls
command it can be piped through the head command ls | head

tree

Use the tree command to show a directory and all subdirectories and files indented as a tree
structure.

&

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

wget

Download a file from the web directly to the computer with wget. So wget
https://www.raspberrypi.org/documentation/linux/usage/commands.md will download
this file to your computer as commands.md

curl

Use curl to 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 with man. To find out more, run man man to view the manual
page of the man command.

Search
grep

Use grep to search inside files for certain search patterns. For example, grep "search" *.txt
will look in all the files in the current directory ending with .txt for the string search.

The grep command supports regular expressions which allows special letter combinations to be
included in the search.

awk

awk is a programming language useful for searching and manipulating text files.

find

pág. 4
The find command searches a directory and subdirectories for files matching certain patterns.

whereis

Use whereis to find the location of a command. It looks through standard program locations
until it finds the requested command.

Networking
ping

The ping utility is usually used to check if communication can be made with another host. It 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). It can specify the number of packets to send with the -c flag.

nmap

nmap is a network exploration and scanning tool. It 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

The hostname command 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

Use ifconfig to display 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 manual
page for more details.

pág. 5
42 of the Most Useful Raspberry Pi
Commands2

General Commands
 apt-get update: Synchronizes the list of packages on your system to the list in the
repositories. Use it before installing new packages to make sure you are installing the
latest version.
 apt-get upgrade: Upgrades all of the software packages you have installed.
 clear: Clears previously run commands and text from the terminal screen.
 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 the Linux text editor Nano.
 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 and 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

2
http://www.circuitbasics.com/useful-raspberry-pi-commands/

pág. 6
/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 user@10.0.0.32:/some/path/file.txt: Copies a file over SSH. Can
be used to download a file from a PC to the Raspberry Pi. user@10.0.0.32 is the
username and local IP address of the PC, and /some/path/file.txt is the path and file name
of the file on the PC.

 touch example.txt: Creates a new, empty file named example.txt in the current
directory.

Networking and 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.

pág. 7
 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 print the last command entered into the command
prompt. This is a quick way to repeat previous commands or make corrections to
commands.
 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.

pág. 8
pág. 9
Useful terminal commands in Ubuntu or Debian 3

This is a compilation of my terminal commands in Ubuntu or Debian I consider useful. Many of


these commands are compatible with other GNU/Linux distributions.

1. ESSENTIAL COMMANDS

 Navigation: pwd, ls path, cd folder_path. Definition of path (computing).


 Help: man command, command --help, command -h, Explainshell.
 Manipulation: cp, mv, rm, mkdir, ln.
 Permissions: chmod, chown, chgrp, su, sudo (enable sudo on Debian).
 Display a line of text: echo, echo $PATH (show the content of variable $PATH).
 Text editor: nano.
 Visualization: cat, more, less, head, tail.
 Redirect standard output to a file: command > file.
 Execute multiple commands (AND): command1 && command2 && commandN.
 Execute a command in the background: command &.
 Run an executable in the current directory that is not in your $PATH: ./executable.
 Run a Bash script: bash script.sh. Executable with header #!/bin/bash.

2. APT COMMANDS

 Install a package locally: sudo apt-get install ./package.deb.


 Install a package from the repository: sudo apt-get install package.
 Uninstall a package from the repository: sudo apt-get purge package.
 Remove packages that are no longer needed: sudo apt-get autoremove.
 Resynchronize the package index files from /etc/apt/sources.list: sudo apt-get
update.
 Install the newest versions of all packages from /etc/apt/sources.list: sudo apt-get
upgrade.

2. BASH SHORTCUTS

 Autocomplete files, folders, commands, packages, etc.: Tab.


 List all available files, folders, commands, packages, etc.: Tab + Tab.
 Go to previous command: ↑, or Ctrl + P.
 Go to next command: ↓, or Ctrl + N.
 Search through previously used commands: Ctrl + R.
 Terminate the current process (SIGINT): Ctrl + C.
 Suspend the current process (SIGTSTP): Ctrl + Z.

2. BASIC COMMANDS

 Show and/or admin current processes: jobs, ps, top.

3
https://milq.github.io/useful-terminal-commands-ubuntu-debian/

pág. 10
 Send a signal to a process: kill.
 List files in databases that match a pattern: locate, sudo updatedb.
 Print lines matching a pattern from a text file: grep.
 Show/manipulate routing, devices, policy routing and tunnels: ip.
 OpenSSH SSH client with X11 forwarding: ssh user@server -X.
 Secure copy (remote file copy program): scp.

3. SCREEN COMMANDS

 Create a screen session: screen.


 Detach from the current screen session: Ctrl + A then D.
 List the screen session identification strings: screen -ls.
 Reattach to a screen session: screen -r session_id_string.
 Terminate the current screen session: exit, or Ctrl + A then :quit.
 Scroll up/down during session: Ctrl + A then Esc then ↑/↓/PgUp/PgDn.

4. OTHER COMMANDS

 Print newline, word, and byte counts for each file: wc.
 Extract an ISO file: 7z x filename.iso.
 Report faked system time to programs: faketime.
 Retrieves files from the web: wget.
 Print shared library dependencies: ldd executable_filename.
 Make symbolic links: ln -s input_filename output_link_name.
 List block devices: lsblk.
 Format USB stick as VFAT: sudo mkfs.vfat -I /dev/sdx -n NAME && sync.
 Restore disk from image: sudo dd bs=1M if=im.iso of=/dev/sdx && sync.
 Report disk space usage: df -H, du -hs folder.
 Compare files line by line: diff.

5. FFMPEG COMMANDS

 Convert image to 5 sec. video: ffmpeg -loop 1 -i 01.png -t 5 out.mp4.


 15 images to 30 Hz 5 sec. video: ffmpeg -r 3 -i %03d.png -r 30 out.mp4.
 Resize video to 720p: ffmpeg -i input.webm -s 1280x720 output.webm.
 Fade in the first 25 frames and fade out the last 25 frames of a 1000-frame video:
ffmpeg -i in.mp4 -vf 'fade=in:0:25, fade=out:975:25' out.mp4.
 Concatenate videos: ffmpeg -f concat -i mylist.txt -c copy out.mp4.

6. SVN COMMANDS

 Create new repository: svnadmin create repository_name.


 Checkout: svn co svn+ssh://user@server/path/to/repository.
 Update working copy: svn update.
 Get status of current copy: svn status.

pág. 11
 Add all items recursively: svn add *.
 Add an item (if folder, adds recursively): svn add item_name.
 Delete an item (if folder, deletes recursively): svn delete item_name.
 Commit with log message: svn commit -m 'message'.

7. FIND COMMAND

 Find files and folders recursively in current directory: find ..


 Run files recursively in current dir.: find . -type f -exec file '{}' \;.
 Delete all .svn folders: find . -iname .svn -prune -exec rm -r '{}' \;.
 Copy all png files to png: find ./ -name *.png -exec cp '{}' ./png \;.

8. PDFTK EXAMPLES

 Join all PDF files into a new PDF file: pdftk *.pdf cat output out.pdf.
 Join 3 PDF files: pdftk in1.pdf in2.pdf in3.pdf cat output out.pdf.
 Extract pages from a PDF: pdftk in.pdf cat 1 25-35 end output out.pdf.

INSTALL COMMAND-LINE INTERFACE (CLI) SOFTWARE

Some of the commands listed below are not available in Ubuntu or Debian by default.

To install the remaining CLI software, download the installation script install-additional-cli-
software.sh, open your terminal, and execute it: bash install-cli-software.sh.

pág. 12
pág. 13

You might also like