marc:linux:bash_snippets
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| marc:linux:bash_snippets [2023/02/09 17:02] – marcv | marc:linux:bash_snippets [2023/02/16 14:23] (current) – [Download files based on URLs in a file] marcv | ||
|---|---|---|---|
| Line 164: | Line 164: | ||
| ===== Replace all files in a directory with empty files with the same name ===== | ===== Replace all files in a directory with empty files with the same name ===== | ||
| + | < | ||
| #!/bin/bash | #!/bin/bash | ||
| Line 185: | Line 185: | ||
| echo "All files in the directory have been replaced with empty files." | echo "All files in the directory have been replaced with empty files." | ||
| + | </ | ||
| ===== Find SSH logins ===== | ===== Find SSH logins ===== | ||
| + | |||
| + | :!: This script assumes that you have the sshpass package installed on your system. You can install it with the following command: sudo apt-get install sshpass (for Ubuntu) or brew install http:// | ||
| + | |||
| + | < | ||
| #!/bin/bash | #!/bin/bash | ||
| Line 222: | Line 227: | ||
| done | done | ||
| done | done | ||
| + | </ | ||
| + | |||
| + | ===== Remove duplicate and empty lines from file ===== | ||
| + | < | ||
| + | #!/bin/bash | ||
| + | |||
| + | # Remove duplicate lines from url.txt and save to temp file | ||
| + | sort -u url.txt > temp.txt | ||
| + | |||
| + | # Remove empty lines from temp file and save back to url.txt | ||
| + | grep -v " | ||
| + | |||
| + | # Remove temp file | ||
| + | rm temp.txt | ||
| + | </ | ||
| + | |||
| + | ===== Download files based on URLs in a file ===== | ||
| + | < | ||
| + | #!/bin/bash | ||
| + | |||
| + | # read list of URLs from file | ||
| + | while read url; do | ||
| + | # download file using wget | ||
| + | wget " | ||
| + | done < url.txt | ||
| + | </ | ||
| + | |||
| + | Do the same but check if file exists. If it does compare sizes and ask what to do. | ||
| + | < | ||
| + | # Read URLs from the url.txt file and download the files | ||
| + | while read url; do | ||
| + | filename=$(basename " | ||
| + | if [ -e " | ||
| + | local_size=$(wc -c <" | ||
| + | remote_size=$(curl -sI " | ||
| + | if [ " | ||
| + | echo " | ||
| + | continue | ||
| + | else | ||
| + | read -p "A file with the name $filename already exists. Do you want to save the new file with a different name? (y/n) " response | ||
| + | if [ " | ||
| + | read -p "Enter the new file name: " new_filename | ||
| + | curl -L -o " | ||
| + | echo "File saved as $new_filename." | ||
| + | else | ||
| + | echo " | ||
| + | fi | ||
| + | fi | ||
| + | else | ||
| + | curl -L -o " | ||
| + | echo " | ||
| + | fi | ||
| + | done < url.txt | ||
| + | </ | ||
marc/linux/bash_snippets.1675958558.txt.gz · Last modified: (external edit)
