The second week assignment of Linux Basic Introduction [Linux Micro Position]

Keywords: Linux DBus network socket

1. What are the file management commands on Linux, their common usage methods and related examples.

1. Directory management class commands:

mkdir: Create directories


Common parameters:

- p: Automatically create parent directory on demand

- v: Display the detailed process

- m MODE: Given permissions directly

[root@localhost ~]# mkdir -pv -m 600 /tmp/a/b
mkdir: created directory '/tmp/a'
mkdir: created directory '/tmp/a/b'
[root@localhost ~]# ls -ld /tmp/a/b
drw-------. 2 root root 6 May  7 21:14 /tmp/a/b



rmdir: Delete empty directories


Common parameters:

- p: After deleting a directory, if its parent directory is empty, delete it together

- v: Display the detailed process

[root@localhost ~]# rmdir -pv /tmp/a/b
rmdir: removing directory, '/tmp/a/b'
rmdir: removing directory, '/tmp/a'
rmdir: removing directory, '/tmp'
rmdir: failed to remove directory '/tmp': Device or resource busy



2. File management class commands:

cp: Copy files

Single source replication: CP [OPTION] [-T] SOURCE DEST

If DEST does not exist: create this file beforehand and copy the data flow from the source file to DEST

If DEST exists: if DEST is a non-directory file, overwrite the target file; if DEST is a directory file, create a file with the same name as the source file in the DEST directory and copy its data stream

[root@localhost ~]# mkdir /tmp/src /tmp/dst
[root@localhost ~]# touch /tmp/src/file{1,2,3}
[root@localhost ~]# ls /tmp/src/ /tmp/dst
/tmp/dst:
/tmp/src/:
file1  file2  file3
[root@localhost ~]# cp /tmp/src/file1 /tmp/dst/
[root@localhost ~]# ls /tmp/dst/
file1
[root@localhost ~]# cp /tmp/src/file2 /tmp/dst/file1
cp: overwrite '/tmp/dst/file1'? y
[root@localhost ~]# ls /tmp/dst/
file1
[root@localhost ~]# cp /tmp/src/file2 /tmp/dst/
[root@localhost ~]# ls /tmp/dst/
file1  file2



Multi-source replication: cp [OPTION]... SOURCE... DIRECTORY

          cp [OPTION]... -t DIRECTORY SOURCE...

If DEST does not exist: error

If DEST exists: if DEST is a non-directory file, error; if DEST is a directory file, copy each file to the target directory separately and keep its original name.

Note: Multi-source replication only supports DEST as directory file

[root@localhost ~]# cp /tmp/src/* /tmp/dst1/
cp: target '/tmp/dst1/' is not a directory
[root@localhost ~]# cp /tmp/src/* /tmp/dst/file
cp: target '/tmp/dst/file' is not a directory
[root@localhost ~]# cp /tmp/src/* /tmp/dst/
cp: overwrite '/tmp/dst/file1'? y
cp: overwrite '/tmp/dst/file2'? y 
[root@localhost ~]# ls /tmp/dst
file1  file2  file3


Common parameters:

- i: Interactive, i.e. alerting users to confirm before coverage

Note: The root user defaults to use the alias command with the - i parameter, the ordinary user defaults to use the command itself, and the root user also wants to use the command itself to add "" before the command.

[root@localhost ~]# alias
alias cp='cp -i'


- f: Mandatory overwriting of target files

[root@localhost ~]# echo "123" > /tmp/src/file1
[root@localhost ~]# cp -f /tmp/src/file1 /tmp/dst/file1
cp: overwrite '/tmp/dst/file1'? y
[root@localhost ~]# \cp -f /tmp/src/file1 /tmp/dst/file1
[root@localhost ~]# cat /tmp/dst/file1
123


- r, -R: Recursive copy directory

- d: Copy the symbolic link file itself, not the source file it points to

