Frequently Used SSH Commands

MySQL Database

SSH database export:

mysqldump --opt --host=[host] --user=[user] -p --no-tablespaces [database] > [filename].sql

SSH database import:

mysql --host=[host] --user=[suer] -p [database] < [filename].sql

SSH Login

ssh-copy-id -i ~/.ssh/id_rsa.pub [username]@[host]

Compressing a directory

Compressing to a .zip file

zip -9 -r example.com.zip example.com

Compressing to a .tar.gz file

tar -zcvf archive.tar.gz archive

Extract a .tar.gz file

tar -xvf archive.tar.gz

Compressing to a .tar.bz2 file

tar -cjvf image_backup.tar.bz2 images

About the tar command: https://www.freecodecamp.org/news/how-to-compress-files-in-linux-with-tar-command/

FlagExplanationUsage
-cCreate a new archive.We use this flag whenever we need to create a new archive.
-zUse gzip compression.When we specify this flag, it means that archive will be created using gzip compression.
-vProvide verbose output.Providing the -v flag shows details of the files compressed.
-fArchive file name.Archive file names are mapped using the -f flag.
-xExtract from a compressed file.We use this flag when files need to be extracted from an archive.

Compressing files

Compressing a file using GZIP

gzip test.log

If you still need the test.log file to exist, run the following instead. This creates a gzipped copy of the file:

gzip -c test.log > test.log.gz

https://linuxize.com/post/how-to-extract-unzip-tar-gz-file

Move the contents of a directory

mv -f /path/subfolder/{.,}* /path/

Throws two errors but does the trick.

Read this…

Certainly! The command you’ve written attempts to move all files, including hidden ones (those starting with a dot), from a subfolder to another path. However, this command might not work as intended because the glob {.,}* could result in an error due to the inclusion of the parent directory ...

A safer and more common approach would be to use two separate commands: one for hidden files and another for the rest. Here’s an improved version:

# Move all hidden files
mv -f /path/subfolder/.* /path/

# Move all other files
mv -f /path/subfolder/* /path/

This ensures that only the hidden files (excluding . and ..) and the regular files are moved without errors. Remember to check the destination directory /path/ to ensure no files are overwritten unintentionally.

If you’re using a shell that supports it, you can also use the shopt command with dotglob to include hidden files in the * glob pattern:

# Enable dotglob to include hidden files
shopt -s dotglob

# Move all files including hidden ones
mv -f /path/subfolder/* /path/

# Disable dotglob to revert to default behavior
shopt -u dotglob

This way, all files including hidden ones are moved with a single command, and then the dotglob option is unset to revert to the default behavior. Always test such commands in a safe environment before running them on important data to avoid accidental data loss.

Show the size of a directory and its contents

From this conversation with Bing

Certainly! To check the size of a directory and its contents in Linux, you have a few options. Let’s explore them:

  1. Using the du Command:
  • The du (disk usage) command estimates and displays the disk space usage of files and directories.
  • Basic syntax: du [option] [directory-or-file]
  • Some useful options:
    • -h, --human-readable: Shows sizes in a human-friendly format.
    • -s, --summarize: Displays only the total size of specified directories (without breaking down subdirectories and files).
    • -a, --all: Shows sizes for both files and directories.
    • -c, --total: Displays the total disk usage in the last line.
    • Example: To display the size of the current directory in a human-friendly format, run:
      du -h
  • Note: The ls -l command shows file and directory information, including sizes, but the displayed size is consistently 4096 bytes for directories, regardless of their actual contents. Use du for accurate sizes¹.
  1. Listing Sizes with ls and du:
  • For files, use ls -l filename to display the size of a specific file.
  • To see sizes of all files in the current directory, use ls -l *.
  • To include hidden files, use ls -al *.
  • For directories, use du -sh /dir to print the size in a human-readable format.
  • The -s option shows only the total size, not individual file sizes⁴.
  1. Sorting by Size:
  • To list sizes of files and directories and sort them by descending size, use:
    du -a -h --max-depth=1 | sort -hr
    This will display the largest items first³.

Remember to adjust the commands based on your specific requirements. Happy exploring! 🐧📂

Source: Conversation with Bing, 2024-04-17
(1) How to Check the Size of a Directory in Linux – phoenixNAP. https://phoenixnap.com/kb/show-linux-directory-size.
(2) linux command to get size of files and directories present in a …. https://stackoverflow.com/questions/11720079/linux-command-to-get-size-of-files-and-directories-present-in-a-particular-folde.
(3) linux – How to list the size of each file and directory and sort by …. https://stackoverflow.com/questions/7463554/how-to-list-the-size-of-each-file-and-directory-and-sort-by-descending-size-in-b.
(4) 4 Ways to Check the size of a directory in Linux – howtouselinux. https://www.howtouselinux.com/post/check-directory-size-in-linux.
(5) How to Check Disk Space in Linux {df and du Commands} – phoenixNAP. https://phoenixnap.com/kb/linux-check-disk-space.

du [option] [directory-or-file]
du -hs [directory-or-file]

History

Ref for history command: https://www.baeldung.com/linux/delete-commands-history

Retrieve the last 20 entries from the bash history. Remember that the bash history hasn’t yet been written to disk until the current session has ended. Previous sessions have written their sessions to: .bash_history

history 20

Delete specific range of history entries

history -d 2025-2026; history -d $(history 1 | awk '{print $1}')

Delete the previous command and this command to delete that command. Basically, leave no trace of the deletion.

history -d; history -d $(history 1 | awk '{print $1}')

Download with wget

To download a ZIP file to a server using wget, you can use the following command format:

wget -O /path/to/save/file.zip https://example.com/file.zip

Here’s a breakdown of the command:

  • wget: The command-line utility for downloading files.
  • -O /path/to/save/file.zip: This option specifies the output file name and location where the ZIP file will be saved.
  • https://example.com/file.zip: The URL of the ZIP file you want to download.