Getting Around the Command Line

From CS Support
Jump to: navigation, search

Command Line

The command line is an incredibly powerful tool to navigate and use your computer, but it can be a bit daunting at first. This page will show you the basics you need to get started.

Basic Use

Navigation

Commands

Linux has many useful and powerful tools right in the command line. In this sections we have provided examples for the most common commands you'll need but you can find more in depth explanations of most Linux commands by using the man command. For example, man mv will open a page in your terminal showing usage and explanations of every option flag you can give the mv command.

  • ls - list the files and directories inside your current directory.
ls

Ls command.png

For this command, you do not need any other parameters for it to work. However, you may want to list items differently. One way of doing this is by typing ls -l. This will present the same files you find with a simple ls in a list format including more information. What you see when you run this command may be daunting before you know what this is showing you. For now, we'll focus on the easy list looking format of the names to the far right. If the first character shown on the far left is a d, then the name you are looking at is a directory (aka, a folder). For more information on what the rest of the output means, see Permissions.

Ls-l.png

Another command you can use to see everything you have in your folder (including hidden files) is the ls -a command.

Ls -a.png

You may have seen another command flag -la used with ls. This combines the two previously explained flags, and will list all files and folders in the directory you are in. The first two folders shown are "." and ".." and this signifies the your current directory (".") and your parent or previous directory (".."). This list also includes hidden files, which are denoted by a "." at the front of a file name. Explanation on what these are can be found later in this section.

Ls-la.png

  • pwd - print the name of your current working directory.
  • cd - change directory, move into other folders so that you can access the information there.
cd folderName

For example, cd ./folderName will move location from your current location (signified by the dot ".") to the location of folderName. However, if you are already in the directory of the folder you want to be in, you can also simply write cd folderName.

Navigation.png

On the other hand, if you're looking to move into a folder that is beyond the folder you're already in, you can type out the specific directory path. For example, you might type something like this cd /home/folderName. Another way of getting to a completely different folder, you can cd back to the folder you would like to move from. To accomplish this, you would change your directory back to the previous folder you came from by typing cd ...

AdvancedNavigation.png

  • mkdir - make directory, make a new folder.

Mkdir.png

  • mv - move a file or directory from one place to another. Can also be used to rename a file.
mv <location of source> <location of destination>

MoveFolder.png

And an example of using the mv command to rename a file.

Rename.png

  • rm - remove file or directory as described by telling the command line the location of the file or directory you would like to remove.

To remove a file, simply type

rm filename.extenstion

To remove a directory will require a flag, depending on whether you have information in the directory already. While the following line works only on empty directories, it might be nice for you to see if you're about to lose something important, so type

rm -d directory

This will give a warning if there is anything in your folder and then you have the chance to go in and see if you truly want it all removed. If you're positive about all the information going away, one way to do it is

rm -r directory 

Removing.png

However, this is just one way. In fact, many of these commands are only one way of accomplishing a task. This is just to get you started but there are boundless nuances in accomplishing a task.

[command] *.[file extension] 

The wildcard character is used to call a command on specific files. For example when you say ls *.docx that means you want to list all and only files of type .docx

Wildcardex1.png

You can also use this character to call commands on filenames.

Wildcardthename.png

cp <file source> <file destination>

With the cp command you can copy files from the command line from one folder to another. Here we have an example of using the command with the wildcard.

Cp.png

What's in Your Directory

The purpose of this section is to understand what you might find as you navigate around in your terminal.

Permissions

If you recall from the Basic Use section, the ls -l and ls -la commands have a bunch of seemingly random letters and things:

Ls-l.png

All of these are important to understand to understand permissions, especially the first block of letters and dashes.

Let's take a look at the first line and break it down:

