These commands will give you a basic understanding of how to navigate and work with files in Linux terminal. In this tutorial we will cover Top 17 Basic SSH commands.
List of Basic SSH Commands
|Show directory contents (list the names of files).
|Create a new folder (directory).
|Create a new file.
|Remove a file.
|Show contents of a file.
|Show current directory (full path to where you are right now).
|Search for a specific phrase in file/lines.
|Search files and directories.
|Show last 50 used commands.
|Clear the terminal screen.
|Create & Unpack compressed archives.
|Download files from the internet.
|Get file size.
What you’ll need
Before you begin this guide you’ll need the following:
- Access to the Terminal.
Step 1 — Accessing remote server
It’s recommended to have a virtual server with a newly built template so that if you accidentally delete something not meant to be deleted, you could rebuild the server and start everything from scratch.
SSH stands for Secure Shell. It’s a protocol used to securely connect to a remote server/system. More detailed explanation on how does SSH work can be found here.
The basic command you should use is:
This command connects you to a server which has an IP address
serverip and username
user. Another, even simpler way to connect would be to use
ssh serverip, that way shell will think that you are connecting with the same user you are logged in with right now.
Once you enter this command, you will be prompted for a password (if you are connecting for the first time, you will also be prompted with a warning message that the server you are connecting to is not recognized, simply type in
yes on the command line).
That’s it, you are connected and can continue reading this guide on how to manage your files via Terminal! If you want to exit the remote server and get back to your local machine, simply type in
exit in the command line.
A more detailed guide on how to connect to the account using Putty SSH client can be found here.
Step 2 — Learning the commands
In this step, we will go through the main/most commonly used shell commands that you should learn about!
IMPORTANT: This applies for all commands in the shell. When writing an argument next to the command, for example,
cd 'Folder One'(where the folder name consists of two separate words), you must enter the folder name between quotes. Command
cd Folder One(without quotes) won’t work since shell will interpret it with two arguments (“Folder” and “One”).
ls– This command is used to list all files and directories. We recommend using this command with an option
-l, that would be
ls -l, in such way, all files will be listed in a more convenient manner and with more details/information about them. Another useful option is
-a, this will also show all the other files as well, which includes hiddenfiles/directories (dot files with a
.in front of them, for example: .ssh directory).
cd– This command is used to “walk” between directories (
cdstands for “change directory”). After listing all the files and directories with
ls, you can pick a directory to “walk” to. For example, let’s say there is a directory home which you want to enter. Enter the command
cd homeand you will instantly change your current location to “home”. You can try using
lsagain to see that the information which was outputted to the screen has changed. You can also type in a full path to a certain directory if, for example, your want to enter a directory which is few levels deep. You can use for example:
cd home/TestDirectory/AnotherDirectory. This way you will be immediately taken to the directory named “AnotherDirectory“. Use command
cd ..(space and two dots after cd) to move up one level (in our example, we would be moved back to “TestDirectory” from “AnotherDirectory“).
mkdir– This command is used to create a new directory (stands for “make directory”). It simply creates a new directory with your chosen name, for example
mkdir NewFolderwill create a new directory with a name “NewFolder” in your currently active directory (where you are right now).
touch– This command is used to create a new file with a chosen extension. For example,
touch NewFile.txtwill create a new txt file named “NewFile” in your current directory (the extension could actually be anything you want, you can even create a new file without extension at all, for example
rm– This command is used to remove a chosen file/directory. For example,
rm NewFilewill remove the previously created file named “NewFile”. If you wish to remove a directory and all the directories inside of it, use
rm -r NewFolder, this will delete the folder “NewFolder” and all the other folders inside of it.
cat– This command is used to display the contents of the file. For example,
cat info.txtwill bring the contents of the file to the screen. Other example:
cat info.txt info2.txt > mergedinfo.txtwill merge two files together (“info.txt” and “info2.txt“) and write the merged content to a file “mergedinfo.txt“.
pwd– This command shows your current location in the file system. For example, type in
pwd, the output could be something like this: “home/user/public_html“.
cp– This command is used to copy files and folders. The syntax is:
cp [options] source destBasically, instead of source, you write the file which you want to copy. Instead of dest, write the destination path/folder/file. Now, if you wrote a destination name which does not exist, for example, you have a source file oldfile.txt and write a destination file newfile.txt, bash will simply copy the file and paste it with a new name. Additionally, here are a few options you can use with a command
cp -f source dest– Force the copy procedure by removing the destination file if needed.
cp -i source dest– Will give you a warning message before overwritting file.
cp -u source dest– Update option. Will only copy if the source file is newer than destination file.
cp -n source dest– Won’t copy if the file already exists (Does not overwrite).
cp -a source dest– This option will archive the files.
mv– This command works the same way as cp, but instead, it moves the file instead of copying it. This command can also be used to rename the file. If we took the same example from the cp command, (in our current directory, we have one file oldfile.txt) and we write this command:
mv oldfile.txt newfile.txtbash will simply rename the file oldfile.txt to newfile.txt.
grep– This command looks for given string in files/folders. For example:
grep 'word' filewould search for a phrase ‘word’ in a file named “file”. grep will return the whole line from the file if the phrase is found. For example, there is a line ‘All in all it’s just another word in a sentence’ in the file named “file”, using command
grep 'word' file, this line will be outputted to the screen since
find– Now this command is used to search folders for a file or files that meet given criteria (name, size, file type). For example:
find . -name "*.html"This command will output all files in the current directory which have an ending/extension of “.html” (notice how we used “*” symbol in our command, that is a wildcard which basically tells bash that it does not matter how the file is named before the “.html” extension, it’s only important that the file ends with “.html“.
vi/nano– This command is used to enter a text editor. For example
nano newfilewill either create a new file called “newfile” and start the nano editor or edit an existing file “newfile” (if it exists) with the nano editor. Same thing applies for command
vi, which starts another editor called “vi”.
IMPORTANT: nano, unlike vi is not a default editor, you will most likely have to install it first if you wish to use it. You can find a guide on how to install nano editor here.
history– This command is used to display the last used commands. For example:
history 20will show the last 20 entered commands in the Terminal.
clear– This command clears all the text from the Terminal screen.
tar– This command is used to create or extract tar.gz archives. Most third party software binaries are in the form of tar.gz archives.
- To create a tar.gz archive of a folder, use the command:
tar cvzf ArchiveName.tar.gz /path/to/directory
- To unpack these files, use the command:
tar xvzf FileName.tar.gz
Here are what the options for the two commands represent:
xtells tar to extract files
ctells tar to create an archive
vstands for ‘verbose’ i.e. tar will print out all the file names in the console
zinstructs tar to uncompress the archive
ftells tar that you are supplying the file name of the archive
- To create a tar.gz archive of a folder, use the command:
wgetis used to download files from the internet. For example, to fetch a file from the internet and store it in your current directory. use command:
duis used to find the size of a file. Use
du -h /filepathto find the space occupied by a file. If you wish to know more about the
ducommand, you can check our dedicated tutorial on how to manage disk space in Linux here.
We hope this tutorial was clear enough to make you understand the basic SSH commands and how to use them. We recommend checking out this page for more details regarding the commands explained in this tutorial and many others!