Bash Utilities
lsof
sshfs (mount remote drives)
ffmpeg (linux)
Using rsync over SSH (better copy vs scp)
Copy all files in docs folder to current folder in terminal (with progress and results -avp
)
rsync -avP "USER@NETWORK:/home/USER/docs/*" .
Using Sed to modify files and pipe
# search and replace without -i it just outputs to console
sed 's/{SEARCH}/{REPLACE}/' FILENAME
# search and replace all matches in file
sed 's/{SEARCH}/{REPLACE}/g' FILENAME
# search and replace but can use escaped characters
sed 's;{SEARCH};{REPLACE};' FILENAME
# outputs file with LINENUMBER deleted
sed '{LINENUMBER}d' FILENAME
# ouputs line to be deleted
sed '{LINENUMBER}!d' FILENAME
# searches string then deletes the string
sed '/{STRING}/d' FILENAME
# searches string then returns only selected strings (opposite of delete)
sed '/{STRING}/!d' FILENAME
# adds string to line
sed '{LINENUMBER}a\{VALUE SPACES ESCAPED}' FILENAME
# saves output to file
sed -i {COMMANDS}
# saves .bak file of previous contents then saves file
sed -i.bak {COMMANDS}
# does not show output
sed -n {COMMANDS}
# piping sed (-E)
echo "test,ts2,t3" | sed -E 's/,/\n/g'
Archive files with tar (compressed)
Archive folder:
tar -czf backup.tar /home/user
Archive files:
tar -czf archive.tar file1.txt file2.txt file3.txt
Add to existing archive:
tar -rvf archive.tar file.txt
View files in tar archives
tar -tvf archive.tar
Extract tar archive
Extract all to folder
tar -xvf archive.tar -C ~/Downloads
tar -xvf archive.tar -C ~/Downloads folder/file file
tar -xvf archive.tar -C ~/Downloads --wildcards '*.jpg'
IMPORTANT: Put -C ~/Downloads
before extracting
In the tar man page, -C
changes the directory, so it changes directory then extracts, putting -C
after files/folders will just extract it in the current directory
Extracting different files to current folder and a different folder:
tar -xvf archive.tar file.mp3 -C ~/Downloads file2.mp4 -C ~/Textfiles file3.txt
# should be like (generated by `tree -L 2`):
#├── Downloads
#│ └── file2.mp4
#├── Textfiles
#│ └── file3.txt
#├── archive.tar
#└── file.mp3
Note: The same file cannot be moved to multiple folders (no duplicating) AND it has to be the absolute directory, not relative directory because -C
changes directory as stated previously
Delete file form tar archive
tar --delete -f archive.tar file.txt
Install ifconfig
yum install net-tools -y
Convert windows text file to linux (dos2unix without dos2unix)
With tr:
for f in *.sh; do echo converting $f; tr -d "\15\32" < "$f" > $(basename "$f" .sh)_linux.sh; echo output: $(basename "$f" .sh)_linux.sh; rm -f "$f"; mv "$(basename "$f" .sh)_linux.sh" "$f"; done
With perl:
for f in *.sh; do echo converting $f; perl -p -e 's/\r$//' < "$f" > $(basename "$f" .sh)_linux.sh; echo output: $(basename "$f" .sh)_linux.sh; rm -f "$f"; mv "$(basename "$f" .sh)_linux.sh" "$f"; done
Roughly trim youtube caption subtitle file with sed
cat 'subtitlefile.vtt' | sed '/align:start position/d' | sed '/</d' | sed '/\[Music\]/d' | sed '/^foreign/d' | sed '/^$/d' | sed '/^ /d' | sed '1~2d'