Intro to Linux Commands

Intro to Linux Commands

6/25/2017


Introduction

No matter what sort of development job you have, a good understanding of Linux is essential. In this post, I'll walk through a lot of the basic Linux commands that will help you get your job done quicker and easier. This post assumes zero prior knowledge to Linux or working in a command line environment.


Basic commands

man

usage: man command-name

The reason I chose to put the man command first is that once you know this, it'll help you understand all sorts of other commands. The purpose of man is to display the 'manual page' for a specific command. So, if I was curious what whoami does, I can run man whoami and it'll print information like the usage and different parameter options. I highly recommend you using the man command on a lot of the commands below as you go through them.


cd

usage: cd desired-path

cd stands for change directory. Say you're in /user/david and you want to switch to /user/tim, you can simply run cd /user/tim and you'll be moved to that directory. Another useful command along with cd is pwd, which will tell you what directory you're currently in. Along with cd, here are some useful shortcuts that get used with cd to help you move even quicker.

  • cd : just typing cd with no arguments will take you to your home directory (same as cd ~)
  • cd .. : moves you one directory back. You can move multiple levels back by adding a slash between the two dots: cd ../..
  • cd - : moves you to the last directory you were in


ls

usage: ls desired-folder

ls gives you a listing of the contents of the directory. Just typing ls will list the contents of the current directory, but you can also pass a directory to ls to list its contents I would run man ls to see all of the different options you can pass, but here are some I find useful.

  • ls -l : long listing. This will show things like the date and size of the file and have the results formatted a bit nicer
  • ls -lt : long listing sorted by time with the newest showing at the top
  • ls -ltr : long listing sorted by reverse time with the newest showing at the bottom
  • ls -lh : long listing with human readable file sizes
  • ls -a : list showing hidden files

mv

usage: mv file-name desired-path

mv will move a file to the given path. mv is also used to rename files. Say I have a file called file1 in my current directory, and I want to rename it to file2. I can simply run mv file1 file2, which will essentially just rename the file.


cp

usage: cp file-name desired-path

cp is used to make a copy of a file. There's not too much to say on this one, but it gets used a ton. Run man cp to see some of the options.


File viewing commands

cat

usage: cat file-name

cat will display the contents of a file. You can also use tac to display the contents of the file in reverse order (pretty clever right?).



vi

usage: vi file-name

vi is a text editor that can be used on nearly all unix based systems. It is extremely powerful and has a ton of options inside it. vi could be its own series of posts, so I'll just keep this one brief. Once you have a file opened up in vi, you can make edits to the file by typing i, then making your inserts. Once you're done, hit the escape key, then :w to write the file, then :q to quit vi. You can also use those together by just typing :wq which will write the file and quit. If you want to quit without saving your changes, type :q!


Other file viewing commands

Here's some more file viewing commands that are available on most systems. I don't use them as often, but I figured I'd throw them out there. Give them a shot if you're interested.

  • less
  • more
  • nano
  • view
  • pico
  • emacs


Other commonly used commands

ssh

usage: ssh user-name@server-name

ssh stands for secure shell. This is most commonly used to get terminal access to a remote server or computer. If you're at work and have a development server in your data center, you can use ssh to get terminal access to that server. When using ssh, you can leave user-name blank if your username on the server is the same as the username that you are currently logged in as.


scp

usage: scp file-name username@server-name:desired-directory

scp stands for secure copy and is used to copy files from one server to another. If you want to copy a directory, you can use scp -r directory-name username@server-name:desired-directory


df

usage: df directory-name

df is used to see how much disk space is available. The directory name parameter is optional. This is commonly ran as df -h, which will print the size value in human readable format.


ps

usage: ps

ps is used to get a list of all the processes currently running on the machine. This is commonly ran as ps aux, which gives you a more complete picture on what is running. Once you run a ps command and have the pid (process ID), you can kill the process by running kill pid.


That's about all I have for now. Hopefully this was useful to get a basic understand of Linux commands. Feel free to leave a comment or question below. Thanks for reading!


Check out more posts in the Linux category!