Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

How to create banner in bash


Install figlet

  
sudo apt update
sudo apt install figlet
    
figlet is a fun command-line tool that turns regular text into large ASCII art-style letters. It’s mostly used for decoration in terminal scripts, welcome messages, or just to make things look cooler in the shell.

Bash Code

  
#!/bin/bash
# Define colors for printouts
GREEN="\033[1;32m"
BLUE="\033[1;34m"
YELLOW="\033[1;33m"
RESET="\033[0m"
CYAN="\033[1;36m"
MAGENTA="\033[1;35m"

# Impressive entry dialog
clear
echo -e "${BLUE}"
figlet -c 'PARKLIZE'
echo -e "${RESET}"
sleep 3  # Pause for a few seconds to allow the user to read the banner
    

How to get the size of each sub directories and sort them by size?

 



To get the size of each sub directory, we can use the directory usage (du) command.

For example, how to get the size of sub-directory with max-depth 1?

how to get the size of sub-directory with max-depth 1?

  
$ du -d 1 -h
    

How to sort the output by each directory's size from the output of the du -h?

How to sort the output by each directory's size from previous output?

  
$ du -d 1 -h | sort -hr
    
sort -h gives the trick, and -r further sort it in the reverse order.

For more options that are available for du utility, you can use du --help to see more.

How to sort the output by each directory's size from previous output?

  
Usage: du [OPTION]... [FILE]...
  or:  du [OPTION]... --files0-from=F
Summarize disk usage of the set of FILEs, recursively for directories.

Mandatory arguments to long options are mandatory for short options too.
  -0, --null            end each output line with NUL, not newline
  -a, --all             write counts for all files, not just directories
      --apparent-size   print apparent sizes, rather than disk usage; although
                          the apparent size is usually smaller, it may be
                          larger due to holes in ('sparse') files, internal
                          fragmentation, indirect blocks, and the like
  -B, --block-size=SIZE  scale sizes by SIZE before printing them; e.g.,
                           '-BM' prints sizes in units of 1,048,576 bytes;
                           see SIZE format below
  -b, --bytes           equivalent to '--apparent-size --block-size=1'
  -c, --total           produce a grand total
  -D, --dereference-args  dereference only symlinks that are listed on the
                          command line
  -d, --max-depth=N     print the total for a directory (or file, with --all)
                          only if it is N or fewer levels below the command
                          line argument;  --max-depth=0 is the same as
                          --summarize
      --files0-from=F   summarize disk usage of the
                          NUL-terminated file names specified in file F;
                          if F is -, then read names from standard input
  -H                    equivalent to --dereference-args (-D)
  -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)
      --inodes          list inode usage information instead of block usage
  -k                    like --block-size=1K
  -L, --dereference     dereference all symbolic links
  -l, --count-links     count sizes many times if hard linked
  -m                    like --block-size=1M
  -P, --no-dereference  don't follow any symbolic links (this is the default)
  -S, --separate-dirs   for directories do not include size of subdirectories
      --si              like -h, but use powers of 1000 not 1024
  -s, --summarize       display only a total for each argument
  -t, --threshold=SIZE  exclude entries smaller than SIZE if positive,
                          or entries greater than SIZE if negative
      --time            show time of the last modification of any file in the
                          directory, or any of its subdirectories
      --time=WORD       show time as WORD instead of modification time:
                          atime, access, use, ctime or status
      --time-style=STYLE  show times using STYLE, which can be:
                            full-iso, long-iso, iso, or +FORMAT;
                            FORMAT is interpreted like in 'date'
  -X, --exclude-from=FILE  exclude files that match any pattern in FILE
      --exclude=PATTERN    exclude files that match PATTERN
  -x, --one-file-system    skip directories on different file systems
      --help     display this help and exit
      --version  output version information and exit
    

How to extract or remove lines with grep?

 


In this post, we go through some examples of extracting or removing lines of a file with the grep command line tool.

Suppose we have a file text.txt with the following content.


sed command stands for stream editor
sed allows us to substitute, insert or delete, find and replace strings in a file
sed is quite useful and sed is awesome!

How to filter lines having "useful"? 

Command:

$grep useful text.txt
Output:

sed is quite useful and sed is awesome!

How to filter those lines with case insensitive? 

For example, filtering lines with sed or SED

Suppose we have an updated file text.txt with the following content.


SED command stands for stream editor
SED allows us to substitute, insert or delete, find and replace strings in a file
SED is quite useful and sed is awesome!
Command (with -i option):

$grep -i sed
Ouput:

SED command stands for stream editor
SED allows us to substitute, insert or delete, find and replace strings in a file
SED is quite useful and sed is awesome!

How to filter lines with regex patterns? 

