DEV Community

Ishan Sharma
Ishan Sharma

Posted on • Originally published at ishan.co on

7 Essential Linux Commands You Need To Know

A close friend and mentor once told me that you only need to know around 10 commands to use Linux properly. After 10 years of Linux server use, I can confidently say that the number is pretty accurate.

However, if you are only looking for basic operations for a small project, you can do with much less. In this article, I’ll go through 7 Linux commands that you’ll use 99.99% of the times.

Quick note : I’m only covering the most frequent uses here. If you want to learn more, type man <command> in your terminal to see the manual (you can quit using q)

File Navigation

ls

ls is a simple command to list all the files in current directory. If you go ahead and just type ls in your (macOS or Linux) terminal, you’ll see the list of files in your home folder.

By itself, ls doesn’t give much information. I mostly use it with 3 arguments, like this: ls -lah. Here’s what the arguments do:

  1. l: This is lowercase ‘ell’, not ‘ei’. It will display the list in long format with file permissions (don’t bother with these for now) and lot of other details
  2. h: By default, Linux will display file sizes in ls -l list in bytes, without any units. Using h argument will make these file sizes human readable.
  3. a: This shows all hidden files (files started with . [dot] in linux).

cd

You most probably know cd from Windows. It lets you navigate to a new folder (directory).

Quick 3 step usage:

  1. cd folder-name switches to folder with name folder-name
  2. cd goes to your home folder (the one where you are by default when you log in, usually /home/<username>
  3. cd .. goes to the parent folder

A quick detour about paths : cd can take both relative and absolute paths. Absolute paths have either / or ~ in the beginning, for example /etc/hosts or /usr/local/bin.

Relative paths are, well, relative to the current folder. If you’re in songs folder and it has another folder maroon_5 inside it, cd maroon_5 will switch currently active folder to maroon_5, but only in songs folder.

File Management

These commands are useful for copying, moving and removing files. As a general rule, remember that these commands follow this format:

command <source> <destination>

cp & mv

cp <source> <destination> copies the source file to destination.

Quick example: You have a file called notes.txt inside your home directory and want to move it to the folder all_notes. Then you’ll use cp notes.txt all_notes/

This will fail if you try to copy a folder (directory in Linux). For copying folders, you have to use r parameter. To copy a folder notes_2018 to the folder all_notes, you’ll use cp -r notes_2018 all_notes

mv moves the files instead of copying, think cut and paste in windows. It follows same format as cp.

rm

rm removes a file or directory. Just like cp, you’ll need to specify -r argument if you are trying to remove folder.

P.S. I’m not covering editing files here. But if you ever need to do that, you can use vi <filename> or (much preferred) nano <filename> on Ubuntu like distros. Beware of vi though:

I've been using Vim for about 2 years now, mostly because I can't figure out how to exit it.

— I Am Devloper (@iamdevloper) February 17, 2014

Interacting with servers

ssh

ssh lets you login to remote servers. If you need to login to a Linux server, you’ll need:

  1. It’s IP address or hostname
  2. A valid username & password for the server

Use ssh <username>@<server> to start authentication. In most cases, server will prompt you for password and if it’s valid, you’ll be logged in.

Quick tip: Once you’re done, you can use exit to log out of the remote server.

scp

scp is handy when you want to transfer files between different servers or machines.

You’ll use it when you have some files on your laptop and want to move them to a remote server. Just use scp <local-file> <username>@<server>:<path>. For example, if you have a file on your Mac at /Users/sam/secret.txt, and you want to copy it to a secure server at 192.168.0.1 in the folder /tmp, you will use scp like this:

scp secret.txt <server-username>@192.168.0.1:/tmp

Done? These were the 7 commands that’ll work on any Linux (or Mac) server and desktop.

Liked the post? Do share it on Twitter or Reddit.

Have questions? Leave a comment and I’ll answer soon.

This post was originally published on my blog, 7 Essential Linux Commands You Need To Know

Top comments (1)

Collapse
 
gillchristian profile image
Christian Gill

TIL scp