Ls-l explain.png

  • The first grouping of letters (A-D) is the file type and the read/write/execute permissions:
    • A: The file type
      • d: directory (which this example is)
      • -: regular file (like a .txt)
      • l: symbolic link (like a shortcut on your desktop)
    • B: Owner permissions. Each permissions field has three character positions:
      • r if the file is readable; - if it is not
      • w if the file is writeable; - if it is not
      • x if the file is executable; - if it is not
    • C: Group permissions. r/w/x have the same meaning as B
    • D: Other permissions. r/w/x have the same meaning as B
  • E: Number of links to this location
  • F: Owner name (Your username)
  • G: Owner group (most likely grp.csci.Students)
  • H: File size in bytes
  • I: Date modified
  • J: File/directory name

Changing Permissions

If you're having trouble with permissions on a file that you own, you can update the permissions with chmod. chmod is generally called with the following format:

chmod mode fileName

Where the mode specifies what permissions get changed and for who. chmod has two ways you can change the permissions: octal notation (numbers 0-7), or symbolic notation (letters for group and permission). We will cover both ways so you can choose which you prefer.

Octal

With octal notation, we have this basic usage:

chmod ### fileName

The numbers make up what is called the mode where the first # represents the permission level of the owner, the second for the group, and the third for other.

The numbers breakdown like so:

  • 4 - read
  • 2 - write
  • 1 - execute

And any combinations of r/w/x can be achieved by adding the numbers. For example, if I want to give read and execute permissions I would use 5.

Therefore:

chmod 764 fileName

Changes the permissions for fileName so that the owner has rwx, the group has, rw, and other has r only.

Symbolic

With symbolic notation, we have this general usage:

chmod <u|g|o><=|+|-><r|w|x> fileName

This may look confusing, but stick with me. Let me give you an example:

chmod ug=wr fileName

This changes the user's (owner's) and group's permissions to wr for the file filename. It does not change permissions for other.

The mode in the symbolic notation consists of three sections:

  1. Groups affected. This can be any combination of u/g/o for user (owner), group, and other respectively.
  2. Operator. This can be =, + or - where the operator sets, adds, or removes the specified permissions respectively.
  3. Permissions. This can be any combination of r/w/x for read, write, and execute respectively.

A few examples:

chmod ug+wx fileName

adds write and execute permissions to the user and group without affecting the read permission or the other group; and,

chmod go-rwx fileName

removes all read, write, and execute permissions for group and other.

Dot Files

When you hear the word "dotfiles" that tends to refer to files or directories that begin with a "." prefix. If you've seen the Git page, you know that when you create a repository your local machine gains a .git folder. These directories or files won't show up if you simply type an ls command as you remember from the basic command section of this survival guide. Dotfiles are also known as hidden directories/files and a UNIX based terminal (like the one in the labs) is configured to recognize that and not list its information.

A dotfile's purpose is to store information that a user doesn't need to access often or at all and will often hold configuration information for various programs. Nearly every application on your computer that you use can be configured to look and act the way you like. Your configurations will go into dotfiles usually ending in "conf" or "rc" like Vim's .vimrc or Tmux's .tmux.conf. Note: changing these files could have disastrous results if you change the wrong thing. We implore you to understand what you are changing before you change it by doing some research into your dotfile.

In an ideal world, there would be a one stop walk-through that tells you exactly what to do to a file to make it do what you want, but the configuration of dotfiles varies depending on the program you are configuring so we suggest you do research on your specific dotfile before diving in to change it. If you notice somebody with a feature you like--whether it's the color text in the terminal or how a certain application in the terminal interacts--ask to see their dotfiles to learn a few things from what they've done and steal a line or two for your own config. There are endless dotfile repositories on GitHub and anyone would be glad to answer your questions.

Advanced Use

Text Editors

These handy things let you write and edit code and other text-based things!

Vim

Vim is an editor that lives in the terminal accessible with the command vim. Simply use a command like vim filename.txt to open up the file you want to work on. To the right is a cheat sheet for vim keystrokes and commands.

Click to enlarge

Here are some external links that will introduce you to the basic usage of vim:

  1. Interactive Text-centric Vim tutorial
  2. Gameified Vim Tutorial

And here are some external links for anyone who wants to explore advanced vim usages:

  1. Vim as a Language
  2. Operations and "Motions"

If you find any other helpful links on your quest to understanding vim, feel free to let us know at cs.support@wwu.edu.

Emacs

Emacs is another text editor that lives inside your terminal widely supported by its customizability. Like Vim, Emacs can be either a graphical interface or just an editor right in your terminal.

According to this page, emacs is at minimum an interpreter for Lisp, which is a language (or a variation of Lisp) that you'll encounter in department which means you will be able to easily change and configure Emacs to your will. If you need inspiration, this is an article by a self-proclaimed "computer dummy" who learned Emacs in a year.

Here is Emac's wiki for in depth help.

Please feel free to take a look at our guide on Emacs created through the Peer Lecture Series: File:Workshop Emacs.pdf

If you find any other helpful links on your quest to understanding Emacs, feel free to let us know at cs.support@wwu.edu.

Atom

Atom is available to you on either Linux or Windows. Atom is useful for converting Markdown plain text into pretty formatted text with Shift + Ctrl + M or just as a decent text editor outside the terminal with other cool package addons.

There's not much to learn about atom, but if you have any questions, consult the docs: https://atom.io/docs

Tar Compression

File and folder compression can be useful for sending lots of files to another person or computer in a convenient archive package. There are a few different file extensions that you will need to understand to wrap your head around using the tar command in the terminal.

Most common:

  • .tar: also called a tarball, a .tar archive can contain many files and folders in a single .tar file for easier transport. This does not compress the files in any way.
  • .gz: gzip can only compress a single file and is most often used to further compress a .tar archive into a .tar.gz archive.

Less common:

  • .bz2 | .bz: similar to gzip, bzip2 only compresses single files and is most often used to further compress .tar archives into .tar.bz2 archives.
  • .xz: this is generally used for Linux architecture files like kernel archive files. It's slow and resource hungry, but can compress to a smaller size depending on the circumstances. More info.

Tar compression and decompression can be a doozy, but we're here to help! The most useful tools you have at your disposal when you're trying to compress or decompress a .tar or .tar.gz file are the man tar manual page and your favorite search engine.

Here are a few basic, useful commands.

Compression

tar -czvf archiveName.tar.gz ~/Desktop/thisFile.txt ~/Downloads/thatFolder moar.jpeg

This will compress the two files (thisFile.txt and moar.jpeg) and the folder (thatFolder) into an archive named archiveName.tar.gz.

  • -c will create the tarball archive.
  • -z will tell tar that we will compress the tarball with the .gz extension.
  • -v will output all file names put into the archive to the terminal.
  • -f will name the output archive.

Decompression

tar -xzvf anotherArchive.tar.gz

This will extract all the files and folders from anotherArchive into the current working directory.

  • -x: will extract from the archive.

If you want to extract the archive contents into a new folder, you can add the -C flag like so:

tar -xzvf anotherArchive.tar.gz -C folderToExtractTo

This command will extract the contents of anotherArchive.tar.gz to a new folder folderToExtractTo.

Other Flags

  • -j: will tell tar that we will compress the tarball with the .bz2 extension as in .tar.bz2 or just .bz2.
  • -J: will tell tar that we will compress the tarball with the .xz extension.

Zip Compression

Like a .tar.gz, a .zip file can compress many files and folders into one archive to make sending or storing lots of data easier.

Creating a .zip from the command is much more simple than creating tar files:

Compression

zip archiveName.zip thisFile.py ~/Documents/thatDocument.docx

This will create an archive named archiveName.zip containing thisFile.py from the working directory and thatDocument.docx from the Documents folder.

If the archive will contain a folder you will need the -r flag.

zip -r folderArchiveName.zip ~/Documents/ thisFile.java

This will create an archive named folderArchiveName.zip containing the entire Documents folder and thisFile.java from the working directory.

Decompression

unzip archiveName.zip

This simple command will extract and decompress the contents of archiveName.zip into the current directory. The archive will remain the same.

SSH

SSH stands for Secure Shell. It is used to establish a secure connection to an account on another computer to access files or run programs without physical access to the machine.

At Western, we allow you to ssh into our Linux COW (Cluster Of Workstations) from anywhere inside or outside the University to access the files in your account's home directory.

Accessing Linux with SSH

