Secure Shell, or SSH, is a powerful tool for securely accessing and managing remote servers. Whether you’re a beginner or an experienced professional, understanding these commonly used SSH commands will enhance your proficiency and productivity in server management.
Let’s explore the essential Secure Shell (SSH) commands that every user should know:
Key Secure Shell Commands
SSH (Secure Shell) is a network communication protocol enabling secure communication between devices. This encrypted connection is also used for file transfers, terminal access, and application tunneling.
Basic SSH Commands With Description:
Secure Shell Command (SSH) | Description |
ls | Lists directory contents, displaying file names. |
cd | Changes the current directory. |
mkdir | Creates a new directory. |
touch | Creates a new file. |
rm | Removes a file or directory. |
cat | Displays the content of a file. |
pwd | Shows the current working directory’s full path. |
cp | Copies a file or directory. |
mv | Moves or renames a file or directory. |
grep | Searches for a specific text pattern within files. |
1. ls Command
The ls
command is used to display the contents of a directory, listing all files and subdirectories.
Syntax: ls [options] [paths]
Useful options with the ls
command include:
-l
— Shows detailed file information (size, modification date, owner, permissions).-a
— Displays all files, including hidden files (those starting with a dot).
2. cd Command
The cd
command (Change Directory) is used for navigating between directories. Enter cd
followed by the directory name to switch directories.
Syntax: cd [directory]
- To navigate to your server’s home directory, type:
cd /home
- You can also specify the full path of a directory, regardless of depth:
cd /home/TestingDirectory/AnotherTestingDirectory
This will move you into the AnotherTestingDirectory
directory.
- To go back one directory level, use
cd ..
. To go back multiple levels, use a forward slash to separate each instance, such as:
cd ../..
This example will move you back two directory levels.
3. mkdir Command
The mkdir
command creates a new directory (folder).
Syntax: mkdir [folder name]
To create a directory named “myfirstfolder,” for example:
mkdir myfirstfolder
4. touch Command
The touch
command is used to create a new, empty file.
Syntax: touch [file name]
To create a new text file named “myfirstfile.txt,” use the following command:
touch myfirstfile.txt
You can create files with any extension or without one.
5. rm Command
The rm
command removes a specified file or directory.
Syntax: rm [file name]
- To delete “myfirstfile.txt”, run:
rm myfirstfile.txt
- To remove a directory and all its contents, use the
-r
option:
rm -r home/milesweb/myfirstfolder
6. cat Command
The cat
command displays the content of a file.
Syntax: cat [file name]
- You can also use
cat
to combine multiple files into a new one:
cat info1.txt info2.txt > info3.txt
This command combines the content of info1.txt
and info2.txt
, and stores it in a new file called info3.txt
.
7. pwd Command
The pwd
command displays the full path of your current working directory.
Syntax: pwd
You might see an output similar to this:
home/user1/public_html
This is useful when the current directory isn’t visible in your prompt.
8. cp Command
The cp
command is used to copy a file or directory.
Syntax: cp [options] [source] [destination]
Here:
- Source file is the file or directory to be copied.
- Destination file is the new copy, created if it doesn’t exist.
To copy “myfirstfile.txt” to “myfirstfile2.txt” in the same directory:
cp myfirstfile.txt myfirstfile2.txt
- To copy the file to another directory:
cp /home/milesweb/myfirstfile.txt /home/etc/
Be careful when specifying the destination. If it exists, cp
will overwrite it without warning. If it doesn’t, cp
will create it.
9. mv Command
The mv
command moves or renames a file or directory.
Syntax: mv [source] [destination]
To move “myfirstfile.txt” from `/home/milesweb/ftp` to `/home/milesweb/myfirstfolder`:
mv /home/milesweb/ftp/myfirstfile.txt /home/milesweb/myfirstfolder
- Unlike the
cp
command,mv
doesn’t require the `-r`option to move directories:
mv /home/milesweb/ftp/ /home/milesweb/myfirstfolder/
The above command moves the ftp
directory and all its subdirectories to myfirstfolder
.
10. grep Command
The grep
command searches for a specific text pattern within files.
Syntax: grep [search_word] [file_name]
To search for the term “hosting” in “info1.txt”:
grep 'hosting' info1.txt
This will display each line from the file containing the word ‘hosting’.
grep
is case-sensitive. Use the `-i` option to ignore case sensitivity.
Mastering Secure Shell (SSH) commands is crucial for efficiently managing Linux servers, enabling you to navigate your system, and modify files and directories effectively.