Linux 命令速查

Linux一些常用的命令整理

环境

  • CentOS 7

笔记

User Operations

Profile

man : Show manual of the following command

format:

1
$ man {command}

e.g:

1
$ man date

whereis : show path of the following command

format:

1
$ man {command}

e.g.:

1
$ whereis passwd

id : show current user identifications

who : show current user information

whoami: show current user name

who am i: show current user name and last login

date: show current system date time

/etc/passwd/: saving user identifications

/etc/group/: saving group identifications

clear: clear current screen

echo: print information on the screen

pwd: show current directory (Print Working Directory)

/opt: save the APPs and products

/bin: save the basic linux operation files

/usr/lib: user library

/sbin: save commands running when system startup and shutdown

/etc: system file

/dev: containing hardware device files

/var: system log and spool file

.: current directory

..: parent directory

File/Director Operations

ls: show filenames and directory names

options:

1
2
3
4
5
6
-a: show hidden files (dot files)
-d: show directory status
-l: show file detailed information
-F: show files in types. Executable file with * and directory with /ls
-R: show all children files under this directory in Recursive
-i: show inode infomation

cd: change current directory

find: search something in the PC

format:

1
$ find {options} {conditions}

options:

1
-name: search file name

E.g.:

1
$ find . -name "*file" // search in current folder

mkdir: create a directory

format:

1
$ mkdir [-options] {path}

operations:

1
2
3
4
5
6
7
8
9
-p: create all directories in the path if not exists
-m {mod}: create and with {mod} (755, 644) permission.
```


#### rm: remove a file/directory
format:
```text
$ rm [-options] path

options:

1
2
-r: directory
-f: forced delete

cat: show file content

more: show file content in pages

tail: show the tail in some files

options:

1
2
-n: set how many lines will be showed (default 10)
-f: show the file (real time)

lrp: insert the file into printing queue

rm: remove

options:

1
2
3
-f: force remove
-r: remove all directory
-i: interaction

chmod: change file/dir pomission

format:

1
$ chmod [options] {mod} {filename}

options:

1
2
3
4
5
6
7
8
-R: for directory permission
4 (100): write
2 (010): read
1 (001): excute
u+x: user (owner) for excute
u=rw: user (owner) for read/write permissions
g=rw: group for read/write permissions
o=: other for nothing

umask: set default permission when file created (Mask code: 002->775)

format:

1
$ umask [-options] {mod}

options:

1
-S: show current status

e.g.:

1
$ umask 775

touch: update file timestamp

options:

1
2
3
4
-a: update access datetime
-m: update modified datetime
-t: update access and modified datetimes
-c: create if file not exists

su: change user ID/Group ID

format:

1
$ su {username}

alias: give alias for command

e.g.:

1
2
3
4
$ alias dir=ls
$ alias mstat=/home/abc/scv/
$ alias copy='cp -i'
$ alias (show all alias)

umalias: remove alias

e.g.:

1
$ unalias dir

history: how history commands

env: show environment

wc: count words in the file

options:

1
2
3
4
-l: lines count
-w: words count
-c: count Byte
-m: count charactors

grep: global search regular expression(RE) and print out the line

format:

1
$ grep [-options] {keyword} {target path}

options:

1
-e: multiple keywords: grep -e i -e love /root/funfile.txt

e.g.:

1
$ ps -ef | grep java : search all proceess about the Java

|: left as the stdout, right as the stdin; get information from left and process on right

cut: show file indented content

tr: change text

e.g.:

1
$ who | tr -s " " : remove all spaces

tee: output strout and print on screen

e.g.:

1
$ cat 1.txt | tee 3.txt

Management

Disk Management

dmesg: show hardware information

e.g.:

1
$ dmesg | preg usb

parted: Partition

options:

1
2
3
4
- unit GB: Change to GB display
- print: Print partition status
- mkpart: Make partition
- rm Number: Remove partition

pvcreate: instant the partition

mkfs.xfs: init file system

e.g.:

1
$ mkfs.xfs /dev/sda7

mount: mount partition to direction

format:

1
$ mount [options] {partition} {direction}

e.g.:

1
2
$ mkdir /mountdir1
$ mount /dev/sda7 /mountdir1

mount forever

