Basic SSH Commands

When connecting to a server via SSH, all commands are entered in lowercase. For example, you need to enter the cd command. Cd, CD or cD is an invalid command name that will not be accepted by the system.

Navigation

Listing the Current Working Directory

The pwd command is used to get information about the current directory. For example, enter:

username@server:~$ pwd

/home/u/username

Displaying Directory Contents

The ls command is used to view the contents of a directory.

To display the current directory (file and directory names) in several columns, enter:

ls .

To display the current directory (file and directory names) in one column, enter:

ls -1

To display detailed information about the current directory (including hidden files), enter:

ls -la

For information about the directory, enter:

ls directory-name

Moving Between Directories

To go to another directory, use the cd command.

To go to a directory in the current directory, enter:

cd directory-name

To go to the parent directory of the current directory (one level up) enter:

cd ..

To go to your home directory, enter:

cd 

Or

cd ~

To return to the previous directory enter:

cd -

To go to a directory using a full path, enter:

cd /home/u/username

Actions with Files and Directories

Creating Directories

The mkdir command is used to create directories.

To create a directory enter:

mkdir directory-name

To create multiple directories, enter:

mkdir directory-name-1 directory-name-2

To create a directory with nested directories enter:

mkdir -p /directory-name-1/directory-name-2

File Creation

You can create files using various commands, depending on the specific task.

To create a file, enter:

touch file-name.txt

If a file with the same name already exists in this directory, it will not change. That is, this command is suitable for creating new files.

To create or replace a file, you can use the command:

> file-name.txt

If a file with the same name already exists in this directory, it will be replaced with a new one.

Also, to create or replace a file, as well as make an entry in this file, you can use the command:

echo "line-text" > file-name.txt

In this case, a new file will be created (if a file with the same name already exists, it will be replaced). The new file file-name.txt will be written with the string "line-text".

To open the file with the nano text editor, enter:

nano file-name

If there is no file with this name in the current directory, it will be created and opened automatically. To save changes to the file, enter Crtl+X, Y and Enter.

Copying Files and Directories

The cp command is used to copy files and directories.

To copy the file file1.txt to file2.txt enter:

cp file1.txt file2.txt

To copy directory1 to directory2 enter:

cp  dir1 dir2

In order to copy all its contents together with the directory, use the –r key (the recursive copying is performed).

cp -r dir1 dir2

Moving and Renaming Files

 

The mv command is used to move and rename files.

To rename the file file1.txt to file2.txt enter:

mv file1.txt file2.txt

To move the file file1.txt to directory1 enter:

mv file1.txt directory1 

Removing Files and Directories

The rm command is used to remove files and directories.

To delete the file file1.txt (in the current directory) enter:

rm file1.txt

To remove all files (in the current directory) enter:

rm /path/to/directory/*

To remove all .jpeg files in a specific directory, enter:

rm /path/to/directory/*.jpeg

To remove directory1 (and its contents) enter:

rm -r directory1

When deleting a large number of files, you can add the –f key. All files will be deleted without confirmation.

rm -f /path/to/directory/*.txt

If you are not sure about the need to delete all files, add the –i key. In this case, you will need to confirm the deletion of each file.

rm -i /path/to/directory/*.txt

Link Creating

The ln command is used to create links.

To create a hard link, enter:

ln file1 file2

To create a symbolic link, enter:

ln -s file1 file2

Setting Access Rights to Files and Directories

The chmod command is used to set the permissions for files and directories.

To set XXXX permissions for file1, enter:

chmod XXXX file1

To set XXXX permissions for directory1, enter:

chmod xxxx dir

Working with File Contents

Concatenate File Contents

The cat command concatenates files and prints their contents to the output. It can also be used to display the contents of a file.

Search by File Content

Grep command used to search for content in a file.

File Search

The find command is used to find files.

For example, to search for files and directories named “filename” in the current directory, enter:

find ./ -name 'filename'

To search for files and directories starting with “filename” in the current directory, enter:

find ./ -name 'filename*'

To search for files with the extension “.php” in the current directory, enter:

find ./ -name '*.php'

Archives

To create an archive of a specific directory, enter:

tar -cf archive-name.tar directory-name

To unpack an archive with a .tar extension into the current directory, enter:

tar -xvf archive.tar

To unpack a .zip archive into the current directory, enter:

unzip archive.zip

Databases

To dump the database, enter:

mysqldump -u'database' database` -p'database-password' > dump-name.sql

To import data from a dump into the database, enter:

mysql -u'database' database -p'database-password' < dump-name.sql

To check the data for errors, enter:

mysqlcheck -c -u'database' database -p'database-password'

To restore the database (if there are errors) enter:

mysqlcheck -r -u'dabatase' database -p'database-password'

To connect to the database server, enter:

mysql -u database-name -p

Then press Enter and enter the base password.

To get information about MySQL parameters, enter:

SHOW VARIABLES LIKE 'parameter';

SHOW SESSION VARIABLES LIKE 'parameter';

For example, to get information about the max_join_size parameter, enter:

SHOW SESSION VARIABLES LIKE 'max_join_size';

Terminating Processes

The killall command is used to kill current processes.

For example, to terminate all Apache web server processes, enter:

killall -9 apache2

To end the MySQL process, enter:

mysqladmin -u'database' -p'database-password' kill id_request

To view MySQL processes enter:

mysqladmin -u'database' -p'database-password' pr

Disk Space

To display information about files and directories in the current directory, enter:

du -sch *

We recommend you to read our article Disk space analysis for more information on how the ncdu program works.

Other Auxiliary Commands

  • Ctrl-L – clear the screen.
  • Ctrl-D – close the SSH connection.
  • Ctrl-C  interrupt the execution of the current command.
  • Ctrl-Shift-C  copy the selection.
  • Ctrl-Shift-V  insert.

To display the command history, enter:

history

If you have any questions, please create a ticket to technical support.