ssh commands cheatsheet

My SSH Commands Cheatsheet


Lately, I’ve been doing a lot of work dealing with servers and I couldn’t help but turn to using SSH to do stuff in order to save time.

Before, I’d really prefer not doing tasks on my web servers via SSH because I did not start making things for the web using the command line. I was not used to it, so I’m kinda not really familiar with all those crazy Linux commands. They’re very intimidating to newbies. But at some point, I was forced to use it and as soon as I was using it, suddenly everything I used to do before, became a lot faster.

For example, before, when I transfer files from one server to another, I used to download the files to my computer and then upload it again to the other server. There was no other way for me to do that using the graphical interfaces provided by hosting companies. But then it gets really troublesome when you’re trying to move huge files and you do it this way. With SSH, you can skip the middleman and do transfers like this directly from server to server. And several gigabytes of files will be a breeze!

You see, SSH is like a shortcut for doing stuff. But I’d say it’s for advanced web admins only, because you really have to get yourself acquainted with the commands you’ll have to run for each specific task you want to get done. But it saves a lot of time, I mean lots of time. Mostly because you never see any graphics, all you see is a bunch of text on pure black background.

That’s the beauty in it, and also the one thing that makes it so intimidating to the weak-hearted. Makes you realize the reason why loser guys are so scared to approach beautiful women, is very much the same to the reason why newbies to web dev are scared to use SSH.

But the only hurdle you really have to overcome is learning how to use it, familiarizing the commands and actually understanding what they do. I was having a little hard time with that. For each simple task I tried to do, I had to google it and look at a few results before I could actually apply it.

So to stop (or at least minimize) the googling and to save time, I’ve compiled a list of these commands and other important notes about SSH’ing. There might be a gazillion similar posts already published out there about this but most of the ones I’ve found are only talking about one or two specific commands. I’ve never come across one that has this exact list.

This is a collection of the commands that I found myself using very often. So here it is for all those who might be looking for this also:

Paste copied text to command line prompt

If you are on a Windows PC and you are using Putty as your SSH client, you can just right-click on the putty window to paste copied texts to into the command prompt.

If you’re on a GNU-Linux computer, you should use the middle mouse button to paste your copied text into the command line.

Create a TAR archive
tar -czvf archivename.tar.gzip file_or_folder_to_archive

 
What the options mean:

  1. c – Create a TAR archive
  2. z – Compress the files in the archive
  3. v – Verbose (output what’s going on into the screen)
  4. f – File (meaning the filename for the resulting archive is provided)
Extract a TAR archive
tar -xzvf archive_to_extract.tar.gzip

 
What the options mean:

  1. x – Extract a TAR archive
  2. z – Contents of the archive has been compressed
  3. v – Verbose (output what’s going on into the screen)
  4. f – File (meaning the filename of the archive is provided)
Get file or directory size

for a file:

du -h filename.ext

 
for a directory:

du -ch | grep total

 
What the options mean:

  1. c – Display grand total
  2. h – Human readable file size digits
  3. grep totalGREP pipe for recursive file size estimation
Delete file
rm -v filename.ext

 
or

rm -v path/to/file.ext

 
What the options mean:

  1. v – Verbose (output what’s going on into the screen)
Delete folder & its contents
rm -rvf directory_name

 
What the options mean:

  1. r – Recursive (apply the command to the directory and its contents and subdirectories.)
  2. v – Verbose (output what’s going on into the screen)
  3. f – Force (ignore non-existent files, never prompt)
Copy files from remote location (another server)
wget https://domain.tld/filename.ext
Copy file to another folder
cp file_to_copy.ext path/to/dest_directory/filename.ext
Copy file to same folder
cp file_to_copy.ext dest_filename.ext
Copy whole folder including subfolders
cp -rvf * path/to/dest_directory/

 
What the options mean:

  1. r – Recursive (apply the command to the directory and its contents and subdirectories.)
  2. v – Verbose (output what’s going on into the screen)
  3. f – Force (overwrite existing files with the same filename, never prompt)
Move file to another directory
mv -fv file_to_copy.ext path/to/dest_directory/filename.ext

 
What the options mean:

  1. f – Force (overwrite existing files with the same filename, never prompt)
  2. v – Verbose (output what’s going on into the screen)
Move whole folder and subdfolders
mv -uv * path/to/dest_directory/

 
What the options mean:

  1. u – Update (only move file if source is newer than destination or if it doesn’t exist in destination)
  2. v – Verbose (output what’s going on into the screen)
Search for a file
find . -name myFile.txt

or:

find . -name "myFile*"

 
What the options mean:

  1. name – Filename to find
