Unix / Linux for Beginners

创建: 2022/12/9

完成: 2022/12/12

 https://www.tutorialspoint.com/unix/index.htm

Get Started
 Kernel The kernel is the heart of the operating system. It interacts with the hardware and most of the tasks like memory management, task scheduling and file management.
 Shell The shell is the utility that processes your requests. When you type in a command at your terminal, the shell interprets the command and calls the program that you want. The shell uses standard syntax for all commands. C Shell, Bourne Shell and Korn Shell are the most famous shells which are available with most of the Unix variants.
 Commands and Utilities There are various commands and utilities which you can make use of in your day to day activities. cpmvcat and grep, etc. are few examples of commands and utilities. There are over 250 standard commands plus numerous others provided through 3rd party software. All the commands come along with various options. 
 Files and Directories All the data of Unix is organized into files. All files are then organized into directories. These directories are further organized into a tree-like structure called the filesystem
 Examples
  •  login
  • passwd
  • cal
  • ls
  • whoami
  • users, who, w
  • logout
  • halt, init 0, init 6, poweroff, reboot, shutdown
   
File Management
 Files' Type  
Ordinary Files An ordinary file is a file on the system that contains data, text, or program instructions.
Directories Directories store both special and ordinary files. For users familiar with Windows or Mac OS, Unix directories are equivalent to folders.
Special Files Some special files provide access to hardware such as hard drives, CD-ROM drives, modems, and Ethernet adapters. Other special files are similar to aliases or shortcuts and enable you to access a single file using different names.
Listing Files  
ls

 

 
ls  
ls -l

 

$ls -l
total 1962188

drwxrwxr-x  2 amrood amrood      4096 Dec 25 09:59 uml
-rw-rw-r--  1 amrood amrood      5341 Dec 25 08:38 uml.jpg
drwxr-xr-x  2 amrood amrood      4096 Feb 15  2006 univ
drwxr-xr-x  2 root   root        4096 Dec  9  2007 urlspedia
-rw-r--r--  1 root   root      276480 Dec  9  2007 urlspedia.tar
drwxr-xr-x  8 root   root        4096 Nov 25  2007 usr
drwxr-xr-x  2    200    300      4096 Nov 25  2007 webthumb-1.01
-rwxr-xr-x  1 root   root        3192 Nov 25  2007 webthumb.php
-rw-rw-r--  1 amrood amrood     20480 Nov 25  2007 webthumb.tar
-rw-rw-r--  1 amrood amrood      5654 Aug  9  2007 yourfile.mid
-rw-rw-r--  1 amrood amrood    166255 Aug  9  2007 yourfile.swf
drwxr-xr-x 11 amrood amrood      4096 May 29  2007 zlib-1.2.3
$

 

First Column

Represents the file type and the permission given on the file. Below is the description of all type of files.

- Regular file, such as an ASCII text file, binary executable, or hard link.
b Block special file. Block input/output device file such as a physical hard drive.
c Character special file. Raw input/output device file such as a physical hard drive.
d Directory file that contains a listing of other files and directories.
l Symbolic link file. Links on any regular file.
p Named pipe. A mechanism for interprocess communications.
s Socket used for interprocess communication.

 

Second Column Represents the number of memory blocks taken by the file or directory.
Third Column Represents the owner of the file. This is the Unix user who created this file.
Fourth Column Represents the group of the owner. Every Unix user will have an associated group.
Fifth Column Represents the file size in bytes.
Sixth Column Represents the date and the time when this file was created or modified for the last time.
Seventh Column Represents the file or the directory name.

 

ls -a  
Metacharacters  
* to match 0 or more characters
? to match with a single character
Hidden Files An invisible file is one, the first character of which is the dot or the period character (.). Unix programs (including the shell) use most of these files to store configuration information.
Creating Files  
vi filename

 

Editing Files  
Display Content of a File

 

cat filename

 

  • -b with line number
Counting Words in a File

 

wc filename1( filename2 ...)

 

 

$ wc filename
2  19 103 filename
$

 

First Column Represents the total number of lines in the file.
Second Column Represents the total number of words in the file. 
Third Column Represents the total number of bytes in the file. This is the actual size of the file.
Fourth Column Represents the file name.

 

Copying Files  
cp source_file destination_file

 

Renaming Files  
mv old_file new_file

 

Deleting Files  
rm filename

 

rm filename1 filename2 filename3

 

 

Standard Unix Streams  
stdin This is referred to as the standard input and the associated file descriptor is 0. This is also represented as STDIN. The Unix program will read the default input from STDIN
stdout This is referred to as the standard output and the associated file descriptor is 1. This is also represented as STDOUT. The Unix program will write the default output at STDOUT
stderr This is referred to as the standard error and the associated file descriptor is 2. This is also represented as STDERR. The Unix program will write all the error messages at STDERR.
   
Directories
 Home Directory

  

~
~username

 

 

Absolute/Relative Pathnames  
root /
Absolute Pathnames
  • begin with a /
Relative Pathnames
  • never begin with a /
pwd  

 

 

Listing Directories   
ls dirname

 

