Advertisement
IssouLinux

Useful_Oneliners

Feb 16th, 2022 (edited)
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.97 KB | None | 0 0
  1. #Find and move
  2. find /home/ -maxdepth 2 -name "*.yolo" -exec mv -t ../folder/ {} +
  3.  
  4. #Grep and move:
  5. grep -l string *.pdf | xargs -I % mv % /dest/folder
  6. mv `grep -lir 'string' ~/directory/*` destination/
  7.  
  8. #Add a prefix at each line in a file:
  9.     # If you want to edit the file in-place
  10.     sed -i -e 's/^/prefix/' file
  11.     # If you want to create a new file
  12.     sed -e 's/^/prefix/' file > file.new
  13.  
  14. #Add a suffix at each line in a file:
  15.     sed -i -e 's/$/string after each line/' filename
  16.  
  17. #Break lines exceeding xx caracters:
  18.     fold -w 130 -s file
  19.     -w tells the width of the text
  20.     -s tells to break at spaces, and not in words.
  21.  
  22. #Grep from a specific column in csv file:
  23. awk -F\; '$20 == "keyword"' *.csv        
  24. (-F=separator, $xx=column number)
  25.  
  26. #Extraire toutes les url d'un fichier:
  27. grep -o -E "https?://[][[:alnum:]._~:/?#@!$&'()*+,;%-]+" file | grep "twitter" > liste_tweets
  28.  
  29. #Extraire toutes les adresses email d'un fichier:
  30. grep -oe "[a-zA-Z0-9._]\+@[a-zA-Z]\+.[a-zA-Z]\+" input > output
  31.  
  32. #Get all filenames from a directory:
  33. find . -type f -printf "%f\n" > output
  34.  
  35. #Get all filenames from a directory (filenames will be showed with the relative path):
  36. find . -type f > list_of_files.txt
  37.  
  38. #Copy files from a list of files
  39. xargs -a list_of_files.txt -I % cp % /destination/folder/
  40.  
  41. #Change last date of modification:
  42. touch -d "$(date -R -r filename) - 3 days" filename
  43.  
  44. #Delete first character of each line in a file:
  45. sed -i 's/^.\{1\}//'
  46. #Delete last character of each line in a file:
  47. sed -i 's/.$//'
  48.  
  49. #Delete the first line of a file:
  50. sed -i 1d filename.txt
  51. #Delete the first 10 lines of a file:
  52. sed -i 1,10d filename.txt
  53.  
  54. #Delete a range of lines and one particular line
  55. #(for instance: let's delete lines from 5 to 10 and line 12th):
  56. sed -i '5,10d;12d' file
  57.  
  58. #Replace 2nd line with new line:
  59. sed -i '2s/.*/New 2nd line/' myfile
  60.  
  61. #Add a string at a specific line:
  62. sed -i "1i new string added on the first line" file
  63.  
  64. #Delete special characters (i.e : " ) in a file:
  65. sed -i 's/\"//g'
  66.  
  67. #Delete a word in a file
  68. sed -i 's/\<WordToDelete\>//g' filename.txt
  69.  
  70. #Delete lines in a text file that contains a specific string
  71. sed -i '/pattern to match/d' ./infile
  72.  
  73. #Extract lines from a file:
  74. If you want to extract lines 5 and 10:
  75.     sed -n '5p;10p' input > output
  76. If you want to extract lines from line 5 to 10:
  77.     sed -n '5,10p' input > output
  78.  
  79. #Remove non-printable ASCII characters
  80. tr -cd '\11\12\15\40-\176' < file-with-binary-chars > clean-file
  81.  
  82. #Check if a file contains lines over or under a specific length:
  83. awk 'length>20' input
  84.  
  85. #Delete lines that are 3 characters long or smaller:
  86. sed -i -r '/^.{,3}$/d' filename
  87.  
  88. #Delete lines that are 25 characters long or longer:
  89. sed -i -r  "/.{25,}/d" filename
  90.  
  91. #Remove duplicates
  92. awk '!(count[$0]++)' old.txt > new.txt
  93.   #Replace 'count' by 'seen' in order to preserve the order of the text
  94. awk '!_[$0]++' old.txt > new.txt
  95.  
  96. #Remove all blank lines
  97. sed -i '/\S/!d' filename.txt
  98.  
  99. #Remove both leading and trailing whitespaces
  100. sed -i 's/^[\t ]*//g;s/[\t ]*$//g' filename
  101. #Remove all trailing whitespace
  102. sed -i 's/[\t ]*$//g' filename
  103. #Remove all leading whitespace
  104. sed -i 's/^[\t ]*//g' filename
  105.  
  106. #Replace a string in a file
  107.     #METHOD1
  108. sed -i 's/find/replace/' *.csv
  109.     #METHOD2
  110. grep -rl matchstring ./ | xargs sed -i 's/string1/string2/g'
  111.  
  112. #Find and replace all occurrences of a string in multiple files
  113. grep -rl 'old_string' ./ | xargs sed -i 's/old_string/new_string/g'
  114.  
  115. #Find and delete all files with a specific string:
  116. grep -l --null 'specific_string' ./* | xargs -0 rm
  117.  
  118. #How to Count Word Occurrences in a Text File
  119. grep -o -i mauris example.txt | wc -l
  120. #Using grep -c alone will count the number of lines that contain the matching
  121. #word instead of the number of total matches
  122.  
  123. #Ajouter un prefixe au nom des fichiers
  124. rename 's/^/PREFIXE_/' *
  125.  
  126. #Ajouter un suffixe au nom des fichiers
  127. for file in *; do mv "$file" "$(basename "$file")SUFFIXE"; done;
  128. #Ajouter un suffixe au nom des fichiers avec extension:
  129. for f in *.txt; do mv "$f" "${f%.mp3}_SUFFIXE.mp3"; done
  130.     OU
  131. rename 's/\.pdf$/_string_to_add.pdf/' *pdf
  132.  
  133. #Remplacer les espaces par des underscores dans un nom de fichier
  134. find ./ -depth -name "* *" -exec rename 's/ /_/g' "{}" \;
  135.  
  136. #Supprimer les espaces dans un nom de fichier
  137. rename "s/ *//g" *
  138.  
  139. #Supprimer les points dans un nom de fichier
  140. find -name "*.*.*" | xargs -I % bash -c 'mv "%" "$(echo "%" | sed "s|^./||;:a;s/\.\([^.]\+\)\./\1./;ta")"'
  141.  
  142. #Rename multiple files
  143.     #To rename all files starting with letter “a” to “b”, simply run:
  144.     mmv a\* b\#1
  145.     #To rename files with a number (fileA,fileB --> 01_fileA,02_fileB):
  146.     n=1; for f in *.txt; do mv "$f" "0$((n++))_$f"; done
  147.     (le "0$" = préfixe, "_$"= suffixe)
  148.  
  149. #Sort by length (ordre décroissant,pour ordre croissant, enlever le -r)
  150. awk '{ print length($0) " " $0; }' <tonFichier> | sort -r -n | cut -d ' ' -f 2- > new.txt
  151.  
  152. #Sort by alphabetical order
  153. sort old.txt | uniq > new.txt
  154.  
  155. #Merge multiple text files into one
  156. cat file1.txt file2.txt > combined.txt
  157.  
  158. #Erase everything but the ones without duplicate
  159. sort fichier.csv | uniq -u > fichier2.txt
  160.  
  161. #Show only duplicate strings
  162. sort file.txt | uniq file.txt -d > newfile.txt
  163.  
  164. #Show only unique strings (those without duplicate)
  165. sort data.txt | uniq -u
  166.  
  167. #Supprimer les x premier caractères de chaque ligne (les 2 premiers dans l'ex:)
  168. sed -i 's/^..//'
  169.  
  170. #Count number of pages in pdf files
  171. exiftool *.pdf | grep 'Page Count' | cut -c35-
  172.  
  173. #Search and mv files containing a specific string
  174.     #For one single expression
  175. for i in $( grep -i "123456" *.ini)
  176. do
  177. base_fname="$(echo $i | cut -d. -f1)"
  178. mv ${base_fname}.ini
  179. mv ${base_fname}.txt
  180. done
  181.     #For multiple expressions
  182. for i in $( grep -i "123\|456\|789" *.ini)
  183. do
  184. base_fname="$(echo $i | cut -d. -f1)"
  185. mv ${base_fname}.ini
  186. mv ${base_fname}.txt
  187. done
  188.     #By using a list of filenames:
  189. for i in $( grep -f ~/test/file.txt *.ini)
  190. do
  191. base_fname="$(echo $i | cut -d. -f1)"
  192. cp ${base_fname}.ini ../test
  193. cp ${base_fname}.txt ../test
  194. done
  195.  
  196.  
  197. #Compress a series of identical characters to a single character:
  198.    tr -s 'input_characters' < filename > newfile
  199. #Translate the contents of a file to upper-case:
  200.    tr "[:lower:]" "[:upper:]" < filename > newfile
  201.  
  202. #Convert tabulation separator with commas:
  203.         cat input.csv | tr "\\t" ";" > output.csv
  204.  
  205. #Convert csv to txt with tabulation separator (pip install csvkit):
  206.     csvformat -T input.csv > output.txt
  207.  
  208. #wget all files of a specific format:
  209.     wget -c -A '*.mp3' -r -l 1 -nd
  210.     -c: continue getting a partially-downloaded file.
  211.     -A: only accept mp3 files. change this format with another format you want to download.
  212.     -r: recurse
  213.     -l 1: one level deep (ie, only files directly linked from this page)
  214.     -nd: don't create a directory structure, just download all the files into current directory.
  215.  
  216. #Create a text file with a range of numbers
  217. seq 1 1000 > numbers.txt
  218.  
  219. #Create a text file with a range of numbers and increment by 5
  220. seq 5 5 1000 > numbers.tx
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement