Bash: Navigating Server - pcgoddard/Burchardlab_Tutorials GitHub Wiki

Bash Commands for Navigating the Server

Notes by: Pagé Goddard

Tutorial session by: Jennifer Liberto


Tutorial Contents

look at your directory contents

here are a few commands to list the contents of your directory. The core command is ls,which is short for "list." There are more flags (a flag is a - + alphanumeric symbol that tells the command to do something specific) that can be added for greater flexibility of the command, but here are some basics

the typical format is: ls + path/to/your/directory

  • ls lists just the file and subfolder names in your directory
  • ls -1 does the same, but puts it all in a single column
  • ll lists contents with permissions, size, and date information
  • ls -lrta list in reverse time, anything (lists latest last in list)

for example:

ls mydir
myfile1  myfile4
myfile2  myfile5
myfile3

note: to look in your current directory you can simply enter ls without including the directory path.

permissions

to help you understand which files and directories you and others have permission to access and what you can do with them

drwxr-xr-- example 1 -rwxr-xr-- example 2

  • initial d = directory; - = file
  • w = can write (can alter this file/directory and save it)
  • x = can execute (can run this script)
  • r = can read (can read this file/directory)

changing directories

because no one can spend his/her entire life in one room alone...

  • pwd - print working directory tells you where you are right now
  • cd - change directory tells the computer you want to move; you do need to tell it where you want to move to (i.e. which directory)
  • ~ - root directory; the common ancestor of all your directories, if you will. never try to remove this
  • . - shorthand for current directory (whatever it is)
  • .. - shorthand for parent directory (whatever directory contains your current directory); these can stack
    • eg: in ~/user/you/project/data, data is your current directory (.), project is your parent directory (..), you is the grand-parent directory (../..), etc.
    • using .. means that the path you provide is a relative path - meaning that it is relative to your current directory
    • the absolute path is the path from the root directory ~
pwd
/media/burchardraid01/datafreeze # example directory

cd .. && pwd # change then prnt new directory
/media/burchardraid01 # parent directory

cd ~ && pwd
/media/.../your.directory # you are now in your home directory

cd path/to/desired/directory && pwd
path/to/desired/directory # you are now in the directory you asked for

pwd = print working directory; tells you where you are

cd = change directory

. = current directory

.. = previous directory

~ = home

note: when typing a path, use / not backslash; if you get an error, check that you are not trying to cd into a file instead of the directory; if error persists, check your slashes, your capitalization and your path

visualize files

less filename = opens 1 page view of data without printing the whole thing to screen; move by line (arrow keys) or page (space)

head filename = prints first 10 lines of file

  • head -3 filename = prints first 3 lines
  • tail filename = prints last 10 lines of file
  • tail -3 filename = print last 3 lines
  • head 10 filenme | tail -3 - = print 3 lines after line 10 of file
  • cut -d' ' -f 1-5 filename = print columns 1-5 of file; d is your file delimiter (the symbol that separates columns, usually space ' ', comma ',', or tab '\t')
  • cut -d' ' -f 1-5 filename | head -5 - = print only rows 1-5 of columns 1-5 of file

vim filename = let's you view and edit file

  • to edit, press i (for insert) and write as you please
  • navigate lines with arrow keys
  • to save click Esc and type :w
  • to quit, click Esc and type :q
  • to save and quit type :wq

run a process in the background

this is especially useful if you are running an automated script that will take a while and doesn't require interaction after it starts

  • note: on a Sun Grid server, you would use qsub to submit jobs to the cluster computer, rather than running screens in the background as will be described here.

start a new screen

screen 
# this changes your console window to a detachable "screen"
# title will now read "screen 0: username@hostname:~/PATH/TO/current_directory"
# normal title bar reads: "username@hostname:~/PATH/TO/current_directory"

start a new screen with a particular name

screen -S name

detach your screen

# while in the new screen  type
ctrl+A ctrl+D

return to your screen

screen -r  # if you only have one screen
# if you have multiple screens, the list will print here

screen -r [number/name] # if returning to one of multiple screens

kill an old screen

screen -r [name/number] # return to screen

# type
crtl+A K

copying files to your computer

on a mac/linux
exit # leave server
scp [email protected]:path/to/file/filename.csv ~/Desktop
  • translation: copy/paste [this file on server] [to my personal desktop]
  • scp = secure copy paste (copy paste over secure ssh connection)
  • to push to server, just switch ~/Desktop and [email protected]:/path
or use filezilla GUI

Angel's step-by-step instructions for setting up FileZilla can be found on the Wiki here

Congrats! you can now read your data into R studio/excel/your favorite program on your personal computer