Shell Commands - ankit-jha/commands GitHub Wiki

find -name "filename" #Find a file

find . -iname "*.log" -mtime -1h -print #Find *.log files in the current directory modified in last one hour and print

find . -type f -name '*.log' -delete #Find *.log files recursively and delete them

su - dev #Change to user dev

sudo adduser username #Add new user

sudo usermod -aG sudo username #Append(-a) user to sudo group(G)

newgrp sudo #Refresh the user groups

chsh -s $(which zsh) #Change the current user shell to zsh

whoami #Check whose the user

grep 'word' file.txt #Find occurrences word in file.txt

grep 'word1|word2' file.txt #Find occurrence of word1 or word2 in file.txt

grep -A1 'word' file.txt #Find occurrences word in file.txt and print word and 1 line after the match

grep -B2 'word' file.txt #Find occurrences word in file.txt and print word and 2 line before the match

grep -C3 'word' file.txt #Find occurrences word in file.txt and print word and 3 line before and after the match

ls directory | wc -l #List the number of files in a directory

ls directory/*.txt | wc -l #List the number of files with extension txt in the directory

*ls -d / #List only the subdirectories Details

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\ /]*\ //--/g' -e 's/^/ /' -e 's/-/|/' #List folders in form of a tree

fuser 8081/tcp #Shows all processes at the 8081 port.

fuser -k 8081/tcp #Kills all processes at the 8081 port.

scp file1.txt file2.sh [email protected]:~/pathtoupload #Copy from local to server

scp [email protected]:"file1.log file2.log" "~/yourpathtocopy" #Copy server to local:

nc -zv 192.168.1.15 22 #Check if the port 22 is open on machine 192.168.1.15

curl -o /path/to/local/mywork.py http://url.com/work.py #Save a file to local system from web in the given directory with name mywork.py

curl -O http://url.com/work.py #Save a file to local system from web with the remote name work.py

import sys; sys.ps1 = '\033[01;33m>>>\033[00m' #Colors the python prompt

echo "Hello!" >> file.txt #Append Hello! to file.txt

echo "Hello!" > file.txt #Overwrite Hello! to file.txt

echo "Hello!" | tee file.txt #Writes the output both to the screen (stdout) and to the file

wc -l file.txt #Count the number of lines in file.txt (wc - Word Counter Unix Utility)

mkdir -p htg/{articles,images} #Create directory htg and sub-directories articles,images

shuf -n 10 file.txt #Sample and Display 10 lines from file.txt

set -x #Adding this option inside any script prints trace for all commands and helps in debugging

cmp file1 file2 #Compare two files

diff file1.txt file2.txt #Shows difference between two files (Usage)

diff -qr dir1 dir2 #Shows difference(-q report only when files differ) between two directories and subdirectories(-r)

truncate -s 0 filename #Empty contents of a file

*ls .json | xargs wc #Using xargs to feed parameters in other commands (Usage)

echo "245MB" | rev | cut -c 3- | rev #rev command used to remove last two characters

general form of here document

command << delimiter
document
delimiter
$ cat <<EOF > print.sh 
#!/bin/bash
echo $PWD
EOF

https://stackoverflow.com/questions/2500436/how-does-cat-eof-work-in-bash

export PS1="\e[0;31m[\u@\h \W]$ \e[m " #Color the prompt red (Details)

stty -a #Show screen size

stty rows 50 columns 132 #Set rows and columns in a remote terminal

ln -s TARGET LINK_NAME #Creates symbolic link (-s stands for symbolic)

cp -R path_to_source path_to_destination/ #Copy all files in a directory to other

Use of Parentheses In Shell

zip -vr folder.zip folder/ -x "*.DS_Store" #Zip files on mac terminal. -x flags skips files during compression

nohup python script.py & #Run python scripts in background.

nohup makes sure not to send hangup signals to background process. & Runs the process in background

nohup python script.py &> $(date +"%Y%m%dT%H%M%S").log &

&> redirects stderr to the same output file as stdout which is in this case $(date +"%Y%m%dT%H%M%S").log

ctrl-u #Clear long commands on the terminal

alias wr="cd /var/www/html" #Create an alias. To save aliases permanently, add them in user’s shell configuration profile file e.g.ZSH – ~/.zshrc

Delete files on time basis ??

ZSH Shell + OH MY ZSH

Option+RightArrowKey #Invokes forward-word widget

ripgrep

rg -l "gson" #Show list of files having word "gson"

ffmpeg

ffmpeg -i input.mp4 -ss 00:06 -to 00:20 output.mp4 #Only keep video from 00:06 to 00:20 and save to output.mp4

ffmpeg -f concat -i fileList.txt -c copy output.mp4 #Combine multiple files into one

fileList format

cat fileList.txt
file 'Workshop1.mp4'
file 'Workshop2.mp4'

youtube-dl

youtube-dl --format 'best[height<=?720,ext=mp4]' https://www.youtube.com/watch\?v\=Rz6racFuW_Q #Download 720p or less mp4 video

youtube-dl --list-formats https://www.youtube.com/watch\?v\=Rz6racFuW_Q #List all available formats

youtube-dl --yes-playlist https://www.youtube.com/watch\?v\=Rz6racFuW_Q #Download the playlist, if the URL refers to a video and a playlist

⚠️ **GitHub.com Fallback** ⚠️