SSH Command in Linux with examples

Shell basics and scripting commands in Linux

Small commands that make the terminal faster, and the first steps of scripts.

#echo

Print text

Syntaxecho text
  • echo "Hello"

    Print a line

  • echo $HOME

    Print a variable

  • echo "new line" >> notes.txt

    Append to a file

#man

Read the manual

Syntaxman command
  • man tar

    The full manual for tar (q to quit)

  • tar --help

    A shorter summary

  • man -k "copy files"

    Search the manuals

#clear

Clear the screen

Syntaxclear
  • clear

    Same as pressing Ctrl+L

#alias

Make your own shortcut

Syntaxalias name='command'
  • alias ll='ls -lah'

    Now ll does the long list

  • alias

    Show your aliases

TipPut alias lines in ~/.bashrc so they are there every time you log in.

#export

Set an environment variable

Syntaxexport NAME=value
  • export EDITOR=nano

    Use nano as your default editor in this session

  • export PATH="$PATH:$HOME/bin"

    Add a folder to the program search path

#source

Run a file in the current shell

Syntaxsource file
  • source ~/.bashrc

    Reload your shell settings

#redirects and pipes

Connect commands

Syntax> >> < | 2>
  • ls > list.txt

    Write output to a file (replaces it)

  • ls >> list.txt

    Append instead

  • command 2> errors.txt

    Save only the error messages

  • command > all.txt 2>&1

    Save output and errors together

  • ps aux | grep ssh

    Pipe the output of one command into another

#for / if

Loops and tests

Syntaxfor x in list; do ...; done
  • for f in *.jpg; do echo "$f"; done

    Do something for every jpg

  • if [ -f notes.txt ]; then echo yes; fi

    Test whether a file exists

  • for h in web1 web2; do ssh $h uptime; done

    Run uptime on two servers

#bash script

Your first script

Syntax#!/bin/bash
  • nano hello.sh

    Create the file with #!/bin/bash on the first line, then: echo "Hello"

  • chmod +x hello.sh

    Make it runnable

  • ./hello.sh

    Run it

#watch

Repeat a command

Syntaxwatch [-n seconds] command
  • watch -n 2 df -h

    Refresh every two seconds

#time

Measure how long it takes

Syntaxtime command
  • time ./backup.sh

    Prints real, user and system time

#sleep

Wait

Syntaxsleep seconds
  • sleep 5

    Pause five seconds

#exit

Leave the shell or SSH session

Syntaxexit
  • exit

    Log out (Ctrl+D does the same)

Run these commands with SSH Command

SSH Command is a desktop app for Mac, Windows and Linux that puts a real terminal, SFTP file panes, a sudo switch and a searchable Linux command finder in one window. Type what you want to do, and send the command to your server with one click.

Download free Windows commands with examples SFTP & FTP client