how tosplit one text file into multiple *.txt files

September 18, 2018 - Reading time: 4 minutes

You can use the Linux Bash core utility split:

split -b 1M -d  file.txt file

Note that M or MB both are OK but size is different. MB is 1000 * 1000, M is 1024^2

If you want to separate by lines you can use -l parameter.

Update:

a=(`wc -l yourfile`) ; lines=`echo $(($a/12)) | bc -l` ; split -l $lines -d  file.txt file

Another solution as suggested by Kirill, you can do something like the following

split -n l/12 file.txt

Note that is l not one, split -n has a few options, like N, k/N, l/k/N, r/N, r/k/N.

Using Bash:

readarray -t lines < file.txt
count=${#lines[@]}

for i in "${!lines[@]}"; do
    index=$(( (i * 12 - 1) / count + 1 ))
    echo "${lines[i]}" >> "file${index}.txt"
done

Using AWK:

awk '{
    a[NR] = $0
}
END {
    for (i = 1; i in a; ++i) {
        x = (i * 12 - 1) / NR + 1
        sub(/\..*$/, "", x)
        print a[i] > "file" x ".txt"
    }
}' file.txt

Unlike split, this one makes sure that the number of lines are most even.


reload .bashrc settings without logging out and back in

August 10, 2018 - Reading time: 4 minutes

You can enter the long form command:

source ~/.bashrc

or you can use the shorter version of the command:

. ~/.bashrc

or you could use:
exec bash

To complement and contrast the above commands with . ~/.bashrc and exec bash:

Both solutions effectively reload ~/.bashrc, but there are differences:

  • . ~/.bashrc or source ~/.bashrc will preserve your current shell session:

    • Except for the modifications that reloading ~/.bashrc into the current shell (sourcing) makes, the current shell process and its state are preserved, which includes environment variables, shell variables, shell options, shell functions, and command history.
  • exec bash, or, more robustly, exec "$BASH"[1], will replace your current shell with a new instance, and therefore only preserve your current shell's environment variables (including ones you've defined ad hoc, in-session).

    • In other words: Any ad-hoc changes to the current shell in terms of shell variables, shell functions, shell options, command history are lost.

Depending on your needs, one or the other approach may be preferred.


[1] exec bash could in theory execute a different bash executable than the one that started the current shell, if it happens to exist in a directory listed earlier in the $PATH. Since special variable $BASH always contains the full path of the executable that started the current shell, exec "$BASH" is guaranteed to use the same executable.
A note re "..." around $BASH: double-quoting ensures that the variable value is used as-is, without interpretation by Bash; if the value has no embedded spaces or other shell metacharacters (which is not likely in this case), you don't strictly need double quotes, but using them is a good habit to form.


how to download specific files from some url path with wget

July 11, 2018 - Reading time: ~1 minute

wget -r -l1 --no-parent -A ".deb" http://www.shinken-monitoring.org/pub/debian/

-r recursively
-l1 to a maximum depth of 1
--no-parent ignore links to a higher directory
-A "*.deb" your pattern


generate a list of a site's URLs using wget

June 12, 2018 - Reading time: ~1 minute

You can use wget to generate a list of the URLs on a website.

Spider example.com, writing URLs to urls.txt, filtering out common media files (css, js, etc..):

wget --spider -r http://www.example.com 2>&1 | grep '^--' | awk '{ print $3 }' | grep -v '\.\(css\|js\|png\|gif\|jpg\|JPG\)$' > urls.txt

Note that this gives a list that duplicates URLs.

If you mirror instead of spider you seem to get a more comprehensive list without duplicates:

wget -m http://www.example.com 2>&1 | grep '^--' | awk '{ print $3 }' | grep -v '\.\(css\|js\|png\|gif\|jpg\|JPG\)$' > urls.txt

This will download all pages of the site into a directory with the same name as the domain.


switch monitors from the command line

February 20, 2018 - Reading time: ~1 minute

With the commands

xrandr --output VGA-0 --auto
xrandr --output LVDS --off 

The screen automatically transfers to the external display. It doesn't even need sudo powers. To find out the name of the displays just do:

xrandr -q

Which should give something like:

VGA-0 connected 1280x1024+0+0 (normal left inverted right x axis y axis) 338mm x 270mm
...
LVDS connected (normal left inverted right x axis y axis)
...

Extending the displays can probably be achieved in a similar manner.


export a recursive directory & file listing to a text file in Linux Bash shell with an SSH command

June 23, 2017 - Reading time: 3 minutes
find is a good utility to obtain (recursively) all the content of a directory. If you want the file (and directory) names with their permissions:
find /home/kparisi -printf "%M %p\n"

You can then use ssh to run this command on the remote server:

ssh kparisi@remote.com 'find /home/kparisi -printf "%M %p\n"'

And finally, if you want to store this in a file on your local server:

ssh kparisi@remote.com 'find /home/kparisi -printf "%M %p\n"' > file 

Alternatively:

tree > fsstruct.txt

for me this format is simpler to read.

It is easy also to use other features of tree:

tree -p > fsstruct.txt

prints file type and permissions before file and it is more accessible when you are reading plain text, which is a file and which is a directory.

tree -h > fsstruct.txt

this prints sizes of files in a human readable format:

tree -ph > fsstruct.txt

Also, it is possible to send output to the file:

tree -ph . -o fsstruct.txt

or create HTML:

tree -Hph . -o tree.htm