### Linux Commands

A brief overview of selected Linux commands and at the end of the list some hints & tips for the Raspberry Pi (RPi).
But a nice one to start with is:
Update Software
sudo su
apt-get update
apt-get upgrade
apt-get dist-upgrade
or use
sudo apt-get update
...
Get SuperUser Privileges
sudo -i
Linux Commands (just few selected only)
cat = View file
cat filename
Option
cat /proc/cpuinfo = information about the device’s processor
cat /proc/meminfo = details about RPi memory
cat /proc/partitions = reveals the size and number of partitions on the SD card or HDD
cat /proc/version = version of the Pi used
cat /proc/sys/kernel/hostname = get the hostname, like raspberry
cat /proc/sys/kernel/ostype = get the os type, like Linux
cat /proc/sys/kernel/osrelease = get the os release, like 3.2.27
cat /proc/sys/kernel/pid_max = get the max pid, like 32768
cat /proc/sys/kernel/poweroff_cmd = get the poweroff command like /sbin/poweroff
Check Raspian Version
cat /etc/os-release
with output example:
NAME="Raspbian GNU/Linux"
VERSION_ID="8"
VERSION="8 (jessie)"
ID=raspbian
ID_LIKE=debian
HOME_URL="http://www.raspbian.org/"
SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
cat /etc/debian-version = get the dabian version, i.e. 9.1
cat Alternatives
cat filename
less filename
less -FX filename
alias aliasname='less -FX'
or filename
ch = Change Directory
cd /home/pi/b4j
chown owner: filename
Change the ownership of a file to use.
sudo chown pi /home/pi/b4j/tempjars
chown openhab: /etc/openhab/configurations/persistence/rrd4j.persist
clear = Clear Terminal screen
cp = Copy file
cp myfile mynewfile
Option: -i prevent overwriting, Wildcards
df = file system information
Option: -h
dmesg = external disc device information
Display Detected Systems Serial Support:
dmesg | grep tty
echo = Show text on screen
echo Hello World
fdisk = comprehensive disc information
Option: -l /dev/sda (Note/dev/sda is a disc)
find = Find files
find / -name filename
head/tail = View first or last lines of a file
head filename OR tail filename
Option: -f display any new lines
hostname = Get RPi IP address
Option: -I is required to show the IP
ifconfig = Get interface configuration information
Option: -a = Viewing all interfaces
Option: eth0 = Viewing specific interface
User friendly: ifconfig | grep inet
kill = Kill a running process
Option: -9 PID
sudo kill -9 2326
ls = Listing Directory
ls
Option: -l for file information
lsmod = Check if modules are loaded
lsusb = Explore the USB devices connected
Option: -t
mkdir = Make Directory
mkdir newdir
Option: -p to create parent directories
mount = Information about mounted disks
mv = Move / Rename file or directory
mv myfile /home/pi/mynewfolder
Option: -i prevent overwriting
passwd = Change password
sudo passwd root
pwd = Print Working Directory
pwd
Shell variables: Working directory:
PWD variable, previous directory: $OLDPWD.
ps = List active processes
Option: axg | grep processname
ps ax | grep java
rm = Remove file
rm myfile.txt
Option: -i prompt before deleting
rmdir / rm = Remove directory
rmdir mydir
rm -r dirname
sudo = most important command
sudo apt-get update && apt-get upgrade
sudo apt-get install package = Install a package
sudo apt-get purge package = Removes a package and the associated configuration files
sudo raspi-config = RPi configuration
sudo rebbote = Reboot RPi
sudo rpi-update = Update the RPi Firmware & OS
sudo shutdown –h now = Immediately begin the system halt and shutdown process
sudo apt-cache search software = Checks if software is installed, i.e. sudo apt-cache search thonny
touch = New File / Change Timestamps
touch newfile
Rules creating a file:
* Descriptive name
* Only use alphanumeric characters Uppercase, lowercase, numbers, #.@-_
* Avoid blanks and *?<>/&;:![]$\'"
* Not start with + or -
* Not the same as as a system command
* Starting with . are hidden (use ls -a to list)
* Less than 255 characters
Option: -a change access time
Option: -m change last modified time
Option: -t set new time
Option: -r same as another file.
top = List processes
uname
Option: -v = Linux Version
vcgencmd = Hardware info
Option: measure_temp = Measure Temperature
Option: get_mem arm && vcgencmd get_mem gpu = reveal the memory split
Manage Raspberry Pi Hints & Tips
X11 Clean
sudo apt-get purge xserver* x11-common x11-utils x11-xkb-utils x11-xserver-utils xarchiver xauth xkb-data console-setup xinit lightdm libx{composite,cb,cursor,damage,dmcp,ext,font,ft,i,inerama,kbfile,klavier,mu,pm,randr,render,res,t,xf86}* lxde* lx{input,menu-data,panel,polkit,randr,session,session-edit,shortcut,task,terminal} obconf openbox gtk* libgtk* alsa* python-pygame scratch tsconf xdg-utils desktop-file-utils omxplayer && sudo apt-get autoremove --purge && sudo apt-get autoclean
Remove Wolfram
sudo apt-get remove wolfram-engine
Firmware Update
First additional software has to be installed:
sudo apt-get install git
wget https://raw.github.com/Hexxeh/rpi-update/master/rpi-update -O /usr/bin/rpi-update
chmod +x /usr/bin/rpi-update
rpi-update
reboot
Change the SSH Login Message
After logging in via SSH, following message is displayed:
...The programs included with the Debian GNU/Linux system are free software;
To change the message:
nano /etc/motd
Define your message.
Set root access to avoid typing in sudo for every command
sudo su -
System Reboot
sudo reboot
System Shutdown
sudo shutdown -h now
sudo shutdown -h 0
Clean up etc
(Remove unneeded packages, fix broken installs, update package list, upgrade installed packages).
sudo apt-get autoremove && sudo apt-get -f install && sudo apt-get update && sudo apt-get upgrade -y
Fixing broken installs
sudo apt-get install somename --fix-missing
Start a process
java -jar yourjar.jar &
Subsequent commands if the previous command succeeded
Make use of "&&"
sudo ifdown eth0 && sudo ifup eth0
Subsequent commands not waiting for the previous command to complete
Make use of ";"
sudo ifdown eth0 ; sudo ifup eth0
​
Folders
Go to Folder: cd folder
Go to $HOME folder: cd, cd ~
Go up a folder: cd ..
Go to previous folder: cd -
Show current folder: pwd
List folder content: ls
List all files and folders: ls -alh
Create a folder: mkdir my_folder_name
Delete a folder and any files it contains: sudo rm -r folder
Files
Remove file or files: rm somefilename.*
Move or rename file: mv {old-dir-name} {new-dir-name}
Download file: wget http://downloads.domainname.com/...
Search for files
Install mlocate
sudo apt-get install mlocate
sudo updatedb
locate filename.a
Disk
Get help on parameter: df --help
Used Space: df -h
Free space in kB or MB or GB
df -k /tmp | tail -1 | awk '{print $4}'
df -BM /tmp | tail -1 | awk '{print $4}'
df -BG /tmp | tail -1 | awk '{print $4}'
Disk Full
Some suggested actions:
Remove old compressed log files from the system
sudo rm -v /var/log/*.gz
Clean-Up Installation Packages (see also next hint)
sudo apt-get autoclean
sudo apt-get clean
sudo apt-get autoremove
Remove unneeded packages, fix broken installs, update package list, upgrade installed packages
sudo apt-get autoremove && sudo apt-get -f install && sudo apt-get update && sudo apt-get upgrade -y
Remove packages not used, like
Wolfram:
sudo apt-get remove wolfram-engine
Remove X11 f.e. for a headless Raspberry Pi
sudo apt-get purge xserver* x11-common x11-utils x11-xkb-utils x11-xserver-utils xarchiver xauth xkb-data console-setup xinit lightdm libx{composite,cb,cursor,damage,dmcp,ext,font,ft,i,inerama,kbfile,klavier,mu,pm,randr,render,res,t,xf86}* lxde* lx{input,menu-data,panel,polkit,randr,session,session-edit,shortcut,task,terminal} obconf openbox gtk* libgtk* alsa* python-pygame scratch tsconf xdg-utils desktop-file-utils omxplayer
sudo apt-get autoremove --purge
sudo apt-get autoclean
Networking
IP Address: ip addr
Check Interface Connected: ip link show wlan0
Processes
Show processes: ps -ef
List processes: ps ax
List specific process: ps -ef | grep brickd
Kill a running program: sudo killall myprogram.a
Root
Get root user privileges
sudo su
Exiting root user mode
exit
or su username:
su pi
Running command as another user
su - pi -c \"myapplication > /dev/null\"
Enable Root Account
Set password
sudo passwd root
If not working, check SSH:
sudo nano /etc/ssh/sshd_config
Search for PermitRootLogin and change to yes
Undo logging in as root
sudo passwd -l root
Shutdown
sudo halt
sudo shutdown -h now
sudo shutdown -h 0
Sleep
sleep seconds
sleep 5
Tar and Zip
Tar Help: tar --help
Create an Archive: tar -vfcz outputfilename.gz inputfile1 inputfile2
Zipping Files and Folders
cd projects/myfolder
Zip 1 File: tar -vczf zip_output_filename.gz file_to_zip.a
Zip 2 Files: tar -vczf zip_output_filename.gz file_to_zip1.a file_to_zip2.a
Zip A File & 2 Folder:tar -vczf zip_output_filename.gz file_to_zip1.a folder_to_zip1 folder_to_zip2
Option
Conpres: -vczf
​Unziping
tar zxvf zip_filename.gz
Option
overwrite if any file exist: zxvf
Fix Broken Links
Try
sudo aptitude install yourapp
sudo aptitude install nodered
Check Software
Check Linux version:
uname -v
Check for Updates: apt-cache policy software
Example: apt-cache policy python3-thonny
Clean Up Diskspace
rm -R /home/pi/python_games
cd /var/log/
sudo rm 'find . -type f'
sudo apt-get -y purge wolfram-engine scratch minecraft-pi sonic-pi dillo gpicview penguinspuzzle subversion
sudo apt-get -y autoremove
sudo apt-get -y clean

Changed: 2018-03-26 13:07, Category: Raspberry Pi, File: T6C234M1714.htm, ID: 1714