Shell, Commands
Tags: linux, bash, shel
Published: 2022-09-01 08:28:31

Now we that we learned about shell,git and Github, we need to learn more shell commands to operate on the Linux shell system.

---

> Before we start with this topic take a tour on the Linux system. [Tour](http://linuxcommand.org/lc3_lts0040.php)

---

## type

We use type command to displays the kind of command the shell will execute and identifies whether this is a shell built-in command, subroutine, alias, or keyword. 

> alias is a command in various command-line interpreters, which enables a replacement of a word by another string.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h39z167uhwbwfoxy3x3c.png)

> Tip: for more info on type use man command or read [type](http://linuxcommand.org/lc3_lts0060.php)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ki4dy9x7os4irgcv1441.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmrzev8781z8alvga33t.png)

---

## which

We use which command to determine the exact location of a given executable.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fy70j4j2pl82muikc9u8.png)

> Tip: for more info on which use man command or read [which](http://linuxcommand.org/lc3_lts0060.php)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0sxqtq704wje82uvyjn.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6h7mnj2oi726e9eyhz8.png)


---

## ln

We use ln command to create links between files, like a shortcut in windows.

A link in UNIX are pointers pointing to a file or a directory and there are two types of links:- 

- **Soft Link or Symbolic links**: is similar to the file shortcut feature which is used in Windows Operating systems,any changes to the data in either file is reflected in the other.

we create a soft link by:
`ln  -s <original filename> <link name>`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69t1eie2pq1aj9s9xivy.png)

- **Hard Links**: Hard links more flexible and remain linked even if the original or linked files are moved throughout the file system.

we create a hard link by:
`ln  <original filename> <link name>`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dmbnbkryk147xa8mlc3n.png)

> Tip: for more info on which use man command or read [ln](https://www.geeksforgeeks.org/ln-command-in-linux-with-examples/)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cz5zcaepgef36ufaxjws.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o57hpw5sx8zdd7u09axa.png)

---

## help

we use the help command to get extra help if you are stuck on understanding the command.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6vly1k0irxpimbqvvt6o.png)

**help vs man**

Help is a built-in "usage" of the command, and not all commands implement it, or at least not the same way, however, man is a command by itself which is a single page that contains all information.

> Tip: for more info on help command read [help](http://linuxcommand.org/lc3_lts0060.php)


 ---

## man

We use man command to display the manual for each command in the shell.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqibsao8iast3bk85eaa.png) 

The man command is really important you can find ways to solve most of Linux problems, so the man is a really so important that there is a slang for it called RTFM.

> RTFM: RTFM is an initialism and internet slang for the expression "read the fucking manual" i guess some one was so mad that a programmer didn't read the manual, so RTFM. 

For example you might not know that `cd -` command print the previous dir and move back to the previous di,next time just checkout the manual for new things.

**How to read a man page**

To read a man page use this description of each concepts.

- **NAME** a short description of what the command is doing.

- **DESCRIPTION** a longer description of Mandatory arguments to long options are mandatory for short options too. .

- **SYNOPSIS** will help you understated the structure of the command.

> Tip: for more info on man, man the man page.

---

## file

we use file command to determine what kind of data a file contains before we try to view it like.

**.file** type may be of human-readable(e.g. ‘ASCII text’) or MIME type(e.g. ‘text/plain; charset=us-ascii’).

`file <file_name>`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/555av5cf7pg4ee31yqdd.png)

It has three sets of tests as follows:

- **filesystem test**:  The program verifies that if the file is empty, or if it’s some sort of special file.

- **magic test**: These tests are used to check for files with data in particular fixed formats.

- **language test**: This test search for particular strings which can appear anywhere in the first few blocks of a file.

> Tip: for more info on file command file use man or read [file](http://linuxcommand.org/lc3_lts0030.php)

---


## magic file

a magic file is used to find a file contains lines describing magic numbers which identify particular types of files.

Each line consists of four fields, separated by one or more tabs:

- The first field is a byte offset in the file

- The next field is a type: byte, short, long, string.

- The next field is a value, preceded by an optional operator.

- The rest of the line is a string to be printed if the particular file matches the template.

Example: 

0  byte  0x80        OMF object file (Microsoft relocatable)
0  short 0x5A4D      extended DOS executable (.EXE)
>0x32  string PKWARE         Self extracting Zip

**How to create and use a magic file**

To create a magic file we first need to create a file with a .mgc extension.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/66hm3kkjc0fer3zse252.png)

Add the argument a file that has R string at offset 0.
`0 string R has r data`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fj0oqmbv6phq7wfi3g5k.png)

Then compile the magic file using:
`file -C -m <your magic file>`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mplfjc20v3kkf4z1gsy8.png)

To use you magic file type the command:
`file -m <compiled magic file> *`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xdvbszfb5sezz2t9f455.png)

This will output all the file that has R string at offset in the directory  

> Tip: for more info on magic file use the command man magic or read [magic](https://www.mkssoftware.com/docs/man4/magic.4.asp)