Search for a directory
find . -name directory_name -type d

 
What the options mean:

  1. name – Name of the directory to find
  2. type d – Filter results to include only directories
Search for a specific text inside files
grep -ril search_string path/to/directory

 
What the options mean:

  1. r – Recursive, search files in subdirectories also
  2. i – Ignore case
  3. l – Display only the name of the file that matches the search pattern (when using -r)
  4. c – Display total number of lines matching the search pattern
Search for specific files/folders and execute a command on them

This one is very useful when looking for multiple files with the same filename in different folders and doing something with them. For example, finding and deleting all files named error_log in all folders and subfolders.

Here’s how to do it:

find . -options -exec command -command_options {} /path/to/dest/if/required \;

 
To illustrate further, here are some real examples:
 
1. Find all subfolders within the current working directory and change permissions setting to 775:

find . -type d -exec chmod 775 {} \;

 
2. Find all files within the current directory and subdirectories and change permissions setting to 644:

find . -type f -exec chmod 644 {} \;

 
3. Find all .eml files in the current directory and subdirectories and transfer to another directory:

find . -name "*.eml" -exec mv -uv {} /home/user/mail/domain/account/cur \;

 
4. Find and delete all error_log files within the current directory and subdirectories:

find . -name "error_log" -exec rm -v {} \;

 

Print working directory (current location)
pwd
List contents of current directory
dir

or:

ls
Count number of files in current directory & subdirectories
find . -type f | wc -l

 

find . -type f finds all files ( -type f ) in this ( . ) directory and in all sub directories, the filenames are then printed to standard out one per line.

This is then piped ( | ) into wc (word count) the -l option tells wc to only count lines of its input.

Together they count all your files.

Get the size of a directory and its content
du -hs /path/to/directory

 
What the options mean:

  1. h – Get the figures in human-readable format, e.g. 140M instead of 143260 (size in KBytes)
  2. s – Summary, meaning you’ll get only the summary or total size of all the contents in the directory. Without this option, you’ll get not only the size of the folder but also the size of everything in the folder separately.
Read/Edit a file
pico -w /path/to/file.ext

or:

nano /path/to/file.ext

 
What the options mean:

  1. w – Disable word wrap (allowing edit of long lines)
  2. v – View only, disallowing editing

 

If you are viewing a really long file like a log file for example, it can take a long while to scroll to the end of the file. Here’s the shortcut for going to the end of the file: Ctrl + w + v

Create a directory
mkdir -vp directory_name

 
What the options mean:

  1. v – Verbose (output what’s going on into the screen)
  2. p – Parents (no error if existing, make parent directories if needed)
Export MySQL database
mysqldump -p -u db_user db_name > filename.sql

 
What the options mean:

  1. p – Password (not including your password in the command will cause it to prompt for the password when you hit enter)
  2. u – Username (user that is assigned/has access to the database being exported)

 

Just replace  db_user  with the actual username,  db_name  with the actual database name and  filename.sql  with the actual filename you want to give the exported file.

Import MySQL database
mysql -p -u db_user db_name < filename.sql

 
What the options mean:

  1. p - Password (not including your password in the command will cause it to prompt for the password when you hit enter)
  2. u - Username (user that is assigned/has access to the database being imported to)

 

Just replace  db_user  with the actual username,  db_name  with the actual database name and  filename.sql  with the actual filename of the file you want to import.

Check for the status of a command/program (process status)
ps aux|grep ssh|grep -v grep

 
What the options mean (see ref):

  1. a – List the processes of all users on the system rather than just those of the current user, with the exception of group leaders and processes not associated with a terminal. A group leader is the first member of a group of related processes.
  2. u – Provide detailed information about each process.
  3. x – Add to the list processes that have no controlling terminal, such as daemons, which are programs that are launched during booting.
  4. | – Pipe the output to another command/program
  5. grep – The command for searching text. In the above example, we searched for the text “ssh”, to filter out the results and be left with only the status for the ssh process.
  6.  |grep -v grep  – In this portion, we further filtered the results from the first piping to grep, so that we exclude grep’s own process which is finding the “ssh” process (in the first grep). The v option here inverts the selection.

As you keep using these commands, they sort of stick in your head and then you won't be needing a list like this anymore. But anytime you get stuck, if you can just remember the name of the command, you can just add the --help option to print the available options you can use for that command. That will save you a minute or two compared with going to Google.

Hope you'll find this list handy! Drop me a line below if you did...

Nimrod

People don't really know what they want until they learn about it. Marketing educates people about what they want. I'm a marketer, therefore I'm an educator.
Menu