[root@localhost ~]# ln -s /tmp/src/file1 /tmp/src/filelink
[root@localhost ~]# ls -l /tmp/src/
total 4
-rw-r--r--. 1 root root  4 May  7 23:55 file1
-rw-r--r--. 1 root root  0 May  7 23:46 file2
-rw-r--r--. 1 root root  0 May  7 23:46 file3
lrwxrwxrwx. 1 root root 14 May  8 00:08 filelink -> /tmp/src/file1
[root@localhost ~]# cp -d /tmp/src/filelink /tmp/dst/
[root@localhost ~]# ls -l /tmp/dst/
total 4
-rw-r--r--. 1 root root  4 May  8 00:02 file1
-rw-r--r--. 1 root root  0 May  7 23:57 file2
-rw-r--r--. 1 root root  0 May  7 23:57 file3
lrwxrwxrwx. 1 root root 14 May  8 00:09 filelink -> /tmp/src/file1
[root@localhost ~]# cp /tmp/src/filelink /tmp/dst/filelink1
[root@localhost ~]# ls -l /tmp/dst/
total 8
-rw-r--r--. 1 root root  4 May  8 00:02 file1
-rw-r--r--. 1 root root  0 May  7 23:57 file2
-rw-r--r--. 1 root root  0 May  7 23:57 file3
lrwxrwxrwx. 1 root root 14 May  8 00:09 filelink -> /tmp/src/file1
-rw-r--r--. 1 root root  4 May  8 00:12 filelink1


- a:-dR --preserve=all, archive, for archiving

- preserv=mode: permissions

          ownership: principal and subgroup

          Timestamps: timestamps

          Context: security label (selinux security context)

          xattr: Extended attributes

          Links: symbolic links

          all: all of the above attributes



mv: Moving files or renaming

mv [OPTION]... [-T] SOURCE DEST

mv [OPTION]... SOURCE... DIRECTORY

mv [OPTION]... -t DIRECTORY SOURCE..


Common parameters:

- i: Interactive, i.e. alerting users to confirm before moving

- f: Forced movement

[root@localhost ~]# mv /tmp/dst/file1 /tmp/dst/file4
[root@localhost ~]# ls -l /tmp/dst/
total 8
-rw-r--r--. 1 root root  0 May  7 23:57 file2
-rw-r--r--. 1 root root  0 May  7 23:57 file3
-rw-r--r--. 1 root root  4 May  8 00:02 file4
lrwxrwxrwx. 1 root root 14 May  8 00:09 filelink -> /tmp/src/file1
-rw-r--r--. 1 root root  4 May  8 00:12 filelink1


rm: Delete files

rm [OPTION]... FILE...


Common parameters:

- i: Interactive, which reminds users to confirm before deleting

- f: Forced deletion

- r: Recursive deletion

[root@localhost ~]# rm -rf /tmp/dst/
[root@localhost ~]# ls /tmp/dst
ls: cannot access /tmp/dst: No such file or directory



3. File View Class Command:

ls: Lists the contents in the specified directory


Common parameters:

- a: Display all files, including hidden files

[root@localhost ~]# ls -a /
.   bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
..  boot  etc  lib   media  opt  root  sbin  sys  usr


- A: Display all files except. and..

[root@localhost ~]# ls -A /
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr


- l: Long format lists, which show detailed properties of files

- h: Convert file size units in a human-readable manner; the results may be inaccurate

[root@localhost ~]# ls -lh /
total 32K
lrwxrwxrwx.   1 root root    7 Aug  3  2016 bin -> usr/bin
dr-xr-xr-x.   3 root root 4.0K Aug  2  2016 boot
drwxr-xr-x.  19 root root 3.1K Aug  2  2016 dev
drwxr-xr-x. 132 root root 8.0K Aug  2  2016 etc
drwxr-xr-x.   4 root root   90 Aug  2  2016 home
lrwxrwxrwx.   1 root root    7 Aug  3  2016 lib -> usr/lib
lrwxrwxrwx.   1 root root    9 Aug  3  2016 lib64 -> usr/lib64
drwxr-xr-x.   3 root root   18 Aug  2  2016 media
drwxr-xr-x.   3 root root   17 Aug  2  2016 mnt
drwxr-xr-x.   3 root root   15 Aug  3  2016 opt
dr-xr-xr-x. 505 root root    0 Aug  3  2016 proc
dr-xr-x---.  14 root root 4.0K Aug  2  2016 root
drwxr-xr-x.  35 root root 1.2K May  7 21:26 run
lrwxrwxrwx.   1 root root    8 Aug  3  2016 sbin -> usr/sbin
drwxr-xr-x.   2 root root    6 Mar 13  2014 srv
dr-xr-xr-x.  13 root root    0 Aug  3  2016 sys
drwxrwxrwt.  17 root root 4.0K May  7 21:54 tmp
drwxr-xr-x.  13 root root 4.0K Aug  3  2016 usr
drwxr-xr-x.  22 root root 4.0K Aug  3  2016 var


