How to Manage Files from within SSH

In our previous articles, we showed you how to login via ssh and also how to navigate through your folders via ssh. In this article, we’ll show you how to manage your files from within the command line (ssh). When referring to managing your files, we are referring to common tasks such as:

  • creating folders
  • creating files
  • copying files
  • moving / renaming files
  • deleting files

How can I create a folder?

Creating folders from within the command line is done using the mkdir command (make directories). The usage is simple, mkdir folder-name. In this example, we are going to create a folder named testa.

[email protected] [~]# mkdir testa

If we use the ls command, we can list the current files and folders in this directory. You can now see that the testa folder is listed:

[email protected] [~]# ls -alh
total 64K
drwx–x–x 10 user5 user5 4.0K Dec 5 12:52 ./
drwx–x–x 36 root root 4.0K Dec 5 11:15 ../
-rw-r–r– 1 user5 user5 24 Dec 5 11:15 .bash_logout
-rw-r–r– 1 user5 user5 191 Dec 5 11:15 .bash_profile
-rw-r–r– 1 user5 user5 124 Dec 5 11:15 .bashrc
-rw——- 1 user5 user5 18 Dec 5 11:15 .contactemail
-rw-r–r– 1 user5 user5 20 Dec 5 12:27 .dns
-rw-r–r– 1 user5 user5 147 Dec 5 11:15 .gemrc
drwxr-x— 2 user5 nobody 4.0K Dec 5 11:15 .htpasswds/
drwxr-x— 2 user5 mail 4.0K Dec 5 11:15 etc/
drwxr-x— 8 user5 user5 4.0K Dec 5 11:15 mail/
drwxr-xr-x 2 user5 user5 4.0K Dec 5 11:23 perl5/
drwxr-xr-x 3 user5 user5 4.0K Dec 5 11:15 public_ftp/
drwxr-x— 3 user5 nobody 4.0K Dec 5 11:15 public_html/
drwxr-xr-x 2 user5 user5 4.0K Dec 5 12:52 testa/
drwxr-xr-x 2 user5 user5 4.0K Dec 5 11:15 tmp/
lrwxrwxrwx 1 user5 user5 11 Dec 5 11:15 www -> public_html/

Now that we’ve created the testa folder, we will go into that folder using the cd command:

[email protected] [~]# cd testa/

We can confirm that we are in the testa folder by running the pwd (print working directory) command:

[email protected] [~/testa]# pwd
/home/user5/testa

How can I create a file?

There are several ways to create files while you’re in the command line. Using the touch command is one of the more basic commands. The basic syntax for the touch command is touch filename.txt. For example, to create a file named test-file-1.txt, we’ll run:

[email protected] [~/testa]# touch test-file-1.txt

Using the ls command, we can see that this file was created successfully:

[email protected] [~/testa]# ls -alh
total 8.0K
drwxr-xr-x 2 user5 user5 4.0K Dec 5 12:56 ./
drwx–x–x 10 user5 user5 4.0K Dec 5 12:52 ../
-rw-r–r– 1 user5 user5 0 Dec 5 12:56 test-file-1.txt

How can I copy a file?

Copying a file is done by using the cp (copy) command. The basic cp command syntax is cp original-file new-file. If we wanted to copy test-file-1.txt to test-file-2.txt, we can run:

[email protected] [~/testa]# cp test-file-1.txt test-file-2.txt

You can confirm the new file was created by running the ls command:

[email protected] [~/testa]# ls -alh
total 8.0K
drwxr-xr-x 2 user5 user5 4.0K Dec 5 12:58 ./
drwx–x–x 10 user5 user5 4.0K Dec 5 12:52 ../
-rw-r–r– 1 user5 user5 0 Dec 5 12:56 test-file-1.txt
-rw-r–r– 1 user5 user5 0 Dec 5 12:58 test-file-2.txt

How can I move / rename a file?

Those users familiar with Microsoft Windows are aware of the ‘cut’ and ‘paste’ commands. What these commands are actually doing is moving a file from one location to another. You can move files using the mv command (move). The basic usage of the command is mv original-file new-file. To move test-file-2.txt to test-file-3.txt:

