Shell Tips for Beginners (tested for MacOS)

1) Environment variable setting in the bash shell
1a) temporarily:
export VARNAME=value
1b) store variable setting in a shell configuration file
for bash, the shell configuration is stored in the home directory in a file named .profile. Example:
If the CLASSPATH variable should be set for every new bash shell opened, add the following lines to your .profile file:
# Java settings
export JARPATH=/home/user/path
export CLASSPATH=$JARPATH/lib/X.jar:$JARPATH/lib/Y.jar
To display the value of a variable, use echo $VARNAME.
2) Aliases
You can define aliases to shorten long commands. Example:
alias ll="ls -lisa"
This alias defines an abbreviation for the ls -lisa command, which lists all information available for a file or directory.
3) Awk
Awk is a language designed for text processing. It can be used for extracting strings from a text, to match and replace patterns in texts and so on. Take for example the command:
awk '{print $1 "\t" $4}' exampletable
This command will print the first and fourth column of the table in the file with the name example file. It will separate the printed columns by tab.
4) Sed
Sed is the abreviation of stream editor. It can be used to replace strings in files. Example:
sed 's/old/new/g' file1>file2
This command will replace all occurrences of the string old with the string new in the file file1 and will store the result in file file2. Note that if any of the strings contains special characters (?\.[]^$/), they can be escaped with backslash (\).
5) Pipes
Pipes are used to feed the output of one program as input into the next program. For example the command:
ls | wc
will parse the output of the command ls to the command wc, which will count the number of lines, words and bytes.
6) Redirection
The output of commands is normally written to standard output (console). It can be redirected easily to a file. Example:
ps x > processes
This command will write the output of the command ps x to a file named processes.
ps x >> processes
This will append the output of ps x to a file named processes.
7) Pico
Pico is a user-friendly command-line word editor and a (less powerful) alternative to vi for all those that are too lazy to learn vi. It runs everywhere and is sufficient for most configuration tasks. It may come in a variety called jpico. Launch it with
8) Navigation on command-line
Ctrl+A: beginning of current line
Ctrl+E: end of current line
Ctrl+W: delete word in current line

Useful Shell Commands
Find is used to find folders/files or text in files. Example:
find . -name *string* -type f
This command tells find to search the current directory and its subdirectories and then list all files that contain string in its name.
Grep is used for text extraction. Example:
grep string textfile
This command will list all lines in the file textfile that contain string.
grep -v string textfile
This command will list all lines of textfile that do not contain string.
The command du displays the disk usage. Example:
du -sk -h .
This command will display the disk usage of the current directory in human readable form.
This command will display a table summarizing free disk space.
The command uname -a displays information about the machine (processor type, machine hardware name, operating system).
List users that are currently logged in.
Job handling
ps x
Lists all processes of the user together with their status. S means suspended (interruptible sleep), R running, l multi-threaded and N low priority. ps ax lists all processes of all users.
jobs -l
List all jobs with their job ids.
Run a process with reduced priority (be nice to other users).
kill jobid
Kill job with given id. Kill -9 jobid is the strongest command to kill a job.
Submit job to PBS cluster. Options:
-q: queue and host (example: medium@arthur)
-N: job name
-j oe -o: where to write standard output and standard error (name of logfile)
qsub -q short@arthur -N karo1 -joe -V -o logfile.log NameOfScriptToSubmit
List cluster jobs. Option:
qstat -u karo
list cluster jobs belonging to user karo
qdel jobid
Delete cluster job with given job id.
Backup and compress
Save files and directories on remote machine. Example:
rsync -avz /home/karo/Documents karo@backup:/home/karo/Documents
Compress files or directories. Example:
zip -r zipdir directory
Compress files and directories. To compress, type:
tar -czvf archive.tgz directory
To uncompress, type:
tar -xzvf archive.tgz directory
Files and Folders
touch filename
Create a new file with name filename.
Change file permission. Example:
chmod 755 test
Give owner rights to read, write and execute and give group and others right to read and execute.
Change the file/folder owner. Example:
chown tomcat:tomcat .
Make user tomcat and group tomcat the owner of the current directory. Warning: This command needs superuser rights.
Copy a file to a remote machine. Example:
scp file.txt user@machine:path/file.txt
rm -r
Remove recursively. Use carefully to delete directories. If you are not sure, better use it with option -i (command will ask for confirmation before deleting a file in the given directory).
ssh -Y user@machine:path
Use ssh in X window system, such that remote X window can be displayed in local machine.
Execute shell script (Warning: the shell script should be executable). Example:
Perform command with superuser rights.