By default, SSH will use port 22 for reasons. The CS department uses port 922 for a little additional security. This means the ssh command will need the -p flag to specify port 922.

ssh -p 922 ${username}@linux.cs.wwu.edu

Where ${username} is your CS account username.

Running this command in your terminal will prompt for your password and after authentication, will allow you to interact with the file system as though you were in a terminal in one of the CS labs.

SSH Config

If you find typing the whole ssh -p 922 ${username}@linux.cs.wwu.edu line into your terminal exhausting, there's a perfect solution for you: SSH configuration!

SSH configuration will allow you to use any shorthand alias to connect to a remote server, e.g. ssh linux. Your ssh config file is found at ~/.ssh/config.

Here is an example of an ssh config file for linux.cs.wwu.edu:

SSH config for linux.cs.wwu.edu

Note:

  • You can call the Host anything, it doesn't have to be linux. It is just your alias for the ssh command.
  • Replace ${username} with your username or the account you have permission to use.

Getting Files with SCP

To copy files from a remote machine that you can access with ssh, you can use Secure Copy, scp.

From your local machine, use the following command:

scp -P 922 ${username}@linux.cs.wwu.edu:~/document.txt ~/Documents/

Assuming this file exists, this command will copy the file document.txt from your CS account home directory (denoted with ~) to your local machine's Documents directory.

Notice that scp uses a capital P for its port option flag.

This can also work in the opposite direction: copying files from your local computer to a remote destination.

scp -P 922 ~/Documents/document.txt ${username}@linux.cs.wwu.edu:~/copied.txt

This command will copy the file document.txt from your local machine to the home directory of your CS account renamed to copied.txt.

SSH Keys

Using an SSH key will make your login more secure. This section will give you the skinny on SSH keys and key usage. At the end of this section will be a link to a more in depth description on SSH security.

SSH keys are generated in a public/private key pair. When you ssh into another machine, it sends your public key to that machine's ~/.ssh/authorized_keys file. When you connect to that machine later, it checks your private key against the public key it has through cryptographic algorithms to verify your identity.

Your private key should never be given out to ANYBODY!

Western uses EdDSA in the form of Ed25519 for our key encryption method. To generate the key pair use this command in the terminal:

ssh-keygen -t ed25519 -C "$(whoami)@$(hostname) $(date -I)"

This command will ask for a passphrase and then generate two files in the ~/.ssh directory: id_ed25519 and id_ed25519.pub. The former is your private key (Keep it secret! Keep it safe!), and the latter is your public key signified with the .pub extension.

Here is an example of what the interaction will look like:

Ssh-keygen.png

  • note: the password field appears blank because password characters are hidden in the terminal.
  • also note: if you leave the Enter the file in which to save the key line blank, it will default to the path in the parentheses.
Other Key Formats

While all of the Computer Science department's SSH servers accept newer key formats like Ed25519 this is not universally true. Some services outside the university might not. You may wish to consider another algorithm like RSA or ECDSA. You can generate those types of keys by changing which argument you give to ssh-keygen's -t flag. For example, if you wanted to generate a 2048-bit RSA key you would do

ssh-keygen -t rsa -b 2048 -C "$(whoami)@$(hostname) $(date -I)"

You are now all set to use these keys!

Here is a more in depth page if you need more info.

Extra!

Using SSH Agent

Tunneling MySQL ports through SSH

Virtual Environments

In the CS department, we have some permissions in place to block installation of many outside programs and modules without admin privileges to avoid bloat and viruses on the computers. Using python Virtual environments can be helpful when developing a program that you need more permissions for. In a virtualenv, you can pip install whatever you want, including the program you're writing if you have that set up.

virtualenv command

1. To start, you need to build a virtual environment. This command will create a folder called ENV containing the virtual environment files. ENV can be any name you want.

virtualenv ENV

2. Before you can pip install anything, you have to activate the virtualenv to work in it. Once you activate the virtual environment, you can use it from anywhere.

source ENV/bin/activate

3. Deactivate to stop working in it

deactivate

More info can be found in virtualenv's user guide.