Category Archives: bash

Pipe mysqldump to ssh/sftp

If you need to send mysqldump over ssh on different server without creating any files on the local server, mostly if you are low on space you can use this:

mysqldump -u MYSQL_USERNAME -p DATABASE | gzip -c | ssh USER@HOST 'cat > ~/dump.sql.gz'


And for the restore from remote location:
ssh USER@HOST "cat /path/to/db.sql" | mysql -uUSER  -pPASSWORD DATABASE

Top linux commands to use.

Table with the command I most often use and think are essential. Or some that are cool, but easy to forget/not so often used.

ctrl+rSearch in bash history
ctrl+eGo to the end of the line
ctrl+uCut the characters before the cursor
ctrl+yYank/paste, it can paste what you cut with ctrl+u
ctrl+shift+cCopy the marked text
ctrl+shift+vPaste the text from the previous command
ctrl+dClose bash sessions, same as to type exit
!$Get the last argument from the previous command
!*Get all the arguments from the previous command
historyDon’t add command to bash history. There is space in front of the command
disown -a && exitExit terminal, detach all background process, so they can run. Useful for long tasks.
fcOpen last command in editor. Fix very long one-liners if you mess them up.
ctrl+x+eCompose command in the default editor and execute it on save.
curl ifconfig.me Get your public IP from CLI.
very_long_command # labelLabel long commands, so it it easier to find in history. Everything after # is not executed as it is bash comment.
rm !(*.foo|*.bar|*.html) Remove all files except the ones with these extensions.
vim -x <FILENAME>Encrypt file in vim.
man hierShow filesystem hierarchy.
cat /etc/issue Get distro name.
ps aux | grep [p]rocess-name Find the process you are looking for, without showing the grep command itself.

Remove files that are not in a list

Needed to remove bunch of files that are not matching list given to me, so to leave only the files that are on the list, which turned out to be a little hard, or at least for me.

That did the trick:

 find . -name "*" | grep -vFf update_images.csv | xargs rm -f

If the file names contain white spaces, you will have to use the following command, but I haven’t tested it myself:

find . -name "*" | grep -vFf update_images.csv |sed 's/^/"/;s/$/"/' | xargs rm -rf