blog;

Terminal on Android.

Using the Terminal on my Phone

Terminal on Android.

Originally posted on Fri Jan 15 2021

In a previous post I wrote about how I used Termux on my phone to sync my notes from a GitHub repo onto my phone. I wanted to go into a bit more detail on how I set up Termux and how I use it now.

Termux is a really great app which allows you to use the Linux Terminal which exists on your Android phone. Android is basically built on top of Linux, which is the reason you can do this. You do not need to have rooted your phone, unless wanted greater control and access over the command line. I have not rooted my phone, so everything below is achievable without doing so.

Setting Up Termux

After downloading the app, the very first thing I did is install nano using the below command:

pkg install nano

Termux has vi already, but I really hate vi and vim, so I don't even attempt to use them! We are going to be editing some files and writing some code, so pick an editor you like.

To access files on your phones internal storage, you need to run termux-setup-storage.

The next thing I did was install oh-my-zsh to make the terminal on my phone be as similar as possible to the one on my laptop. I wrote a blog about OMZ previously as well, which you can checkout here. I found the easiest way to install it was to use oh-my-termux, by running the below command:

bash -c "$(curl -fsSL https://git.io/oh-my-termux)"

# set zsh as default shell
chsh -s zsh

Installing this way also changes the theme to Solarized Dark, and I prefer the default dark theme, so I changed it back. To do this, you can delete the file at ~/.termux/colors.properties.

rm -f ~/.termux/colors.properties

Next I setup some command line shortcuts. I spoke about this in the previous post about OMZ, but I will give a walkthrough here too. Navigate to ~/.oh-my-zsh/custom, and there is a file called example.zsh. You can either delete or rename this file, I renamed it to custom.zsh. Inside the file, I added my shortcuts, which you can see below:

alias zsh-profile="cd ~/.oh-my-zsh/custom && nano custom.zsh && cd -"
alias ll="ls -lhaG"
alias refresh-source="cd ~/ && source .zshrc && cd -"
alias home="cd ~/"
alias goback="cd -"

# I will get on to the pi stuff later
alias pi-connect="ssh pi@$PI_IP_ADDRESS"

Navigate back to the home directory and refresh the source with the below:

cd ~/
source .zshrc

You can see in my shortucts that I have assigned the above to refresh-source, so now I can use that command instead of having to type the above everytime.

Next I installed a couple of plugins for OMZ. There's two I use; zsh-autosuggestions and zsh-syntax-highlighting. To install them, first we need to clone the git repos. Use the below commands to do this:

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

git clone https://github.com/zsh-users/zsh-syntax-highlighting ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

Once they have been cloned, open up the .zshrc file at the home directory in your editor. Scroll down to the plugins part and add the name of the plugins like below:

plugins=(
    git
    zsh-autosuggestions
    zsh-syntax-highlighting
)

Now either refresh the source manually again, or if you have setup the shortcut, run refresh-source. Now both plugins should be active.

Example of Zsh Syntax Highlighting
Example of the Zsh Syntax Highlighting Plugin in Action
Example of Zsh Autosuggestions
Example of the Zsh Autosuggestions Plugin in Action

The last setup thing I did, was increase the size and the amount of keys on the touch keyboard that comes with the app. Navigate to the home directory and open up .termux/termux.properties. In here you can add extra-keys to change how the touch keyboard works. BE CAREFUL here though as if you mess it up the keyboard will just disappear, in that case refer to the touch keyboard page linked above to see the other shortcuts using the Volume Up and Down buttons.

I added the below to mine:

extra-keys = [ \
  ['ESC','CTRL','ALT','HOME','END'], \
  [], \
  ['TAB','|','/','UP','-'], \
  [], \
  ['FN','=','LEFT','DOWN','RIGHT'] \
]

Which makes it look like this:

Termux Touch Keyboard
Customised Touch Keyboard

Raspberry Pi

The reason I set all this up again, is because I wanted to have an easy way to copy files over to my Raspberry Pi 4. Previously I was having to plug my phone into the Pi via USB, which was not easy as it's not in an easy to access place. I was always able to SSH to my Pi and connect to it that way, which made me think there must be a way to transfer files wirelessly too. I did some Google-fu and found SCP (Secure Copy Protocol). I could use scp to copy files over, using a command like the below:

scp test.txt pi@$PI_IP_ADDRESS:/home/pi/Desktop

This would copy the file test.txt on to the Desktop of my Raspberry Pi.

To make this easier, I wrote a script:

set -e

piip="pi@$PI_IP_ADDRESS:"

if [ "$2" = "movies" ]; then
    path=$piip"/disks/Elements/Media/Movies"
elif [ "$2" = "tv" ]; then 
    path=$piip"/disks/Elements/Media/TV"
elif [ "$2" = "desktop" ]; then 
    path=$piip"/home/pi/Desktop"
else 
    echo "Copying to "$piip$2
    path=$piip$2
fi

scp "$1" "$path"

I used chmod to allow my script to run. Now I could run ./copyToPi test.txt desktop to copy the file to my desktop.

SCP File to Pi
Using scp to copy file to Raspberry Pi

This worked, but after a but more Google-fu, I found that scp is not the best way to do this, and that SFTP (Secure File Transfer Protocol) is the more modern way to do it. So I wrote another script to do this:

set -e

piip="pi@$PI_IP_ADDRESS:"

# This replaces any spaces with "\ "
file=`echo "$1" | sed 's/\ /\\\ /g'`

if [ "$2" = "movies" ]; then
    path=$piip"/disks/Elements/Media/Movies"
elif [ "$2" = "tv" ]; then 
    path=$piip"/disks/Elements/Media/TV"
elif [ "$2" = "desktop" ]; then 
    path=$piip"/home/pi/Desktop"
else 
    echo "Copying to "$piip$2
    path=$piip$2
fi

echo put $file | sftp $path 

Which also worked just fine:

SFTP File to Pi
Using sftp to copy file to Raspberry Pi

Now I can wirelessly transfer files from my phone to my laptop. Eventually I would like to setup a DNS and do some port forwarding so that I can SSH to my Pi outside of my home, but thats for another day!


Me

Post by

Josh Glasson

Software Developer. Creator and owner of this blog.