# Linux Guide


This page is under construction

This guide aims to provide common and useful Linux commands, popular Linux operating systems that you make encounter, as well as ways to accomplish tasks that you may encounter frequently. Please note that Linux is case sensitive, so any commands shown here should have the same capitalization.

# Linux Commands

# About

For the sake of brevity and making things easier to find, we will not be going over every single command available in Linux here, but rather, a list of some of the more common and useful commands. For a good list of all Linux commands, you can follow this link (opens new window). Remember that the man command will also bring up a manual for using Linux. The commands are split into sections based on their uses, and within each section are ordered alphabetically.

The command examples are formatted as follows:

  • command_name Description
Examples Description of example
command_name <parameter>

# Bash

Bash (opens new window) is a shell for navigating an OS without the need for GUI. Bash is a part of the GNU Project (opens new window), aiming to provide "free" software to aid in computing. Bash is included in most UNIX/Linux distributions, as well as OSX (Mac). For more information, please see the embedded links provided above

# General Tips

  • To save on typing, you can hit the TAB key on your keyboard and Linux will attempt to auto-complete your command. This works with directory names and commands that you have used in the past. Press multiple tabs to scroll through suggestions.
  • To re-use previous commands, hit the Up Arrow Key to scroll through your most recently typed commands. You can press the Down Arrow Key to go back down.
  • ".." refers to the directory above your current one. So if you had two folders, inside your home directory, First and Second, and you wanted to access something inside second while working in First, you could do something like: command ../Second/file_name For more examples, see cd.
  • To stop execution of a program, you can use CTRL+Z to kill the current process.
  • Remember that Bash, like many programming languages, is case sensitive. If a file contains a specific case, make sure that the case you are typing in matches.

# Files and Directories

  • cat is used to display files, as well as concatenate two files together. Press the enter key to scroll down when viewing the file. For an extensive guide on cat, consider this link (opens new window)
cat sample.txt          // displays the contents of the file sample.txt
cat hello.cpp world.cpp // displays the contents of the file hello.cpp, followed by the contents of the file world.cpp
  • cat [file1] > [file2] Writes the contents of file1 to the name of file2. This will overwrite file2 if it already exists. If you leave file 1 blank, you can use this command to create a new file.
cat input.txt > output.txt  // Moves the contents of input.txt to a file name "output.txt".
cat > newfile.txt           // Creates a new file called "newfile.txt". Type in the text you want to be in the file, and use CTRL+D to save.
  • cat file >> file2 Concatenates file1 and file2. file1 will be added to the end of file2
Hello World
This is file 2 

cat File1.txt >> File2.txt Appends "File1.txt" to the end of "File2.txt". 

File2.txt afterwards:
This is file 2
Hello World 

File1.txt remains unchanged.
  • cat -n [filename] Displays file with line numbers next to each line
cat -n hmm.py
  • cd [directory_name] Changes directory to the specified directory. Only works if the given directory is inside the current one. You can change to a directory multiple layers deep if you specify all the directories that lead there.
cd ..       // Changes the directory to the one "above" the current one. Basically, goes backwards one level in the directory.
cd          // Documents/CS135 Changes directory to the folder "CS135", which is located inside the Documents folder. 
cd -        // Returns to the last directory you were in, regardless of what level it was on.
cd ../../'  // Moves up 2 levels in the directory structure. To move up 3 levels, use ../../.., 4 ../../../.., etc.
cd ~        // Moves to the home directory, regardless of where you currently are.
  • chmod [number] [file] Changes the "mode" of the file. Basically, changes the permissions for the file. The first number corresponds to the user, the second to the user's group, and the third to others. Numbers should be between 0 and 7, inclusive, and are octal. The first bit is read, the second is write, and the third is execute. For a more detailed explanation, consider this guide (opens new window).
chmod 755 generator.py      // Gives the file "generator.py" read, write, and execution privileges for the current user, and everyone else write and execution privileges. If a file is not executing, it can be a good idea to try chmod 755 <file_name>.
chmod 700 a.out             // Gives the current user read, write, and execution privileges on the file "a.out", and everyone else no privileges. 
chmod +x file               // The +x shortcut gives the file execution privileges. Again, this is a good thing to try if a file you want to execute is not working. 


