Skip to main content

Linux Commands 3

1. tmux

tmux uses Ctrl-b as a command prefix.

  1. Starting.

    • tmux -- start
    • C-b t then q -- show the time
    • ls -al and date -- some commands
    • C-b ? then q -- show key bindings
    • C-b d -- detach
    • tmux 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.

  2. We can have several "windows".

    • C-b c -- create another window
    • C-b c -- create a third one
    • C-b n -- go to the next window
    • C-b p -- go to the previous one
    • C-b 0 -- go to window 0
    • C-b 1 -- go to window 1
    • C-b w -- select a window from the list
    • C-b , -- rename the current window
    • C-b . -- renumber the current window
  3. Each window may be split into several "panes".

    • ls -- run a command
    • C-b " -- horizontal split
    • ls -l -- run a command
    • C-b % -- vertical split
    • C-b Up -- go to the pane above
    • C-b Down -- go to the pane below
    • C-b Left -- go to the pane on the left
    • C-b Right -- go to the pane on the right
    • C-b o -- go to the next pane
    • C-b o -- go to the next pane
    • C-b q -- show pane numbers
    • C-b q 2 -- go to the pane with this number
    • C-b Alt-Left -- resize by 5 chars
    • C-b Ctrl-Right -- resize by 1 char
    • C-b x -- delete the current pane
    • C-b f -- find a pane/window
    • C-b d -- detach
  4. We can have several "sessions".

    • tmux -- start another session
    • C-b c -- create a new window
    • C-b " -- split horizontally
    • C-b % -- split vertically
    • C-b s -- select a session
    • C-b w -- select a window
    • C-b f -- select a pane/window
    • C-b d -- detach
    • tmux ls -- list sessions
    • tmux a -t 0 -- attach to a specific session
    • C-b $ -- rename session
    • C-b s -- select a session
  5. Copy mode.

    • C-b [ -- start the "copy" mode, move around with arrows
    • C-b Space -- start selection, move around with arrows
    • Alt-w -- copy the selected text
    • q -- end copy mode
    • C-b ] -- paste the copied text

For more details see: https://linuxcommand.org/lc3_adv_termmux.php

2. Archiving (2)

  1. 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
  2. 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 foo

    We can also use --wildcards, like this:

    tar -xf ../testdir.tar \
    --wildcards 'testdir/dir-*/file-A'
    tree -C | less -r
    cd .. ; rm -rf foo
  3. 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 | less
    find testdir -name 'file-A' \
    | tar -cf - -T - \
    | gzip > testdir.tgz

    The 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 of tar to gzip in order to compress it.

  4. 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

4. mc

See: https://linuxcommand.org/lc3_adv_mc.php