Linux Commands 3
1. tmux
tmux
uses Ctrl-b
as a command prefix.
-
Starting.
tmux
-- startC-b t
thenq
-- show the timels -al
anddate
-- some commandsC-b ?
thenq
-- show key bindingsC-b d
-- detachtmux a
-- attach to the running session
tmux will keep the session even if we loose the connection to the server. When we reconnect we can attach the existing session.
-
We can have several "windows".
C-b c
-- create another windowC-b c
-- create a third oneC-b n
-- go to the next windowC-b p
-- go to the previous oneC-b 0
-- go to window 0C-b 1
-- go to window 1C-b w
-- select a window from the listC-b ,
-- rename the current windowC-b .
-- renumber the current window
-
Each window may be split into several "panes".
ls
-- run a commandC-b "
-- horizontal splitls -l
-- run a commandC-b %
-- vertical splitC-b Up
-- go to the pane aboveC-b Down
-- go to the pane belowC-b Left
-- go to the pane on the leftC-b Right
-- go to the pane on the rightC-b o
-- go to the next paneC-b o
-- go to the next paneC-b q
-- show pane numbersC-b q 2
-- go to the pane with this numberC-b Alt-Left
-- resize by 5 charsC-b Ctrl-Right
-- resize by 1 charC-b x
-- delete the current paneC-b f
-- find a pane/windowC-b d
-- detach
-
We can have several "sessions".
tmux
-- start another sessionC-b c
-- create a new windowC-b "
-- split horizontallyC-b %
-- split verticallyC-b s
-- select a sessionC-b w
-- select a windowC-b f
-- select a pane/windowC-b d
-- detachtmux ls
-- list sessionstmux a -t 0
-- attach to a specific sessionC-b $
-- rename sessionC-b s
-- select a session
-
Copy mode.
C-b [
-- start the "copy" mode, move around with arrowsC-b Space
-- start selection, move around with arrowsAlt-w
-- copy the selected textq
-- end copy modeC-b ]
-- paste the copied text
For more details see: https://linuxcommand.org/lc3_adv_termmux.php
2. Archiving (2)
-
By default,
tar
removes the leading/
from absolute filenames:echo $(pwd)/testdir
tar -cf testdir2.tar $(pwd)/testdir
tar -tf testdir2.tar | less
mkdir foo
tar -xf testdir2.tar -C foo/
tree foo -C | less -r
rm -rf foo -
We can extract only some files from the archive (not all the files):
mkdir foo
cd foo
tar -tf ../testdir.tar testdir/dir-001/file-A
tar -xf ../testdir.tar testdir/dir-001/file-A
tree
tar -xf ../testdir.tar testdir/dir-002/file-{A,B,C}
tree
cd .. ; rm -rf fooWe can also use
--wildcards
, like this:tar -xf ../testdir.tar \
--wildcards 'testdir/dir-*/file-A'
tree -C | less -r
cd .. ; rm -rf foo -
Sometimes it is useful to combine tar with find and gzip:
find testdir -name 'file-A'
find testdir -name 'file-A' \
-exec tar -rf testdir3.tar '{}' '+'
# the option 'r' is for appending files to an archive.
tar -tf testdir3.tar | less
find testdir -name 'file-B' \
-exec tar -rf testdir3.tar '{}' '+'
tar tf testdir3.tar | lessfind testdir -name 'file-A' \
| tar -cf - -T - \
| gzip > testdir.tgzThe first
-
makes tar to send the output to stdout instead of a file. The option-T
or--files-from
includes in the archive only the files listed in the given file. In this case we are reading the list of files from-
, which means the stdin and is the list of files coming from the command find. Then we are passing the output oftar
togzip
in order to compress it. -
We can also use the options
-z
or-j
to compress the archive:find testdir -name 'file-A' \
| tar -czf testdir.tgz -T -
ls -lh
find testdir -name 'file-A' \
| tar -cjf testdir.tbz -T -
ls -lh
3. netcat
Netcat is a simple tool for network communication.
Let's use it to listen to the port 12345
on server1
:
ssh server1
nc -l 12345
On the client, connect to that port on server1
like this:
nc 10.92.186.210 12345
Now, any line that you type here is sent and displayed to the other
terminal. Interrupt them with Ctrl-c
.
The test above shows that the TCP port 12345
on server1
is
accessible from the client (in case that there is a firewall, for
example).
For checking a UDP port we can add the option -u
to both of these
commands.
nc -u -l 12345 # on the server
nc -u 10.92.186.210 12345 # on the client
It can also be used as a simple tool for file transfer:
nc -l 12345 < file.txt # on the server
nc -w 3 localhost 12345 > /etc/passwd # on the client
As another example, let's combine it with tar
to transfer a whole
directory:
# on the server
mkdir cptest
cd cptest
nc -l 12345 | tar xzpf -
# on the client
cd testdir
ls
tar czpf - . | nc -w 3 localhost 12345