sciware

Sciware

Intro to the Command Line

https://sciware.flatironinstitute.org/40_SummerIntro/cli.md

Agenda

What is a Command Line Interface?

Example Shell Command

CLI and GUI

Where am I?

pwd prints working directory:

$ pwd
TKTKTK

ls lists what’s in this directory:

$ ls
TKTKTKTK

To follow along…

Clone a git repository:

$ git clone REPO-TK
$ ls

Going somewhere

cd changes the directory you’re in

$ cd REPO-TK
$ pwd
$ ls

My neighborhood

$ pwd
TKTK
$ cd .
$ pwd
TKTK
$ cd ..
$ pwd
TKTK

Getting information

ls can give more details if you ask:

$ ls Mainfile.idx
MainFile.idx
$ ls -l Mainfile.idx
-rw-rw-r-- 1 jsoules jsoules 43 Jun  3 14:51 MainFile.idx

Those columns are:

Aside: keystrokes

$ ls M # hitting tab will auto-complete MainFile.idx
$ ls m # tab once will give myfile_
$ ls m # tab twice will show the possible completions of myfile_

Moving and Renaming

$ mkdir newdir
$ ls
dir1  MainFile.idx   myfile_02.txt  myfile_04.txt  myfile_06.txt  myfile_08.txt  myfile_10.txt  myfile_12.txt  myfile_14.txt  myfile_16.txt  myfile_18.txt  myfile_20.txt
dir2  myfile_01.txt  myfile_03.txt  myfile_05.txt  myfile_07.txt  myfile_09.txt  myfile_11.txt  myfile_13.txt  myfile_15.txt  myfile_17.txt  myfile_19.txt  newdir
$ cp MainFile.idx newdir
$ cp .hidden.file newdir
$ mv newdir newdir2
$ ls -a newdir2
.  ..  .hidden.file  MainFile.idx

Removing

$ rm newdir2
rm: cannot remove 'newdir2/': Is a directory
$ rmdir newdir2
rmdir: failed to remove 'newdir2/': Directory not empty
$ rm newdir2/MainFile.idx
$ rm newdir2/.hidden.file
$ rmdir newdir2
$ ls
dir1  MainFile.idx   myfile_02.txt  myfile_04.txt  myfile_06.txt  myfile_08.txt  myfile_10.txt  myfile_12.txt  myfile_14.txt  myfile_16.txt  myfile_18.txt  myfile_20.txt
dir2  myfile_01.txt  myfile_03.txt  myfile_05.txt  myfile_07.txt  myfile_09.txt  myfile_11.txt  myfile_13.txt  myfile_15.txt  myfile_17.txt  myfile_19.txt

Spaces Separate Arguments

$ ls -l myfile_01.txt myfile_02.txt
-rw-rw-r-- 1 jsoules jsoules 0 Jun  3 14:50 myfile_01.txt
-rw-rw-r-- 1 jsoules jsoules 0 Jun  3 14:50 myfile_02.txt

Words Are Worth a Thousand Pictures

Globbing

$ ls *
MainFile.idx   myfile_02.txt  myfile_04.txt  myfile_06.txt  myfile_08.txt  myfile_10.txt  myfile_12.txt  myfile_14.txt  myfile_16.txt  myfile_18.txt  myfile_20.txt
myfile_01.txt  myfile_03.txt  myfile_05.txt  myfile_07.txt  myfile_09.txt  myfile_11.txt  myfile_13.txt  myfile_15.txt  myfile_17.txt  myfile_19.txt

dir1:
 datafile_1.txt  'file with spaces.doc'   subdir1

dir2:
done

$ ls myfile_0*
myfile_01.txt  myfile_02.txt  myfile_03.txt  myfile_04.txt  myfile_05.txt  myfile_06.txt  myfile_07.txt  myfile_08.txt  myfile_09.txt

$ ls m*6*
myfile_06.txt  myfile_16.txt

Printing values

More glob characters

$ ls myfile_?6.txt
myfile_06.txt  myfile_16.txt
$ ls myfile_?[1,3,5].txt
myfile_01.txt  myfile_03.txt  myfile_05.txt  myfile_11.txt  myfile_13.txt  myfile_15.txt

Spooky action at a distance

$ ls dir1/subdir1
processed_22.data  processed_24.data  processed_26.data  processed_28.data  processed_30.data  processed_32.data  processed_34.data
processed_23.data  processed_25.data  processed_27.data  processed_29.data  processed_31.data  processed_33.data

Putting them together

$ mv dir1/subdir1/processed_* dir2/done/
$ mv myfile_0[1,3,5,7,9]* dir1/subdir1/
$ ls . dir1/subdir1/ dir2/done/
.:
dir1  MainFile.idx   myfile_04.txt  myfile_08.txt  myfile_11.txt  myfile_13.txt  myfile_15.txt  myfile_17.txt  myfile_19.txt
dir2  myfile_02.txt  myfile_06.txt  myfile_10.txt  myfile_12.txt  myfile_14.txt  myfile_16.txt  myfile_18.txt  myfile_20.txt

dir1/subdir1/:
myfile_01.txt  myfile_03.txt  myfile_05.txt  myfile_07.txt  myfile_09.txt

dir2/done/:
processed_22.data  processed_24.data  processed_26.data  processed_28.data  processed_30.data  processed_32.data  processed_34.data
processed_23.data  processed_25.data  processed_27.data  processed_29.data  processed_31.data  processed_33.data

This only scratches the surface!

and much more

CLI – Under the Shell

Most shell commands are just programs

$ which chromium
/snap/bin/chromium
$ /snap/bin/chromium # launches a web browser

And that’s exactly what happens if you double-click the file in a gui window.

$ which python3  # this lets me know if I'm in the virtual environment I expect
/usr/bin/python3
$ echo $PATH # $ means "retrieve the value of the variable"

Who Does What

$ LSVAR="ls -l"
$ echo $LSVAR  # just prints out the variable's contents
ls -l
$ $LSVAR    # will expand "ls -l" and then try to execute that
total 16
drwxrwxr-x 3 jsoules jsoules 4096 Jun  5 11:46 dir1
drwxrwxr-x 3 jsoules jsoules 4096 Jun  5 14:15 dir2
-rw-rw-r-- 1 jsoules jsoules   43 Jun  3 14:51 MainFile.idx
-rw-rw-r-- 1 jsoules jsoules    0 Jun  5 12:52 myfile_01.txt
-rw-rw-r-- 1 jsoules jsoules   22 Jun  5 14:17 myfile_02.txt
[...]
$ *  # this will expand to everything in the directory then treat those as commands
Command 'dir1' not found, did you mean:
  command 'dirb' from deb dirb (2.22+dfsg-5)
  command 'dir' from deb coreutils (8.32-4.1ubuntu1.2)
  command 'dirt' from deb dput-ng (1.34)
Try: sudo apt install <deb name>

$ myfile*  # matches all the myfile... files, but those files are not executable
bash: ./myfile_01.txt: Permission denied

Here the “permission denied” means that the file myfile_01.txt does not have execution permissions set on it. If it did, the shell would try to execute it.

This has security implications!

Process management

Aside: Essential Control Characters

Keys pressed with the control modifier usually have special meanings:

$ ls -la dir1/subdir1/ [C-a C-k]
$
$ [C-y]
$ ls -la dir1/subdir1/