Basic Linux Commands with Examples

Basic Linux Commands with Examples

I believe it’s very important to know at least basic commands of Linux (Also, other Operating Systems). You might say, “I’m a developer/business analyst/tester etc., I don’t need to know these Linux commands!“.

Oh sweet Linus Torvalds (Creator of Linux)… Be sure he’s not around when you say those kind of things, or else he might rage-mail (Read it, you won’t regret) you with the hatred of ten thousand years.

Screen Shot 2018-02-07 at 14.55.17
Linus Torvalds’ Ragemail to his employee

Let me tell you something. You’re going to regret saying that. I was thinking the same, then one day, I needed to see logs of our consumer website because it was returning 500 – Internal Server Error. Oh boy, didn’t I struggle. Opening those logs is easy even with the knowledge of basic Linux commands. Long story short, learn them. It’s easy.

1issnv
Linus Torvalds making sweet love with the camera

Also, it won’t hurt to know advanced commands which I’ll write whole another post for that.

And I provide you this online UNIX terminal if you want to practice commands:
https://www.tutorialspoint.com/unix_terminal_online.php

Typical Structure of a Command

Command         Option        Argument
ls                      -l                     /usr


pwd

pwd” is abbreviation for “Print Working Directory“. It basically prints the directory you are currently in. It is useful after you run the “cd” command (We are going to investigate it soon) to see where you are at. You are going to use this a lot.

Example:

(Print current directory)
[email protected]:~$ pwd
–>
/home/codeogre


cd

cd” is abbreviation for “Current Directory“. You will use this command with arguments to navigate in directories. Again, you are going to use this a lot.

Examples:

(Print current directory)
[email protected]:~ $ pwd 
–> /home/codeogre

(When you specify path with leading ‘/’, you give the exact path (from root))
[email protected]:~ $ cd /usr/lib
–>

(Print current directory)
[email protected]:lib $ pwd
–> /usr/lib

(Go to upper category)
[email protected]:lib $ cd ..
–>

(Print current directory)
[email protected]:usr $ pwd
–> /usr

(Go to lib, base directory is current directory)
[email protected]:usr $ cd lib
–>

(Print current directory)
[email protected]:lib $ pwd
–> /usr/lib

(Go to home directory)
[email protected]:lib $ cd ~
–>

(Print current directory)
[email protected]:~ $ pwd
–> /home/codeogre

(Go to root)
[email protected]:~ $ cd /
–>

(Print current directory)
[email protected]:/ $ pwd
–> /

(Go to previous directory)
[email protected]:lib $ cd –
–>

(Print current directory)
[email protected]:~ $ pwd
–> /home/codeogre


ls

ls” is abbreviation for “List“. ls lists directories and files under specified directory. There are lots of options that can be used with the command.

Examples:

(Print current directory)
[email protected]:~ $ pwd
–> /home/codeogre

(List files and directories inside under directory)
[email protected]:~ $ ls
–>
log   mysql   root   src   tmp

(List hidden and non-hidden files and directories under current directory)
[email protected]:~ $ ls -a
–> .   ..   .bash_history   .bash_logout   .bash_profile
.bashrc   log   mysql   root   src   tmp

(List files and directories with details under current directory)
[email protected]:~ $ ls -l
–>
drwxrwxrwx   2   cg         cg       4096    Sep    9    10:54    log
drwxrwxrwx   8   cg         cg       4096    Sep    9    10:49   mysql
drwxrwxrwx   1   cg         cg       4096    Feb    7    10:48   root
drwxr – xr – x   1   root     root    4096    Sep    9    20:42   src
drwxr – xr – x   2   root     root    4096    Aug   26  12:50   tmp

(List hidden and non-hidden files and directories with details under current directory)
[email protected]:~ $ ls -la
–>
drwxrwxrwx   1   cg        cg        4096    Sep    9    10:53   .
drwxr – xr – x   1   root     root    4096    Sep    9    10:25   ..
– rw – – – – –  –  –  1    cg        cg        540      Sep    9    10:49  .bash_history
– rw – r – – r – –   1    cg        cg        18        May   30  2017  .bash_logout
– rw – r – – r – –  1     cg        cg        193      May   30  2017  .bash_profile
– rw – r – – r – –  1     cg        cg        231      May   30  2017  .bashrc
drwxrwxrwx   2   cg         cg       4096    Sep    9    10:54    log
drwxrwxrwx   8   cg         cg       4096    Sep    9    10:49   mysql
drwxrwxrwx   1   cg         cg       4096    Feb    7    10:48   root
drwxr – xr – x   1   root     root    4096    Sep    9    20:42   src
drwxr – xr – x   2   root     root    4096    Aug   26  12:50   tmp

