Here we will present some commone commands used to connect and work with server. We will even write a simple bash script to make our life a bit easer to automate some of this tasks. So where do we start? Fosrs of all your hosting provider must to allow for SSH connectino (most hosting providers does that in entry level hosting plans).

Login to your server from SSH

You can login using cmd line / terminal through ssh command. This is available for both linux and windows (powershel) or windows using sobe unix subsytems often installed with termianl software such as git bash / comdr / cygwin and so on.

Just be aware that not all commands will be available on windows even if you use on of the solutions above.

SSH to a server with password

Connecting to a SSH requier from you to know user name password and remote machine address. Knowig that you are ready to go! If you dont tknow this credencial simple ask your hosting provider to poin them out for you. It is very possibgle that you already uing them to log in to your hosting service.

# ssh someuser4567@
[password prompe for server user login]

SSH to a server with key (without a password – generate key)

Generate the local key and copy server key as below:

ssh-keygen -t rsa # ENTER to every field
[hit enter for no pass for the key]
[propmp - accept server finger print]
[password prompe for server user login - need only when establishing connection for first time]

Send command to the server using SSH

You can send commands or even bash scripts to the server to be executed and returned. This give us infinite posibilities to interact with remote machine and oportunity to automate common tasks we usually would have to performed manually from hosting admin panel / file manager.

ls | ssh
[promp - will ask for pass or if you sucessfully established ssh key it will just login to the server and run command]

Write simple bash script

Filter directory name using terminal

# general filter dir name with find and grep
find . -maxdepth 1 -mindepth 1 -type d -print | grep -v \.tar\.gz$ | grep ^\./web_
ls -h | grep -v \.tar\.gz$ | grep ^web_ 

# archive single folder
tar -czf foldername.tar.gz foldername.tar.gz

Write bash script to zip all folder in the current directory (method I)

find . -maxdepth 1 -mindepth 1 -type d -print – find all folder in current directory only

grep grep -v [.]/^[.] – reverse selection / do not match if a filename starts with “.”.

grep – show only filenames including web

# Drir structure scenario: 
tree .
├── web_demozip1
│   └── somefile.txt

├── web_demozip2
│   └── somefile.txt

└── web_ spacedir3
    └── somefile.txt

Archive specyfic folders in directory

# archive specyfic folders in directory based on some naming cryteria [method I - find]
cdate=$(date '+%Y-%m-%d-%H-%M')
find . -maxdepth 1 -mindepth 1 -type d \
  -name 'web_*' \
  -exec tar czf {}$cdate.tar.gz {} \;
# archive specyfic folders in directory based on some naming cryteria [method II - for dir in]
cdate=$(date '+%Y-%m-%d-%H-%M')
for dir in ./web_*/; do
    tar -czf "$tarname" "$dir"
# archive specyfic folders in directory based on some naming cryteria [method III - ls grep xargs]
cdate=$(date '+%Y-%m-%d-%H-%M')
ls -h | grep -v \.tar\.gz$ | grep ^web_ | xargs -I {} tar -czf {}_$cdate.tar.gz {}
All three methods above would autput same result:
tree .
├── web_demozip1
│   └── somefile.txt
├── web_demozip1_2021-08-23-15-08.tar.gz
├── web_demozip2
│   └── somefile.txt
├── web_demozip2_2021-08-23-15-08.tar.gz
├── web_ spacedir3
│   └── somefile.txt
└── web_ spacedir3_2021-08-23-15-08.tar.gz

To unpack directory with tar, tar.gz and tar.bz2 extensions use the fallowing:

  tar xvf  <.tar file>
  tar xzvf <.tar.gz file>
  tar xjvf <.tar.bz2 file>

See also how to output progress for tar command here

Fro more infrmation try tar --help or see the tar man page.

Write bash script to zip all folder in the current directory (method II)

This bash script will ask you to select compresion type and then run this comresion on all the folders with in curennt directory.


for dir in */ 
    echo "$dir"

cdate=$(date '+%Y-%m-%d-%H-%M-%S')

PS3='Compress all folders to separate dir. Choose compresion type: '
options=(".tar.gz" ".zip" "Quit")
select opt in "${options[@]}"
    case $opt in
        ".tar.gz") # works fine
	    echo "Compressing to .tar.gz"
	    echo "..."
	    for i in */; do tar -czvf "$cdate ${i%/}.tar.gz" "$i"; done 
        ".zip") # works fine
            echo "Compressing to .zip"
	    for i in */; do zip -r "$cdate ${i%/}.zip" "$i"; done
        *) echo "invalid option $REPLY";;

Write bash script to backup mysql database to a current folder then tar.gz ora zip the folder

….. (commig soon)

Write script to unzip the folder and load database

…. (commig soon)

Login to remote mysql server

Sometimes mysql -u dbusername -p is not enough to connect to database. Whenever your database is not on runing on the same server it will requier few more parameters to connect to your database. This is how it works in “” hosting provider, who decouple those two services. For that hosting provider connectin script looks like this:

mysql -u serverX9Y_dbname -p'Som%^&*Pass$%^&' \
mysql -u serverX9Y_dbname -p'Som%^&*Pass$%^&' \
-h -P 3306 \
-D local

Useful resources:

Would love your thoughts, please comment.x