For example, lines starting with SED and containing substitute 


Command (using -E option):

$grep -E '^SED.*substitute.*'
Ouput:

SED allows us to substitute, insert or delete, find and replace strings in a file

Bash scripting - How to delete lines of a file?

 



In this tutorial, we play with deleting some lines of a file using Bash.

Content
  • Create a simple file for the tutorial
  • How to delete n-th line?
  • How to delete several lines within a range?
  • How to delete lines with patterns?

Create a simple file for the tutorial. 


First, let's create a simple file for this tutorial using the following command with seq and tee.

$seq 6 | tee delete.txt

1
2
3
4
5
6


How to delete n-th line? with sed

$sed '5d' delete.txt	# delete 5th line
Output:

1
2
3
4
6

$sed '$d' delete.txt	# delte the last line, $ inidicates the last line in sed
Output:

1
2
3
4
5


How to delete several lines within a range?

$sed '2,4d' delete.txt
Output:

1
5
6


How to delete lines with patterns?

$sed '/2/d' delete.txt	# /pattern/ to delete
Output:

1
3
4
5
6

Bash scripting - How to filter lines of a file?



In this tutorial, we play with filtering some lines of a file using Bash.

Content
  • Create a simple file for the tutorial
  • How to filter the first/last n lines?
  • How to remove n lines?
  • How to filter specific lines?

Create a simple file for the tutorial. 


First, let's create a simple file for this tutorial using the following command with seq and tee.

$seq 6 | tee filtering.txt

1
2
3
4
5
6

How to filter the first n lines?


Given the dummy file - filtering.txt -we've just created, we now move on to several ways of filtering n=3 lines as an example.

Using head

$< filtering.txt head -n 3 
Using sed

$< filtering.txt sed -n '1,3p'
Using awk (NR refers to Number of Records)

$< filtering.txt awk 'NR<=3'
Filtering the last 3 lines is straighforward with tail

$< filtering.txt tail -n 3
How to filter the last n lines?

$< lines tail -n 3

How to remove n lines? (e.g., n=3)


Using tail

$< filtering.txt tail -n +4
Using sed

$< filtering.txt sed ‘1,3d’
How to remove the last n lines?

$< lines head -n -3

How to filter specific lines? For example, 4-6 lines


Using sed

$ < filtering.txt sed -n '4,6p'
Using awk

$ < filtering.txt awk '(NR>=4)&&(NR<=6)'
Using head and tail

$ < filtering.txt head -n 6 | tail -n 3  

How to filter odd or even lines?

 
Using sed

$< filtering.txt sed -n '1~2p'	# '0~2p' for even 
Using awk

$< filtering.txt awk 'NR%2'	# '(NR+1)%2' for even

How to count the number of occorrences of each word in each line of a file?



Suppose we have a file - words.txt - with its content where each line is a word and we would like to count each unique word and sort them in ascending order. 

More specifically, we have words.txt as follows:

  1 foo
  2 bar
  3 foo
  4 foo
  5 bar
And we would like to get the result as follows:

  word,count
  foo,3
  bar,2

We use the following steps to approach this problem:
  1. Read and sort the content to count unique words and their counts
  2. Sort in reversing order by comparing their count values
  3. Print the count and word informatin in the "<count>,<word>" format
  4. Finally, append the column names "word,count" and print out





1. Read and sort the content to count unique words and their counts

$cat words.txt | sort | uniq -c
Note you can always check options available for a command, e.g., uniq, with --help option

$uniq --help
Usage: uniq [OPTION]... [INPUT [OUTPUT]]
Filter adjacent matching lines from INPUT (or standard input),
writing to OUTPUT (or standard output).

With no options, matching lines are merged to the first occurrence.

Mandatory arguments to long options are mandatory for short options too.
  -c, --count           prefix lines by the number of occurrences
This will give us the following results so far:

2 bar
3 foo

2. Sort in reversing order by comparing their count values

$cat words.txt | sort | uniq -c | sort -rn
where -rn indicate comparing numerical values and sort in reversed/descending order. And we get the results so far as:

3 foo
2 bar

3. Print the count and word informatin in the "<count>,<word>" format using awk command (awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan)

cat words.txt | sort | uniq -c | sort -nr | awk '{print $2","$1}'
Now, we are almost done with the output as

foo,3
bar,2
and just need to append headers on top.

4. Finally, append the column names "word,count" and print out using header command line tool from dsutils

$cat words.txt | sort | uniq -c | sort -nr | awk '{print $2","$1}' | header -a word,count

Putting everythign together, we have the final solution as follows:

$cat words.txt | sort | uniq -c | sort -nr | awk '{print $2","$1}' | header -a word,count
with our desired output

  word,count
  foo,3
  bar,2