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.

user5@yourdomain.com [~]# 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:

user5@yourdomain.com [~]# 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:

user5@yourdomain.com [~]# cd testa/

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

user5@yourdomain.com [~/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:

user5@yourdomain.com [~/testa]# touch test-file-1.txt

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

user5@yourdomain.com [~/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:

user5@yourdomain.com [~/testa]# cp test-file-1.txt test-file-2.txt

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

user5@yourdomain.com [~/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:

user5@yourdomain.com [~/testa]# mv test-file-2.txt test-file-3.txt

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

user5@yourdomain.com [~/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:

user5@yourdomain.com [~/testa]# mkdir folder3
user5@yourdomain.com [~/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:

user5@yourdomain.com [~/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:

user5@yourdomain.com [~/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:

user5@yourdomain.com [~/testa]# rm test-file-1.txt

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

user5@yourdomain.com [~/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:

user5@yourdomain.com [~/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:

user5@yourdomain.com [~/testa]# rm -rf folder3/

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

user5@yourdomain.com [~/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 ../

Did you find this article helpful?

We value your feedback!

Why was this article not helpful? (Check all that apply)
The article is too difficult or too technical to follow.
There is a step or detail missing from the instructions.
The information is incorrect or out-of-date.
It does not resolve the question/problem I have.
How did you find this article?
Please tell us how we can improve this article:
Email Address
Name

new! - Enter your name and email address above and we will post your feedback in the comments on this page!

Related Questions

Here are a few questions related to this article that our customers have asked:
Ooops! It looks like there are no questions about this page.
Would you like to ask a question about this page? If so, click the button below!
Ask a Question

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

0 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!