1
2
3
4
5
6
7
# cat /etc/mtab
# cat /proc/mounts
# cat /etc/fstab
# mount -o remount /mountdir1
# cat /etc/fstab
# blkid /dev/sda7
confirm UUID

df: monitoring file system status

e.g.:

1
$ df -h

swap

create swap:

1
# mkswap /dev/sda8/

activate swap:

1
# swapon /dev/sda8/

User Management

/etc/passwd : user register file

e.g.:

1
# cat /etc/passwd

/etc/shadow : user password file

e.g.:

1
# cat /etc/shadow

/etc/group : group register file

e.g.:

1
# cat /etc/group

useradd : add a user

format:

1
# useradd [options] <username>

options:

1
2
3
4
5
6
7
8
-u : UID
-g : GID
-G : GID,GID
-d : /home/path
-m : make new home directory
-s : indicate login shell e.g. /bin/bash
-c : comment
-D : set/change default value

/etc/skel : this folder will be copied as home folder when user created

usermod : change user profile

format:

1
# usermod [options] <username>

options:

1
2
3
4
5
6
7
8
9
-u : UID
-g : GID
-G : GID,GID
-d : /home/path
-m : make new home directory
-s : indicate login shell
-l : new user name
-L : lock password
-U : unlock password

userdel : delete user

format:

1
# userdel [options] <username>

options:

1
-r : delete the home folder

Boot and Shutdown Management

/etc/default/grub : change bootloader configration

e.g.:

1
2
3
# vim /etc/default/grub
// activate setting
# grub2-mkconfig -o /boot/grub2/grub.cfg

systemd.unit=rescue.target : rescue mode

steps:

1
2
3
e
[quiet] systemd.unit=rescue.target
ctrl + x

systemctl : confirm/change target

e.g.:

1
2
3
4
5
6
7
8
9
10
11
12
13
# systemctl get-default //show current target
# systemctl set-default multi-user.target //startup in console
# systemctl isolate multi-user.target //change current target
# systemctl list-units --type=target // show all roaded targets
# systemctl list-dependencies graphical.target // show dependence of indicated target

# systemctl start httpd.service //start apache
# systemctl status httpd.service //show apache status
# systemctl enable/disable httpd.service // run/or not apache when system startup
# systemctl is-enable httpd.service // show is apache.service enable
# systemctl list-unit-files // show all auto running services

# systemctl status systemd-journald //show system status

/usr/lib/systemd/system : unit setting

shutdown

e.g.:

1
2
# shutdown -h //shutdown immediately
# shutdown -r //reboot

Package Management

rpm : package management

format:

1
# rpm [-options] {package name}

options:

1
2
3
4
5
6
-i : package name
-v : show detail information
-h : show processbar
--nodeps : do not check depandence
--force : forced install
--test : install to sandbox

e.g.:

1
2
3
4
5
6
# rpm -ivh {package name} //install package
# rpm -Fvh {package name} //upgrade : do not install if package has not installed
# rpm -Uvh {package name} //upgrade : newly install if package has not installed

# rpm -qa | grep bash //search package about bash
# rpm -e {package name} //remove package

Software Management

yum : package management

config:

1
2
3
# vim /etc/yum.conf //Yum basic config

# vim /etc/yum.repos.d/*.repo //repository config

format:

1
# yum [options] {package name}

options:

1
2
3
4
5
6
7
8
install : install
remove : remove
update : update a package
check-update
info : show package information
search : search a package
grouplist : show list of package group
groupinstall: install all packages in the group

e.g.:

1
# yum install system-config-kickstart

Log Management

journalctl : operating log database

format :

1
$ journalctl [-options] {condition}

options:

1
2
3
4
5
6
7
8
-b : boot logs
-k : kernal logs
-f : show logs on real time
-u {unit name} : show unit logs
--since={yyyy-MM-dd hh:mm::ss} : since datetime
--until={yyyy-MM-dd hh:mm::ss} : until datetime
_PID={process ID} : show logs of process
--no-pager : do not paging

Persistence:

1
2
3
# mkdir /var/log/journal
# vim /etc/systemd/journald.conf
# systemctl restart systemd-journald

/etc/rsyslog.conf : config rsyslog

logger: save a message

format:

1
# logger [options] {message}

options:

1
2
-p facility.level
-t tag

e.g.:

1
2
# logger -p daemon.err -t rsyslogd Test Test Test
# tail /var/log/messages