Please make sure you are confident that you trust a file before giving it execution privileges, for any of these cases.

  • **chown [user] [file_name] Changes ownership of the file to the user given.
chown root hello_world.cpp    // Gives the root ownership of the file "hello_world.cpp"
  • chpasswd Used to change many passwords at once. To change only your password, consider passwd.

  • cmp [file1] [file2] Compares two files. Can be used to check for differences between 2 files. Compares byte by byte (Compares characters). To compare line by line, use diff.

cmp output.txt input.txt    // Tells the differences, byte by byte, between "output.txt" and "input.txt".
  • cp [source] [destination] Makes a copy of the source and puts it in destination. If you'd like to move the file without copying it, try mv. The source can be either a file or a directory.
cp important.txt Documents/Secret           // Makes a copy of "important.txt" and moves it into the directory Documents/Secret.
cp file.txt backup.txt                      // Makes a copy of "file.txt" called "backup.txt" in the current directory.
cp homework1.cpp CS135/homework_copy.cpp    // Makes a copy of "homework1.cpp" and moves it into the CS135 directory, renaming the copy to "homework_copy.cpp".
  • diff [file1] [file2] Compares two files, looking for differences. Similar to cmp, but compares the files line by line, rather than byte by byte.
diff original.txt new.txt   // Finds the difference on a line per line basis between "original.txt" and "new.txt".
  • diff3 [file1] [file2] [file3] Compare 3 files line by line. Works like diff, but with 3 files, instead of 2.

  • dir Lists the contents of the current directory, similar to ls.

  • dircolors Allows you to set the system colors when using ls command.

  • echo "Echoes" whatever follows the command. Essentially, prints the command to whatever the default output is. This is usually the screen/terminal. Echoing could mean printing text, files, or more. This command has many uses, and is commonly used when writing bash scripts. For a more comprehensive guide, check out this link (opens new window).

echo "Hello, everyone"          // Prints the text "Hello, everyone" on the next line of the terminal.
echo "Welcome!\nLet's go!"      // Prints "Welcome" on the next line of the terminal, followed by "Let's go!" on the line after that.
echo *                          // Prints all of the files and directories in the current directory. Not formatted as nicely as ls, though.
echo *.png                      // Prints all files in the current directory with a ".png" file extension.
  • emacs A text editor utility that works within the terminal itself. Useful if you want to quickly edit a file without using the GUI.

  • file [file_name] Tells the file type of a file.

file calculator.cpp' This will tell you that calculator.cpp is a C++ source file and that it contains ASCII text.
  • ln [file] [link_destination] Creates a symbolic link for the file at the given destination.

  • locate Used to find the directory where a file exists. Will find multiple files if they contain the string used in the search.

    • locate [search_string] -n [number] Displays the first [number] results that match the given search string.
    • locate -c [search_string] Counts the number of files that match the search string.
    • locate -i [search_string] Ignores case while searching.
locate my_program.cpp   // Looks across the user's directories, returning where to find "my_program.cpp", if it exists.
locate "*.png"          // Looks for all .png files across 
locate "*.java" -n 15   // Locates the first 15 files with a .java extension.
  • ls Displays all the files and directories in a directory.
    • ls -l Shows all the files as normal, alongside permissions, owner, and other metadata.
    • ls -a Lists all the files, including hidden ones. Hidden files begin with a ".".
    • ls -R Recursively display contents of all directories contained within the target directory
ls              // Displays all the files in the current directory.
ls ..           // Lists all files in the directory above the current one.
ls CS135        // Shows all files in the directory "CS135".
ls -a ../Secret // Shows all files, including hidden ones, in the directory "Secret", which is at the same depth as the current directory.
  • mkdir [name] Makes a directory with the given name in the current directory.
  • more Similar to cat in function. Displays the contents of a file or multiple files.
more essay.txt                          // Shows the contents of "essay.txt".
more first_program.cpp hello_world.java // Shows the contents of "first_program.cpp", followed by the contents of "hello_world.java"
  • mv [source] [destination] Moves the file or directory from source to the location given by destination.
