Linux: Basic Shell Commands
This is a list of most frequently used linux commands. These are essential commands. Most of them are used everyday by every linux user.
The code here are based on Ubuntu Linux, but 99% of them work in any unix, including Mac OS X.
most important commands are listed in the navigation panel.
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
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. Seeman 7 glob
. cmd1 | cmd2
- Pass the output of cmd1 to the input of cmd2
cat fileName | cmd
- Feed the content of fileName to the input of cmd
cmd > fileName
- Write the output to file
cmd >> fileName
- Append output to file
cat fileName1 fileName2 > newFileName
- Join contents of fileName1 fileName2 to newFileName
cmd1; cmd2; …
- Run several 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.) … `cmd` …
-
Generate the output of cmd and use it in your whole command. For example,
ls -l `which more`
cmd &
- Run the command cmd in background.
echo $?
- Show exit status of previous command.