Shell Tips for Beginners (tested for MacOS)
1) Environment variable setting in the bash shell|
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:
2) Aliases |
You can define aliases to shorten long commands. Example:
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:
Sed is the abreviation of stream editor. It can be used to replace strings in files. Example:
Pipes are used to feed the output of one program as input into the next program. For example the command:
will parse the output of the command ls to the command wc, which will count the number of lines, words and bytes.
The output of commands is normally written to standard output (console). It can be redirected easily to a file. Example:
This command will write the output of the command ps x to a file named processes.
This will append the output of ps x to a file named processes.
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:
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:
This command will list all lines in the file textfile that contain string.
This command will list all lines of textfile that do not contain string.
The command du displays the disk usage. Example:
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.
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.
List all jobs with their job ids.
Run a process with reduced priority (be nice to other users).
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)
List cluster jobs. Option:
list cluster jobs belonging to user karo
Delete cluster job with given job id.
|Backup and compress|
Save files and directories on remote machine. Example:
Compress files or directories. Example:
Compress files and directories. To compress, type:
To uncompress, type:
|Files and Folders|
Create a new file with name filename.
Change file permission. Example:
Give owner rights to read, write and execute and give group and others right to read and execute.
Change the file/folder owner. Example:
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:
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.