Tuesday, April 6, 2021

20 TOP MOST USED & COMMON SSH COMMANDS

As promised, I will teach babies* to manage unmanaged server from scratch. It means I’ll try to bring those babies from zero to hero being a Server Ninja. Hence, for my early posts I will firstly write all basic things for newbie server admins should know. In this page I list all necessary, most-used and common SSH commands useful to navigate through SSH client like Putty. I believe these commands are working on any Unix-based servers.

* babies = newbies with no knowledge about Linux and server at all

If you are really a newbie, then you should bookmark this page otherwise simply skip this post.

My next plan: I will post other articles of basic guides how to do things in your server via SSH. Browse it all here.

REQUIREMENTS TO SSH

  • A working SSH client like Putty (Windows) or Terminal (Mac and Linux)
  • A working Linux-based server with SSH enabled
  • A cup of coffee if you wish
  • A computer with stable Internet connection

SOME CONVENTIONS

  1. Hit Enter or Return key on your keyboard after every each command / line
  2. Each line is a single raw of command unless specified otherwise
  3. Replace domain.com with your own domain name and TLD
  4. Replace xxx.xxx.xxx.xxx with IP address of your own server
  5. Replace example paths and file names according to your server information
  6. If a path ends with slash “/” then its a directory
  7. A directory: “/path/to/directory/” will always end with slash “/”
  8. A file: “/path/to/file” won’t have slash “/” in the end
  9. A file is not always having extension in the end of the file name

OK, so lets start with the most common commands to SSH you should know..

NAVIGATING

1. How to move into another directory
Use command below to change directory

1
cd [another directory]

example: move to directory “download”

1
cd download

2. How to go to home directory

1
cd ~

3. How go to the last directory you were in

1
cd -

4. How to go up a directory level

1
cd ..

5. How to show the full path of the current directory
Use this command to find out where are you currently in.

1
pwd

6. How to list files and/or directories in a directory

1
ls (just type ls and hit enter)

7. How to list all files and information

1
ls -al

8. How to list all files ending with certain extension

1
ls *.ext

example:

1
ls *.php

9. How to list all files and folders with detailed information including file size

1
ls -alh

10. How to quit and exit SSH client

1
exit

FILE MANAGEMENT

11. How to copy and rename file
Use this command to copy and rename a file

1
cp [filename] [new filename]

example: we’ll rename banner.jpg to banner728px.jpg

1
cp banner.jpg banner728px.jpg

example: we’ll cope banner.jpg to a folder called “ads”

1
cp banner.jpg ads/banner.jpg

example: copying and renaming at once

1
cp banner.jpg ads/banner728px.jpg

ps: original file will remain, it is just copied.

12. How to move and rename file
Use this command to move and rename file

1
mv [old filename] [new filename]

example: moving a file to another directory

1
mv banner.jpg ads/banner.jpg

example: moving a file to another directory and renaming it at once

1
mv banner.jpg ads/banner728px.jpg

ps: original file will be deleted as it was moved to another location
pps: you can also move a folder

example: moving “image” folder to “media” folder

1
mv image/ media

example: moving “image” folder to upper directory

1
mv image/ ..

13. How to delete / remove a file

1
rm [file name]

example:

1
rm banner.jpg

14. How to delete / remove all files in current directory

1
rm *

15. How to delete files with certain extension

1
rm *.extension

example: remove all files with .jpg extension

1
rm *.jpg

16. How to copy a folder with all files and folders in it

1
cp -r [directory] [new directory]

17. How to create new folder

1
mkdir [folder name]

example:

1
mkdir image

18. How to search for a file starting within current directory

1
find . -name [filename] -print

example: find a file called “banner.jpg” in current folder

1
find . -name banner.jpg -print

19. How to search for text within a file

1
grep [text] [filename]

example: find the word “sidebar” in file index.php

1
grep sidebar index.php

20. CHMOD – how to change file permission

1
chmod [permission type] [file/folder name]

example:

1
chmod 777 config.php

Available permission type: (below is not command)

1
2
3
4
5
6
7
8
9
First number is for the owner, second for the group, and third for everyone.
7 = Read + Write + Execute
6 = Read + Write
5 = Read + Execute
4 = Read
3 = Write + Execute
2 = Write
1 = Execute
0 = All access denied


http://www.servermom.org/top-most-used-common-ssh-commands/56/


HOW TO ZIP (COMPRESS) AND UNZIP (EXTRACT) FILES

 Compressing several files and folders into a single distributable file is the most common way done by many computer users around the world. In Windows, you can simply use Winzip, WinRAR, 7Zip, and so on to zipping and unzipping file. If you’ve installed full Linux distros with GUI, you can also find and use similar app to do similar tasks. But what about a Linux server which you can only access it via SSH client? Realizing that many files are zipped and distributed as .zip file, also that you may need to zip several files into one .zip file so you can download it or transfer it elsewhere, so here it is I tell you how to do zipping and unzipping in a Linux-based VPS or Dedicated server via SSH client (in this case I use Putty, in Linux or Mac you can use Terminal).

Purpose:
I will show you how to zip / compress file and to unzip / decompress / extract file in Linux server via SSH

HOW TO UNZIP / EXTRACT FILE?

Step 1.
Open Putty or Terminal then login to your server via SSH

read: How to use Putty to SSH

Step 2.
Once you are logged into your server via SSH, now navigate to the directory where the .zip file you wish to unzip is located there.

read: How to navigate from one directory to another in SSH


Step 3.
Then type following command to try unzipping

1
unzip [filename].zip

example

1
unzip screenshot.zip

you may be successful but you may not.

error message:
– bash: unzip: command not found

In this case you have to install it first.

Step 4.
Use following command:

for Debian and Ubuntu:

1
apt-get install unzip

for Red Hat Linux/Fedora/CentOS users:

1
yum install unzip

Step 5.
That’s it. Now you can try unzipping it again using command in step 3 above.


HOW TO ZIP / COMPRESS FILE?

Step 1.
Open Putty or Terminal then login to your server via SSH

Step 2.
Once you are logged into your server via SSH, now navigate to the directory where the files and folders you wish to zip / compress are located there.

Step 3.
Use following command:

1
zip [zip file name] [file 1] [file 2] [file 3] [file and so on]

example:

1
zip example.zip 1.txt 2.txt 3.txt

In this example we’ll compress files 1.txt, 2.txt and 3.txt into a single example.zip file


you may be successful but you may not.

error message:
– bash: zip: command not found

In this case you have to install it first.

Step 4.
Use following command to install zip function:

for Debian and Ubuntu:

1
apt-get install zip

for Red Hat Linux/Fedora/CentOS users:

1
yum install zip


Step 5.
That’s it. Now you can try zipping it again using command in step 3 above.


Link: http://www.servermom.org/how-to-zip-compress-and-unzip-extract-files/65/