Linux Basic Commands- pwd,mkdir,touch,ls,echo, cat,rm,cp,mv,history,grep

TjMan 31/May/2019 Shell Scripting

Introduction:

These are most widely used Linux command which we need almost every time we want to do anything on Linux. Open a Linux terminal and play with these commands.

PWD (print working directory):

It refers to print working directory. It prints the absolute path of directory where you are in right now.

[TuneToTech]$ pwd
/home/tjman

MKDIR (make directory):

Using this command you can create a directory on your present directory.

[TuneToTech]$ mkdir Shell_Scripting

CD (change directory):

[TuneToTech]$ cd Shell_Scripting/
[TuneToTech]$ pwd
/home/tjman/Shell_Scripting

TOUCH:

This command can create blank files.

[TuneToTech]$ touch x.txt y.txt z.txt

**Note: If you create a file with name stating with . ("dot") then it will be a hidden file.**

LS(list directory contents):

ls is probably the most used command in Linux to see the files and directories persent in current directory. It has many useful options as well. -a to show all files including hidden files -l list with general information -t sort content by recently modified first -r reverse the list -R recursive

[TuneToTech]$ ls
x.txt  y.txt  z.txt

[TuneToTech]$ ls -a
.  ..  .a.txt  .b.txt  .c.txt  x.txt  y.txt  z.txt

[TuneToTech]$ ls  -lrta
total 8
drwx------ 9 tjman tunetotech 4096 Apr 24 11:58 ..
-rw-r--r-- 1 tjman tunetotech    0 Apr 24 12:04 z.txt
-rw-r--r-- 1 tjman tunetotech    0 Apr 24 12:04 y.txt
-rw-r--r-- 1 tjman tunetotech    0 Apr 24 12:04 x.txt
-rw-r--r-- 1 tjman tunetotech    0 Apr 24 12:17 .c.txt
-rw-r--r-- 1 tjman tunetotech    0 Apr 24 12:17 .b.txt
-rw-r--r-- 1 tjman tunetotech    0 Apr 24 12:17 .a.txt
drwxr-xr-x 2 tjman tunetotech 4096 Apr 24 12:17 .

** Note: Lets understnd what the above output actually indicates. The first word is indicating a file or a directory. If it is 'd' means a directory and '-' refers to a file. rw-r--r-- This shows the file permission. We will discuss it in great details in next post. Then you can see a number 9,1,1.. so on, these are the number of hard link associated with the file. tjman is the owner of the file and the files is belong to tunetotech group. Next numeric values the size of the files in byte. Then you can see timestamp and lastly the file name at the extream right. **

ECHO:

echo will print whatever you write after it except quote. This can also be used to insert some text on a file as below.

[TuneToTech]$ echo "Welcome to TuneToTech"
Welcome to TuneToTech
[TuneToTech]$ echo Welcome to TuneToTech
Welcome to TuneToTech
[TuneToTech]$ echo 'Welcome to TuneToTech'
Welcome to TuneToTech
[TuneToTech]$ echo 'Welcome to TuneToTech' > x.txt

CAT:

cat is veryful to see every thing inside a files. As we just now insert some text on x.txt, we can see it using cat.

[TuneToTech]$ cat x.txt
Welcome to TuneToTech

RM:

To delete any file/directories, we can use rm command. -r , -R remove directories and their contents recursively
[TuneToTech]$ ls -lrt
total 0
-rw-r--r-- 1 tjman tunetotech 0 Apr 24 12:04 z.txt
-rw-r--r-- 1 tjman tunetotech 0 Apr 24 12:04 y.txt
-rw-r--r-- 1 tjman tunetotech 0 Apr 24 13:03 x.txt
[TuneToTech]$ rm y.txt
[TuneToTech]$ ls -lrt
total 4
-rw-r--r-- 1 tjman tunetotech  0 Apr 24 12:04 z.txt
-rw-r--r-- 1 tjman tunetotech 22 Apr 24 13:03 x.txt
[TuneToTech]$ ls -lrt
total 8
-rw-r--r-- 1 tjman tunetotech    0 Apr 24 12:04 z.txt
-rw-r--r-- 1 tjman tunetotech   22 Apr 24 13:03 x.txt
drwxr-xr-x 2 tjman tunetotech 4096 Apr 24 13:25 folder1
[TuneToTech]$ rm folder1
rm: cannot remove ‘folder1’: Is a directory
[TuneToTech]$ rm -r folder1
[TuneToTech]$ ls -lrt
total 4
-rw-r--r-- 1 tjman tunetotech  0 Apr 24 12:04 z.txt
-rw-r--r-- 1 tjman tunetotech 22 Apr 24 13:03 x.txt

