You are here:

Typical SSH Commands

Typical SSH Commands

Overview

This article serves as an introduction to navigating your server through SSH.

  • Press Enter or Return after every command unless specified otherwise.
  • Replace the domain example.com with your own domain name.
  • Replace example paths and file names with your specific server information.

Requirements

The tutorial assumes that:

  1. You have enabled SSH access for a user, either root or a sudo user.
  2. You have an SSH terminal ready and open to log in to the server from vpsserver.com.
  3. You should have an open command prompt.

Home

Upon initial login to the server, this will be your starting directory.

root@sample:~#

alternately, if you’re not a root user:

thisuser@sample:~#

Folder Organisation

  • /bin: Contains executable binary programs required during booting, repairing, and other basic commands like cat, du, df, tar, rpm, wc, history, etc.
  • /boot: Holds important files during the boot-up process, including the Linux Kernel.
  • /dev: Contains device files for all hardware devices on the machine (e.g., cdrom, cpu, etc.).
  • /etc: Contains application configuration files, startup/shutdown scripts, and other configuration files for various programs.
  • /home: Home directory of users, each user has their own subdirectories (Desktop, Downloads, Documents, etc.).
  • /lib: Contains kernel modules and shared library images required to boot the system and run commands in the root file system.
  • /lost+found: Installed during the Linux installation, useful for recovering files broken due to unexpected shutdown.
  • /media: Temporary mount directory created for removable devices like media/cdrom.
  • /mnt: Temporary mount directory for mounting file systems.
  • /opt: Abbreviation for optional, contains third-party application software.
  • /proc: A virtual and pseudo file-system that contains information about running processes with a particular Process ID (PID).
  • /root: Home directory of the root user.
  • /run: The directory provides a clean solution for the early-runtime-dir problem.
  • /sbin: Contains binary executable programs required by the system administrator for maintenance (e.g., iptables, fdisk, ifconfig, swapon, reboot, etc.).
  • /srv: Abbreviation for service, contains server-specific and service-related files.
  • /sys: A virtual filesystem that stores and allows modification of devices connected to the system.
  • /tmp: System’s temporary directory accessible by users and root, stores temporary files until the next boot.
  • /usr: Contains executable binaries, documentation, source code, and libraries for second-level programs.
  • /var: Stands for variable and contains log, lock, spool, mail, and temporary files. This directory’s contents are expected to grow over time.

Modifying the Present Directory

With the cd command, you can modify the current directory.

cd /etc/sample

To go back to the home directory:

cd

or

cd ~

To move to the parent directory, assuming you are in the directory `/etc/sample/items/`:

cd ..

This command will bring you to /etc/sample/.

Display your Current Directory

To display your current directory, use the `pwd` command.

pwd

List a Directory's Content

To list the files and folders in a directory, use the ls command.

ls

Use to display hidden files:

ls -a

To display the contents of a directory with formatting and size information, use:

ls -l

To display the size of files in a directory with human-readable format, use:

ls -lh

Establish/Delete a Directory

To create a directory, use the mkdir command.

mkdir /root/newdir

To create a directory within a non-existing parent directory:

mkdir -p /root/notexists/newdir

In order to delete a directory:

rmdir /root/newdir

Recursively deleting a directory:

rmdir -p /root/notexists/newdir

Establish/Delete a File

To create an empty file, use the touch command.

touch /root/sample.txt

To permanently delete a file:

rm /root/sample.txt

To prevent yourself from accidentally removing a file:

rm -i /root/sample.txt

Then it will prompt you with a question asking if you really want to remove the specified file.

Duplicate a File or Directory

To copy a file, use the cp command.

cp /root/sample.txt /root/newfile.txt

To duplicate the whole directory:

cp -r /root/olddir /root/newdir

To avoid mistakenly overwriting existing files using cp.

cp -i /root/sample.txt /root/newfile.txt

If the file already exists in the destination, it will prompt you whether you want to overwrite the existing file.

Move/Rename a File or Directory

To rename a file or move it to another directory, use the mv command.

Renaming a file:

mv file12.txt file31.txt

Renaming a directory:

mv olddir newdir

To request authorization for a file rename:

mv -i file1.txt file20.txt

Utilising File Content

To show the file’s initial ten lines.

head /etc/passwd

In order to see a file’s first n lines:

head -5 file23.txt

Displays a file’s first five lines.

In order to see a file’s last ten lines,

tail file1.txt

To show a file’s final n lines:

tail -15 file1.txt

Will show a file’s final 15 lines.

Was this article helpful?
Dislike 0