1 Bash: fist steps
greenleaves edited this page 2021-12-09 20:05:06 +00:00
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head>

</head>

Variables

System variables are stored in users environment

  • $PATH Finds the content of a variable ("PATH")
  • "env" variables defined in shell environment

Add directory to path

"Environment variables" in: Arch wiki

To add a directory to the PATH for local usage, put the following in "~/.bashprofile":

export PATH="${PATH}:/home/my_user/bin"

This will add the directory at the end of $PATH. To update the variable, re-login or source the file:

source ~/.bash_profile

Then check directories in "$PATH"

echo $PATH

Adding a new user

Check users that are logged

users

New user called "test"

sudo useradd -p -m test

The configuration file "useradd" sets options for this command:

sudo less /etc/default/useradd

PS: should use "sudo" to see the file. And less() or more() will show the file

The following file contains a list of fields about each user:

head -4 /etc/passwd

#RESULTS:

root0:0::/root:/bin/bash
bin1:1::/:/sbin/nologin
daemon2:2::/:/sbin/nologin
mail8:12::/var/spool/mail:/sbin/nologin

PS: head() is used to show only the first four lines.

Describe hardware

Here using piping to show only the first four lines in the output

lshw | head -4

#RESULTS:

myuser    
description: Computer  
width: 64 bits
capabilities: smp vsyscall32

Execute stuffs

Streaming from terminal

To watch a stream use video player such as 'mpv' or 'vlc' that supports streaming And pass the streaming address as argument to the name of the media player

mpv https://live0.emacsconf.org/main.webm

Run a bash script

Write script

After writing, save the file with '.sh' extension. This script will echo the words within quotes

#!/bin/bash
echo "Hellow hu-hu"

#RESULTS:

Hellow hu-hu

Option a: make it executable

Use the command "chmod" to change permissions for the file

chmod +x script.sh

Then, execute

./script.sh

Option b: run from terminal

bash script.sh

Or

sh script.sh

PS: to execute the script from any place in the machine include the script in a directory that can be read from $PATH

Option c: run from graphical interface

Manuals

Use the command man or info followed by the name of the function or program

man

#RESULTS:

What manual page do you want?
For example, try 'man man'.
man man
man hier

Showing only the first four lines

info | head -4

#RESULTS:

File: dir, Node: Top, This is the top of the INFO tree.
                       
This is the Info main menu (aka directory node).      
A few useful Info commands:              
info bash

Show command history

history

The file can be found in the user home:

/home/user/.bash_history

Translators and dictionaries

Apertium translator

Installing in Parabola GNU-linux

sudo pacman -S apertium-eng-spa-git
sudo pacman -S apertium-lex-tools
sudo pacman -S apertium-viewer-2.5.3-1

Apertium can be used in the command line

echo Hola | apertium spa-eng
echo Hello | apertium eng-spa

"|" pipe lets the input to be passed to apertium

Files and directories

Create empty file

Create "file.txt" with command touch

touch file.txt

Create a file redirecting the shell output using ">" operator

echo I will put this text wthin a file > file2.txt

List files

Use ls in the directory you want to explore

ls

List with properties using options -l and -la

ls -l
ls -la

List using wildcards

List all files ending in ".csv"

ls *.csv

List all files containing the characters "moda" within name, e.g., "acomoda", "comoda.txt"…

ls *moda*

Remove "x"

rm x

Copy a file using "cp source destination"

cp /home/text.csv  /home/myuser/text.csv

Copy multiple files, directories or disk using "rsync" (source, destination). The command accepts arguments:

  • "-r" recursive
  • "-v" verbose
  • "-a" archive (keeps info about files)
rsync -rav /home/user/Documents /run/media/user/disk/backup/

rsync can be also used with selective copyng based on file type