CP:

cp refers to copy. You can make a copy of a file or directory using cp command. -r use it for folders to copy internal recursive files, otherwise folder copy will not work.

[tjman]$ cp welcome.txt welcome_copy.txt
[tjman]$ ls -lrt
total 12
-rw-r--r-- 1 tjman tunetotech   75 Apr 24 13:49 welcome.txt
drwxr-xr-x 2 tjman tunetotech 4096 Apr 24 14:05 folder1
-rw-r--r-- 1 tjman tunetotech   75 Apr 24 14:05 welcome_copy.txt
[tjman]$ cp folder1 folder1_copy
cp: omitting directory ‘folder1’
[tjman]$ cp -r folder1 folder1_copy
[tjman]$ ls -lrt
total 16
-rw-r--r-- 1 tjman tunetotech   75 Apr 24 13:49 welcome.txt
drwxr-xr-x 2 tjman tunetotech 4096 Apr 24 14:05 folder1
-rw-r--r-- 1 tjman tunetotech   75 Apr 24 14:05 welcome_copy.txt
drwxr-xr-x 2 tjman tunetotech 4096 Apr 24 14:05 folder1_copy

MV:

mv can be used to rename a file/directories or we can move the file/directories to another folder.

[tjman]$ ls -lrt
total 8
-rw-r--r-- 1 tjman tunetotech   75 Apr 24 13:49 welcome.txt
drwxr-xr-x 2 tjman tunetotech 4096 Apr 24 14:05 folder1
[tjman]$ mv welcome.txt welcome_2.txt
[tjman]$ ls -lrt
total 8
-rw-r--r-- 1 tjman tunetotech   75 Apr 24 13:49 welcome_2.txt
drwxr-xr-x 2 tjman tunetotech 4096 Apr 24 14:05 folder1
[tjman]$ mv welcome_2.txt folder1/
[tjman]$ ls -lrt
total 4
drwxr-xr-x 2 tjman tunetotech 4096 Apr 24 14:11 folder1
[tjman]$ ls -lrt folder1/
total 4
-rw-r--r-- 1 tjman tunetotech 75 Apr 24 13:49 welcome_2.txt

HISTORY:

history command is useful to see all perviously executed commands.

[TuneToTech]$ history
  1  ls -lrt
  2  rm folder1
  3  rm -r folder1
  4  ls -lrt
  5  touch rm
  6  rm rm
  7  man rm
  8  history

Change prompt name. As you can see so far the prompt name is "[TuneToTech]$". This can be change using below command(actually it is an environment variable).

[TuneToTech]$
[TuneToTech]$ PS1="[tjman]$ "
[tjman]$

Command pipelining("|"):

We can use pipe to execute one command after another where the output from first command is the input to second command. To understnd this lets insert some lines to a file.

[tjman]$ echo "Welcome to TuneToTech" >> welcome.txt
[tjman]$ echo "This is Shell Scpriting Tutorial" >> welcome.txt
[tjman]$ echo "Thanks for watching" >> welcome.txt
[tjman]$
[tjman]$ cat welcome.txt
Welcome to TuneToTech
This is Shell Scpriting Tutorial
Thanks for watching

GREP:

grep command can be piped with any suitable command and can show us those lines which is provided as a argument to grep. Below you can see argument to grep is 'watching' so cat provides all the lines to grep and grep filterout all lines but "Thanks for watching" as it is contains 'watching'.

[tjman]$ cat welcome.txt |grep watching
Thanks for watching

[tjman]$ history|grep touch
  165  touch x.txt y.txt z.txt
  169  touch .a.txt .b.txt .c.txt
  197  touch x.txt
  257  history|grep touch

Recent Post