mv my_program.lsp MyDocuments               // Moves the file "my_program.lsp" into the directory "MyDocuments". This will preserve the name "my_program.lsp".
mv example.txt MyExamples/new_example.txt   // Moves "example.txt" into the directory "MyExamples", and renames the file to "new_example.txt".
mv test_program.cpp final_program.cpp       // Renames the file "test_program.cpp" to "final_program.cpp".
<code>mv picture.jpg picture.png            // Changes the file extension of the file "picture" from a jpg to a png. Please consider that this does not actually change the file format, just the extension.
  • pwd Present the current working directory. Displays the name of the directory you are currently in.

  • rcp Remote copy. Used to copy files from remote servers. This command is not installed on UNLV Servers, so please consider using an FTP to transfer files instead, such as FileZilla (opens new window) or WinSCP (opens new window).

  • rev [file] Shows the contents of a file, with each line printed in reverse order.

  • rm [file] Removes the specified file, permanently deleting it.

rm assignment1.cpp  // Deletes the file, "assignment1.cpp".
rm *.jpg            // Deletes all files with a .jpg extension.
rm *                // Deletes all files. Do not do this.
  • rmdir Like rm, but for directories instead of files. Only removes empty directories.

  • sdiff [file1] [file2] Shows differences between two files side to side. This is similar to diff.

  • tac Like cat, but prints things backwards instead.

  • tail [file] Displays the last 10 lines of a file.

  • tar Handles .tar files. Depending on the option, can extract or compress files. These are similar in function to .zip or .rar files.

    • tar -cf [output_tar] [file1] ... [fileN] Compresses files 1 - N into the output_tar file.
    • tar -tvf [tar_file] View the contents of the given tar file. Shows the files inside the .tar without actually extracting it.
    • tar -xf [tar_file] Extracts the given tar file in the current directory.
tar -cf assignment1.tar main.cpp animal.h animal.cpp    // Compresses the files: "main.cpp", "animal.h", and "animal.cpp", into a single .tar file named  "assignment1.tar.
tar -cf Gallery.tar picture.png image.jpg               // Compresses "picture.png" and "image.jpg" into a single .tar file called "Gallery.tar".
  • unrar [rar_file] Unzips the given rar file into the current directory.
  • zip [zip_file] [file1] ... [fileN] Zips the files 1 - N into a single zip file. Similar in function to the tar -cf, but for zip files. Use unzip to unzip already zipped files.

# SSH and Network

  • dig [domain_name] "Domain Information Groper". Finds information on the given domain name, such as IP address.
dig bobby.cs.unlv.edu   // Prints information about the UNLV server, Bobby. Will display its IP, as well as information on the packet that it sent to the server.
  • dnsdomainname Shows the domain name of the server you are on.
dnsdomainname   // On the UNLV servers, such as Bobby or Sally, this will show "CS.UNLV.EDU".
  • ping [location] Send a small packet to the location to ping it. Returns the response time in milliseconds, alongside some other data.
ping bobby.cs.unlv.edu  // Pings the Bobby server with a small packet.
  • rusers Provides a list of all users logged into the server.

  • sftp user@server Connects with a secure file transfer protocol to the given server. Consider using an FTP client, such as FileZilla (opens new window) or WinSCP (opens new window), or using this guide to transfer files.

sftp username@bobby.cs.unlv.edu     // Connects to the UNLV server, Bobby, to transfer files. This will prompt you for your password, along with making sure that you trust the server.
  • ssh [server] Use secure shell to connect to the given server.
ssh bobby.cs.unlv.edu           // Connects to the Bobby server.
ssh username@sally.cs.unlv.edu  // Connects to the Sally server as the user, "username"
  • telnet [server] Connects to the given server using the telnet protocol. This is very similar to ssh in function. Like ssh, it is very extensive. For a comprehensive list of telnet options, consider this link (opens new window).

  • touch [file] "Touches" the file, effectively opening and closing it. This is useful for editing the time metadata for when a file is changed.

# Compilers and Interpreters

Devtoolset-7 and devtoolset-8 are now installed on bobby.cs.unlv.edu. To use the latest GNU development tools (gcc, g++, etc.) run the command below

