The Linux Commandline (bash)¶

8/30/2022¶

print view

Linux¶

Happy Birthday Linux! Here's your cake:

Installing¶

For this class we suggest all of you to run the codes on your own machines. Getting to install the requisites is a part of the learning process.

Linux¶

For linux users- Use the system package manager (apt) and the python package manager (pip) to install additional software. sudo runs a command with super user privileges.

macOS¶

Macs also have a built-in commandline (Terminal.app). Many of the packages we use are also available of OS X. MacPorts is most convenient for installing software (https://www.macports.org), but not all packages that we use are readily available.

Windows¶

As of Windows 10, can install Linux Subsystem. In theory should work just like Ubuntu and is much less cumbersome to use.

Anaconda¶

Distribution of python and R programming languages. Preffered for simplified package management and deployment. Users can install Anaconda from (https://www.anaconda.com/)

Microsoft Visual Studio¶

We recommend everyone to use the Microsoft Visual Studio Integrated Development Environment for python coding in Linux, Windows and macOS.

We will direct you to specific packages/softwares that needs to be installed for each class.

In [2]:
%%html
<div id="prepared" style="width: 500px"></div>
<script>

	jQuery('#prepared').asker({
	    id: "cdquestion2",
	    question: "Do you have the bash shell or PuTTY installed?",
		answers: ["No","Yes, PuTTY","Yes, Windows BASH","Yes, MacOS"],
        server: "http://bits.mscbio2025.net/asker.js/example/asker.cgi",
		charter: chartmaker})
    
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();

</script>

Commands¶

The first word you type is the program you want to run. bash will search PATH environment variable for an appropriately named executable and run it with the specified arguments.

  • man - manual pages in linux
  • ssh hostname - connect to hostname
  • passwd - change your password
  • ipython - start interactive python shell (more later)
  • nano - a user-friendly text editor.
  • vi, gedit -other editors.

ssh¶

Secure shell. On Windows can use PuTTY.; ssh username@10.5.29.73

scp¶

Secure copy: scp file.py python.mscbio2025.net:.

A BASH cheatsheet¶

http://mscbio2025.net/notes/bash_cheatsheet.pdf

ls - directory listing¶

$ ls -l
total 8
-rw-rw-r--  1 anupam06 anupam06    5 Aug 29 13:05 hello.txt
drwxrwxr-x  2 anupam06 anupam06 4.0K Aug 29 13:03 lecture
$ pwd
/home/anupam06/intro
$ cd lecture/../..
$ pwd
In [7]:
%%html
<div id="question1" style="width: 500px"></div>
<script>

	jQuery('#question1').asker({
	    id: "cdquestion",
	    question: "What prints out?",
		answers: ["~/","/home/anupam06", "/home/anupam06/intro","/home/anupam06/intro/lecture","An Error"],
		extra: ["","","","","",""],
        server: "http://bits.mscbio2025.net/asker.js/example/asker.cgi",
		charter: chartmaker})
    
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();

</script>

ls - directory listing¶

$ ls -lah
total 16K
drwxrwxr-x  3 anupam06 anupam06 4.0K Aug 29 13:05 .
drwxr-xr-x 15 anupam06 anupam06 4.0K Aug 29 13:02 ..
-rw-rw-r--  1 anupam06 anupam06    5 Aug 29 13:05 hello.txt
drwxrwxr-x  2 anupam06 anupam06 4.0K Aug 29 13:03 lecture
$ ls *.txt

In [4]:
%%html
<div id="question2" style="width: 500px"></div>
<script>

	jQuery('#question2').asker({
	    id: "lsquestion",
	    question: "What prints out?",
		answers: ["hello.txt","*.txt", "hello.txt lecture", "An Error"],
		extra: ["","","","","",""],
        server: "http://bits.mscbio2025.net/asker.js/example/asker.cgi",
		charter: chartmaker})
    
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();

</script>

Spacing Out¶

du - disk usage of files/directores

[dkoes@n052 tmp]$ du -s
146564  .
[dkoes@n052 tmp]$ du -sh
144M    .
[dkoes@n052 tmp]$ du -sh intro
4.0K    intro

df - usage of full disk

[dkoes@n052 tmp]$ df -h .
Filesystem      Size  Used Avail Use% Mounted on
pulsar:/home     37T   28T  9.3T  75% /net/pulsar/home

Dude, where's my stuff?¶

locate -find a file system wide

find -search directory tree

which -print location of a command

man -print manual page of a command

Getting at your variables¶

 

NAME=value set NAME equal to value No spaces around equals

user@python$ X=3
user@python$ echo $X
3
user@python$ X=hello
user@python$ echo $X
hello
user@python$ echo X
X
In [5]:
%%html
<div id="questionvarprint" style="width: 500px; "></div>
<script>

	jQuery('#questionvarprint').asker({
	    id: "varquestion",
	    question: "Which does <b>not</b> print the value of X?",
		answers: ["echo $X","echo ${X}","echo '$X'","echo \"$X\""],
		extra: ["","","","","",""],
        server: "http://bits.mscbio2025.net/asker.js/example/asker.cgi",
		charter: chartmaker})
    
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();

</script>

Capturing Output¶

`cmd` evaluates to output of cmd

$ FILES=`ls`
$ echo $FILES 
hello.txt lecture

History¶

history show commands previously issued

up arrow cycle through previous commands

Ctrl-R search through history for command AWESOME

.bash_history file that stores the history

Shortcuts¶

Tab autocomplete Ctrl-D EOF/logout/exit Ctrl-A go to beginning of line Ctrl-E go to end of line alias new=cmd
make a nickname for a command
$ alias l='ls -l'
$ alias
$ l

Loops¶

for i in x y z
do
 echo $i
done

for i in *.txt
do
 echo $i
done

Lots more... (TLDP)

String Manipulation¶

http://tldp.org/LDP/abs/html/string-manipulation.html

$ i="ovolo"
$ echo ${i%o}
In [6]:
%%html
<div id="stringm" style="width: 500px"></div>
<script>
$('head').append('<link rel="stylesheet" href="http://bits.csb.pitt.edu/asker.js/themes/asker.default.css" />');


	jQuery('#stringm').asker({
	    id: "stringm",
	    question: "What prints out?",
		answers: ["ovolo","volo", "ovol","vl","An Error"],
        server: "http://bits.mscbio2025.net/asker.js/example/asker.cgi",
		charter: chartmaker})
    
$(".jp-InputArea .o:contains(html)").closest('.jp-InputArea').hide();


</script>

Batch processing files¶

for file in *.png
do
 convert $file ${file%.png}.tif
done