- d: View the directory itself rather than its internal file list

[root@localhost ~]# ls -ld /etc
drwxr-xr-x. 132 root root 8192 Aug  2  2016 /etc


- r: Reverse Display

[root@localhost ~]# ls -r /
var  tmp  srv   run   proc  mnt    lib64  home  dev   bin
usr  sys  sbin  root  opt   media  lib    etc   boot


- R: Recursive display


Note: File types on Linux systems are:

- file, regular file

d: directory, directory file

b: block device, block device file, supports random access in "block" units

c: character device, character device file, supports linear access in character

major number: the main device number, which identifies the device type and then determines the driver to load

minor number: secondary device number, used to identify different devices in the same type

l: symbolic link, symbolic link file

p: pipe, named pipe

s: socket, socket file



File: View the file content type

[root@localhost ~]# file /sbin/ss
/sbin/ss: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for 
GNU/Linux 2.6.32, BuildID[sha1]=0x14230a7d5743155147230c263cb721757a49db89, stripped



cat: Connect and display files


Common parameters:

- n: Number the text line displayed

[root@localhost ~]# cat -n /etc/fstab 
     1
     2#
     3# /etc/fstab
     4# Created by anaconda on Tue Aug  2 21:31:19 2016
     5#
     6# Accessible filesystems, by reference, are maintained under '/dev/disk'
     7# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
     8#
     9/dev/mapper/rhel-root   /                       xfs     defaults        1 1
    10UUID=ce40e87b-854d-42dc-ac50-e1309101c43d /boot                   xfs     defaults        1 2
    11/dev/mapper/rhel-swap   swap                    swap    defaults        0 0
    12/dev/cdrom/media/cdromiso9660defaults0 0


- E: Display line terminator$

[root@localhost ~]# cat -E /etc/issue
\S$
Kernel \r on an \m$
$



tac: Connect and display files, which is the reverse display of cat


Common parameters:

- n: Number the text line displayed

- E: Display line terminator$



head: View the first n lines of the file


Common parameters:

- n # (-#): Line before viewing the file

[root@localhost ~]# head -n 10 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin



tail: n lines after viewing the file


Common parameters:

- n #(-#): After viewing the file # line

[root@localhost ~]# tail -5 /etc/passwd
gnome-initial-setup:x:993:991::/run/gnome-initial-setup/:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
linuxprobe:x:1000:1000:linuxprobe:/home/linuxprobe:/bin/bash


- f: Do not quit after viewing the contents of the end of the file, follow the new line (usually used to view system log information)

[root@localhost ~]# tail -f /var/log/messages 
May  7 22:18:40 localhost dbus[1088]: [system] Activating via systemd: service name='net.reactivated.Fprint' 
unit='fprintd.service'
May  7 22:18:40 localhost systemd: Starting Fingerprint Authentication Daemon...
May  7 22:18:41 localhost dbus-daemon: dbus[1088]: [system] Successfully activated service 
'net.reactivated.Fprint'
May  7 22:18:41 localhost dbus[1088]: [system] Successfully activated service 'net.reactivated.Fprint'
May  7 22:18:41 localhost systemd: Started Fingerprint Authentication Daemon.
May  7 22:18:41 localhost fprintd: ** (fprintd:4822): WARNING **: fprint init failed with error -99
May  7 22:18:41 localhost systemd: fprintd.service: main process exited, code=exited, status=157/n/a
May  7 22:18:41 localhost systemd: Unit fprintd.service entered failed state.
May  7 22:20:01 localhost systemd: Starting Session 9 of user root.
May  7 22:20:01 localhost systemd: Started Session 9 of user root.