rsync /home/user/Desktop/*.jpg /home/user/Desktop/backupdata/

Create directory

mkdir new_dir

Removes directory

rm -r new_dir

Removes a file

rm file.txt

Space used by a directory

du -hs /usr

Display directory

pwd

Change directory you are working from terminal.

Go to the home of the user

cd

Go one level up in the directory tree

cd ..

Go to "Documents" directory

cd /home/myuser/Documents

Use rmlint to search for duplicated or empty files

Install

sudo pacman -S rmlint

Run in the directory you want to check for duplicated or empty files

rmlint -g

This will create two files:

  • rmlint.sh
  • rmlint.json

The shell script can be used to delete duplicated files

OPTIONS:

  • h Show this message.
  • d Do not ask before running.
  • x Keep rmlint.sh; do not autodelete it.
  • p Recheck that files are still identical before removing duplicates.
  • r Allow deduplication of files on read-only btrfs snapshots. (requires sudo)
  • n Do not perform any modifications, just print what would be done. (implies -d and -x)
  • c Clean up empty directories while deleting duplicates.
  • q Do not show progress.
  • k Keep the timestamp of directories when removing duplicates.
  • i Ask before deleting each file
chmod +x rmlint.sh
bash -xcp rmlint.sh

Convert pdf to text or html using poppler

Use poppler and redirect the text to a org file. pdftotext converts a pdf to "txt" if no other format is specified

pdftotext foo.pdf foo
pacman -S poppler
pdftotext foo.pdf ->> foo.org # first option
pdftotext foo.pdf foo.org # second option

Convert pdf to html

pfdftohtml foo.pdf foo.html

Convert files using pandoc

Use pandoc with "-s" flag to produce a standalone document and "-o" to redirect output to a file.

In the example a file "foo" is converted from odt to org

pandoc -s -o foo.org foo.odt

Convert files to pdf using libreoffice

Convert odt file "tclonline3.odt" to pdf

$ libreoffice --headless --convert-to pdf tcl_online3.odt

Using 'sed' to substitute text

"sed" is a stream editor for filtering and transforming text"…

This command in emacs editor

M-x woman [RET] sed

Visits the following file with info about sed

usr/share/man/man1/sed.1.gz

A brief example: I have a long comma separated text file with strings and need to include a line break after each comma (\n)

cat filetest

#RESULTS:

'string1', 'string2', 'string3'

Using sed and redirecting the output to "filetest1"

$ sed 's/\,/&\n/g' filetest > filetest1

#RESULTS:

'string1',
'string2',
'string3'

Send files to another machine with "croc"

sudo pacman -S croc

Then run:

croc send clown.txt

This will give a code

> Code is 34-04-paprika-sugar-micro
> On the other computer run
> croc 34-04-paprika-sugar-micro

More info about croc in Github repo

Move or rename

Use "mv source destination" command

mv  /home/myuser/text.txt /home/myuser/Documents/text.csv

Example: moving the ".config" files to ".config3", then moving ".config1" to ".config" to arrange configuration archives.

This creates a copy of each file in the same directory

pwd # check where are you now
# moving files
mv file1.txt file3.txt

Moving multiple files can be possible. Selecting all the files ending in ".mp3" within Downloads

mv ~/Downloads/*.mp3 ~/Music/

Moving multiple files included within a text file. Files are included in "filesmv2.txt" and are moved to "tmpfiles" directory. "t" argument stands for "target directory"

Each line in the "txt" file looks like this: '/home/user/file1.pdf' # original

$ cat filesmv2.txt | xargs mv -t ~/tmpfiles

Source: stackexchange

Moving multiple files using wildcards. This moves all files containing the sequence "nasa" in the middle of the name. '*' indicates that there are characters or numbers before and after the word "nasa"

mv *roco* -t ./direct

Flashing USB with "dd"

"dd" (convert and copy a file) is useful to copy information from a disk. It can be used to create bootable USB. An example with parabola:

sudo dd if=/home/user/Documents/parabola-2016.11.03-dual.iso of=/dev/sdb1

The ISO image is copied to "sdb1" (USB volume)

More files with examples of use of "dd" [../bash/ddcommand.org][here]] and here

Display files

Show content of a file. Concatenates and writes file. Using "tac" reverses the result of "cat" command

cat file.txt

Using head, tail and less to show parts of files

Showing the first two entries

head -n 2 /etc/hosts

#RESULTS:

127.0.0.1 localhost
127.0.1.1 myuser

Showing the last two entries

tail -n 2 /etc/hosts

#RESULTS:

ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
less /etc/hosts

#RESULTS:

127.0.0.1 localhost    
127.0.1.1 myuser    
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes    
ff02::2 ip6-allrouters    

Backup a file

Using "cp"

cp bbdb{,.bak}
bbdb.bak

Backups with tar (tape archiver)

Back up of "home"

  • c = create file
  • v = verbose
  • f = write to a file/device
  • z = compress the file (gzip) "tar.bz"
  • j = compress the file (bzip2)
tar -czvf /tmp/home.tar.gz /home

Simple code to encrypt

Encrypt:

gpg -c X
gpg X

Use the code to remove the original file:

shred --remove X

Searching

Print lines that match patterns

grep

Example: searching for all entries that have "root"

grep 'root' /etc/passwd

#RESULTS:

root:x:0:0::/root:/bin/bash

Download files

This code download the file and the argument -O is to rename the file

wget https://filetodownload.org -O new_file

Devices

  • mount /what /where
    • what = device name
    • where = directory
      • /mnt = devices that mount occasionally
      • /media = devices that mount frequently
run/$USER/media/$LABEL

List process

Use ps, top or htop to list process:

  • ps reports a snapshot of the current process
ps aux | less
  • top display dynamic real-time view of linux process
top
  • htop displays an interactive process viewer
htop

To kill a process use:

kill [PID]

or

pkill [PID]

PID = process identificator

Evaluate time to load

In emacs:

time emacs --eval '(save-buffers-kill-terminal)'

Cool commands

cal # calendar
clear # clear output
uname # display system information (-r, -p, -a)
wc # wordcount (file name)
date # check the date
</html>