A repository consisting of useful commands required in daily tasks to reduce stackoverflow searches.
Just click on the right of the codeblock to copy the command and use it directly in your work.
find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u
find . -type f | sed -e 's/.*\.//' | sed -e 's/.*\///' | sort | uniq -c | sort -rn
Alternate command for symlinks too
find . -type d -exec echo dirs \; -o -type l -exec echo symlinks \; -o -type f -links +1 -exec echo hardlinks \; -o -type f -exec echo files \; | sort | uniq -c
find . -type f -size +4G
find . -type f -size -4G
nvidia-smi && nvidia-smi pmon -c 1 | awk -F ' ' '{print $2}' | xargs -0 > t1 && export i=0; export l2="-"; while IFS='' read -r line;do if [[ "$i" -lt 2 || -z "$line" || "$line" == $l2 ]];then echo $i; else ps -p "$line" -o pid,vsz=MEMORY -o user,group=GROUP -o comm,args=ARGS | awk '{for ( x=1 ; x<=1 ; x++ ) { printf("%s\t",$x) } for (x=2;x<=2;x++) { if(NR>1) { printf("%13.2fMb\t",hr=$x/1024) } else { printf("\t%s\t",$x) } } for ( x=3 ; x<=NF ; x++ ) { printf("%s ",$x) } print "" }'; fi; i=$((i+1)) ;done < t1 && rm t1
for FILENAME in *; do FILENAME1=${FILENAME#*_}; mv $FILENAME $FILENAME1; done
Note - this above for loop can be used to do many stuff accordingly. Useful to have this in mind
awk 'BEGIN{FS=OFS=","}{$0=$2 FS $2 FS $1}1' <your comma delimited file>
Note - read awk documentation to perform lots of crazy commmands based on the above template. Learn awk - its very useful nifty tool and no installation needed on any Linux OS.
tar cf - * | mbuffer -m 1024M | ssh user@host '(cd /home/path; tar xf -)'
Extract wav audio files form an mp4 extension of video - crude way - more customization is possible.
for f in *.mp4;do ffmpeg -i "$f" "${f%mp4}wav";done
Replace spaces with underscores -- very irritating to handle spaces in filenames -- easy tool to fix that so your code doesnt need to handle.
for file in */*; do echo $file; mv "$file" $(echo $file | sed 's/ /_/g'); done
awk 'BEGIN {FS=" "};{sum+=$NF} END {print sum}' <your file>
comm -12 <(sort file1) <(sort file2)
grep -Fxf file1 file2
both grep and comm has same execution time roughly for relatively small txt files less than 50mb. Post that grep is better for larger files.
comm -23 <(sort file1) <(sort file2)
comm -13 <(sort file1) <(sort file2)
Ignore case in comm command by passing comm -i
sort file1 | uniq
sort file1 | uniq -c
sort file1 | uniq -d
Only print the lines which are not repeated in a file. (sort if not sorted) (basically count == 1 lines)
sort file1 | uniq -u
Run uniq command on a given field (below command will run uniq only field 1 - useful in delimited files)
uniq -f 1 file1
Note: above command is sample and can be used with previous commands in combination to get what you want from a particular field.
sed -n '1,5p' file1
sed '3,10d' file1
sed -n -e '2,4p' -e '8,12p' file1
sed 's/word1/word2/g' file1
To ignore case of replacement
sed 's/word1/word2/gi' file1
sed '5,10 s/word1/word2/g' file1
sed -i 's/\r//' file1
sed -i'.orig' 's/word1/word2/gi' file1
sed '/word3/ s/word1/word2/g' file1
sed -i 's/word1/word2/gi;s/word3/word4/gi' file1
who | awk '{print $1, $2}'
awk -F":" '{$2="";$3="";print}' file1
awk '/^[ \t]*$/{next}{print}' file1
awk '{print NR,"-->",NF}' file1
echo "Some long string...." | awk 'length($0) > 5'
awk 'BEGIN {FS="[^a-zA-Z]+" } { for (i=1; i<=NF; i++) words[tolower($i)]++ } END { for (i in words) print i, words[i] }' file1
ls *.WAV | awk '{ printf("mv \"%s\" \"%s\"\n", $0, tolower($0)) }'
awk '{ print FNR ". " $0 ;next}{print}' file1
:%s/\(\zsPATTERN.\{-}\)\{N}/REPLACE/
:%!awk '{print $0" "$1}'
Speed up listing of files in a path consisting of many many files. (difference noticable after 50k files with standard ls command)
ls -1f <your path>
For adventerous folks - compile this c code - https://github.com/ChristopherSchultz/fast-file-count Its faster than ls -1f by about 6x on a folder containing 1 million files. Note - speedup depends on many factors such as your kernel version, operating version, gcc version -- but definitely the fast-file-count c code is faster. I have it in my home path always.
Other solution is gdu
curl -L https://github.com/dundee/gdu/releases/latest/download/gdu_linux_amd64.tgz | tar xz
chmod +x gdu_linux_amd64
Move gdu_linux_amd64 to home path and call the following command from path you want file size.
~/gdu_linux_amd64
Both these tools are faster than ncdu (my previous choice to get faster than du)
Fast Commands with gnu parallel installed (https://anaconda.org/conda-forge/parallel)
You will save a lot of time with this!!! Use parallel with rm with caution. I messed up a linux system due to this error on my personal laptop once.
Slow command
for i in *jpeg; do convert $i $i.png ; done
Fast command with parallel
find . -name "*jpeg" | parallel -I% --max-args 1 convert % %.png
Run a bunch of commands - Keep all commands disjoint (any command should not depend on output of previous command)
Say you have k cores on a machine, create a file called - jobs.txt or something Sample jobs.txt
command1
command2
command3
command4
and so on
Now with k cores run the jobs.txt file (try to run with k-1 commands in parallel)
parallel -- jobs k-1 < jobs.txt
parallel -j10 cp {} output_path/ ::: *
Your bash script should have a command per line. This saves a LOT of time!!!
parallel -a your-script-here.sh
Generate a transfer.log file in /tmp for all the folders/files from a source path you want to get copied
cat /tmp/transfer.log | parallel --will-cite -j 5 rsync -avzm --progress --relative --stats --safe-links --ignore-existing --human-readable {} dest_host:/data/
If you know a better way to do the tasks Or other tasks which can be added here - kindly create a pull request for the repository.
Many of the commands here are created as per my needs and can be easily modified to ones suited task.
This repository will be continuously updated over time.
Star the repository/share it if this helps reduce your time to make searches/helps in your work.