Linux Guide

From Tux
Revision as of 00:23, 4 February 2019 by Sam (talk | contribs) (Grep)
Jump to: navigation, search
This page is under construction

Introduction

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.

List of popular Linux operating systems

  • Ubuntu - Common OS for personal use.
  • CentOS - Another common OS for personal use. CentOS is available in the TBE B-461 lab.
  • Debian - Yet another common Linux OS.
  • Raspbian - OS commonly used on Raspberry Pis for simple projects.
  • Android - The popular phone OS. May require a separate emulator.

Linux commands

List of common commands

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. 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 like:

  • command_name Description
Examples Description of example
  • command_name <parameter>
  • command_name -option

Bash

Bash is a shell for navigating an OS without the need for GUI. Bash is a part of the GNU Project, 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.

Files and Directories

  • cat 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.
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.
File1.txt:
Hello World
File2.txt:
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.
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. 
Note: 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 the 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.
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 want to edit a file without using GUI.

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".

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.

  • 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 \< \>

search_file.txt:
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.

Email

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

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 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)
  • apt-get update Re-synchronizes package index files from their source. Do this before doing upgrade.
sudo apt-get update
  • apt-get upgrade Upgrades existing packages, if there are newer versions to be installed.
sudo apt-get upgrade
  • 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
  • autoconf Automatically configures software packages
  • 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.
  • 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.
  • whoami Tells you the current user you are.
  • yum Similar to apt. Used for downloading and installing packages.

Running programs and scripts

Changing permissions

Running scripts

Installing programs

Running Linux (Ubuntu) on a virtual machine

We have a tutorial for installing a running Ubuntu on the VirtualBox page. You should do this if you need to use Linux, but don't want to install it on your actual machine.