scl enable devtoolset-8 bash
  • g++ A C and C++ compiler. This compiler is recommended to be used for you CS 135/202 programs. There are a crazy amount of g++ options, so we'll just go over common ones, but if you are interested in seeing them all, the manual page for g++ is provided here (opens new window).

    • g++ [source] -o [output_name] Compiles source and outputs the executable as the output_name.

    • g++ -S Compile, but do not assemble, the provided code.

    • g++ -w Compile without showing warnings.

g++ hello_world.cpp                 // Compiles the source "hello_world.cpp". By default, this will output an executable file called "a.out". To run a.out, use ./a.out.
g++ main.cpp helpers.cpp enum.cpp   // Compiles the three files, "main.cpp", "helpers.cpp", and "enum.cpp" together. Do not try to compile .h files when compiling multiple files. Header files should be handled with #include statements, rather than with compilation.
g++ homework1.cpp -o myprogram      // Compiles "homework1.cpp" and outputs it to an executable name "myprogram", instead of the default "a.out". To run, use ./myprogram.
g++ -w calculator.cpp               // Compile "calculator.cpp" without printing warning messages. This will still print error messages, though.
g++ -E source.cpp                   // Outputs the preprocessed code with optimizations made, but does not actually compile the file "source.cpp".
g++ -S my_code.cpp                  // Compiles, but does not assemble, "my_code.cpp".
  • gcc Another C and C++ compiler that is part of the GNU project.
  • javac The default java compiler. Use java [class_name] to execute the program after compiling. The class_name should contain the main function.
  • make Used to compile all of the source files via the makefile in the current directory.
  • python The default Python 2 interpreter. By using this command without an argument, you will be able to enter Python code line by line.
python regression.py    // Executes the python script, "regression.py".
python                  // Opens the Python interpreter and will read line by line. Put a semi colon after a line to execute just that line.
python -v               // Displays the current installed version of Python.
  • python3 The same as python, but for Python 3, rather than Python 2. Python 2 code may not work with this interpreter.

# Grep

Grep (Global Regular Expression Print) is a useful and complex command that allows you to search for a regular expression across multiple files. In simpler terms, you can search for a word or expression across multiple files. Here, we'll cover some variations of grep and several examples, because of how useful this one particular command is. For more info, consider this GNU manual for grep (opens new window).

  • egrep Extended grep. Works almost identical to grep, but will search for regular expressions regardless of if there are extra characters before or after the string generated by the given regular expressions. This command is only recommended if you are familiar with how regular expressions work, but even then, is useful only in specific cases.

  • fgrep Shortcut for using grep -f. Can be used to search a file for a "pattern", rather than a regular expression. To some extent, includes special characters embedded within the regular expression that may be used in regular expression notation. For instance, the Kleene star is typed as "*". Normal grep will consider this as part of your regular expression as a Kleene star, but fgrep will try to look for it as the asterisk character. A simpler explanation for this command is that you should use it specifically when you are searching for a string containing the following characters:

* . ? + {number} - $ \b \B \\
The rai*n is falling down. 

fgrep "rai*n" search_file.txt This will actually highlight and locate the "rai*n", although normal grep will not. This may seem strange if you're unfamiliar with how regular expressions work.
  • grep

# Email

  • fetchmail Fetches email from the mail server and forwards it to the local email system.

  • mail Utility for writing and receiving emails.

  • sendmail [address] Sends an email to the email address given in [address]. After hitting enter, you will be able to type a message. To send the message, use CTRL+D.

# Miscellaneous

  • apt Advanced Package Management tool for Debian-based operating systems.

  • apt-get Advanced packaged tool get. Use this for installing packages.

    • apt-get install The option install will install the downloaded package.

    • apt-get update Re-synchronizes package index files from their source. Do this before doing upgrade.

    • apt-get upgrade Upgrades existing packages, if there are newer versions to be installed.

    • apt-get remove

    • apt-get purge

      • apt-get -d Download the package, but don't install it.
      • apt-get -f Attempts to fix broken packages.
      • apt-get --force-yes Automatically chooses "yes" during installation.
      • apt-get -h Provides help for using apt-get
      • apt-get -m Ignore missing files
      • apt-get --reinstall Reinstalls packages that are up to date.
      • apt-get -v Shows the current version
apt-get install python3-pip         // Will install the pip package installer for Python3.
sudo apt-get install mysql-server   // Will download install the package mysql-server. (We'll cover sudo later)

sudo apt-get update