(List files and directories with details and total size under current directory)
[email protected]:~ $ ls -ls
–> total 20K
drwxrwxrwx   2   cg         cg       4096    Sep    9    10:54   log
drwxrwxrwx   8   cg         cg       4096    Sep    9    10:49   mysql
drwxrwxrwx   1   cg         cg       4096    Feb    7    10:48   root
drwxr – xr – x   1   root     root    4096    Sep    9    20:42   src
drwxr – xr – x   2   root     root    4096    Aug   26  12:50   tmp

(List files and directories recursively (all files) under current directory)
[email protected]:~ $ ls -R
–> ./src/samples/R:
README.txt
./src/samples/angularjs:
index.htm
.
.
.

(List files and directories with total size)
[email protected]:~ $ ls -s
–> total 20
4 log 4 mysql 4 root 4 src 4 tmp

(List files and directories then sort them by their size)
[email protected]:~ $ ls -S
–> root  mysql  log  src  tmp

(List files and directories then sort them by their date/time)
[email protected]:~ $ ls -t
–> root  src  log  mysql  tmp

(List files and directories under root directory)
[email protected]:~ $ ls root
–> README.txt

(List files and directories under root (root of system) directory, notice ‘/’)
[email protected]:~ $ ls /
–> bin  boot  dev  etc  home  lib  lib64
lost+found  media  mnt  opt  proc  root
un  sbin  srv  sys  tmp  usr  var


cp

cp” is abbreviation for “Copy“. I don’t think this needs an explanation as I already did at the past sentence.

Examples:

(Print current directory)
[email protected]:~ $ pwd
–> /home/codeogre

(List files and directories under current directory)
[email protected]:~ $ ls
–>
log   mysql   root   src   tmp

(List files and directories under root directory)
[email protected]:~ $ ls root
–>
README.txt

(List files and directories under log directory)
[email protected]:~ $ ls log
–>

(Copy the README.txt file under root directory to log directory)
[email protected]:~ $ cp root/README.txt log
–>

(List files and directories under log directory)
[email protected]:~ $ ls log
–> 
README.txt


mv

mv” is abbreviation for “Move“. You move files around.

Examples:

(Print current directory)
[email protected]:~ $ pwd
–> /home/codeogre

(List files and directories under current directory)
[email protected]:~ $ ls
–>
log   mysql   root   src   tmp

(List files and directories under root directory)
[email protected]:~ $ ls root
–>
README.txt

(List files and directories under log directory)
[email protected]:~ $ ls log
–>

(Move the README.txt file under root directory to log directory)
[email protected]:~ $ mv /root/README.txt log
–>

(List files and directories under log directory)
[email protected]:~ $ ls log
–> 
README.txt

(List files and directories under root directory)
[email protected]:~ $ ls root
–> 


touch

touch” is not an abbreviation (or you can think it’s abbreviation of itself…). You create files with this command by your given extension (empty file if not given any). Or you can use “touch” to change the timestamps (i.e., dates and times of the most recent access and modification) on existing files or directories (in case of you need to change modified date of your homework ( ͡° ͜ʖ ͡°)).

Examples:

(Print current directory)
[email protected]:~ $ pwd
–> /home/codeogre

(List files and directories under current directory)
[email protected]:~ $ ls
–>
log   mysql   root   src   tmp

(Create an empty file named Codeogre)
[email protected]:~ $ touch Codeogre
–>

(List files and directories under current directory)
[email protected]:~ $ ls
–>
log   mysql   root   src   tmp   Codeogre

(List files and directories under root directory)
[email protected]:~ $ ls root
–>
README.txt

(Create a text file under root category named Codeogre.txt)
[email protected]:~ $ touch root/Codeogre.txt
–>

(List files and directories under root directory)
[email protected]:~ $ ls root
–>
README.txt  Codeogre.txt


mkdir

mkdir” is abbreviation for “Make Directory“. In case you need/want to create a directory, you use this command.

Examples:

(Print current directory)
[email protected]:~ $ pwd
–> /home/codeogre

(List files and directories under current directory)
[email protected]:~ $ ls
–>
log   mysql   root   src   tmp

(Create a directory named test under current directory)
[email protected]:~ $ mkdir test
–>

(List files and directories under current directory)
[email protected]:~ $ ls
–>
log   mysql   root   src   tmp  test


rmdir

rmdir” is abbreviation for “Remove Directory“. In case you need/want to remove an empty directory, you use this command. Notice that you can only delete empty directories with this command. If you want to remove also the files inside of the directory, you need to use “rm” command which deletes files and directories without any warning. We will investigate “rm” next.

