A while ago I made a tiny function in my ~/.zshrc to download a video from the link in my clipboard. I use this nearly every day to share videos with people without forcing them to watch it on whatever site I found it. What’s a script/alias that you use a lot?

# Download clipboard to tmp with yt-dlp
tmpv() {
  cd /tmp/ && yt-dlp "$(wl-paste)"
}
    • thingsiplay@beehaw.org
      link
      fedilink
      arrow-up
      0
      ·
      1 year ago

      With how many new Linux users we get recently, I don’t like this joke at all without a disclaimer. Yes yes, its your own fault if you execute commands without knowing what it does. But that should not punish someone by deleting every important personal file on the system.

      In case any reader don’t know, rm is a command to delete files and with the option rm -r everything recursively will be searched and deleted on the filesystem. Option -f (here bundled together as -rf) will never prompt for any non existing file. The / here means start from the root directory of you system, which in combination with the recursive option will search down everything, home folder included, and find every file. Normally this is protected todo, but the extra option --no-preserve-root makes sure this command is run with the root / path.

      Haha I know its funny. Until someone loses data. Jokes like these are harmful in my opinion.

      • Kawawete@reddeet.com
        link
        fedilink
        arrow-up
        0
        ·
        1 year ago

        So, as a beginner, you would know how to make an alias but not the most famous Linux joke ever ? Explain your mental gymnastics.

        • thingsiplay@beehaw.org
          link
          fedilink
          arrow-up
          0
          ·
          1 year ago

          You don’t need to understand a command in order to copy paste an alias or Bash function. Especially newcomers could tend to do it, without knowing what the command actually does. We are also in a posting with helpful commands, so its double harmful. And you doubling down without adding any sort of disclaimer shows you don’t care.

  • INeedMana@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago
    $ which diffuc
    diffuc: aliased to diff -uw --color=always
    
    $ which grepnir
    grepnir: aliased to grep -niIr
    
    $ cat `which ts`
    #!/bin/bash
    
    if [ "$#" -lt 1 ]; then
                    tmux list-sessions
                    exit
    fi
    
    if ! tmux attach -t "$1"
    then
                    tmux new-session -s "$1"
    fi
    
  • Lexi Sneptaur@pawb.social
    link
    fedilink
    English
    arrow-up
    0
    ·
    1 year ago

    I usually set up an alias or script to update everything on my system. For example, on Ubuntu, I would do this: alias sysup='snap refresh && apt update && apt upgrade'

    And on Arch, I do this: alias sysup ='flatpak update && paru'

    Funny enough you’d need to use sudo to run this on Ubuntu, but not in the Arch example because paru being neat

        • Random Dent@lemmy.ml
          link
          fedilink
          English
          arrow-up
          0
          ·
          1 year ago

          For me, I find it handy because it catches a bunch of stuff I always forget, like updating Docker containers. Also if you have Am installed it’ll even update your Appimages.

        • GideonBear@lemmy.ml
          link
          fedilink
          arrow-up
          0
          ·
          1 year ago

          Because:

          1. If you install any new software that needs updating, you don’t need to update your alias.
          2. If any software makes changes that break your alias, (theoretically) the bit of software should be able to fix it quickly, without you needing to pay any attention to it.
          3. The bit of software can more easily do advanced things than the simple alias. For example, I added functionality to update JetBrains Toolbox and IDE’s installed with it. A simple alias could not do this, because Toolbox does not have a simple update command, however I made it work by enabling automatic updates temporarily, and then inspecting the log for updates. Now the end-user doesn’t have to think about this at all. Other things that could be done but are not implemented yet include parallelization, and listing updated components in a neat summary (PR linked).

          Of course if you’re a minimalist, then you probably don’t have that much stuff that needs upgrading in the first place. For me personally I have deb-get, uv, cargo, and flatpak, to name a few; the alias was getting longer and longer until I was able to remove it completely by switching to Topgrade.

    • thingsiplay@beehaw.org
      link
      fedilink
      arrow-up
      0
      ·
      1 year ago

      Here is mine for EndeavourOS (based on Arch, BTW):

      alias update='eos-update --yay'
      alias updates='eos-update --yay ;
        flatpak update ; 
        flatpak uninstall --unused ; 
        rustup self update ; 
        rustup update'
      

      And related for uninstalling something:

      alias uninstall='yay -Rs'
      
  • mina86@lemmy.wtfdeleted by creator
    link
    fedilink
    English
    arrow-up
    0
    ·
    1 year ago

    For doing stuff in a directory, I use a replacement for cd command.

    For aliases:

    alias +='git add'
    alias +p='git add -p'
    alias +u='git add -u'
    alias -- -='cd -'
    alias @='for i in'
    alias c='cargo'
    alias date='LANG=C date'
    alias diff='cdiff'
    alias gg='git grep -n'
    alias grep='grep --color=auto'
    alias ll='ls -o'
    alias ls='ls -vFT0 --si --color=auto --time-style=long-iso'
    alias rmd='rmdir'
    

    I also have various small scripts and functions:

    • a for package management (think apt but has simplified arguments which makes it faster to use in usual cases),
    • e for opening file in Emacs,
    • g for git,
    • s for sudo.

    And here’s ,:

    $ cat ~/.local/bin/,
    #!/bin/sh
    
    if [ $# -eq 0 ]; then
    	paste -sd,
    else
    	printf '%s\n' "$@" | paste -sd,
    fi
    
  • KR1Z2k@lemm.ee
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    For docker: I’m not following best practices. I have a giant docker compose file for my entire home lab, this is how I update things:

    alias dockpull="docker compose pull"
    alias dockup="docker compose up -d --remove-orphans"
    
  • gonzo-rand19@moist.catsweat.com
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    Here are probably the most useful ones. I prefer for rm to be interactive so I don’t accidentally delete something important and for mkdir to create a parent directory if necessary.

    alias rm='rm -i'
    alias mkdir='mkdir -p'
    alias podup='podman-compose down && podman-compose pull && podman-compose up -d'
    

    This extract function (which I didn’t make myself, I got it from when I was using nakeDeb) has been pretty useful too.

    function extract()
    {
         if [ -f $1 ] ; then
             case $1 in
                 *.tar.bz2)   tar xvjf $1     ;;
                 *.tar.gz)    tar xvzf $1     ;;
                 *.bz2)       bunzip2 $1      ;;
                 *.rar)       unrar x $1      ;;
                 *.gz)        gunzip $1       ;;
                 *.tar)       tar xvf $1      ;;
                 *.tbz2)      tar xvjf $1     ;;
                 *.tgz)       tar xvzf $1     ;;
                 *.zip)       unzip $1        ;;
                 *.Z)         uncompress $1   ;;
                 *.7z)        7z x $1         ;;
                 *.xz)        unxz $1         ;;
                 *)           echo "'$1' cannot be extracted via >extract<" ;;
             esac
         else
             echo "'$1' is not a valid file"
         fi
    }
    
    • frozen@lemmy.frozeninferno.xyz
      link
      fedilink
      arrow-up
      0
      ·
      1 year ago

      I have a similar docker/podman alias, except I pull first. This greatly reduces downtime between down and up, which is nice for critical services.

  • juipeltje@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    For me it’s pretty basic. It’s mostly aliases for nix related commands, like rebuild-switch, updating, garbage collecting, because those nix commands are pretty lenghty, especially with having to point to your flake and everything. I’m thinking of maybe adding an alias for cyanrip (cli cd ripper), because i recently ripped my entire cd collection, but going forward if i buy another cd every now and then, i’ll probably end up forgetting about which flags i used.

  • odc@lemmy.sdf.org
    link
    fedilink
    English
    arrow-up
    0
    ·
    1 year ago

    I’ll share 3:

    alias chx='chmod +x'
    alias rr='rm -rf'
    alias shrug="echo '¯\_(ツ)_/¯'"
    
  • utopiah@lemmy.ml
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    To answer your question realistically I did history | sed "s/.* //" | sort | uniq -c | sort -n

    which returned as first non standard command lr which from my grep lr ~/.bashrc is alias lr="ls -lrth"

    • thingsiplay@beehaw.org
      link
      fedilink
      arrow-up
      0
      ·
      1 year ago

      A few days ago I posted a one-liner to do the same thing too. It will resolve aliases from your history and expand program paths to its fullpath. I thought you might be interested: https://beehaw.org/post/20584479

      type -P $(awk '{print $1}' ~/.bash_history | sort -u) | sort
      
  • WQMann@lemmy.ml
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    I replaced rm with trash-put, just in case I realize I need some files that I removed down the line.

    alias rm='trash-put'
    

    Official author don’t recommend it due to different semantics. But honestly for my own personal use case its fine for me.


    Also I like to alias xclip:

    alias clippy='xclip -selection clipboard'
    
    # cat things.txt | clippy
    
    • thingsiplay@beehaw.org
      link
      fedilink
      arrow-up
      0
      ·
      1 year ago

      Little tip: In case you need to use rm directly, even with the alias in effect, you can put a backslah in front of the command to use its original meaning: \rm filename

        • thingsiplay@beehaw.org
          link
          fedilink
          arrow-up
          0
          ·
          edit-2
          1 year ago

          I’m not sure what you mean with the question. If you have any alias like alias rm='ls -l in your .bashrc in example, then you cannot use the original command rm anymore, as it is aliased to something else. I’m speaking about the terminal, when you enter the command. However, if you put a backslash in front of it like \rm in the terminal, then the alias for it is ignored and the original command is executed instead.

          Edit: Made a more clear alias example.

          • XXIC3CXSTL3Z@lemmy.ml
            link
            fedilink
            arrow-up
            0
            ·
            1 year ago

            Oh ty ty that answers my question! I am fairly new to being a poweruser on linux so I may have worded that wrong XD

    • IsoKiero@sopuli.xyz
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 year ago

      Official author don’t recommend it due to different semantics. But honestly for my own personal use case its fine for me.

      I don’t recommend that either. If you get used to that ‘rm’ doesn’t actually remove files and then your alias is missing for whatever reason it’ll bite you in the rear at some point. And obviously the same hazard goes with a ton of other commands too.

  • kibiz0r@midwest.social
    link
    fedilink
    English
    arrow-up
    0
    ·
    1 year ago

    I often want to know the status code of a curl request, but I don’t want that extra information to mess with the response body that it prints to stdout.

    What to do?

    Render an image instead, of course!

    curlcat takes the same params as curl, but it uses iTerm2’s imgcat tool to draw an “HTTP Cat” of the status code.

    It even sends the image to stderr instead of stdout, so you can still pipe curlcat to jq or something.

    #!/usr/bin/env zsh
    
    stdoutfile=$( mktemp )
    curl -sw "\n%{http_code}" $@ > $stdoutfile
    exitcode=$?
    
    if [[ $exitcode == 0 ]]; then
      statuscode=$( cat $stdoutfile | tail -1 )
    
      if [[ ! -f $HOME/.httpcat$statuscode ]]; then
        curl -so $HOME/.httpcat$statuscode https://http.cat/$statuscode
      fi
    
      imgcat $HOME/.httpcat$statuscode 1>&2
    fi
    
    cat $stdoutfile | ghead -n -1
    
    exit $exitcode
    
  • qpsLCV5@lemmy.ml
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    it’s somewhat vibe coded but the one i probably use the most is this one to swap between speakers and headset. the device name to look for is just put directly in there, it’d take some adjustment to run it on different machines. this is in my .bashrc:

    # switch sinks
    toggle_audio() {
      # Find headset sink ID dynamically
      headset_id=$(pactl list sinks short | grep "Plantronics" | awk '{print $1}')
      
      # Find speakers sink ID dynamically
      speakers_id=$(pactl list sinks short | grep "pci-0000_05_00.6" | awk '{print $1}')
      
      # Get current default sink
      current_sink=$(pactl get-default-sink)
      
      # Get current sink ID
      current_id=$(pactl list sinks short | grep "$current_sink" | awk '{print $1}')
      
      # Toggle between the two
      if [ "$current_id" = "$headset_id" ]; then
        pactl set-default-sink "$speakers_id"
        echo "Switched to speakers (Sink $speakers_id)"
      else
        pactl set-default-sink "$headset_id"
        echo "Switched to headset (Sink $headset_id)"
      fi
    }
    

    generally i try not to use too many custom things because for work i regularly work on all kinds of different servers and i’ve just been too lazy to set up some solution to keep it all in sync. someday…

  • Ritsu4Life@lemmy.world
    link
    fedilink
    arrow-up
    0
    ·
    1 year ago

    I have started my daily drawing journey which i still am bad at it. To create a new .kra files files every day I use this

    #/usr/bin/bash
    
    days=$(</var/home/monika/scripts/days)
    echo "$days"
    
    file_name=/var/home/monika/Pictures/Art/day$days.kra
    
    if [ -f $file_name ]; then
      echo file is present
    else
      if [[ $days%7 -eq 0 ]]; then
        echo "Week completed"
      fi
      cp "/var/home/monika/scripts/duplicate.kra" $file_name
      flatpak run org.kde.krita $file_name
      echo $(($days + 1)) >/var/home/monika/scripts/days
    fi
    
    
  • Daniel Quinn@lemmy.ca
    link
    fedilink
    English
    arrow-up
    0
    ·
    1 year ago

    I have a few interesting ones.

    Download a video:

    alias yt="yt-dlp -o '%(title)s-%(id)s.%(ext)s' "
    

    Execute the previous command as root:

    alias please='sudo $(fc -n -l -1)'
    

    Delete all the Docker things. I do this surprisingly often:

    alias docker-nuke="docker system prune --all --volumes --force"
    

    This is a handy one for detecting a hard link

    function is-hardlink {
      count=$(stat -c %h -- "${1}")
      if [ "${count}" -gt 1 ]; then
        echo "Yes.  There are ${count} links to this file."
      else
        echo "Nope.  This file is unique."
      fi
    }
    

    I run this one pretty much every day. Regardless of the distro I’m using, it Updates All The Things:

    function up {
      if [[ $(command -v yay) ]]; then
        yay -Syu --noconfirm
        yay -Yc --noconfirm
      elif [[ $(command -v apt) ]]; then
        sudo apt update
        sudo apt upgrade -y
        sudo apt autoremove -y
      fi
      flatpak update --assumeyes
      flatpak remove --unused --assumeyes
    }
    

    I maintain an aliases file in GitLab with all the stuff I have in my environment if anyone is curious.

    • golden_zealot@lemmy.ml
      link
      fedilink
      English
      arrow-up
      0
      ·
      1 year ago

      Execute the previous command as root

      Fun fact if you are using bash, !! will evaluate to the previous command, so if you miss sudo on some long command, you can also just do sudo !!.

      • jwt@programming.dev
        link
        fedilink
        arrow-up
        0
        ·
        1 year ago

        With the added benefit of it looking like you’re yelling at your prompt in order to get it to use sudo.