CLI Trivia

Post tutorials, HOWTO's and other useful resources here.
Bogomips
Full of knowledge
Full of knowledge
Posts: 2564
Joined: 25 Jun 2014, 15:21
Distribution: 3.2.2 Cinnamon & KDE5
Location: London

CLI Trivia

Post#1 by Bogomips » 06 Apr 2016, 18:09

Thought to Share some Recent Experiences (Some of the nature: Oh I didn't know you could do this)
Others, if they wish to, can also add their $0.02.

Moving Movie to USB (2.0)

Code: Select all

nice -n 20 mv Videos/Notting_Hill.mp4 /mnt/sdc2/Flm &
Running in the background also resulted in jamming up the system. So had to reduce priority in order to keep system functional.

CAUTION Using mv name.* target/directory Found recently to my cost when moving bunch of old movies, that if for some reason you omit target/directory,
  • one will be found for you: :twisted:

    Code: Select all

    guest@porteus:~$ cat /proc/filesystems > Movie.mp4       # Movie of several hundred MB (for example)
    guest@porteus:~$ touch Movie.en.srt                                  # Subtitle file of around 50 KB  (for example)
    guest@porteus:~$ ls -l Movie.*
    -rw-r--r-- 1 guest users   0 Apr 19 22:09 Movie.en.srt                             
    -rw-r--r-- 1 guest users 268 Apr 19 22:24 Movie.mp4
    
  • mv Movie.* [target/directory somehow omitted] expands with consequences:

    Code: Select all

    guest@porteus:~$ mv Movie.*
    guest@porteus:~$ ls Movie.*                                                        
    Movie.mp4                                                          
    
    Move of several hundred MB was instantaneous, and subtitles file has vanished!
  • mv Movie.mp4 target/directory Moving Just the movie is almost instaneous, and further investigation reveals that one has ended up with a useless subtitles file :evil:

    Code: Select all

    guest@porteus:~$ mv Movie.*  # had expanded to
    guest@porteus:~$ echo mv Movie.*                                                   
    mv Movie.en.srt Movie.mp4
    guest@porteus:~$ ls -l Movie.mp4 
    -rw-r--r-- 1 guest users 0 Apr 19 22:09 Movie.mp4
    
Porteus (useful for testing) Packages
  • tgz
    txz2xzm [/<Path to>/]<package>.tgz

    Code: Select all

    root@porteus:/home/guest# txz2xzm gecko-mediaplayer-1.0.9-x86_64-1alien.tgz
    ...
    Creating /home/guest/gecko-mediaplayer-1.0.9-x86_64-1alien.xzm
    
  • rpm
    rpm2xzm [/<Path to>/]<package>.rpm

    Code: Select all

    root@porteus:/home/guest# rpm2xzm p10/Por/tmp64/libicu-54.1-1.fc22.x86_64.rpm 
    ...
    Creating /tmp/rpm2xzm2070/libicu-54.1-1.fc22.x86_64.xzm
    root@porteus:/home/guest# ls p10/Por/tmp64/libicu-54.1-1.fc22.x86_64.xzm -lh
    -rw-r--r-- 1 root root 8.7M Apr  5 13:44 p10/Por/tmp64/libicu-54.1-1.fc22.x86_64.xzm
    
All Commands

Code: Select all

compgen -c | sort -u
donald wrote:@ Bogomips
You can also use the bash built-in compgen

compgen -c will list all the commands you could run.
compgen -a will list all the aliases you could run.
compgen -b will list all the built-ins you could run.
compgen -k will list all the keywords you could run.
and many more....see the man pages..
Edit in Place: sed -i

Code: Select all

guest@porteus:~$ sudo sed -i 's/taper/bear/' /etc/usm/mirrors-alien.txt 
guest@porteus:~$ cat /etc/usm/mirrors-alien.txt 
# List of alien Slackware mirrors
# Structure: $VER/$ARCH

# NETHERLANDS
http://bear.alienbase.nl/mirrors/people/alien/sbrepos
For example, could go into guiexec script, doing which will lend support to alienbob in the financial arena.

Code: Select all

root@porteus:/home/guest# usm -u alien
 Starting alien database update 
Downloading: vercheck.txt  DONE
Downloading: CHECKSUMS.md5.gz  DONE
Downloading: MANIFEST.bz2  DONE
Downloading: PACKAGES.TXT.gz  DONE
Downloading: LIBS.TXT.gz  DONE
File verification was good.
Optimizing manifest
 alien  database updated. 
Files Updated: Mon Apr 11 19:33:57 UTC 2016

Rava's cat trick (demonstrated for script to get version of package on the system)
cat > file
Data read from stdin
^D or ^C signifies end of data:

Code: Select all

cat > gsv
# Get Slackware Version
gsv () 
{ 
    local s;
    local t;
    s=${1##*/};
    s=${s%-*-*};
    s=${s##*-};
    s=$(expr "$s" : '\([0-9.]*\)');
    s=${s%.};
    t=${1##*-};
    t=$(expr "$t" : '\([0-9]*\)');
    echo $s-$t
}
# Version of Package on System
ver () 
{ 
    local s;
    local a;
    local w=/var/log/packages/$1*;
    for s in $w;
    do
        [[ "${s##*\*}" == "" ]] && return;
        a=${s##*/};
        [[ ${a%-*-*-*} != $1 ]] && continue;
        echo $(gsv $s);
    done
}
^D or ^C  Terminates Input
Ending up with a file: /home/guest/gsv. Thereafter from any terminal tab:

Code: Select all

source gsv;  # Needed just the once for a terminal tab
guest@porteus:~$ ver flashplayer-plugin
11.2.202.577-1
as an example.

Initialising a File: echo -n > file

Code: Select all

guest@porteus:~$ cat /proc/filesystems > fs                                        
guest@porteus:~$ ls -l fs                                                          
-rw-r--r-- 1 guest users 268 Apr 11 21:19 fs                                       
guest@porteus:~$ echo -n > fs                                                      
guest@porteus:~$ ls -l fs                                                      
-rw-r--r-- 1 guest users 0 Apr 11 21:19 fs     
donald wrote:"Initialising a File: echo -n > file"
or simply
> filename
I use it to quickly empty log files while they are in use.
e.g.
root@localhost:~# > /var/log/messages
8)

Code: Select all

guest@porteus:~$ cat /proc/filesystems > fs
guest@porteus:~$ ls -l fs
-rw-r--r-- 1 guest users 268 Apr 13 17:05 fs
guest@porteus:~$ >fs
guest@porteus:~$ ls -l fs
-rw-r--r-- 1 guest users 0 Apr 13 17:05 fs
Appending Line to File or Creating new Single Line File: echo line >> file

Code: Select all

guest@porteus:~$ ls -l fs                                                      
-rw-r--r-- 1 guest users 0 Apr 11 21:19 fs 
guest@porteus:~$ echo tmpfs                                   on  /dev/shm                          type         tmpfs  >>  fs                         
guest@porteus:~$ ls -l fs                                                          
-rw-r--r-- 1 guest users 29 Apr 11 21:26 fs                                        
guest@porteus:~$ cat fs                                                            
tmpfs on /dev/shm type tmpfs               
Creating or Overwriting with Single Line of Data: echo line > file

Code: Select all

guest@porteus:~$ echo   exec dbus-launch --exit-with-session /usr/bin/startxfce4 > fs
guest@porteus:~$ cat fs                                                          
exec dbus-launch --exit-with-session /usr/bin/startxfce4   
Directories

COMPARE DIRECTORIES Update of: 15.09.2016
  • Code: Select all

    diff  -qr  dir1  dir2
  • Code: Select all

    guest@porteus:~$ diff -qr /mnt/sda4/cnrc_rc3/  /mnt/sda4/cin_rcp/
    Only in /mnt/sda4/cin_rcp/etc/rc.d: rc.S
    Files /mnt/sda4/cnrc_rc3/etc/rc.d/rc.local and /mnt/sda4/cin_rcp/etc/rc.d/rc.local differ
    Only in /mnt/sda4/cin_rcp/etc/rc.d: rc.local_shutdown
    diff: /mnt/sda4/cnrc_rc3/etc/sudoers: Permission denied
    diff: /mnt/sda4/cin_rcp/etc/sudoers: Permission denied
    Only in /mnt/sda4/cin_rcp/home/guest/.config: geany
    Files /mnt/sda4/cnrc_rc3/home/guest/Prolog/pmx.sh and /mnt/sda4/cin_rcp/home/guest/Prolog/pmx.sh differ
    Files /mnt/sda4/cnrc_rc3/home/guest/Prolog/rc.local and /mnt/sda4/cin_rcp/home/guest/Prolog/rc.local differ
    Files /mnt/sda4/cnrc_rc3/home/guest/Prolog/rc_bak.local and /mnt/sda4/cin_rcp/home/guest/Prolog/rc_bak.local differ
    Files /mnt/sda4/cnrc_rc3/home/guest/Prolog/usm.conf and /mnt/sda4/cin_rcp/home/guest/Prolog/usm.conf differ
    Only in /mnt/sda4/cin_rcp/home/guest/Prolog: usm_rc4.conf
    
File System
  • Number of Files in Filesystem
    • Code: Select all

          sudo  find  /  -xdev  -printf "\n" | wc -l
    • Code: Select all

      guest@porteus:~$ sudo find /mnt/sda4 -xdev -printf "\n" | wc -l
      132175
      guest@porteus:~$ sudo find /mnt/sda8 -xdev -printf "\n"  | wc -l
      13
      guest@porteus:~$ sudo find /mnt/sda8 -xdev -printf "\n" | od -cx
      0000000  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n  \n
                 0a0a    0a0a    0a0a    0a0a    0a0a    0a0a    000a
      
Live Running Updated: Sun Apr 24 2016
    • If running in Always Fresh mode (without saving changes), makes no sense to update a data base on the live system, especially if it means swap being used for this purpose, which means, in my case, the disk being accessed non-stop for 3 minutes or more, every once in a while. Avoid thus:

      Code: Select all

      # Suspend 'updatedb'
      rm /etc/cron.daily/slocate
      
      which would best go in /etc/rc.d/rc.local
      • If strapped for ram, watching flash video using broadband connection, could mean flash using up all available ram for its cache, some 10-15 minutes into an hour long video.

        Finally ascertained, with the help of the net, caching as taking place in /tmp. Redirecting /tmp to a real filesystem resolves issue:

        Code: Select all

        mkdir   /mnt/sdXy/Path/to/tmp
        chmod  1777 /mnt/sdXy/Path/to/tmp   # Making a /tmp compatible directory.         
        Above commands only needed if no suitable /tmp available on real filesystem.

        Another point from the web was that redirection is best done before X starts. So either

        Code: Select all

        mount --bind /mnt/sdXy/Path/to/tmp  /tmp
        in /etc/rc.d/rc.local or append the boot parmeter cliexec=mount~--bind~/mnt/sdXy/Path/to/tmp~/tmp, the latter been using so far without incident.
      Check the latest release of kernel with: check-kernel 27.09.2016
      Last edited by Bogomips on 31 Jan 2017, 16:43, edited 14 times in total.
      Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
      NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

      donald
      Full of knowledge
      Full of knowledge
      Posts: 2064
      Joined: 17 Jun 2013, 13:17
      Distribution: Porteus 3.2.2 XFCE 32bit
      Location: Germany

      Re: CLI Trivia

      Post#2 by donald » 07 Apr 2016, 07:51

      What is currently mounted (file systems), where and how, in a nice layout.
      (enlarge / maximize the terminal window)

      Code: Select all

      guest@localhost:~$ mount | column -t
      Execute Previous Command:
      You can search for it by pressing the Up key on your keyboard,
      or use the history command instead.

      Code: Select all

      guest@localhost:~$ history
      You will get a list and each line has a number.
      To execute one of them simply type e.g. !12
      (the ! character + the number of the command)

      supplemental info from Bogomips

      Events Designator

      NOTE:
      Any one only working as root would be well advised to read about the "p = safe mode" Modifier.

      An events designator '!string?' refers to a command line containing 'string'.
      also '!?string?' refers to a command line containing (not necessarily starting with) 'string'.
      Example:
      $ awk --help
      $ perl --help
      The events designator '!p' or '!perl' or '!per' will execute the 'perl --help' command.
      Similarly, '!a' will execute the awk command.

      Another events designator is '^string1^string2^' which takes the last command,
      replaces string1 with string2 and executes it.
      Example:

      Code: Select all

      $ ehco foo bar baz
      bash: ehco: command not found
      $ ^ehco^echo^
      foo bar baz
      The '^ehco^echo^' designator has replaced the incorrectly typed 'ehco' command
      with the correct 'echo' command and executed it.

      Modifier
      ( Follows events designator separated by a colon.)

      A Safe Mode 'p' modifier prints the resulting command after history expansion but does not execute it.

      Code: Select all

      guest@porteus:~$ sudo less /var/log/syslog
      guest@porteus:~$ !!:p
      sudo less /var/log/syslog
      guest@porteus:~$ !le:p
      less /var/log/syslog
      Up Arrow to Edit and/or Execute printed Command.

      Example of !string:p !?string:p ^string1^string2^:p (Last useful when statement very long.)

      Code: Select all

      guest@porteus:~$ less /var/log/syslog
      guest@porteus:~$ sudo less /var/log/syslog
      guest@porteus:~$ !le:p
      less /var/log/syslog
      
      guest@porteus:~$ echo Now is the time for all god men to come to the aid of the party!
      Now is the time for all god men to come to the aid of the party!
      guest@porteus:~$ dconf load /org/cinnamon/desktop/keybindings/wm/ < p10/Por/cinwm_prf
      guest@porteus:~$ cp usm.conf p1/Por/cn32
      
      guest@porteus:~$ !?aid?:p
      echo Now is the time for all god men to come to the aid of the party!
      guest@porteus:~$ ^god^good^:p
      echo Now is the time for all good men to come to the aid of the party!
      guest@porteus:~$ echo Now is the time for all good men to come to the aid of the party!
      Now is the time for all good men to come to the aid of the party!
      This one is for newbies.
      It will generate random man pages; so you will always get something to learn
      and never get bored.

      Code: Select all

      man $(ls /bin | shuf | head -1)
      press q to exit.

      "save" the current path in order to easily return there later.
      You're in a directory and need to go elsewhere
      but don't want to lose where you were, use pushd instead of cd.
      Example:

      Code: Select all

      current directory:
      guest@localhost:~$ /some/long/and/complicated/path/i/dont/want/to/forget/
      now use pushd instead of cd:
      guest@localhost:~$ pushd /where/you/want/to/go
      Afterwards you can continue navigating with "cd".
      once you need to go back:

      Code: Select all

      guest@localhost:~$ popd
      It returns you to /some/long/and/complicated/path/i/dont/want/to/forget/

      btw
      One might say 'cd -' is the same, no it's not, as it only remembers the last directory you were in.
      if you want to "save" a directory long term then push/pop is the way to go.
      Last edited by donald on 17 Apr 2016, 14:23, edited 4 times in total.

      Bogomips
      Full of knowledge
      Full of knowledge
      Posts: 2564
      Joined: 25 Jun 2014, 15:21
      Distribution: 3.2.2 Cinnamon & KDE5
      Location: London

      Re: CLI Trivia

      Post#3 by Bogomips » 11 Apr 2016, 00:10

      NB Both previous posts have had more added to them. :)
      Linux porteus 4.4.0-porteus #3 SMP PREEMPT Sat Jan 23 07:01:55 UTC 2016 i686 AMD Sempron(tm) 140 Processor AuthenticAMD GNU/Linux
      NVIDIA Corporation C61 [GeForce 6150SE nForce 430] (rev a2) MemTotal: 901760 kB MemFree: 66752 kB

      Post Reply