Skip to content

Instantly share code, notes, and snippets.

@Hansimov
Last active September 5, 2023 03:52
Show Gist options
  • Save Hansimov/cab44ae4bc3a6d3c9b5447d258fbbd07 to your computer and use it in GitHub Desktop.
Save Hansimov/cab44ae4bc3a6d3c9b5447d258fbbd07 to your computer and use it in GitHub Desktop.
Collection of useful Linux commands in daily work

List some useful Linux commands here.

@Hansimov
Copy link
Author

Hansimov commented Jun 30, 2021

Print file content between a line number range (do not forget the p after the end line number):

sed -n 11,23p <filename>

@Hansimov
Copy link
Author

Hansimov commented Jul 1, 2021

ls sort by date:

ls -halt

  • -h: human readable (file size)
  • -a: show hidden
  • -l: print details
  • -t: sort by date

@Hansimov
Copy link
Author

Hansimov commented Jul 1, 2021

move/rename folder:
mv <folder1> <folder2>

@Hansimov
Copy link
Author

Hansimov commented Jul 1, 2021

copy directory content recursively:

cp -a <src>/. <dst>/

  • -a option is an improved recursive option, that preserve all file attributes, and also preserve symlinks.

  • . at end of the source path is a specific cp syntax that allow to copy all files and folders, included hidden ones.


command line - How can I copy the contents of a folder to another folder in a different directory using terminal? - Ask Ubuntu



Another solution:

cp -R <src>/* <dst>


@Hansimov
Copy link
Author

Hansimov commented Jul 5, 2021

get ip from domain:

dig +short xxx.yyy.com

@Hansimov
Copy link
Author

Hansimov commented Jul 5, 2021

Get head of http(s) response from url:

curl -I <url>

@Hansimov
Copy link
Author

Hansimov commented Jul 7, 2021

Check disk space usage:

ncdu --exclude ~/.snapshot ~

@Hansimov
Copy link
Author

Hansimov commented Jan 30, 2023

Find text or string in all files:

grep -rnw '/path/to/somewhere/' -e 'pattern'
  • -r or -R: recursive
  • -n: show line number
  • -w: match the whole word
  • -l (lower-case L): just show the file name of matching files
  • -e: pattern used during the search

Example:

grep --include=\*.{py,sh} -rnw './script' -e 'threshold'

References:

@Hansimov
Copy link
Author

Hansimov commented Apr 11, 2023

List file structure by tree:

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'

also contain files:

find . | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"

Add alias to ~./.bashrc:

alias lr=ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
# use `lr` instead of `lt` to avoid default settings `alias lt='ls -lt'`
alias lf=find . | sed -e "s/[^-][^\/]*\//  |/g" -e "s/|\([^ ]\)/|-\1/"

References:

@Hansimov
Copy link
Author

nvidia-smi -q -x | grep pid | sed -e 's/<pid>//g' -e 's/<\/pid>//g' -e 's/^[[:space:]]*//'`

References:

@Hansimov
Copy link
Author

Hansimov commented Sep 5, 2023

List process run on port 28888:

lsof -i:28888

Kill a process run on port 28888:

kill $(lsof -i:28888 | awk 'NR>1 {print $2}')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment