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.
pwd stands for print working directory and takes two options.
-L displays your logical path
-P is the default.P displays your physical location.
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]#
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]#
- [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 /]#
- [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 ~]$
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 ~]$
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