Skip to content

Instantly share code, notes, and snippets.

@AtmaMani
Last active December 2, 2022 05:10
Show Gist options
  • Save AtmaMani/27ac5994dd0b2b3c840261eccb65fdf9 to your computer and use it in GitHub Desktop.
Save AtmaMani/27ac5994dd0b2b3c840261eccb65fdf9 to your computer and use it in GitHub Desktop.
Bash commands

Frequently used bash commands

Recursively list or count files of a particular extension

To search for all notebook files: find . -type f -name "*.ipynb" | wc -l will return the count in all subdirectories.

To just list the files, do: find . -type f -name "*.ipynb"

Compress and archive a directory

Run $ tar -czvf <archive_output.tgz> <dir_name>

  • c is for creation of archive
  • z is for gzip compression
  • v is for verbose
  • f is for filename

Extract an archive

tar -xzf <archive.tgz> -C <dest_path>

  • x is for expand or decompress
  • z is for gzip
  • f is for filename
  • v for verbose
  • C to pass destination path

List contents of an archive

tar -tvf <archive.tgz>

List with human friendly file sizes

$ ls -lh

[root@65a2b6746132 outputs]# ls -lh
total 3.5G
-rw-r--r--  1  504 root 503M Sep 18 04:51 schout_1.nc
-rw-r--r--  1  504 root 503M Sep 18 04:51 schout_2.nc
-rw-r--r--  1  504 root 503M Sep 18 04:51 schout_3.nc

List recursively

Use ls -ltR <pattern>

# find all folders that have a `outputs` nested under 1 level
$ ls -ltR **/**/outputs
64109250/grid_10/outputs:
total 604460
-rw-rw-rw- 1 developer developers 154711326 Sep 21 01:28 schout_4.nc.gz
-rw-rw-rw- 1 developer developers 154741545 Sep 21 01:19 schout_3.nc.gz
-rw-rw-rw- 1 developer developers 154745086 Sep 21 01:10 schout_2.nc.gz
-rw-rw-rw- 1 developer developers 154747647 Sep 21 01:00 schout_1.nc.gz

10963506/grid_3/outputs:
total 24776864
-rw-r--r-- 1 developer developers         0 Sep 19 18:53 looper.txt
-rw-rw-rw- 1 developer developers 497419103 Sep 18 07:41 schout_39.nc

Size of directories

Summary info:

$ du -hs schout_combined.zarr/   # h gives human readable info, s is for summary

13G	schout_combined.zarr/

Tree info:

$ du -h schout_combined.zarr/

1.3G	schout_combined.zarr/SCHISM_hgrid_edge_y
881M	schout_combined.zarr/depth
17M	schout_combined.zarr/wetdry_side
412K	schout_combined.zarr/node_bottom_index
786M	schout_combined.zarr/SCHISM_hgrid_face_x
16K	schout_combined.zarr/time
587M	schout_combined.zarr/zcor

Change screenshot default format from PNG to JPG

defaults write com.apple.screencapture type jpg;killall SystemUIServe

Then run, cmd + shift + 5 to take a screenshot.

Add a folder to $PATH env var

The format is env_var=$env_var:new_value which will append new values with : as the separator.

export PATH=$PATH:<new_path>
export PATH=$PATH:/opt/homebrew/bin

If your path has spaces, then enclose in double quotes:

export PATH="$PATH:/Library/Application Support/iODBC/bin"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment