List of content you will read in this article:
Unsecure networks, poor connection, and slow internet have caused many problems for network administrators, website owners, etc. when managing their files remotely. Imagine transferring your website files from one server to the other. It’s almost impossible over an internet connection. It’ll take years to transfer, not to mention how insecure it would be.
This is when SSH comes in handy. Two servers (computers) can easily connect through SSH over an open network. It’ll create strong encryption that lets people transfer and manage files remotely by running commands. At first, using this protocol might be daunting; however, after a while, you’ll learn everything about SSH file management and its practical use.
This article will cover how to manage files in SSH and what commands to use. Keep reading to find out more.
What is SSH protocol?
If you are a beginner, let’s see this protocol before we learn how to manage files in SSH (Secure Shell). If you are not a beginner, feel free to scroll down to find your answers more quickly.
In simple words, SSH is a network protocol that makes an unsecured network safe to use. How? Well, it can create a secure, encrypted channel on any network you want to make it invulnerable to possible attacks. However, unlike other third-party FTP apps, SSH only operates within the command line. So. to take advantage of this protocol, use Command Prompt if your operating system is Windows. For Mac and Linux systems Terminal will do.
In Mac and Linux, SSH is a native feature you can use immediately. However, if you have Windows 10, you may need third-party apps, such as Putty, to use this feature.
How to manage files in SSH
Although you can execute countless commands in SSH, here, we’ll only lay out SSH file manager commands. Whether you want to list, create, move, edit, or delete files and folders or want to learn how to manage your archives, we’ve got you covered.
First things first! Follow these steps to access the SSH terminal or command prompt:
- Login to your hosting
- Launch SSH terminal or Command Prompt
- Copy your password and username
- Paste them on the terminal
Now, you are logged in. Based on what action you want to perform, use the below commands.
How to list and open files and folders
If you want to list all files and folders, use the ls command. It will show you all files and folders available in the root directory. Now, to change the directory and get into one of the listed folders, write the cd command, then add the folder or file name.
For example, imagine after executing the ls command, you’ll see the below list:
If you want to get to the “Gdjoslld” folder, write the following command:
After getting into “Gdjoslld,” write the ls command again to see all files and folders in Gdjoslld. Repeat the steps until you reach the file or folder you want.
How to create a folder
To learn how to manage files in SSH, you must first create folders within the command line. mkdir is used for creating folders. For example, if you want to create a folder named “Folder 1” in SSH, paste the below command:
mkdir Folder 1
To check whether your folder has been successfully created or not, use the ls command to see the list. Do you want to get into the folder? Easy! Run the cd Folder 1 command.
If you want to see where you are or ensure you are in the right folder, use the PWD command.
How to create a file
The SSH command line for creating files is:
Write the name and extension in front of the command based on what type of file you want to create, whether it’s a PHP or txt file. For instance, if you want to create File1.php, run the following command:
To see whether your file has been successfully created or not, run the ls command.
How to rename a file or folder
The mv command is used for renaming files and folders. However, the command line is slightly different depending on whether you rename a file or folder. Suppose you are renaming text-file.php to newfile.php. Use:
mv text-file.php newfile.php
So basically, you are following this structure: mv old-file-name new-file-name
It’s the same for folders without the extension part:
mv myfolder renamedfolder
By executing this command, myfolder will be renamed to renamedfolder
How to move a file
Now, you almost know how to manage files in SSH. Let’s learn a few more tricks. Moving a file to another folder has the same command as renaming files and folders. The command syntax is:
mv source destination
For example, if you want to move “newfile.php” to “My-folder,” run the following command:
mv newfile.php My-folder
Then use “ls” to list folders and files, run the cd My-folder command to get into My-folder, and use ls again to see whether newfile.php has been successfully moved or not.
Moving multiple files at once
Don’t worry. It’s easier than you think. Moving multiple files follows the same command with a slight change. Name all the files you want to move and add (./) before the folder’s name.
For example, to move newfile.php and text-file.txt to My-folder, run the following command:
mv newfile.php text-file.txt ./My-folder
How to copy a file
The SSH file manager command for copying a file is cp. If you want to copy text-file.php to My-folder. Follow the cp source destination command:
cp text-file.php My-folder
How to delete a file or folder
To remove a file or folder in the SSH command line, use rm:
This command will delete the text-file.php file from the folder you are in. You can use the same for removing an empty folder. However, if you want to remove a folder and all files and subfolders inside it, use the rm -r command. For example:
rm -r My-folder
How to edit a file
The next command on our “how to manage files in SSH” list is vi, which is used for editing files:
- Run the vi text-file.php command to open the file and write the text.
- Press the INSERT key on your keyboard and start writing.
- To exit from the editor’s mode, press the ESC key.
- Then press the SHIFT+: keys to get back to the command line.
Then use any of these shortcut lines to finalize your editing status:
- q!: It will exit from the editors without saving.
- x!: It will save changes before exiting.
How to run a file
Running files command depends on the type of script you are running. For instance, if you want to run a PHP script, use the following command:
How to create and extract archives
The command line will differ depending on the archive file you want to extract or create. The most commonly used archive format is tar.gz. So to archive My-folder and create an archive named newarchive, use:
tar -zcf newarchive.tar.gz My-folder/
If you want to extract an archive, look at its extension first. Then follow the below commands based on each format.
- Zip file: unzip archive.zip
- Tar file: tar -xvf archive.tar
- Gz file: tar -zxvf archive.tar.gz
- Rar file: rar x archive.rar
How to search for a file or folder
The find command searches a file or folder's name, type, or modified time. If you want to search for the exact name of a file or folder under the current folder, run:
find . -name myFile.txt
However, if you don’t know the exact name or want to search for a part of the name, use the following:
find . -name "myFile*"
If you only want to find related directories, use the following:
find . -type d
To search for by modified time, in this case, 2 days ago, run:
find . -mtime -2
How to search for a text in files
If you are looking for a specific text in the content of one of your files, use the grep command. For example, to find the “database” word in “text-file.php,” run:
grep "database" text-file.php
However, if you don’t know which file contains the “database” word, use the following command to search all files under the current directory for the text:
grep -r -H "database" *
As you can see, learning how to manage files in SSH is quite easy. Commands are simple, and only a simple click is needed to run and execute them. But it is worth a try, as SSH protocol will provide a secure and fast network to get your work done.