Linux has a hierarchy of directories that lists contents of files in a tree-like format, starting from the file system root (
Linux is case-sensitive, e.g.
MyFile are four unique files.
Linux does not require filename extensions such as
. extension is simply part of the filename. Sometimes data files are named with extensions (.hdf, .cdf, .tar) for human readability, though this is optional.
A file with
. at the beginning will be considered a hidden file.
You can use the
<TAB> key to autocomplete commands, paths, and environment variables. For example, you can type
cal on your terminal followed by
<TAB> to test this. If there is more than one option for the autocomplete to choose from, pressing
<TAB> twice will provide a list of all possible options based on what you have typed.
up-arrow will display the previous command you have typed and if you press the
down-arrow, it will refer to the following command.
history command will show all of the previous commands you have entered during the last session(s).
Exit a terminal, same as typing
Clears the screen, same as typing
Breaks/cancels an ongoing operation
Pauses (stops) an ongoing operation
Opens a new terminal
📝 Note: If you want to learn more shortcuts, please consult more documentation here.
pwd: Print Current Working Directory
The output of
pwd in this case, is the home directory of the user user, which is shown with the complete path starting from root(
ls: List the contents of the current directory
lsDocuments/ 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
cd: Change directory
In this case, we are entering the "Documents" directory.
If you want to go directly to your home directory (user), you can type
cd without any specification of which directory.
In the case of nested folders, you can jump one directory level upwards by typing
alias: In case of deeply nested folders (/path/to/project/com/java/lang/morefiles) that might take more than 4 directory levels upwards, you can 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, you may 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
whoami: Shows the user ID as a name
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
If you want to see all the users that are logged in to the computer, you can type
date: Display the date and time of the system
dateWed 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: Display a calendar of the current month
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.
To display the Eastern date of the current year, please type
cat: Creates a single or multiple files, views the contents of a file, concatenates files, and redirects output into the terminal or into files
cat /Users/user/myfile.txthello world
In this case, we want to display the content of
myfile.txt which is located inside the
If we are positioned inside the
user 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, please type
cat -n myfile.txt.
echo: Display a line of text or a string on standard output or into a file
echo "Hi CCLA"Hi CCLA
In this example, the string
Hi CCLA is shown because we send that message to the terminal.
To view the value assigned to a variable, add
$ before the variable name:
x=10; echo "The value of 'x' is: $x").
If you need a new line
\n, use the option
echo -e "Hello \n world").
touch: Create a new empty file
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 by typing
touch file1 file2.
If you want to create lots of files that share a common string, e.g.
test3.txt, and so on until 25, you can use
mkdir: make directory
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: Copy files and directories
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, you can type
cp main.c def.h /Users/user/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. The asterisk (
*) is a wild-card character.
mv: Move or rename the files or directories
mv file1 Myfile1
The file called
file1 was renamed as
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
mv -bv *.txt /Users/user/mybak.
rm: Delete files or directories
rm file1 Myfile1
The files called
Myfile1 will be removed.
For directories, the recursive option
-r is needed, e.g.
rm -r modelOutput.
man: Display the manual of the Linux commands
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
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
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.
> Redirecting output
Commands can send and receive streams of data to and from files and devices.
echo "Test report title" > /tmp/test.txt
"Test report title" will be written to the file
test.txt located inside the
It is also possible to send all the content of
/Users/user/hello, by using
/tmp/hi.txt > /Users/user/hello.
Mail -s "Subject" [email protected] < Filename will email the content of
>> Appending (postpending) redirected output
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