Common commands to check your account and environment
history > history.txt #saves your history to a text file, overwriting it if it already exists
history >> history.txt #saves your history to a text file, adding to the end (appending) if it already exists instead of overwriting it
ip a #prints your internet IP addresses (ethernet and wifi)
ls #shows you all the filenames in your directory
pwd #print current working directory; default is /home/username
which #shows you where a command lives (commands are programs so this helps troubleshoot if a command doesn't work)
which ls #example showing you where the ls command lives on your system
Common commands to work with files and directories
touch bar #create an empty file called "bar"
touch bar.sh #create an empty bash file called "bar.sh"
ln -s ~kessnerd/bacteria.fasta #creates a symbolic (-s) link from your own directory to the bacteria.fasta file in kessnerd's directory. Symbolic links are saves just like files. The system treats it like a file.
cd workshop #change directory and go into directory "workshop", assuming that "workshop" is a folder in your current directory
cd ./workshop #same as above, change directory and go into directory "workshop", assuming that "workshop" is a folder in your current directory. The dot slash indicates your current directory.
cd ../ #backs out of the directory you're in, goes back one folder from your current directory. The double dot indicates the previous directory (the one before yours).
cd ~ #go back to your home directory (relative path)
cd /home/yourusername #go back to your home directory (absolute path)
mv testfile testfile2 #renames file (because it's not really being moved)
mv testfile /../../common/myname/testfile
#moves my testfile from /home/myname to common/myname
#on my institution's HPC, the home directory doesn't have much space
#but the common directory has plenty of space
------------------------------------------------------------------------
Read and search files
wc bacteria.fasta #word count of your file
head bacteria.fasta #shows the first 10 lines of that file
tail bacteria.fasta #shows the last 10 lines of that file
cat bacteria.fasta #shows you what is in your file (could be lots of text so try running wc first!)
less bacteria.fasta #like cat but allows you to go back and forth
grep '>NC' bacteria.fasta #searches the text '>NC' in the file bacteria.fasta and outputs the line that contains that text pattern. The single quotes indicate the string pattern you want (taken more literally than double quotes). If you use "double" quotes, it will be evaluated more liberally and could be evaluated as a command instead. Most times it doesn't matter if you use 'single' or "double" quotes.
grep -i '>NC' bacteria.fasta #case insensitive grep search
------------------------------------------------------------------------
Delete things (use with caution!)
rm bar.sh #permanently deletes the file "bar.sh"
rm -r workshop #permanently deletes the directory "workshop"
------------------------------------------------------------------------
Starting a bash script, making it run
touch mycode.sh #first make an empty file "mycode.sh"nano mycode.sh #opens the file "mycode.sh" so you can edit it from the terminal
echo ....Hello Tania!.... #use echo function to print "....Hello Tania!...." when the bash script is run
[control+O] #from nano saves the file
[control+X] #from nano exits the file
chmod +x mycode.sh #makes the bash script "mycode.sh" executable so it can be run
./mycode.sh #runs the bash script, assuming the script is in your current directory (./)
------------------------------------------------------------------------
Bash coding references
Simple tutorials: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.htmlNote that bash is very picky about white spaces, so if something isn't working, check if you added or omitted spaces.
Example:
if [ "tania" = "tania"] #will produce an error
if [ "tania" = "tania" ] #will evaluate as TRUE
The difference is the space before the ending square bracket. Bash likes the square brackets to have spaces before and after.
Other interesting projects:
------------------------------------------------------------------------
Other references
Free software to remotely log into (ssh into) a Linux server
- PuTTY - the old and established way to ssh into a Linux server. Only Windows compatible. Not my favorite.
- Windows terminal (cmd.exe) - Windows 10 onward has better support for ssh. You no longer need PuTTY or other software.
- MobaXterm (Home Edition is free) - my recommend method. The interface is very user friendly.
ssh [username]@[serveraddress] -p [portnumber]
ssh gonzalez@192.168.0.2 -p 555ssh gonzalez@someserver.university.edu -p 9
------------------------------------------------------------------------
Load R on the Cedars HPC cluster (6/2021)
------------------------------------------------------------------------
Log into a server and request X11 forwarding for plotting, plus verbose settings
------------------------------------------------------------------------
Windows Subsystem for Linux (WSL)
cd /mnt/c/ #this is like "cd c:" but for the WSLcd /mnt/c/Users/Gonzalez/Dropbox/
system('bash -c "ls /"') #shows the directorysystem('bash -c "ls /mnt/c/Users/Gonzalez/Dropbox"') #shows my Dropbox directory
system('bash -c plink1 --help') #shows the help screen for plink, a Linux bioinformatics tool
------------------------------------------------------------------------
Accessing connected hard drives
cd ~
cd ../../media/
ls #identify your user name
cd [yourusername]
ls #check for your external storage drive name and cd into it
------------------------------------------------------------------------
Create a symbolic link to Dropbox on WSL
------------------------------------------------------------------------
Run WSL programs from the Windows terminal
C:\Users\Gonzalez> wsl.exe plink --version
------------------------------------------------------------------------
Create a virtual environment for python
pip install virtualenv
python3 -m pip --upgrade virtualenv #optional if you already had virtualenv
Identify any existing virtual environments:
locate -b '\activate' | grep "/home" #finds all virtual environments in /home
Create a directory to store virtual environments:
mkfir environments #keep things neat
Create, enter, and leave a virtual environment called "RNAseq":
python3 -m venv ./environments/RNAseq #create
source ./environments/RNAseq #enter
pip list #shows all packages inside this environment
deactivate #quit the environment (go back to the regular Linux shell)
Last updated October 21, 2023