Creating Directories  
mkdir dir_pathname (b c d e ...)
  •  -p: It creates all the necessary directories for you
Removing Directories   
rmdir a( b c ...)
  •  must be empty
Changing Directories   
cd ...

 

Renaming Directories

 

mv olddir newdir

 

. and .. 

 

. current working directory
 .. parent directory

 

File Permission
   
Owner permissions The owner's permissions determine what actions the owner of the file can perform on the file. 
Group permissions The group's permissions determine what actions a user, who is a member of the group that a file belongs to, can perform on the file.
Other (world) permissions The permissions for others indicate what action all other users can perform on the file.

 

 

The Permission Indicators

 

$ls -l /home/amrood
-rwxr-xr--  1 amrood   users 1024  Nov 2 00:10  myfile
drwxr-xr--- 1 amrood   users 1024  Nov 2 00:10  mydir

 

  • r: read
  • w: write
  • x: execute
  The first three characters (2-4) represent the permissions for the file's owner. For example, -rwxr-xr-- represents that the owner has read (r), write (w) and execute (x) permission.
  The second group of three characters (5-7) consists of the permissions for the group to which the file belongs. For example, -rwxr-xr-- represents that the group has read (r) and execute (x) permission, but no write permission.
  The last group of three characters (8-10) represents the permissions for everyone else. For example, -rwxr-xr-- represents that there is read (r) only permission.

 

Access Modes
  •  File

    Read Grants the capability to read, i.e., view the contents of the file.
    Write Grants the capability to modify, or remove the content of the file.
    Execute User with execute permissions can run a file as a program.
  • Directories

    Read Access to a directory means that the user can read the contents. The user can look at the filenames inside the directory.
    Write Access means that the user can add or delete files from the directory.
    Execute

    Executing a directory doesn't really make sense, so think of this as a traverse permission.

    A user must have execute access to the bin directory in order to execute the ls or the cd command.

Changing Permissions
Symbolic Mode

The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.

+ Adds the designated permission(s) to a file or directory.
- Removes the designated permission(s) from a file or directory.
= Sets the designated permission(s).

 

$chmod o+wx testfile
$ls -l testfile
-rwxrwxrwx  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod u-x testfile
$ls -l testfile
-rw-rwxrwx  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod g = rx testfile
$ls -l testfile
-rw-r-xrwx  1 amrood   users 1024  Nov 2 00:10  testfile

// all in once
$chmod o+wx,u-x,g = rx testfile
$ls -l testfile
-rw-r-xrwx  1 amrood   users 1024  Nov 2 00:10  testfile

 

Absolute Permissions  

The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.

Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that set.

Number Ref
0 ---
1 --x
2 -w-
3 -wx
4 r--
5 r-x
6 rw-
7 rwx

 

$ chmod 755 testfile
$ls -l testfile
-rwxr-xr-x  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod 743 testfile
$ls -l testfile
-rwxr---wx  1 amrood   users 1024  Nov 2 00:10  testfile
$chmod 043 testfile
$ls -l testfile
----r---wx  1 amrood   users 1024  Nov 2 00:10  testfile

 

 

Changing Owners and Groups  
chown

The chown command stands for "change owner" and is used to change the owner of a file. 

chown user filelist

 

 

chgrp

The chgrp command stands for "change group" and is used to change the group of a file.

chgrp group filelist

 

 

SUID and SGID File Permission  
   
   
Environment
 initialization

 When you log in to the system, the shell undergoes a phase called initialization to set up the environment. This is usually a two-step process that involves the shell reading the following files 

  • /etc/profile
  • profile

The process is as follows −

  • The shell checks to see whether the file /etc/profile exists.

  • If it exists, the shell reads it. Otherwise, this file is skipped. No error message is displayed.

  • The shell checks to see whether the file .profile exists in your home directory. Your home directory is the directory that you start out in after you log in.

  • If it exists, the shell reads it; otherwise, the shell skips it. No error message is displayed.

 

 The .profile File  

The file /etc/profile is maintained by the system administrator of your Unix machine and contains shell initialization information required by all users on a system.

The file .profile is under your control. You can add as much shell customization information as you want to this file. 

Setting the PATH   When you type any command on the command prompt, the shell has to locate the command before it can be executed.
PS1 and PS2 Variables  
Environment Variables  
DISPLAY Contains the identifier for the display that X11 programs should use by default.
HOME

Indicates the home directory of the current user: the default argument for the cd built-in command.

~

IFS Indicates the Internal Field Separator that is used by the parser for word splitting after expansion.
LANG LANG expands to the default system locale; LC_ALL can be used to override this. For example, if its value is pt_BR, then the language is set to (Brazilian) Portuguese and the locale to Brazil.
LD_LIBRARY_PATH A Unix system with a dynamic linker, contains a colonseparated list of directories that the dynamic linker should search for shared objects when building a process image after exec, before searching in any other directories.
PATH Indicates the search path for commands. It is a colon-separated list of directories in which the shell looks for commands.
PWD Indicates the current working directory as set by the cd command.
RANDOM Generates a random integer between 0 and 32,767 each time it is referenced.
SHLVL Increments by one each time an instance of bash is started. This variable is useful for determining whether the built-in exit command ends the current session.
TERM Refers to the display type.
TZ Refers to Time zone. It can take values like GMT, AST, etc.
UID Expands to the numeric user ID of the current user, initialized at the shell startup.
   
   
Basic Utilities
Printing Files
The pr Command  
The lp and lpr Commands   
The lpstat and lpq Commands  
The cancel and lprm Commands   
Sending Email

 

