Top 10 commands in terminal you will use everyday

26 Mar 2014

Posted by Milos Dolobac

In previous tutorial about how to create todo list app in rails you saw that we will use terminal with Rails most of the time.

In this tutorial you will learn top ten commands you will use everyday in terminal.

Let’s get started. First go to menu and open up: Command Prompt or PowerShell in Windows and Terminal in Linux and Mac. You should see something like this:

terminal-screen

Ok so everything works let’s begin.

1. cd(change directory)

To change actual working directory you use cd command. Type cd and directory into you want to move.

Move to another directory

To move into Desktop directory you will use this command:

$ cd Desktop

Go back to previous directory

You can go back to previous directory with cd ..

$ cd ..

2.ls or dir(list directory)

ls command shows list of all files and directories in current working directory. For example to see list of files of your home directory type this into command line in Linux and Mac.

$ ls

Type this in Windows

$ dir

You should see something like this:

list-of-directories

3. touch or copy con

Touch command creates empty file. For example try to create empty file in home folder:

$ touch empty.txt

Copy con command serves for creating empty file in Windows

$ copy con empty.txt

You should see your file in home directory.

4. mkdir(make directory)

mkdir command creates empty folder. Navigate to home folder and create folder of your choice:

mkdir my_folder

Check it out if folder has been created with ls or dir.

create-folder

You should see your folder in list. You can create any number of directories what you like. For example try create three directories:

mkdir folder1 folder2 folder3

You should see three new folders in current working directory. Let’s move on.

5. rm(remove)

Remove command deletes folder or file.

Remove folder

You type rm with -r and add name of the directory you want to remove:

$ rm -r my_folder

You should see that our folder has been removed. Option -r means recursive, it removes directory with all of his content. Let’s try something:

remove-directory

Removing files

You can use rm for removing files too. For example create file with touch command or copy con in Windows.

$ touch abc.txt
$ copy con abc.txt

Remove it with rm

$ rm abc.txt

On windows you remove files with del command following name of the file you want to remove.

$ del abc.txt

6. mv(move file or directory)

Move command serves for renaming or moving directories.

Rename directory or file with move

To rename your file type filename of file you want change and name of the file you want to change it.

$ mv abc.txt empty.txt

In Windows this won’t work. You have to use ren command to rename your file.

Check it out with ls or dir. You should see that file abc.txt is now renamed to empty.txt.

rename-file

We can use mv for renaming directories too. Try something like this: Create directory new_folder in your home directory. Now rename it to dir

$ mkdir new_folder
$ mv new_folder dir 
$ mkdir new_folder
$ ren new_folder dir

Moving files or directory with mv

You can use mv command to move files to specific directory. Move your empty.txt file to dir.

$ mv empty.txt ~/dir
$ ls

You can move any number of files as you want. For example create some files.

$ touch test1.txt test2.txt test3.txt
$ copy con test1.txt test2.txt test3.txt

Now move them to your folder.

$ mv test1.txt test2.txt test3.txt ~/dir

In Windows you will use move command instead of mv.

$ move test1.txt test2.txt test3.txt c:\dir

You don’t have to type name of files to terminal, you can use regular expressions. Try something like that:

$ mv *.txt ~/dir
$ move *.txt c:\dir

If you type *.txt it means move all files to folder.

7. cp(copy)

Copy command takes file or group or files and copy it to specific directory. For example try copy our text files back to home directory.

$ cp *.txt ~

On Windows instead of cp you will use copy

$ copy *.txt c:\

Go back to your home directory. You should see our files.

8. sudo or runas(switch user do)

If you want to install some programs on your Linux or Mac operating system you will have to use sudo. Let’s try to install Openbox, tool for creating and editing videos in terminal.

$ sudo apt-get install openbox

Or you can use runas in Windows. Type this:

$ runas /user:<localmachine>\adminstrator.cmd

You will be prompted for your user password.

sudo

Type this in and type letter Y for installation.

9. cat or type(concatenate)

Cat command serves for viewing of contents and concatenation of multiple files.

Viewing content of the file with cat

Create some file and type something in it with your text editor. Then view its content with cat.

$ cat test1.txt

On windows instead of cat you will use type command.

$ Type test1.txt

You should see content of your file like this.

cat-command

Joining multiple files to one file with cat

Let’s try to join our test files to one file with cat.

$ cat test1.txt test2.txt test3.txt > test.txt

10. clear

Clear command remove all previous commands from screen

$ clear

On windows you will use cls command.

$ cls

That’s all for now.

27-03-2014 - I’ve updated my post after critical responses on Reddit. Now it contains Windows equivalent of Linux/Mac/Unix commands.

comments powered by Disqus