sudo apt-get upgrade
  • autoconf Automatically configures software packages
  • clear Clears text out of the terminal. Makes it much easier to read when using a command that outputs large amounts of text.
  • date Shows the system date and time
  • eval Used to evaluate expressions in bash scripts. This is particularly useful when you want to execute the contents of a variable with a command. Can also be used in the command line to evaluate multiple commands at once. Ultimately, concatenates all returned strings into a single string, and then prints.
eval ls | cat                           // Lists all of the files and directories, like ls normally would, but allows you to scroll through them like when normally using cat.
eval cat hello.txt | more program.java  // Displays the contents of "hello.txt", like cat does, followed by displaying the contents of "program.java", which more normally does.
  • exit Exits the terminal. This will close your session if you are connected via an SSH
  • free Shows free and used system memory.
  • gdb Opens the GNU debugger. This can be used to debug programs that already run.
  • gpm Enables cutting and pasting from the terminal.
  • groupadd [group_name] Adds the current user to the group specified.
  • groupdel [group_name] Deletes the current user from the group specified.
  • groups Shows to what groups the current user belongs.
  • help [command_line] Provides a detailed description of the provided command. Does not exist for certain commands.
  • import Saves an image of the screen. Can be used on a single window as well. To see all the options, see the manual page here (opens new window).
  • history Prints a history of commands used by this user. Prints all commands within roughly a month worth of commands.
  • kill [pid] Kill the process with the given process id. Use top to see a list of processes and their ids.
  • killall [process_name] Kills all processes with the given process name.
  • last Shows a list of recent log-in attempts.
  • ld Used to link one or more object files.
  • login Starts a new session.
  • logout Logs out the current user, ending the session.
  • man Shows the manual for Linux Commands. Following man with a specific command will show the manual page for that specific command.
  • nice -n [value] [command_or_program] Runs the given command or program with the specified "niceness" value. The niceness value can range from -20 to 19. The "nicer" a program is, that is, the higher it's "niceness" value, the more likely it is to give up CPU time. If you are familiar with Windows process priority, this is very similar. More simply, bigger numbers give lower priority and smaller numbers give higher priority to the program. Use renice to change the niceness of processes that are already running. A process by default runs with a niceness of 10.
nice -n -10 ./a.out       // Runs the program "a.out" with a niceness of -10, giving it higher priority.
nice -n 16 python3 hmm.py // Runs the Python interpreter on the script, hmm.py with a niceness of 16, giving it a very low priority.
nice -n 5 ls              // Runs the ls command with slightly higher priority than normal.
  • passwd Changes the current user's password

  • reset Resets the terminal as if you were just logging in.

  • renice -n [number] [identifier] Changes the niceness of the process matching [identifier] to the niceness value given by [number]. Similar to nice, but for active processes, rather than one being started.

  • snice Resets niceness value for all current processes.

  • su Attempt to log-in as the super user (similar to administrator).

  • sudo Super User DO. Allows you to execute a command with the privileges of a different user. Usually, this means executing the command with "super user privileges; similar to administrator privileges in Windows. A lot of times, this will require a password.

sudo apt-get install python3    // Installs Python3 with super user privileges.
sudo whoami                     // Shows the user considered to be the "super user". This is usually the root.
  • suspend Suspends the current shell.
  • time [command] Gives the time to execute the given command. Gives real time, user time, and system time.
time ls Executes the ls command, then tells how long it took to execute.
time ./a.out Tells the amount of time it took to execute the program "a.out".
Example: A program runs, waits for 6 seconds for input, and then finishes
Thereal time is likely around 6 seconds. It is the actual amount of time between when the program began execution and when it finished.
The user time is how long the program was executing lines of code (or instructions). The user time on this may be around 0.1 seconds.
The 'system time is how long the system took with the program. This includes going to memory. This would probably be around 0.3 seconds for this example.
  • timeout [command] Runs the given command until the time (in seconds) is hit. After that, the process will be killed.
  • top Displays the currently running processes.
  • uptime Tells how long the system/server has been running for.
  • useradd [username] Creates a user with the given username.
  • userdel [username] Deletes the user with the given username.
  • usermod [username] Allows modification of the user with the given username.
  • users Gives a list of the current users on the system/server.
  • vim An in terminal version of the text editor vim.
  • w Shows a list of who is logged in to the system/server, as well as the last command that he/she executed.
  • wait [PID] Waits for the process with the given PID to terminate before returning its status.
  • whoami Tells you the current user you are.
  • yum Similar to apt. Used for downloading and installing packages.

