Essential Commands

Overview

Common Shortcuts

macOS Windows Action
CMD+D CTRL+D Exit a terminal, same as typing exit
CMD+L CTRL+L Clears the screen, same as typing clear
CMD+C CTRL+C Breaks/cancels an ongoing operation
CMD+Z CTRL+Z Pauses (stops) an ongoing operation
CMD+N CTRL+N Opens a new terminal

📝 Note: To learn more shortcuts, see here.

Basic Commands

  pwd
  /Users/x0y

The output of pwd in this case, is the home directory of the user x0y, which is shown with the complete path starting from root(/)

  ls
  Documents/   Pictures/   Desktop/   Downloads/   document.txt

The output is a list of four directories (followed by a /) and one file. To see information about the contents in a list, type ls -l.

  cd Documents

In this case, we are entering the "Documents" directory.

If you want to go directly to your home directory (x0y), you can type cd without any arguments.

In the case of nested folders, you can jump one directory level up by typing cd ..

alias: In case of deeply nested folders (/path/to/project/com/java/lang/morefiles) that might take more than 4 directory levels up, you may create an alias, for example, alias ..2="cd ../.." or alias ..3="cd ../../.." or alias ..4="cd ../../../..". If you wish to make these aliases a permanent feature of your Bash environment, add the commands to the end of the .bashrc file. Edit the .bashrc file by opening it in your favorite text editor (it is located in your home directory). For example, type vim .bashrc.

  whoami
  x0y

This shows the username that is logged in to the current session of the machine.

If you need additional information about the user, such as, to which groups they are a member, type id.

If you want to see all the users that are logged in to the computer, you can type w.

  date
  Wed Apr  4 09:06:30 EDT 2018

The date is shown in a complex format. Use date +%F format if you want to do a backup of a file including the date in the filename.

If you want to calculate, in seconds, the duration of a program, you can use the date +%s command.

  cal

This command displays the calendar of the current month of the year in which the command is executed.

In case you need the whole year calendar of 2018, you may type cal 2018 or set any other year you want to check.

If you want to display any particular month of the year, you can type, for example, cal March 2018.

  cat /Users/x0y/myfile.txt
  hello world

In this case, we want to display the content of myfile.txt which is located inside the x0y directory.

If we are inside the x0y directory, all that is needed is cat myfile.txt to see its contents, which is "hello world".

You can view the content of two files at the same time with the cat file1.txt file2.txt.

In case you need the lines of a text numbered, type cat -n myfile.txt.

  echo "Hi ORNL"
  Hi ORNL

In this example, the string Hi ORNL is shown because we send that message to the terminal.

To view the value assigned to a variable, add $ before the variable name:

(e.g. x=10; echo "The value of 'x' is: $x").

If you need a new line \n, use the option -e (e.g. echo -e "Hello \n world").

  touch myNEWfile

In this case, myNEWfile was created inside the directory in which you are positioned.

You can create more than one file at the same time with touch file1 file2.

If you want to create lots of files that share a common string, e.g. test1.txt, test2.txt, test3.txt, and so on until 25, you can use touch test{1..25}.txt.

  mkdir myNEWdir

In this case, a new directory called myNEWdir is created in the current path.

If you want to set the permission of the directory while you are creating the directory, you can do so by typing mkdir -m a=rwx myNEWdir. Here, the letters r, w, and x stand for read, write, and execute, respectively. For more information on file and directory permissions, see here.

If you want to create multiple directories at once, run mkdir test1 test2 test3.

If you want to create several subdirectories at one time, type mkdir -p /home/test/test1/test2/test3/test4.

  cp /path/to/file_src /path/to/file_dest

In this case the contents of file_src (source) will be copied to file_dest (destination) and both files will be present in both paths.

If you need to copy more than one file into a directory, type cp main.c def.h /Users/x0y/mydir/.

To copy all the files you have (in your current path) with the extension .c to a directory called bak, you can type cp *.c bak.

  mv file1 Myfile1

The file called file1 was renamed as Myfile1.

If you want to move all of your C files to a subdirectory called bak, you can run mv *.c bak.

If you want to create a backup when copying your .txt files into the mybak directory (to not overwrite existing files within mybak) use: mv -bv *.txt /Users/x0y/mybak.

  rm file1 Myfile1

The files called file1 and Myfile1 will be removed.

For directories, the recursive option -r is needed, e.g. rm -r modelOutput.

  man sudo

A manual related to the sudo command is displayed explaining how the sudo command will grant you privileges to execute commands as the superuser does.

For further information you can do man man to read more about man. To exit a manual page, type q.

Pipe and Redirection

A pipe is a form of redirection that sends the output of a program (written before the pipe) to another one (written after the pipe) for further processing.

To make a pipe, put a vertical bar (|) on the command line between two commands.

  man pipe | cat > /tmp/myMAN.txt

The command man pipe will display the content of all the information about pipe, then that content will be processed by cat (taken as its input) and be redirected to the file /tmp/myMAN.txt. So, the output, the content of myMAN.txt will display the manual information about pipe.

Commands can send and receive streams of data to and from files.

  echo "Test report title" > /tmp/test.txt

"Test report title" will be written to the file test.txt located inside the /tmp directory.

It is also possible to send all the content of /tmp/hi.txt to /Users/x0y/hello, by using /tmp/hi.txt > /Users/x0y/hello.

Mail -s "Subject" to-address@example.com < Filename will email the content of Filename.

This command will append (postpend) information to where it is designated.

  echo "This report was done at $HOSTNAME at $(date+%F)">>/tmp/report.txt

The output of the first part of the command (before the >>) will be added at the end of the file /tmp/report.txt.