Find and Replace string in Files Command Line on Linux

🎉 Replace http to https in files

  • with grep

    sed -i "s#http://tuenhai#https://tuenhai#g" `grep -rl http://tuenhai .`
    
  • with print

    find . -name "*.html" -type f -print | xargs sed -i 's#http://tuenhai#https://tuenhai#g'
    
  • with printf

    find . -type f -name "*.html" -printf '"%p"\n' | xargs sed -i s/http:/https:/
    

🐡 Remove trailing whitespace recursively only at end of file

find . -name "*.md" -type f | xargs --verbose -L 1 sed -n --in-place -r ':loop;/[^[:space:]\t]/ {p;b;}; N;b loop;'

🏈 Delete a carriage return (CR) with sed command

To get ^M type CTRL+V followed by CTRL+M

# Check carriage return symbol in file
cat -A file

# Delete carriage return from file
sed -i 's/^M//g' input

🚌 Regex to find lines that do not contain a certain string value

^((?!tuenhai).)*[\r\n]

🗻 Find many files and compress to tar.xz

find . -type f -name "*.html" -printf '"%p"\n' | xargs tar -rf tuenhai.tar
tar -cJf tuenhai.xz tuenhai.tar

Or:

find . -type f -name "*.html" -print0 | xargs -0 tar -rf tuenhai.tar
tar -cJf tuenhai.xz tuenhai.tar

Download files:

sudo apt install axel
axel -n 5 https://tuenhai.com/tuenhai.tar.xz

extract files:

tar -xJf tuenhai.tar.xz
tar --skip-old-files -xf tuenhai.tar

results matching ""

    No results matching ""