Follows are command lines that I often use (not necessarily the best approach) while interacting with Unix shell.

  • Find files (including files in subdirectories) whose size is larger than x k bytes (replace k with M for mega bytes).

    sudo find . -type f -size + xk
    
  • Re-run through some tasks periodically (e.g., every 10 minutes = 10 * 60s):

    while true; do ...; sleep 600; done
    
  • Get size (disk usage) of a file/directory:

    du -sh path_to_file_or_dir  # -s for summary, -h for humman readable size
    
  • Remove files that do/don’t match a specific pattern:

    find .| grep "match_patten"| xargs rm
    find .| grep -v "except_patten"| xargs rm
    
  • Look up a DNS records of a domain without dig, in Ubuntu:

    host -a <domainame> # e.g.: google.com
    
  • Mount/unmount a remote Samba server in Ubuntu. You will first need to create a mounting point by mkdir (e.g., /media/my_smb_server), and install the Common Internet File System Utilities with apt-get install cifs-utils. You then can use the following command to mount the remote SMB server to the directory just made.

    mount -t cifs -o username=$your_user_name,vers=2.0 //IP_address_or_hostname/shared_dir /media/my_smb_server
    

    Note that the parameter vers= is important, some machine won’t mount if you don’t specify this parameter. It can be either 1.0 or 2.0, if you mount with 1.0 the owner of all directories and files in the mounted folder will have users as owner. With 2.0, the owner will be root. After all, you may want to unmount the sharing folder using this command. Also, you can add password=your_password next to username=$your_user_name, but this is not a good practice since the command gets stored in bash history. For security purpose, you will need to delete bash history later. Without having the password in the command line, you will be prompted to input it.

    umount -a -t cifs -l /media/my_smb_server
    
  • Change the clock of Ubuntu OS to a different timezone:

    sudo timedatectl set-timezone <timeszone> #e.g.: UTC
    

    Use timedatectl list-timezones to list all possible values for timezone.

  • Get current Epoch time of the system, or of the created time of a file in Ubuntu:

    date +%s # current Epoch time of the OS
    date +%s -r <file> # get created time of a file
    

    Advanced tip to remove files which were created more than 15 seconds. (for finding files which were created less than x seconds, see this):

    for i in /dir/*; do if [[ $(expr $(date +%s) - $(date +%s -r $i)) > 10 ]]; then rm $i; fi; done
    

    Note that you may get the following error since the above command is only intended to remove file, not the parent dir. So you may just ignore the error.

    date: '/dir/*': No such file or directory
    expr: syntax error
    
  • Find files which were modified less than x time ago:

    find . -name "*.txt" -newermt 'x seconds ago' # replace x to and seconds to minutes to meet your need
    

    Or, you can also do this to find files modified within the last 0.1 minute, i.e. last 6 seconds:

    find . -name "*.txt" -mmin -0.1
    

    Note that you can change m to a or c which mean accessed or changed, respectively (e.g., -newerct, or -cmin).

  • Pipe multiple lines of string using cat:

    cat << EOF <do_your_job_here>
    # read as End of File, you can pick any other tag (e.g. STOP)
    > line 1
    > line 2
    > ...
    > line n
    > EOF
    

    At <do_your_job_here> you can pipe the output to a file by >> file.txt or chain the output by | other_command. If you pipe the output to a file, it will contain all string from line 1 to line n, but not EOF.

  • All sorts of network-manager commands for Ubuntu, one of my most favorite commands:

    nmcli #[see http://manpages.ubuntu.com/manpages/bionic/man1/nmcli.1.html]
    
  • Check if a remote TCP port is Open/Closed from a Linux machine:

    timeout 1 bash -c '</dev/tcp/remote_IP_address/port_number && echo Port is open || Port is closed' || echo Connection timeout
    
  • Unzip tar.lz4 file (install lz4 on Ubuntu with sudo apt-get install liblz4-tool):

    lz4 -d "$FILEPATH" | tar xfk -
    

    The k flag is to skip if file already exists.