Linux: Basic Misc

By Xah Lee. Date: .

Date and Time

date
Show current date and time
date --rfc-3339=seconds
Show time stamp in this format: “yyyy-mm-dd hh:mm:ss-07:00” the last are time offset to UTC.

Run a Shell Script

source fname
Execute a file fname. source fname is equivalent to . fname

Environment Variable

echo $PATH
View value of a environment variable PATH.
env
Show all environment variables

Misc

uptime
Show how long the system's been running.
bash
Start a new bash. Ctrl+d to exit when done.
alias str=cmd_str;
Make str as shortcut for cmd_str. For example, alias l="ls -al --color"

〔see Show Opened Files, lsof

Generic Useful Bash Syntax

Wildcards for Filenames

cmd *.txt

A asterisk “*” means any character. *.txt means all files ending in “.txt”. Can be used for any command that take list of files or dir. See man 7 glob.

Pipe Output

cmd1 | cmd2
Pass the output of cmd1 to the input of cmd2

Pipe File Content

cat fileName | cmd

Feed the content of fileName to the input of cmd

Write to File

cmd > fileName

Write the output to file

Append to file

cmd >> fileName

Append output to file

Join Content

cat fileName1 fileName2 > newFileName

Join contents of fileName1 fileName2 to newFileName

Sequence Commands

cmd1; cmd2; …

Run several commands.

And Sequence Commands

cmd1 && cmd2

Run cmd1, if success, then run cmd2 (otherwise stop.) (the && is a logical “and” operator. Unix commands returns 0 if success, else a integer error code.)

Embed a Command

… `cmd` …

Generate the output of cmd and use it in your whole command. e.g. ls -l `which more`

run command in background

cmd &

Run the command cmd in background.

Print

echo "hi "

Exit Status

echo $?

Show exit status of previous command.