# Running and Installing Programs and Scripts

Often when working with computers you will encounter scripts that will be necessary to accomplish your task. Here, we will cover common problems you may encounter when you are first learning about scripts and what everything means.

# Running Scripts

If you have done C++ programming at UNLV, you have more than likely typed ./a.out. While a.out is not a script, it is executed in much the same way. The ./ part should preface anything that you'd like to run. For example, if you had a program called "my_program", you would execute it with ./my_program. The same goes for scripts. If you had a script called "my_script", you would execute it with ./my_script.

The reason for this is that Linux will only run executable files if they are inside one of the directories listed in the PATH variable of bash, or if it is given the full path to the executable. The reason ./my_program works is because the "." character references a special file inside the directory that just stands in for the full path of the current directory (in the same way that ".." references the parent of the current directory). This way the full path to your executable is given. If you run the command echo $PATH you'll notice that PATH contains /home/[your_username]/bin. If you place an executable named my_script inside a directory named bin in your home directory, you will be able to run it from anywhere on that system with the command my_script without the "./".

# Changing Permissions

Another problem you may encounter when running scripts are permission errors. Again, if you are used to do C++, you may be used to giving the command ./a.out, and then your program just works. However, not all executable files will run this easily. For example, if you were to download a script or a program and tried to run it, you may get an error. This is because the file does not have permission to run. There are a few solutions to this. The first, and most common solution you'll see is just changing the permissions.

  • The most common example you'll likely find online is chmod 755 [file_name]. This will change the given file_name to have read and execute privileges for all users, as well as write privileges for the current user. Now, if you were to try and run the file, you'll notice that it runs. Chmod changes the privileges of a specific file, where the first number represents the privileges of the current user, the second, the user's group, and the third, everyone else. Each number should be between 0-7, which are (000-111 in binary). This is done because these numbers cover all combinations of three bits. The first bit is for read privileges, the second for write, and the third for execution. It would be beneficial to know how chmod works if you don't know it already. Consider the examples in the chmod section above, as well.

  • Another solution using chmod is chmod +x [file_name]. The +x option automaticall gives the file execution privileges, without having to worry about changing any of the other permissions.

# Installing Programs

Here, we'll cover the basics of installing a program/package in Linux, if you are not already familiar. There are a few built in package managers in most Linux operators, such as apt and yum. For the sake of simplicity, we'll just be using apt here.
First off, let's cover how to download and install a package. For this example, we'll be using MySQL Server, but the same goes for any package. The general way we would do this is with apt-get install mysql-server.

  • apt-get is the package manager; this is the actual program that will handle the downloading and installation of your package. In most cases, apt will work, too.
  • install is the option you'd like to use. apt does not handle only installation, so we need to tell it what it should do. We'll cover two other options here later, but to see more, you can see the apt command section above.
  • The last part we need is the actual package name. Depending on what you are installing, the name be a little different from what you'd expect. Most commercial software will give you an example of how to download their package on their website. When in doubt, it may be a good idea to Google (or whatever search engine you want to use) the software you are trying to download and how to install it on Linux. Here, the name of MySQL Server is mysql-server.

After running this command, you should see that apt will download and install your package. It is possible that you will be prompted for your password to confirm your installation. Apt will also usually ask if the installation size is okay as well. This being said, you may encounter an error as soon as you first run the command. There are two likely reasons why this is:

  1. The package may need super user privileges in order to complete installation. In order to fix this, preface your command with sudo. For more information, please take a look at the sudo command above. Here is an example of what using sudo would look like:

sudo apt-get install mysql-server

  1. It is possible that packages may have changed their sources. To fix this, use apt-get update. Like the previous example, you may need to preface this with sudo.

After downloading, or just in general, you may encounter problems or bugs that exist in the current version. To upgrade all currently installed packages, you can use apt-get upgrade.

# Running Linux (Ubuntu) on a Virtual Machine (VM)

  • WIP