Linux, command line, scripting
Anirban Mandal, Erik Scott, Sajith Sasidharan (RENCI, UNC Chapel Hill)
Ewa Deelman, Karan Vahi, Mats Rynge (ISI, USC)
Matthew Miller, Werner Sun, Peter Ko, Kelly Nygren, Keara Soloway, Rolf Verberg (CHESS, Cornell)
Brandon Sorge (IUPUI)
You have your CLASSE accounts set up.
Options:
ssh
ssh ${username}@lnx201.classe.cornell.edu
lnx201
lnx201.classe.cornell.edu
.lnx201
.
lnx201
is a shared resource.Commands are either programs or shell builtins.
Use one of these commands to read documentation:
man ${command}
info ${command}
${command} --help
(sometimes!)bash
, which is the default on lnx201
.lnx201
, /bin/bash
is the program.info bash
or man bash
.history
command to list your shell history.Unix slogan: Everything is a file!
Command | Task | Example Syntax |
---|---|---|
ls |
list the files in a directory | ls [/tmp] |
cd |
move into a directory | cd [/tmp] |
pwd |
show curent working directory | pwd |
cp |
copy a file to into another directory, or make a copy with a different name | cp [file.txt] [/tmp/file.txt] |
cp -r |
copy a folder to into another directory | cp [file.txt] [/tmp/file.txt] |
mv |
rename or move a file into another directory | mv [file.txt] [file1.txt] |
rm |
delete a file | rm [file.txt] |
rm -r |
remove a directory, recursively | rm -r [dir] |
mkdir |
create a directory | mkdir [dir] |
find |
find a file | find [/tmp] -name [file] |
grep |
search for a text pattern inside a file | grep [text] [/tmp/file.txt] |
less |
to view the text of a text file, one screen at a time | less [/tmp/file.txt] |
exit |
exit and logout of a Terminal (Terminal-xfce4) session | exit |
[ssasidharan@lnx201 ~]$ tree -d -L 1 /
/
├── bin -> usr/bin
├── boot
├── cdat
├── cifs
├── cvmfs
├── dev
├── etc
├── home
├── lib -> usr/lib
├── lib64 -> usr/lib64
├── media
├── misc
├── mnt
├── net
├── nfs
├── opt
├── proc
├── root
├── run
├── sbin -> usr/sbin
├── srv
├── sys
├── tmp
├── usr
└── var
25 directories
lnx201
, this will be /home/$USER
$HOME
$WHATEVER
things$HOME
, $USER
, $PATH
, $SHELL
, etc.?printenv
or env
command to list them.ls
, files in current working directory will be listed, etc./home/$USER
aka $HOME
.cd
(change directory) to move around.pwd
to find where you are.
echo $PWD
.File/folder names are also referred to as paths.
/
.
/home/ssasidharan/Documents/hello.txt
./Documents/hello.txt
(or just Documents/hello.txt
) when I’m in my home directory.ls
.ls -a
(or ls --all
) to list them..
and ..
are special directory names.
.
stands for the current directory...
stands for the directory above the current directory._
(underscore character) instead (example: file_name
), or CamelCase (example: FileName
)."
and "
if they happen to have spaces, or “escape” each space with \
.Some characters are given special treatment:
*
matches any set of characters.?
matches any one character.stdin
).stdout
).stderr
).Note
They are files too: /dev/stdin
, /dev/stdout
, and /dev/stderr
.
You can redirect stdout
to a file with >
operator:
Or append with >>
:
To direct a file to a programs input, use <
operator:
|
(“pipe”) operator, you can “chain” programs such that one programs output is another programs input:Your account belongs to several groups:
Do a “long” file listing (with ls -l
) and behold:
$ ls -l
total 8
drwxr-xr-x 2 ssasidharan chess 44 May 8 10:42 bin
drwxr-xr-x 2 ssasidharan chess 144 Mar 12 00:27 CLASSE_shortcuts
drwxr-xr-x 2 ssasidharan chess 52 Apr 2 00:27 Desktop
drwxr-xr-x 2 ssasidharan chess 28 Apr 2 00:27 Documents
lrwxrwxrwx 1 ssasidharan chess 31 Mar 26 15:21 Downloads -> /cdat/tem/ssasidharan/Downloads
-rw-r--r-- 1 ssasidharan chess 54 Jun 2 12:38 hello.sh
drwxr-xr-x 2 ssasidharan chess 28 Apr 2 00:27 Music
drwxr-xr-x 2 ssasidharan chess 28 Apr 2 00:27 Pictures
drwxr-xr-x 2 ssasidharan chess 28 Apr 2 00:27 Public
drwxr-xr-x 2 ssasidharan chess 28 Apr 2 00:27 Templates
drwxr-xr-x 2 ssasidharan chess 28 Apr 2 00:27 Videos
What do those characters mean?
chmod
command to change file mode bits (the first column in the previous listing).[ssasidharan@lnx201 ~]$ chmod +x test.sh
[ssasidharan@lnx201 ~]$ ls -l test.sh
-rwxr-xr-x 1 ssasidharan chess 0 Mar 28 13:39 test.sh
[ssasidharan@lnx201 ~]$ chmod -x test.sh
[ssasidharan@lnx201 ~]$ ls -l test.sh
-rw-r--r-- 1 ssasidharan chess 0 Mar 28 13:39 test.sh
chown
and chgrp
commands to change owner and group (the third and fourth columns in the previous listing).
ps
command:The four columns:
PID
is process id.TTY
is the terminal associated with the process.TIME
is the elapsed CPU time for the process.CMD
is the command that created the process.&
:fg
command, and terminate it using Ctrl-CCtrl-C:kill PID
command to end one process.killall
command to end many processes.
kill
other user’s processes.Many choices! Use:
screen
and tmux
are two options. Here’s tmux
.
chmod +x hello.sh
./hello.sh
Bash supports a programming language with:
if
statementsfor
, while
, until
statements/etc/bashrc
and $HOME/.bashrc
for a tasteBUT
(Via Stephen Turner.)
https://xcitecourse.org/theme2/sf100/
Contributions are welcome!
Supported by the NSF awards OAC-2320373, OAC-2320374, and OAC-2320375.