Linux Guide

From Tux
Revision as of 13:59, 3 February 2019 by Sam (talk | contribs) (Files and Directories)
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 available on most Linux distributions, as well as OSX (Mac) upon installing the OS. 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.

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

Sending emails

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