Examples:

(Print current directory)
[email protected]:~ $ pwd
–> /home/codeogre

(List files and directories under current directory)
[email protected]:~ $ ls
–> log   mysql   root   src   tmp

(List files and directories under root directory)
[email protected]:~ $ ls root
–>
README.txt

(Remove root directory)
[email protected]:~ $ rmdir root
–>
rmdir: failed to remove ‘mysql/’: Directory not empty

(List files and directories under root directory)
[email protected]:~ $ ls log
–>

(Remove log directory)
[email protected]:~ $ rmdir log
–>

(List files and directories under current directory)
[email protected]:~ $ ls
–> mysql   root   src   tmp


rm

rm” is abbreviation for “Remove“. If you want to delete files but not directories, you use this command. Actually, you can delete directories with -r or -R or –recursive options of this command. It recursively removes everything specified. It will prompt you if file has write-protected. You can bypass the prompt with -f option which is force basically.

Be aware that this command is so powerful. A wrong use (or fantasy) will result in deleting the whole system (Besides the files related with running processes at the initial time, but they won’t matter). Cool right? Do you think I should give you that command? I probably shouldn’t but you deserve to know. Just don’t run it unless you know what you are doing. It is “rm -rf /“. This will recursively delete all files starting from root to the very top.

Examples:

(Print current directory)
[email protected]:~ $ pwd
–> /home/codeogre

(List files and directories under current directory)
[email protected]:~ $ ls
–> log   mysql   root   src   tmp

(Remove log directory)
[email protected]:~ $ rm log
–>
rm: cannot remove ‘log’: Is a directory

(List files and directories under root directory)
[email protected]:~ $ ls root
–>
README.txt

(Remove README.txt file under root directory)
[email protected]:~ $ rm root/README.txt
–>

(List files and directories under root directory)
[email protected]:~ $ ls root
–>

(Create a text file named README.txt under root directory)
[email protected]:~ $ touch root/README.txt
–>

(List files and directories under root directory)
[email protected]:~ $ ls root
–> README.txt

(Remove README.txt file under root directory with prompt)
[email protected]:~ $ rm -i root/README.txt
–>
remove README.txt? n

(Create a directory named test2 under root/test1 directory (-p allows us to create subcategories))
[email protected]:~ $mkdir -p root/test1/test2
–>

(Create a text file named Codeogre.txt under root/test1/test2 directory)
[email protected]:~ $ touch root/test1/test2/Codeogre.txt
–>

(List files and directories under root directory)
[email protected]:~ $ ls root
–> README.txt  test1

(Remove root/test1 directory under root directory recursively (including all files under them))
[email protected]:~ $ rm -R root/test1
–>

(List files and directories under root directory)
[email protected]:~ $ ls root
–> README.txt


cat

cat” is abbreviation for “catenate“. It is the simplest way to display the content of a file. You can also combine two files and read them.

Editing a text file is more advanced so I’ll write a separate post about it.

Examples:

(Print current directory)
[email protected]:~ $ pwd
–> /home/codeogre

(Display contents of root/README.txt file)
[email protected]:~ $ cat root/README.txt
–> Codeogre is the BEST!

(Display contents of root/README.txt and mysql/test.txt files)
[email protected]:~ $ cat root/README.txt mysql/test.txt
–> Codeogre is the BEST!
mysql test file


find

find” is to find. You find files with it. I find finding a file with find command quite finding. Okkkkeyyy. Here is the examples…

Before the examples, you need to specify ‘/’ character to make Linux to start finding the given file from the root directory. It will take some time though. Or if you want to search the file from your initial directory, specify ‘.’ character.

There are other ways to accomplish this quicker, like “locate” command. It locates the file from the database Linux created which stores all files and directories inside. The only advantage of “locate” command is speed.

Examples:

(Print current directory)
[email protected]:~ $ pwd
–> /home/codeogre

(Find README.txt file, starting from the root of the system)
[email protected]:~ $ find / -name README.txt
–> …/home/codeogre/root/README.txt

(Find README.txt file, starting from the current directory of the system)
[email protected]:~ $ find . -name README.txt
–> ./root/README.txt


clear

You can use “clear” to clear the terminal.

Also there is a shortcut for this command: CTRL + L.

Example:

(Clear terminal window)
[email protected]:~ $ clear
–> 


These commands should give you the fundamentals to advance in Linux commands. I’ll provide you with advanced Linux commands later. And finally:

5fe1715d6decd488bc45bc1222b48252663a3c84ed7da2cd40f468eddd92a1ad

Leave a Reply

Close Menu