title | author | date | lang |
---|---|---|---|
A second look at the shell |
CSC Training |
2020-04 |
en |
In this session, we dive a little deeper into shell commands and then discuss a series of commands you might be in need of during your work on your own Linux desktop, but also on remote machines. We cover issues like
- finding files
- searching contents inside files
- managing disk space
- secure connection
- archiving of files
- Text only user interface
- Optimized to work with files --- and everything is a file (text) in Linux
- Programmable User Interface
- Bash is a Turing-complete programming language
- (With some practice) It is easy to write new small programs/commands, scripts
- That is why it is used in professional computing: Be a programmer, not a computer!
- Bash will ...
- expand variables and file name wildcards (globbing)
- split the line into “words”
- connect files and pipes
- interpret the first word as the name of the command, and the rest of the words as the arguments to the command
- Let's break down the following command:
$ echo "Hello!" > foo.txt
stdin -> | echo |
"Hello!" |
> foo.txt |
---|---|---|---|
input from other program or keyboard; not used here | first word = command | argument to command | stdout into foo.txt , stderr to terminal, exit code (0=success, else fail) |
- Lets assume we (want to) have a file with a space (like often in Windows)
- We want to fill the file
Operating System.txt
with the wordsLinux
$ echo "Linux" > Operating System.txt
- What happens when you make a listing of the directory?
- Instead of adding spaces into your filename, you could use snake_case or CamelCase style, i.e.,
Operating_system.txt
orOperatingSystem.txt
- If you insist, use either single quotes or escape
\
character:
$ echo "Linux" > 'Operating System.txt'
$ echo "UNIX" >> Operating\ System.txt
- The hard way:
cd
yourself trough the file-tree followed byls
- The easy way:
$ find /etc -name "*.conf" -print
- searches directory (here `/etc`) recursively and prints matching filenames (here all files with suffix `.conf`)
- this is a real-time search on the disk, hence produces heavy I/O
- The correct way:
$ locate *.conf
- accesses a database and prints matching files
- database is not real-time (can miss new files)
- Search for the occurrence of the word network in any file under
/etc/init.d
$ grep -i network /etc/init.d/*
- `-i` makes search case-insensitive (i.e., it will find `network`, `Network` and `NetWork`)
- Recursively search whole directory-tree (here
/etc
)
$ grep -r network /etc
- getting rid of error messages (redirecting **stderr** by `2>`)
$ grep -r network /etc 2>/dev/null
- everything you send into the device `/dev/null` will disappear
- Piping with
|
for whole workflow of commands:
$ grep -r network /etc 2>/dev/null | grep start | less
- finds any line with
network
under/etc
-tree (ignoring stderr) and then selects those with the wordstart
in the same line and pages (less
) result on screen - Pipes direct the stdout of the previous into the stdin of the following command
- Exercise: try to find out whether stderr is passed on by a pipe
- Rule of life: Computer disk-space always is too small (no matter how much you buy)
- How much space is there left on my devices?
$ df -h
- option
-h
for human readable output
Filesystem Size Used Avail Use% Mounted on
udev 461M 0 462M 0% /dev
tmpfs 99M 1,1M 98M 2% /run
/dev/sda5 11G 7.4G 2.3G 73% /
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 493M 0 493M 0% /sys/fs/cgroup
tmpfs 796M 64K 796M 1% /run/user/1000
- Identifying disk-space consumption of subdirectories (here in
/usr
):
$ du -sh /usr/*
- options
-h
for human readable output and-s
for sum (else it would step into each subdirectory)
392M /usr/bin
44K /usr/games
227M /usr/include
3.8G /usr/lib
832K /usr/libexec
172K /usr/local
45M /usr/sbin
2.7G /usr/share
940M /usr/src
- Secure Shell (SSH):
$ ssh –X [email protected]
- option
-X
(or–Y
) allows tunneling application windows so that they open on your local machines screen. - instead of writing the username in front of the address, you can give it as option
-l username
- Exercise: With the information given on the course-page, log into the target computer (replace
csctrngNN
with the username andvmXXXX
with the computer-name given in course pages!) usingssh
-command
$ ssh –X [email protected]
- To proof that you were there, create an empty file (there was a command for this!) in the home-directory that contains your given and your family-name separated by a space, so something like: `Elvis Presley.txt` (remember, to correctly use the escape character to get the space)
- You can use a browser, but ...
wget
allows to download URL's in the shell
$ wget http://ftp.gnu.org/gnu/hello/hello-2.7.tar.gz
- mind that some URL's can produce weird download names (option
-O
might help) - Exercise: verify with
ls
, whether the creation date of the downloaded file is preserved or not.
scp
(secure-copy) copies your files encrypted between different machines- copying to remote machine (replace
csctrngNN
with the username andvmXXXX
with the computer-name given in course pages!)
$ scp hello-2.7.tar.gz [email protected]:'~/'
- Exercise: Explain, where on
vmXXXX.kaj.pouta.csc.fi
will you find the file? - copying from remote machine
$ scp [email protected]:'~/hello*.tar.gz' .
tar
is one of the oldest commands and stands for tape archive. It takes whole directories and puts their files sequentially (like on a tape) in a single file.- because of its legacy, the options (
t
=text/contents,c
=create,x
=extract,v
=verbose,z
=compress withgzip
,f
=file) can be given without dash-
- list (don't extract) contents
$ tar tvzf hello-2.7.tar.gz
- unpack the file with
$ tar xvzf hello-2.7.tar.gz
- create a new (now uncompressed)
tar
archive
$ tar cvf myownhello-2.7.tar hello-2.7/*
- because of its legacy, the options (
- Exercise: Make a listing showing the size of
hello-2.7.tar.gz
and compare it to the summed size of the newly unpacked subdirectoryhello-2.7
(we learned this commands a slides ago). Also compare it to the size of themyownhello-2.7.tar
. Check the following slide to explain the difference in sizes
- In fact, the file
hello-2.7.tar.gz
is a gnu-zipped tar archive. The compression feature is built intotar
and can be issued withz
option gzip
is the GNU version ofzip
(you might know from Windows).- To obtain a pure uncompressed
tar
archive:
$ gzip -d hello-2.7.tar.gz
- this decompresses and removes the suffix `.gz`
- you could use `gunzip` instead of `gzip -d`, where the option stands for *decompress*
- to create a new `gzip` file (`-9` means highest level of compression)
```bash
$ gzip -9 myownhello-2.7.tar
```
- My colleague sent me a Windows ZIP file, what should I do?
- There is a ZIP-compatible command on Linux/UNIX
- To create
hello.ZIP
recursively (-r
) from the subdirectoryhello-2.7
$ zip -r hello.ZIP hello-2.7/
- To extract
$ unzip hello.ZIP
- To only list contents
$ unzip -l hello.ZIP
- We barely scratched the surface of possible commands in Linux/UNIX.
- With the presented set you can survive
- But perhaps you want to go beyond this level?
- There are commands you can do in the shell that basically replace spread-sheet functionality, like pasting, cutting columns and rows, etc. (
head
,tail
,cut
,paste
) - Even programmable text-editors (
sed
andawk
) exist - If you work on scientific (usually tabular) data, you have an operating system that delivers at its user interface (no additional programs needed)
- There are commands you can do in the shell that basically replace spread-sheet functionality, like pasting, cutting columns and rows, etc. (