Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Find and move
- find /home/ -maxdepth 2 -name "*.yolo" -exec mv -t ../folder/ {} +
- #Grep and move:
- grep -l string *.pdf | xargs -I % mv % /dest/folder
- mv `grep -lir 'string' ~/directory/*` destination/
- #Add a prefix at each line in a file:
- # If you want to edit the file in-place
- sed -i -e 's/^/prefix/' file
- # If you want to create a new file
- sed -e 's/^/prefix/' file > file.new
- #Add a suffix at each line in a file:
- sed -i -e 's/$/string after each line/' filename
- #Break lines exceeding xx caracters:
- fold -w 130 -s file
- -w tells the width of the text
- -s tells to break at spaces, and not in words.
- #Grep from a specific column in csv file:
- awk -F\; '$20 == "keyword"' *.csv
- (-F=separator, $xx=column number)
- #Extraire toutes les url d'un fichier:
- grep -o -E "https?://[][[:alnum:]._~:/?#@!$&'()*+,;%-]+" file | grep "twitter" > liste_tweets
- #Extraire toutes les adresses email d'un fichier:
- grep -oe "[a-zA-Z0-9._]\+@[a-zA-Z]\+.[a-zA-Z]\+" input > output
- #Get all filenames from a directory:
- find . -type f -printf "%f\n" > output
- #Get all filenames from a directory (filenames will be showed with the relative path):
- find . -type f > list_of_files.txt
- #Copy files from a list of files
- xargs -a list_of_files.txt -I % cp % /destination/folder/
- #Change last date of modification:
- touch -d "$(date -R -r filename) - 3 days" filename
- #Delete first character of each line in a file:
- sed -i 's/^.\{1\}//'
- #Delete last character of each line in a file:
- sed -i 's/.$//'
- #Delete the first line of a file:
- sed -i 1d filename.txt
- #Delete the first 10 lines of a file:
- sed -i 1,10d filename.txt
- #Delete a range of lines and one particular line
- #(for instance: let's delete lines from 5 to 10 and line 12th):
- sed -i '5,10d;12d' file
- #Replace 2nd line with new line:
- sed -i '2s/.*/New 2nd line/' myfile
- #Add a string at a specific line:
- sed -i "1i new string added on the first line" file
- #Delete special characters (i.e : " ) in a file:
- sed -i 's/\"//g'
- #Delete a word in a file
- sed -i 's/\<WordToDelete\>//g' filename.txt
- #Delete lines in a text file that contains a specific string
- sed -i '/pattern to match/d' ./infile
- #Extract lines from a file:
- If you want to extract lines 5 and 10:
- sed -n '5p;10p' input > output
- If you want to extract lines from line 5 to 10:
- sed -n '5,10p' input > output
- #Remove non-printable ASCII characters
- tr -cd '\11\12\15\40-\176' < file-with-binary-chars > clean-file
- #Check if a file contains lines over or under a specific length:
- awk 'length>20' input
- #Delete lines that are 3 characters long or smaller:
- sed -i -r '/^.{,3}$/d' filename
- #Delete lines that are 25 characters long or longer:
- sed -i -r "/.{25,}/d" filename
- #Remove duplicates
- awk '!(count[$0]++)' old.txt > new.txt
- #Replace 'count' by 'seen' in order to preserve the order of the text
- awk '!_[$0]++' old.txt > new.txt
- #Remove all blank lines
- sed -i '/\S/!d' filename.txt
- #Remove both leading and trailing whitespaces
- sed -i 's/^[\t ]*//g;s/[\t ]*$//g' filename
- #Remove all trailing whitespace
- sed -i 's/[\t ]*$//g' filename
- #Remove all leading whitespace
- sed -i 's/^[\t ]*//g' filename
- #Replace a string in a file
- #METHOD1
- sed -i 's/find/replace/' *.csv
- #METHOD2
- grep -rl matchstring ./ | xargs sed -i 's/string1/string2/g'
- #Find and replace all occurrences of a string in multiple files
- grep -rl 'old_string' ./ | xargs sed -i 's/old_string/new_string/g'
- #Find and delete all files with a specific string:
- grep -l --null 'specific_string' ./* | xargs -0 rm
- #How to Count Word Occurrences in a Text File
- grep -o -i mauris example.txt | wc -l
- #Using grep -c alone will count the number of lines that contain the matching
- #word instead of the number of total matches
- #Ajouter un prefixe au nom des fichiers
- rename 's/^/PREFIXE_/' *
- #Ajouter un suffixe au nom des fichiers
- for file in *; do mv "$file" "$(basename "$file")SUFFIXE"; done;
- #Ajouter un suffixe au nom des fichiers avec extension:
- for f in *.txt; do mv "$f" "${f%.mp3}_SUFFIXE.mp3"; done
- OU
- rename 's/\.pdf$/_string_to_add.pdf/' *pdf
- #Remplacer les espaces par des underscores dans un nom de fichier
- find ./ -depth -name "* *" -exec rename 's/ /_/g' "{}" \;
- #Supprimer les espaces dans un nom de fichier
- rename "s/ *//g" *
- #Supprimer les points dans un nom de fichier
- find -name "*.*.*" | xargs -I % bash -c 'mv "%" "$(echo "%" | sed "s|^./||;:a;s/\.\([^.]\+\)\./\1./;ta")"'
- #Rename multiple files
- #To rename all files starting with letter “a” to “b”, simply run:
- mmv a\* b\#1
- #To rename files with a number (fileA,fileB --> 01_fileA,02_fileB):
- n=1; for f in *.txt; do mv "$f" "0$((n++))_$f"; done
- (le "0$" = préfixe, "_$"= suffixe)
- #Sort by length (ordre décroissant,pour ordre croissant, enlever le -r)
- awk '{ print length($0) " " $0; }' <tonFichier> | sort -r -n | cut -d ' ' -f 2- > new.txt
- #Sort by alphabetical order
- sort old.txt | uniq > new.txt
- #Merge multiple text files into one
- cat file1.txt file2.txt > combined.txt
- #Erase everything but the ones without duplicate
- sort fichier.csv | uniq -u > fichier2.txt
- #Show only duplicate strings
- sort file.txt | uniq file.txt -d > newfile.txt
- #Show only unique strings (those without duplicate)
- sort data.txt | uniq -u
- #Supprimer les x premier caractères de chaque ligne (les 2 premiers dans l'ex:)
- sed -i 's/^..//'
- #Count number of pages in pdf files
- exiftool *.pdf | grep 'Page Count' | cut -c35-
- #Search and mv files containing a specific string
- #For one single expression
- for i in $( grep -i "123456" *.ini)
- do
- base_fname="$(echo $i | cut -d. -f1)"
- mv ${base_fname}.ini
- mv ${base_fname}.txt
- done
- #For multiple expressions
- for i in $( grep -i "123\|456\|789" *.ini)
- do
- base_fname="$(echo $i | cut -d. -f1)"
- mv ${base_fname}.ini
- mv ${base_fname}.txt
- done
- #By using a list of filenames:
- for i in $( grep -f ~/test/file.txt *.ini)
- do
- base_fname="$(echo $i | cut -d. -f1)"
- cp ${base_fname}.ini ../test
- cp ${base_fname}.txt ../test
- done
- #Compress a series of identical characters to a single character:
- tr -s 'input_characters' < filename > newfile
- #Translate the contents of a file to upper-case:
- tr "[:lower:]" "[:upper:]" < filename > newfile
- #Convert tabulation separator with commas:
- cat input.csv | tr "\\t" ";" > output.csv
- #Convert csv to txt with tabulation separator (pip install csvkit):
- csvformat -T input.csv > output.txt
- #wget all files of a specific format:
- wget -c -A '*.mp3' -r -l 1 -nd
- -c: continue getting a partially-downloaded file.
- -A: only accept mp3 files. change this format with another format you want to download.
- -r: recurse
- -l 1: one level deep (ie, only files directly linked from this page)
- -nd: don't create a directory structure, just download all the files into current directory.
- #Create a text file with a range of numbers
- seq 1 1000 > numbers.txt
- #Create a text file with a range of numbers and increment by 5
- seq 5 5 1000 > numbers.tx
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement