1.1 Decoding the Prompt
root is all-powerful and can do anything on a typical Unix or Linux system.
~ mean you are in your home directory.
$ indicates this is a regular user, not root.
1.2 Showing Where You Are
pwd stands for print working directory and takes two options.
-L displays your logical path
-P is the default.P displays your physical location.
1.3 Finding and Running Commands
type,which,apropos,locate,slocate,find and ls commands.
[root@MaxwellDBA tmp]# type which
which is a function
which ()
{
( alias;
eval ${which_declare} ) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
}
[root@MaxwellDBA tmp]# type ls
ls is aliased to `ls --color=auto'
[root@MaxwellDBA tmp]# type -a ls
ls is aliased to `ls --color=auto'
ls is /usr/bin/ls
[root@MaxwellDBA tmp]# which which
which ()
{
( alias;
eval ${which_declare} ) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot "$@"
}
[root@MaxwellDBA tmp]#
1.4 Getting Information About Files
Use the ls,stat,file,or find commands.
[root@MaxwellDBA tmp]# touch /tmp/sample_file
[root@MaxwellDBA tmp]# ls /tmp/sample_file
/tmp/sample_file
[root@MaxwellDBA tmp]# ls -l /tmp/sample_file
-rw-r--r-- 1 root root 0 Jul 21 13:30 /tmp/sample_file
[root@MaxwellDBA tmp]# stat /tmp/sample_file
File: /tmp/sample_file
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fd01h/64769d Inode: 8579 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2022-07-21 13:30:59.014117361 +0800
Modify: 2022-07-21 13:30:59.014117361 +0800
Change: 2022-07-21 13:30:59.014117361 +0800
Birth: 2022-07-21 13:30:59.014117361 +0800
[root@MaxwellDBA tmp]# file /tmp/sample_file
/tmp/sample_file: empty
[root@MaxwellDBA tmp]# file -b /tmp/sample_file
empty
[root@MaxwellDBA tmp]# echo '#!/bin/bash -' > /tmp/sample_file
[root@MaxwellDBA tmp]#
[root@MaxwellDBA tmp]# file /tmp/sample_file
/tmp/sample_file: Bourne-Again shell script, ASCII text executable
[root@MaxwellDBA tmp]# file -b /tmp/sample_file
Bourne-Again shell script, ASCII text executable
[root@MaxwellDBA tmp]#
1.5 Showing All Hidden(dot) Files in the Current Directory
[root@MaxwellDBA /]# ls -a
. .. .autorelabel bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@MaxwellDBA /]# ls -d .[!.]*
.autorelabel
[root@MaxwellDBA /]# ls -d .??*
.autorelabel
[root@MaxwellDBA /]# ls -d .[!.]* .??* | sort -u
.autorelabel
[root@MaxwellDBA /]#
1.6 Using Shell Quoting
[maxwell@MaxwellDBA ~]$ echo 'A coffee is $5 for' "$USER" '?!'
A coffee is $5 for maxwell ?!
[maxwell@MaxwellDBA ~]$ echo "A coffee is \$5 for $USER?\!"
A coffee is $5 for maxwell?\!
[maxwell@MaxwellDBA ~]$ echo "A coffee is \$5 for $USER?! "
A coffee is $5 for maxwell?!
[maxwell@MaxwellDBA ~]$ WRONG
-bash: WRONG: command not found
[maxwell@MaxwellDBA ~]$ # wRONG
[maxwell@MaxwellDBA ~]$ echo "$USER won't pay $5 for coffee."
maxwell won't pay for coffee.
[maxwell@MaxwellDBA ~]$ # work
[maxwell@MaxwellDBA ~]$ echo "USER won't pay \$5 for coffee."
USER won't pay $5 for coffee.
[maxwell@MaxwellDBA ~]$ #Also works
[maxwell@MaxwellDBA ~]$ echo 'I won'\''t pay $5 for coffee.'
I won't pay $5 for coffee.
[maxwell@MaxwellDBA ~]$
1.7 Using or Replacing Built-ins and External Commands
Use the type and which commands to see if a given command exists and whether it is built-in or external.
[maxwell@MaxwellDBA ~]$ type cd
cd is a shell builtin
[maxwell@MaxwellDBA ~]$ type awk
awk is /usr/bin/awk
[maxwell@MaxwellDBA ~]$ which cd
/usr/bin/cd
[maxwell@MaxwellDBA ~]$ which awk
/usr/bin/awk
[maxwell@MaxwellDBA ~]$
1.8 Determining If You Are Running Interactively
Use the following case statement:
$- is a string listing of all the current shell option flags,
#!/usr/bin/env bash
# cookbook filename: interactive
case "$-" in
*i*) # Code for interactive shell here
;;
*) # Code for non-interactive shell here
;;
esac
本文介绍了Linux系统中的一些基本操作,包括理解root权限、查看当前目录、寻找和运行命令、获取文件信息以及处理隐藏文件。同时,讲解了Shell引用的使用以及如何区分内置和外部命令。此外,还涉及了交互式shell的判断方法。
351

被折叠的 条评论
为什么被折叠?