[email protected] [~/testa]# mv test-file-2.txt test-file-3.txt

You can then use the ls command to confirm that the file was renamed:

[email protected] [~/testa]# ls -alh
total 8.0K
drwxr-xr-x 2 user5 user5 4.0K Dec 5 13:06 ./
drwx–x–x 10 user5 user5 4.0K Dec 5 12:52 ../
-rw-r–r– 1 user5 user5 0 Dec 5 12:56 test-file-1.txt
-rw-r–r– 1 user5 user5 0 Dec 5 12:58 test-file-3.txt

You can also move files to other directories. Let’s start off by creating a folder named folder3:

[email protected] [~/testa]# mkdir folder3
[email protected] [~/testa]# ls -alh
total 12K
drwxr-xr-x 3 user5 user5 4.0K Dec 5 13:08 ./
drwx–x–x 10 user5 user5 4.0K Dec 5 12:52 ../
drwxr-xr-x 2 user5 user5 4.0K Dec 5 13:08 folder3/
-rw-r–r– 1 user5 user5 0 Dec 5 12:56 test-file-1.txt
-rw-r–r– 1 user5 user5 0 Dec 5 12:58 test-file-3.txt

To move test-file-3.txt to folder3, run:

[email protected] [~/testa]# mv test-file-3.txt folder3/

Up to this point, we’ve used ls -alh to list what files are in the current directory. You don’t actually have to be in a directory to see the files in it. You can specify a folder to list the files of when using ls:

[email protected] [~/testa]# ls -alh folder3/
total 8.0K
drwxr-xr-x 2 user5 user5 4.0K Dec 5 13:08 ./
drwxr-xr-x 3 user5 user5 4.0K Dec 5 13:08 ../
-rw-r–r– 1 user5 user5 0 Dec 5 12:58 test-file-3.txt

How to delete a file / folder?

CAUTION! Linux does not include a “trash” folder. Unlike in Windows, once you delete a file in Linux, it is gone. There is not an option to retrieve it from a trash or recycling bin.

Deleting files in the command line can be done using the rm command (remove). The basic usage is rm file-to-delete. You can delete the test-file-1.txt by running:

[email protected] [~/testa]# rm test-file-1.txt

You can then run ls to confirm the file is no longer there:

[email protected] [~/testa]# ls -alh
total 12K
drwxr-xr-x 3 user5 user5 4.0K Dec 5 13:11 ./
drwx–x–x 10 user5 user5 4.0K Dec 5 12:52 ../
drwxr-xr-x 2 user5 user5 4.0K Dec 5 13:08 folder3/

If you were to try to delete the folder folder3, you could try running:

[email protected] [~/testa]# rm folder3
rm: cannot remove `folder3′: Is a directory

If you noticed, we received an error: rm: cannot remove `folder3′: Is a directory. In order to delete a folder, you must delete not only the folder but the files within the folder as well. You can do this with one command, rm -rf:

[email protected] [~/testa]# rm -rf folder3/

You can use the ls command to confirm that the folder folder3 has been removed:

<

p class=”cli”>[email protected] [~/testa]# ls -alh
total 8.0K
drwxr-xr-x 2 user5 user5 4.0K Dec 5 13:14 ./
drwx–x–x 10 user5 user5 4.0K Dec 5 12:52 ../

2 thoughts on “How to Manage Files from within SSH

  1. Dear 

    i have shared hosing (power)  pmclearn.com and i need to install pdfto text, catdoc how cani do that through SSH i already have SSH connection

    thanks in advance

    1. You should be able to use the wget or curl command to this url: https://github.com/jalan/pdftotext that will download the actual script for that program. Any specific/custom installation instructions should be provided in their documentation. However, you will need to make sure that the dependencies needed are installed. In the shared environment this is tricky but you can typically use the Perl modules available in cPanel to install the necessary dependencies. However, if any of the requirements require root access, you would need a VPS/Dedicated server to have that access granted.

Was this article helpful? Join the conversation!