Basic SSH Commands
If you are new to SSH, it is important to familiarize yourself with some basic command line tools.
First you need to log in to your server with your user account.
Before we proceed any further, a few words on one of the most important commands you can possibly know:
cp – Copy a File
The cp command copies a file from one place to another, or can create a new copy of a file in the same location with a different name.
Always backup your files before you modify them using a simple backup command:
cp file file.bak
This command will create a copy of “file” in the same directory called “file.bak”.
Now that you can backup files before modifying them it is much, much safer to start learning other commands.
The commands below are some of the most basic shell commands that you need to know in order to manipulate files (moving, copying, renaming, viewing, etc). Please note that each command has several more options available, all of which you can view using the man command.
man – Display Command Manual Files
Displays the manual file for the cp command.
(When viewing a manual file, type q to quit and return to the command line.)
pwd – Print Working Directory
Displays your location in the directory tree (path).
cd – Change Directories
Changes to your user’s home directory.
Returns you to the previous directory you were working in.
Moves you one directory up/back in the directory structure.
ls – List Directory Contents
Display directory contents in a “long list” format.
Display all files, including hidden, with a human-readable file size.
cp – Copy a File, More Uses
cp -r /home/user/pics /home/user2/
Recursively (meaning the directory and all of its contents) copy the pics directory from /home/user/ to /home/user2/ .
cp -r -v /home/user/pics /home/user2/
Do the same as the previous command but display a verbose (-v) progress list, showing each file as it is copied.
mv – Move and Rename Files
mv blah.txt /home/user/newdir/
Moves the file called blah.txt to the directory /home/user/newdir/ .
mv blah.txt blahblah.txt
Renames the file blah.txt to blahblah.txt.
chmod – Change Permissions
chmod 755 file.php
Changes the permissions on file.php to 755 (rwxr-xr-x).
chown – Change File Ownership
chown website:website file.php
Changes the file.php so that it is owned by the user and group called website.
rm – Remove a File (Delete)
rm -f oldindex.php
Deletes the file called oldindex.php.
rm -rf olddirectory/
Deletes the directory/folder called olddirectory.
cat – Display File Contents
Displays the entire contents of the file index.php.
grep – Search Files or Output
grep email@example.com /var/log/exim_mainlog
Searches the file exim_mainlog for all instances of the e-mail address firstname.lastname@example.org.
tail – Display End of a File
tail -n 50 /var/log/exim_mainlog
Displays the last 50 lines of the file exim_mainlog.
tail -f /var/log/exim_mainlog
Persistently watches the file exim_mainlog as the server makes changes to it (real-time).
w – List Active System Users
Displays a list of users who are logged in to the system through a remote shell or local terminal, what they are doing, and other relevant information.