mail [-s subject] [-c cc-addr] [-b bcc-addr] to-addr

 

 

   
   
   
   
   
Pipes & Filters
   To make a pipe, put a vertical bar (  |  ) on the command line between two commands.
 The grep Command   
$grep pattern file(s)

 The grep command searches a file or files for lines that have a certain pattern.

  • If you don't give grep a filename to read, it reads its standard input; that's the way all filter programs work
-v Prints all lines that do not match pattern.
-n Prints the matched line and its line number.
-l Prints only the names of files with matching lines (letter "l")
-c Prints only the count of matching lines.
-i Matches either upper or lowercase.
   

 

 The sort Command

 The sort command arranges lines of text alphabetically or numerically. The following example sorts the lines in the food file

-n Sorts numerically (example: 10 will sort after 2), ignores blanks and tabs.
-r Reverses the order of sort.
-f Sorts upper and lowercase together.
+x Ignores first x fields when sorting.

 

 The pg and more Commands  
   
   
Processes
Starting a Process   

When you execute a program on your Unix system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the system.

Whenever you issue a command in Unix, it creates, or starts, a new process. When you tried out the ls command to list the directory contents, you started a process. A process, in simple terms, is an instance of a running program.

The operating system tracks processes through a five-digit ID number known as the pid or the process ID. Each process in the system has a unique pid.

Pids eventually repeat because all the possible numbers are used up and the next pid rolls or starts over. At any point of time, no two processes with the same pid exist in the system because it is the pid that Unix uses to track each process.

 
Foreground Processes

The process runs in the foreground, the output is directed to my screen, and if the ls command wants any input (which it does not), it waits for it from the keyboard.

While a program is running in the foreground and is time-consuming, no other commands can be run (start any other processes) because the prompt would not be available until the program finishes processing and comes out.

Background Processes

A background process runs without being connected to your keyboard. If the background process requires any keyboard input, it waits.

The advantage of running a process in the background is that you can run other commands; you do not have to wait until it completes to start another!

The simplest way to start a background process is to add an ampersand (&) at the end of the command.

 
 Listing Running Processes  

 

 ps 

 

 

$ps -f
UID      PID  PPID C STIME    TTY   TIME CMD
amrood   6738 3662 0 10:23:03 pts/6 0:00 first_one
amrood   6739 3662 0 10:22:54 pts/6 0:00 second_one
amrood   3662 3657 0 08:10:53 pts/6 0:00 -ksh
amrood   6892 3662 4 10:51:50 pts/6 0:00 ps -f

 

● columns:

UID User ID that this process belongs to (the person running it)
PID Process ID
PPID Parent process ID (the ID of the process that started it)
C CPU utilization of process
STIME Process start time
TTY Terminal type associated with the process
TIME CPU time taken by the process
CMD The command that started this process

● options

-a Shows information about all users
-f full
-x Shows information about processes without terminals
-e Displays extended information

 

 Stopping Processes  
Foreground Processes ctrl + C
Background Processes

 

kill

 

If a process ignores a regular kill command, you can use kill -9

Parent and Child Processes  

Normally, when a child process is killed, the parent process is updated via a SIGCHLD signal. Then the parent can do some other task or restart a new child as needed. However, sometimes the parent process is killed before its child is killed. In this case, the "parent of all processes," the init process, becomes the new PPID (parent process ID). In some cases, these processes are called orphan processes.

When a process is killed, a ps listing may still show the process with a Z state. This is a zombie or defunct process. The process is dead and not being used. These processes are different from the orphan processes. They have completed execution but still find an entry in the process table.

 Daemon Processes

 Daemons are system-related background processes that often run with the permissions of root and services requests from other processes.

A daemon has no controlling terminal. It cannot open /dev/tty. If you do a "ps -ef" and look at the tty field, all daemons will have a ? for the tty.

To be precise, a daemon is a process that runs in the background, usually waiting for something to happen that it is capable of working with. For example, a printer daemon waiting for print commands.

If you have a program that calls for lengthy processing, then it’s worth to make it a daemon and run it in the background.

The top Command top 
Job ID Versus Process ID

Background and suspended processes are usually manipulated via job number (job ID). This number is different from the process ID and is used because it is shorter.

In addition, a job can consist of multiple processes running in a series or at the same time, in parallel. Using the job ID is easier than tracking individual processes.

   
Communications
The ping Utility

 

ping hostname or ip-address

 

The ping command sends an echo request to a host available on the network. Using this command, you can check if your remote host is responding well or not.

 The ftp Utility  
 The telnet Utility  
 The finger Utility  
   
The vi Editor
   
   
   
   
   
   
posted @ 2022-12-09 19:09  懒虫哥哥  阅读(24)  评论(0编辑  收藏  举报