more: View files separately, flip the screen to the end of the file and exit automatically



less: Screen-split view of files, support up and down page flipping, query operations, press q to exit



2. Return value of command execution status and command line expansion of bash's working characteristics and demonstration of their examples.

1. bash outputs the result of command execution through the return value of state:

Success: 0

Failure: Random numbers between 1 and 255, with different results depending on commands and their functions

After the command is executed, its state return value is saved in the special variable $? Of bash, which can be viewed through echo $?

[root@localhost ~]# ls /boot/
config-3.10.0-123.el7.x86_64
grub2
initramfs-0-rescue-49da9e087d6e4f9dab50f2af4a9a5921.img
initramfs-3.10.0-123.el7.x86_64.img
initramfs-3.10.0-123.el7.x86_64kdump.img
initrd-plymouth.img
symvers-3.10.0-123.el7.x86_64.gz
System.map-3.10.0-123.el7.x86_64
vmlinuz-0-rescue-49da9e087d6e4f9dab50f2af4a9a5921
vmlinuz-3.10.0-123.el7.x86_64
[root@localhost ~]# echo $?
0
[root@localhost ~]# ls /boot/error
ls: cannot access /boot/error: No such file or directory
[root@localhost ~]# echo $?
2


2. What is involved in command line expansion

~ Automatically expand to the user's home directory, or the specified user's home directory

[root@localhost ~]# cp /etc/issue ~/
[root@localhost ~]# ls ~/
2017-05-07  anaconda-ks.cfg  Downloads             Music     Templates
22:50       Desktop          initial-setup-ks.cfg  Pictures  Videos
22:50:00    Documents        issue                 Public


{}: A comma-separated list of paths can be hosted and expanded into multiple paths

[root@localhost ~]# touch /tmp/file{1..10}
[root@localhost ~]# ls /tmp/file*
/tmp/file1   /tmp/file2  /tmp/file4  /tmp/file6  /tmp/file8
/tmp/file10  /tmp/file3  /tmp/file5  /tmp/file7  /tmp/file9



3. Use the command line expansion function to complete the following exercises:

(1) Create / tmp directories: a_c, a_d, b_c, b_d

[root@localhost ~]# mkdir -pv /tmp/{a,b}_{c,d}
mkdir: created directory '/tmp/a_c'
mkdir: created directory '/tmp/a_d'
mkdir: created directory '/tmp/b_c'
mkdir: created directory '/tmp/b_d'
[root@localhost ~]# tree -L 1 /tmp/
/tmp/
├── a_c
├── a_d
├── b_c
├── b_d



(2) Create / tmp/mylinux directory:

mylinux/

├── bin

├── boot

│   └── grub

├── dev

├── etc

│   ├── rc.d

│   │   └── init.d

│   └── sysconfig

│       └── network-scripts

├── lib

│   └── modules

├── lib64

├── proc

├── sbin

├── sys

├── tmp

├── usr

│   └── local

│       ├── bin

│       └── sbin

└── var

     ├── lock

     ├── log

     └── run

