DEV Community

Ahmed Gouda
Ahmed Gouda

Posted on • Updated on • Originally published at ahmedgouda.hashnode.dev

Linux Basic Commands

Architecture of Linux

Hardware

Terminals, Printers, Disks, etc

Kernel

Operating System core

  1. Hardware Dependent

    Code for a specific platform to initialize and configure hardware

  2. Hardware Independent

- Algorithms
- Scheduling Algorithms
- Network System
- File System
- User Management
Enter fullscreen mode Exit fullscreen mode

Shell

Application deals with Kernel through system calls, and also deals with the User through an interpreter.

Application

The user application

Image description

Image description

Linux Distribution

The Linux Distribution is the Linux Kernel and some applications for special purposes.

Embedded Systems Distributions

In Embedded Systems we need OS for specific predefined applications (Maybe we don't need GUI, Server Application, ... etc). We need minimal applications because memory size and hardware resources are very limited. Also, every embedded application is different from another.

There are many projects that manage you to generate your own Embedded Linux Distribution to serve your project and application, like Build Root and Yocto.

What is the Terminal?

Earlier in the past when computers were very large, the terminal was a small device connected with the large computer. Each user has its terminal, so for multiple users, every user will connect his own terminal to the computer.

Image description

Currently, we have Pseudo Terminal which emulates the classic terminal.

Once you open the pseudo terminal, you will find the shell where you can write your commands to execute programs, manage network, manage system, ... etc.

Image description


Basic Linux Commands

man

man is used to get help or documentation of any command. man ls, man printf

Please note that the manual is divided into sections. Section number will appear beside the command name. You can see chapters from the command man man

Image description

Some commands can be defined in more than one chapter like printf. There are printf (1) and printf (3).
printf (1) is a shell command, it is not a C program.

Example on printf shell command:

Image description

If you want the documentation of the C printf, you should write man 3 printf; where 3 represent the chapter number.

Which

which get the path of a command. which ls

file

file get more information about a specific file. Is it a text, executable, program, or whatever? file usr/bin/ls

ls

ls lists the files in the directory

Types of commands in the shell

  1. Built-in Commands: Shell program itself executes these commands.

  2. External Commands: Shell needs another program to execute the command.


File System Hierarchy

Linux is a single tree system starting from root /.

If you have multiple partitions (C, D, E), the kernel will make you see it is a single tree by using a mount point. You have root and under it there are folders, you use the partition under a folder by a specific command and that is called mount point.

Absolute and Relative Path

  1. Absolute Path: Always starts from root to where you are now. realpath command gets the parent of the directory.
  2. Relative Path: The path you want to go according to your current location. pwd command gets the working directory.

Working with Folders

Create Folder

Command mkdir creates a new folder. The sequence below let you make a folder named test go inside it and make another folder named test1

mkdir test/
cd test/
mkdir test1
Enter fullscreen mode Exit fullscreen mode

Rename/Move Folder

Command mv is used to rename a folder in the same directory or move a folder to another directory.

Rename:

mv test1/ test0
Enter fullscreen mode Exit fullscreen mode

Move:

mv test0/ ../
Enter fullscreen mode Exit fullscreen mode

This moved folder test0 to the parent directory Desktop

Copy Folder

Command cp is used to copy folder (directory) cp -r, or to copy file cp.

cp -r test0/ test/
Enter fullscreen mode Exit fullscreen mode

You can copy and rename, or move and rename at the same time using the below pattern:

cp -r ../test0/ test1
Enter fullscreen mode Exit fullscreen mode

You are in directory test, you get test0 using the relative path, copied it to the current path, and renamed it to test1 at the same time.

Working with Files

View File

Commands cat , more , and less are used to dump the file's content in the terminal.

  • cat dumps all the content
  • more dumps content fitted to your screen
  • less dumps content fitted to your screen and you can scroll with keyboard arrows

Command head gets the head of the file. And command tail gets the tail of the file.

Search in File

You can use grep command like that grep pattern path. Or you can use less command then press / and write the keyword you want to search for.

Editing a File

  1. Using Redirection:

    echo Ahmed >echo-output will echo the output to the file named echo-output

  2. Using Text Editor (Vim):

    Vim has two modes:

- Command Mode, which is opened by default
- Insert Mode, you write the character i to enter this mode

To open file in vim `vim file-name`. To edit the file you hit i to switch to insert mode. Then when exiting the file you hit ESC to return to command mode then you write `:!q` to exist without saving, or `:wq` to save and exit.
Enter fullscreen mode Exit fullscreen mode

Hello World in Vim

vim hello.c creates hello.c file in the working directory

Then you hit i to enter insert mode

write your program:

#include <stdio.h>

int main(){
    printf("Hello Linux\n");
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

To compile:

gcc hello.c
Enter fullscreen mode Exit fullscreen mode

To run the output:

./a.out
Enter fullscreen mode Exit fullscreen mode

./ start from the current directory then run a.out

Check Compilation Process Steps

  1. Preprocessing:

    gcc -E hello.c, the output here will be on the terminal. You could direct it to a file gcc -E hello.c > hello.i

  2. Compiling:

    gcc -S hello.i, assembly file hello.s will be created in the directory. You can view it vim hello.s

  3. Assembler:

    gcc -c hello.s, object file hello.o will be generated in the directory. You can view it vim hello.o

  4. Linking:

    gcc hello.o, executable file a.out will be generated in the directory.

    If you want another name instead of a.out use gcc -o myhello hello.o

Top comments (0)