Handy Bash Snippets and Linux Tips
In my day-to-day work with Linux systems and development environments, I've collected a variety of useful command-line snippets and troubleshooting notes. This blog post shares some of my favorites from counting files to fixing display issues designed to boost your productivity and make your life easier.
1. Quickly Count Files in a Folder
fcount() { ls -1q "$1" | wc -l; }
Example:
fcount /home/nguyenlm/folder
→27
2. Force Fix Broken CUDA Installation
sudo apt-get -o Dpkg::Options::="--force-overwrite" install --fix-broken
3. Count Non-Blank Lines in a File
nbl-count() { grep -cve '^\s*$' "$1"; }
Example:
nbl-count file.txt
→10
4. Run Docker Without Sudo
sudo chmod 666 /var/run/docker.sock
⚠️ Warning: This gives broad access to Docker socket. In production, add user to
docker
group instead.
5. Fix Second Monitor Detection in Ubuntu
sudo apt-get purge 'nvidia*'
sudo add-apt-repository ppa:graphics-drivers
sudo apt-get update
sudo ubuntu-drivers autoinstall
6. Fix “Invalid MIT-MAGIC-COOKIE-1” Error (JavaFX Display Issue)
First, check your active DISPLAY:
who
# Example: user :1 2017-10-12 21:58 (:1)
Set the correct environment:
export DISPLAY=:1.0
zenity --info --text "foobar"
Tip: Check
.bashrc
,.zshrc
,/etc/environment
, or desktop environment configs.
Reference: Arch Linux Forum
7. Check Folder Size Quickly
sizeof() {
du -h --max-depth=0 "$1"
}
Example:
sizeof BOOK
→895M BOOK/
8. Sync Files from Local to Remote Using Rsync
rsync -aPz -e "ssh -p port" local_folder/ user@remote_host:remote_folder
Tip: Add
-n
for dry-run to preview changes.
Reference: Rsync Command in Linux - Linuxize
9. Find and Kill Specific Processes
kill $(ps aux | grep '[p]rocess.py' | awk '{print $2}')
[p]
prevents the grep command from appearing in the process list.
10. Recover a Lost Tmux Session
pkill -USR1 tmux
Final Thoughts
These snippets have saved me countless hours when working with Linux environments, machine learning servers, and production systems. Feel free to bookmark or adapt them to fit your own workflow! ⚡