[root@localhost ~]# mkdir -pv /tmp/mylinux/{bin,boot/grub,dev,etc/{rc.d/init.d,sysconfig/network-
scripts},lib/modules,lib64,proc,sbin,sys,tmp,usr/local/{bin,sbin},var/{lock,log,run}}
mkdir: created directory '/tmp/mylinux'
mkdir: created directory '/tmp/mylinux/bin'
mkdir: created directory '/tmp/mylinux/boot'
mkdir: created directory '/tmp/mylinux/boot/grub'
mkdir: created directory '/tmp/mylinux/dev'
mkdir: created directory '/tmp/mylinux/etc'
mkdir: created directory '/tmp/mylinux/etc/rc.d'
mkdir: created directory '/tmp/mylinux/etc/rc.d/init.d'
mkdir: created directory '/tmp/mylinux/etc/sysconfig'
mkdir: created directory '/tmp/mylinux/etc/sysconfig/network-scripts'
mkdir: created directory '/tmp/mylinux/lib'
mkdir: created directory '/tmp/mylinux/lib/modules'
mkdir: created directory '/tmp/mylinux/lib64'
mkdir: created directory '/tmp/mylinux/proc'
mkdir: created directory '/tmp/mylinux/sbin'
mkdir: created directory '/tmp/mylinux/sys'
mkdir: created directory '/tmp/mylinux/tmp'
mkdir: created directory '/tmp/mylinux/usr'
mkdir: created directory '/tmp/mylinux/usr/local'
mkdir: created directory '/tmp/mylinux/usr/local/bin'
mkdir: created directory '/tmp/mylinux/usr/local/sbin'
mkdir: created directory '/tmp/mylinux/var'
mkdir: created directory '/tmp/mylinux/var/lock'
mkdir: created directory '/tmp/mylinux/var/log'
mkdir: created directory '/tmp/mylinux/var/run'
[root@localhost ~]# tree /tmp/mylinux/
/tmp/mylinux/
├── bin
├── boot
│   └── grub
├── dev
├── etc
│   ├── rc.d
│   │   └── init.d
│   └── sysconfig
│       └── network-scripts
├── lib
│   └── modules
├── lib64
├── proc
├── sbin
├── sys
├── tmp
├── usr
│   └── local
│       ├── bin
│       └── sbin
└── var
    ├── lock
    ├── log
    └── run
24 directories, 0 files


4. What are the metadata information of documents, what are their meanings and how to view them? How to modify the timestamp information of a file.

1. The data of files are divided into metadata and data. Metadata describes the characteristics of data itself, including file path, file size, data block information, file type, file permission, ownership group, security information, timestamp information (access time, modification time, change time) and so on.


2. Viewing metadata information of files

stat: Display file or file system status

[root@localhost ~]# echo 123 > /tmp/testfile
[root@localhost ~]# stat /tmp/testfile 
  File: '/tmp/testfile'
  Size: 4         Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768dInode: 102531229   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2017-05-08 01:10:05.142639974 +0800
Modify: 2017-05-08 01:10:05.142639974 +0800
Change: 2017-05-08 01:10:05.142639974 +0800
 Birth: -


3. Modification of file timestamp information

touch: modify file timestamps or create files


Common parameters:

- c: Do not create a specified file path when it does not exist

- a: Modify access time only

[root@localhost ~]# touch -a -t 05101223 /tmp/testfile 
[root@localhost ~]# stat /tmp/testfile 
  File: '/tmp/testfile'
  Size: 4         Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768dInode: 102531229   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2017-05-10 12:23:00.000000000 +0800
Modify: 2017-05-08 01:13:30.103620135 +0800
Change: 2017-05-08 01:14:37.774613584 +0800
 Birth: -


- m: Modify only modify time

[root@localhost ~]# touch -m -t 05111122 /tmp/testfile 
[root@localhost ~]# stat /tmp/testfile 
  File: '/tmp/testfile'
  Size: 4         Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768dInode: 102531229   Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: unconfined_u:object_r:user_tmp_t:s0
Access: 2017-05-10 12:23:00.000000000 +0800
Modify: 2017-05-11 11:22:00.000000000 +0800
Change: 2017-05-08 01:15:16.110609873 +0800
 Birth: -


- t STAMP [[CC]YY]MMDDhhmm[.ss]: Modifying the timestamp



5. How to define an alias for a command and how to refer to the execution result of another command in a command?

1. Define and revoke aliases

alias NAME='COMMAND'

Note: Only valid for the current shell process

unalias NAME

[root@localhost ~]# alias lsver='cat /etc/redhat-release'
[root@localhost ~]# lsver
Red Hat Enterprise Linux Server release 7.0 (Maipo)
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias lsver='cat /etc/redhat-release'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
[root@localhost ~]# unalias lsver 
[root@localhost ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'


2. Execution results of referencing another command

$(COMMAND) or `COMMAND'`

[root@localhost ~]# touch /tmp/file$(date +%F)
[root@localhost ~]# touch /tmp/file`date +%T`
[root@localhost ~]# ls /tmp/file*
/tmp/file01:31:33  /tmp/file2017-05


Posted by JoeZ on Tue, 02 Jul 2019 15:27:47 -0700