This section covers some of the basic commands for using the Linux command-line that will be needed when connecting to the departmental Linux server. These commands will work with any Linux environment, not just our department server.
This section covers only the most basic commands for navigating through the file system (directories, subdirectories, and finding files), and managing your processes.
All built-in Linux commands have a manual page (a full reference for the command). To read the manual page for a command, issue the man command, followed by the name of the command you’d like to know more about. For example, if you wanted to see all of the options available to the ls command, you would issue:
You can scroll the man (manual) page with the up and down arrow keys, and press q to leave the man page. Note that the man page is a reference, and not really a tutorial.
File System Navigation
Linux uses a hierarchical file system just like Windows and MacOS. In fact, MacOS is based on Linux, so the commands outlined here also apply to the MacOS terminal. A hierarchical file system means that hard disk resources are represented as files, those files are held within directories (called folders in lesser operating systems), and those files and directories can be contained within other directories.
Often, you are concerned with which directory is your current working directory. That is the directory that is most readily accessible. To see your current working directory, type the command:
Once you press enter/return, the full file path of your current working directory is displayed. If you have just logged in to a system, you are usually in the home directory for your user name. Each user will have a different home directory that only that user (and the system administrator) can access. The full path shows all of the directories that it takes to get to your current working directory. Each level in the hierarchy is separated by the forward slash character. This means that you cannot have the forward slash in any of your file or directory names. An example file hierarchy is shown in the figure below.
Suppose you are the user named “bubba” in this example. If your current working directory is your home directory, and you issue the pwd command, then you would see:
You can display the files in your current directory with the ls command (which stands for “list”). Again, if you were the user named bubba, then when you issue ls, you will see one file named data.txt displayed. You can change your current working directory with the cd command. If we continue the bubba example, suppose you wanted to change to the bin directory inside of usr – there are several options. You can give the full path to the cd command:
But sometimes the full path is inconvenient. Linux allows you to reference the parent directory with .. (two periods). The parent directory of bubba’s home directory is /home/. So, if you were back in bubba’s home directory, you could navigate to the bin directory in usr with:
These parent references apply regardless of what current working directory you are in. You can also reference the current directory with a single period (.) character. If your current working directory is bubba’s home directory, and you wanted to open bubba’s data.txt file with the nano text editor, you could use:
Finally, there are a handful of useful shortcuts for the cd command. You can always return to your home directory with cd if you issue cd without providing any other directory name after the command. You can also refer to your home directory with the tilder (~) character. So, no matter what your current working directory is, you can go back to your home directory with:
The ls command is used to view (or list) the files in the current working directly. This command, ls, also supports several command-line options. The basic ls will show both files and directories, but without any other information. You can get a lot of useful information if you use the -l option:
You can see the permissions on a file (whether it is readable, writable, or executable), you can see the date/time that it was last modified, the file size, and other properties. Linux allows for hidden files (files that don’t normally show up when you issue the ls command). Any file with a file name that starts with a period (.) will automatically be hidden. To view these hidden files, use ls with the -a option:
Among the visible files, you will also see hidden files, including the reference to the current directory and parent directory (. and .., respectively). You can also combine both command-line options:
If you want to view all of the files and directories at the current working directory and below, you can use the tree command. Simply issue tree with no command-line options, and it will give you a nice visualization of files. Finally, you may know the name of a file that you want, but not know in which directory to find that file. You can use the find command to help you. find requires that you provide a starting directory and it will recursively descend into every directory and show you the paths of all files and directories it encounters along the way. Suppose that you had the file system shown below and your current working directory is /home/.
If you issue the following:
Then you will be shown all of the files and directories for bubba and cowboyNeal. If you just want find to show you the directory for the one file that you’re looking for, then you must give find the -name option. Suppose you knew you were looking for data.txt, but weren’t sure in which directory it appears, then you could issue find with:
find . -name data.txt
and find would print
In Linux, when you run a program, you usually then refer to it as a “process”. You can run a program (start a process) by simply issuing its name from the command-line. As long as Linux is aware of the path to that program, it will start that process. If Linux is unaware of what directory holds that program executable, then it will respond with a “command not found” error message. To correct the situation, you can provide either a relative path (using the .. or . directory references, as needed) or the full path starting from the root.
Bash Profile and Paths
You can tell Linux where to find programs by editing the .bash_profile text file in your home directory (notice the dot before bash profile that makes it a hidden file, and the dot must be there as it is part of the file name). Edit that file and find the line that starts with PATH=. You can add your own paths to that line, separating each path with a colon. Once you save the .bash_profile, you need to log out of your ssh session (or, if you are not using ssh, simply close and reopen your terminal window) and then log back in for the change to take effect. If you don’t have a .bash_profile file in your home directory (you open the file, but it’s empty), you are free to create one. Suppose you wanted Linux to find programs in a bin directory in your home directory, then the .bash_profile should have:
PATH=$PATH:$HOME/bin export PATH
The $PATH is a variable that holds the previous paths that Linux knows about, and $HOME always refers to your home directory.
Killing a Rogue Process
Occasionally, you have an infinite loop in your program; causing it to never stop running. To kill your program, and return to the prompt after starting that errant program, use Ctrl-c.
You can see all of the processes that are currently running on the system with the top command. It will show the programs that are consuming the most CPU resources towards the top of the screen, and will periodically update as processes are started and finished. You can quit top by pressing q.
Linux provides a tool for comparing two files for their differences. The diff utility compares two files, and prints only the lines that differ between the two. Consider the example files given below.
These two example files have lists of fruit. Some fruit are the same in both files, some exist in one file but not the other. We can use diff to show us how the files differ. Given these two files, you could issue the following command:
diff file1.txt file2.txt
After executing, diff will display the following output:
3d2 < peach 6c5 < strawberry --- > raspberry
The first line, 3d2, means that line 3 from the first file has a line has been deleted compared to the second file, and the deletion occurred after line 2 of the second file. The next line < peach shows the line that was deleted. The left-angle bracket indicates that the line exists in the first file, but not in the second.
The 6c5 line means that line 6 from the first file has been changed, compared to line 5 of the second file. The next few lines show what the lines were in each of the files. The < strawberry means that the first file has the line “strawberry”, and the > raspberry line indicates that the line “raspberry” appears in the second file.
Notice in this example that diff is smart enough to recognize that watermelon and pear appear in both files, just on different lines. Since they are the same, it does not report them as a difference even though they appear on different lines.
Since diff shows differences, if the files are the same, then diff does not print anything.
The diff command is most commonly used with text files. But it can be used with binary files. But because there are no line-breaks in binary files, diff will only report whether or not the files differ at all. In that case, diff will output the text: Binary files XXX and YYY differ; where XXX and YYY will be the file names that you provided.
C Program Compilation
This document assumes that you are using the GCC compiler to compile your C programs and that the command to compile a plain C program is simply gcc. Of course, you must tell GCC the names of the c files that you wish to compile. You can either list them after the gcc command or you can wildcard the file names.
For example, suppose you split your program into three source code files that are contained in the same folder and that there are no other c files in that same folder. You have chosen to name your files main.c, functions.c, and helper.c. You can compile with either of the following:
gcc main.c functions.c helper.c
By default, this will create an executable named a.out, which you can run by typing the following command.
If you’d like to give your executable a more meaningful name, then you can have GCC automatically rename by using its -o option. For example, if you wanted to call your executable project then you could compile with:
gcc *.c -o project
And then run with:
It is customary in Linux to have executable names that do not have a file name extension.