Showing posts with label Shell Script. Show all posts
Showing posts with label Shell Script. Show all posts

2019-07-04

How to display number of items in sub-directories from command line



Reference URL:
https://askubuntu.com/questions/1150749/how-to-display-number-of-items-in-sub-directories-from-command-line/1150753#1150753



Here's a little shell function that you can use. Just add these lines to your ~/.bashrc:
lsc(){
    ## globs that don't match should expand to a null string
  shopt -s nullglob
  ## If no arguments were given use the current dir
  if [[ $# -eq 0 ]]; then
    targets="."
  ## Otherwise, use whatever was given
  else
    targets=($@)
  fi
  ## iterate over the arguments given
  for target in "${targets[@]}"; do
    ## get the contents of the target
    contents=("$target"/*)
    ## iterate over the contents
    for thing in "${contents[@]}";  do
      ## If this one is a directory
      if [[ -d "$thing" ]]; then
        ## collect the directory's contents
        count=("$thing"/*)
        ## Print the dir's name (with a '/' at the end)
        ## and the number of items found in it
        printf "%s/ (%s)\n" "$thing" "${#count[@]}"
      else
        ## If this isn't a dir, just print the name
        printf "%s\n" "$thing"
      fi
    done
  done
}
then open a new terminal and run:
lsc /path/to/dir
For example, given the following directory (the \012 are newlines in the names):












2014-11-18

Shell Script to get last month date


Source URL: http://www.unix.com/unix-for-dummies-questions-and-answers/99281-how-get-previous-month-using-date.html




















j=`date +"%m"`; echo $j; i=`expr $j - 1`; echo $i;


Output:
11
10





enhance:

j=`date +"%m"`; echo $j; i=`expr $j - 1`; echo $i; echo `date +"%Y$i%d_%p%H%M"`

Output:
11
10
20141018_AM0340





gzip status progress shell script with Pipe Viewer


Pipe Viewer Source URL: http://www.ivarch.com/programs/pv.shtml

If you don't have Pipe Viewer Current version: 1.5.7:
CentOS / RHEL:Set up my YUM repository or use RepoForge, then do "yum install pv".








Source URL 1 of 2:
http://stackoverflow.com/questions/238073/how-to-add-a-progress-bar-to-a-shell-script

$ bar file1 | wc -l 
$ pv file1 | wc -l

$ tail -n 100 file1 | bar | wc -l
$ tail -n 100 file1 | pv | wc -l

$ copy <(bar file1) file2
$ copy <(pv file1) file2

bar --in-file /dev/rmt/1cbn --out-file \
     tape-restore.tar --size 2.4g --buffer-size 64k









































Source URL 2 of 2:
http://stackoverflow.com/questions/19598797/is-there-any-way-to-show-progress-on-a-gunzip-database-sql-gz-mysql-pr


$ pv database1.sql.gz | gunzip | mysql -u root -p database1

$ pv database1.sql.gz | gunzip | mysql -uroot -p database1
  593MiB 1:00:33 [ 225kiB/s] [====================>              ] 58% ETA 0:42:25

mysqldump -uroot -p database1 | pv | gzip -9 > database1.sql.gz





Create recursively folders using shell script

Source URL: http://stackoverflow.com/questions/8274920/is-it-possible-create-recursively-folders-using-shell-script


mkdir -p folder1/folder2/folder3








Google Referrals