Spyke
lemmy.one

sudo !! to rerun last command as sudo.

history can be paired with !5 to run the fifth command listed in history.

68

@[email protected] I've been working in the bash shell since 1993 and did not know sudo !! was a thing. Good lord, I no longer have to press up, press crtl-left a bunch of times, then type sudo enter space anymore. And I can give it an easy-to-remember alias like 'resu' or 'redo'! Ahahaha, this changes everything! Thank you!!

5

We're all learning tricks in this thread! Grateful for all y'all nerds.

2
communismreply
lemmy.ml

Fifth as in fifth most recent command or fifth oldest?

5

I believe it's the fifth oldest - I think !-5 will get you the fifth impost recent, but I was shown that and haven't put it into practice.

The most common usecase I do is something like history | grep docker to find docker commands I've ran, then use ! followed by the number associated with the command I want to run in history.

7

Love these, I used a terminal select from history with fuzzy finding to do the !5 as redo

1
lemmy.one

Don't forget your pwd thrown in to get back your bearings!

2
discuss.tchncs.de

It's in the official docs for zoxide, you are supposed to use the z alias, and many distros just set it up directly like that. I love doing z notes from wherever I am.

5

Description fifth point (5.)

That doesn't require a separate package, especially one which uses eval on every new shell. And isn't messing with my distros or personal aliases (and doesn't introduce cargo-packaging).

Simply adding one to two (you get the gist) directories and a keybind for cd .. is more slick. There are cases where you might use pushd . but even then other tooling should already cover your needs.

It's also so easy that you can temporarly append to $CDPATH for a specific session. But again, then a second pane or pushd is already available.

Now downvote me, lemmy.

1

When you set it up you tell it which command you want. Default is "cdi" but I changed it to "cd" immediately.

3
Jeenareply
piefed.jeena.net

Hm I wonder, is it really a command? I thought it is just a function of the shell to change the working directory.

6
lemmy.world

tldr because I am too impatient to read through man pages or google the exact syntax for what I want to do.

42
lemm.ee

There are exactly three kinds of manpages:

  1. Way too detailed
  2. Not nearly detailed enough
  3. There is no manpage

I will take 1 any day over 2 or 3. Sometimes I even need 1, so I'm grateful for them.

But holy goddamn is it awful when I just want to use a command for aguably its most common use case and the flag or option for that is lost in a crowd of 30 other switches or buried under some modal subcommand. grep helps if you already know the switch, which isn't always.

You could argue commands like this don't have "arguably most common usecases", so manpages should be completely neutral on singling out examples. But I think the existence of tl;dr is the counterargument.

Tangent complaint: I thought the Unix philosophy was "do one thing, and do it well"? Why then do so many of these shell commands have a billion options? Mostly /s but sometimes it's flustering.

26
lemmy.ml

tldr is the first of 4 ways I rtfm. Then -h, man, and then the arch wiki

8
lemmy.ml

I can appreciate that. Appologies if you know this already, but just don't like them. Here are some tips.

It helps a lot to get title/subtitle/flag highlighting. By default man pages are hard to use simply because of how dense they are. It's much easier to skim when you can separate the parts you are looking for up front from the text.

Don't forget '/', 'n', and 'N'. First way to use man pages more effectively is to search them easily. And you can search via regex. Often I'm looking for more info on a particular flag. So I'll press '/' followed by '^ *-g'. For a g flag.

Take notes on the side. It saves you time later. Your future self will thank you. And you learn a lot by skimming them.

Man pages can be intimidating/confusing, but, imho, it's worth training that skill. Even if you are slower up front, it's totally worth it.

5
vrighterreply
discuss.tchncs.de

thanks for the advice. I knew about the search feature, but sometimes the stuff you need isn't even on the page. I have no idea how to find what I need when it's not in "man cmdname" how am I supposed to know that the feature i want has a dedicated page?

how could I find certain commands if i didn't already know it was a shell builtin and not a command? It's not like you get a manpage saying "this is not a command". And even if i did have the idea to open the bash page, it's still useless, because builtins are their own dedicated page. That sort of stuff. It rarely ever makes things easier for me.

edit, it is occasionally useful phen I have already found what I want on google and just want some more in depth details.

2

Unfortunately, sometimes (often) there is no man page for what you are looking for. So if you get a page not found, that's usually the case. You can usually find associated pages all the way at the bottom. That helps when what you are looking for isn't a command, but a reference. I don't remember exactly where it is, but man pages are stored in a directory. Probably /etc or /usr. You can always dump that list into fzf or use grep to search to see if there is a page for what you are looking for. It's not a perfect system by any means, but it's a good one to have in your toolbelt.

1
JackbyDevreply
programming.dev

If something is a bash built-in run help blah for it's "man page"

But yeah, man pages tick me off. Wait until you learn that there are sometimes more than one per command. I have to Google which page is which because they're all for specific things. man foo is the equivalent of man 1 foo. What's annoying is that the few times I've seen something referenced on another page the entry usually just says something like it's on "the relevant man page" rather than just telling you exactly which.

1

ok but that still entails trying random things until i find it. If I didn't already know it was a builtin i wouldn't know to search there. The bash thing was just an example. I have learned this stuff since i encountered the problem. This is just me recollecting my experience of trying to use man

1
lemmy.world

tldr is great. Basically a crowd-sourced alternative to man with much more concise entries. Example:

$ tldr dhcpcd

  DHCP client.
  More information: <https://roy.marples.name/projects/dhcpcd>.

  Release all address leases:

      sudo dhcpcd --release

  Request the DHCP server for new leases:

      sudo dhcpcd --rebind
38
lemmy.ml

I went a little overboard and wrote a one-liner to accurately answer this question

history|cut -d " " -f 5|sort|uniq -c|sort -nr|head -5

Note: history displays like this for me 20622 2023-02-18 16:41:23 ls I don't know if that's because I set HISTTIMEFORMAT='%F %T ' in .bashrc, or if it's like that for everyone. If it's different for you change -f 5 to target the command. Use -f 5-7 to include flags and arguments.

My top 5 (since last install)

   2002 ls
   1296 cd
    455 hx
    427 g
    316 find

g is an alias for gitui. When I include flags and arguments most of the top commands are aliases, often shortcuts to a project directory.

Not to ramble, but after doing this I figured I should alias the longest, most-used commands (even aliasing ls to l could have saved 2002 keystrokes :P) So I wrote another one-liner to check for available single characters to alias with:

for c in a b c d e f g h i j k l m n o p q r s t u v w x y z; do [[ ! $(command -v $c) ]] && echo $c; done

In .bash_aliases I've added alias b='hx ${HOME}/.bash_aliases' to quickly edit aliases and alias r='source ${HOME}/.bashrc' to reload them.

36
pemptagoreply
lemmy.ml

Yup! Migrated from VSCodium; wanted to learn a modal editor but didn't have the time or confidence to configure vim or neovim. It's been my go-to editor for 2+ years now.

6
lemmy.world

I've been using vi (just the basics) for ~4 years, I don't think I could be arsed to pick up the keybindings the other way around lol. I've heard very good things about Helix, of course

4

As another longtime Vi user - I had a hell of a time & wound up switching back lol

I think for a lot of folks Helix would be intuitive. Vi has her hooks in me, though.

3

Sudo !!

It reruns the last command as sudo.

Pretty useful since I'm always forgetting.

36
lemmy.world

clear because apparently I am too scatterbrained to comprehend more than one full page of text in the terminal

31

I like using CRTL+L to clear. It's nice because you can have a command typed out and still be able to press CTRL+L to clear the screen and keep the command typed out.

6
lemmy.dbzer0.com

I almost never use clear because i'm afraid if i will need the text later.(just like infinity tab number on firefox)

4

Oh god I also do this... See the comment below, I ran history|cut -d " " -f 5|sort|uniq -c|sort -nr|less on my personal laptop, my third most commonly used command (behind ls and cd) is just typing in nothing...

1
lemmy.dbzer0.com

100 tabs is in mobile. I don't even scroll back to clutter my brain but its there. Tabs are history for me... So I use firefox focus and if there is anything important, i open with firefox.

What script are you reffering to? To log all output? I don't wanna store that but need an assurance that its there till i close terminal window lol

1
lemmy.ml

Since nobody has said yet, I use screen pretty heavily. Want to run a long running task, starting it from your phone? Run screen to create a detachable session then the long running command. You can then safely close out of your terminal or detach with ctrl a, d and continue in your terminal doing something else. screen -r to get back to it.

28
gitamarreply
feddit.org

I recently switched to tmux and boy, it's way better. I basically use only tmux now anymore. Creating panes to have two processes in one glance, multiple windows, awesome. Plus all the benefits of screen.

8

Try zellij. Not as popular as tmux, but very intuitive to use.

3

Maybe someone reading wants to now about prefix+s. This doubles your excitement.

1
lemmy.one

In a similar vein, nohup lets you send tasks to the background and seems to be everywhere.

7
pyr0ballreply
sh.itjust.works

You can't mention nohup without at least mentioning kill -9 or pkill to slay the monster you created you madman

3
lemmy.one

Sometimes I'll just reboot the entire damn machine just to be safe ;)

2
Cavemanreply
lemmy.world

Eyyy, don't hate, this is how I start all my work programs. That command is really nice and creates all work programs as children of a single terminal session for easy closing later.

1

No hate! Just need to make sure people know so they don't create a bazillion threads without realizing it, or how to stop them effectively

2

Also, screen can connect to an UART device or serial or anything that offers up a TTY

4
muzzlereply
lemm.ee

I Always forget to run screen first, so I just rely heavily on dtach

2
suppo.fi

Simply change your terminal command to execute the terminal multiplexer of your choice.

man terminal_of_choice, look for (start) command.

1
krashreply
lemmy.ml

How does screen / tmux work when detached from a session, how does it keep the session alive (both when running locally, and while ssh:ing to a server)? Is there a daemon involved?

1

You can find out by running screen and executing pstree, that way you can see how the screen process is run.

1
lemmy.world

pushd and popd to change directory and go back when done there.

24
llukireply
feddit.org

cd - undoes the last cd. Not quite push/popd but still useful. Pro tip, works also: git checkout -

28
medreply
sh.itjust.works

Hell yeah. Every one of these threads makes me more inclined to read man pages

4

Not a specific command, but I learned recently you can just dump any executable script into ~/bin and run it from the terminal.

I suffer greatly from analysis paralysis, I have a very hard time making decisions especially if there's many options. So I wrote a script that reads a text file full of tasks and just picks one. It took me like ten minutes to write and now I spend far more time doing stuff instead of doing nothing and feeling badly that I can't decide what to do.

23

This is because $HOME/bin is in your $PATH environment variable. You can add more paths that you'd like to execute scripts from, like a personal git repo that contains your scripts.

23
lud
lemm.ee

As primarily a Windows admin (Yes, we exist on Lemmy ;) ) here are few I use often.

  • Enter-PSSesion
  • Get-ADUser (also group and computer)
  • CLS (aka the superior clear)
  • ii . (short for Invoke-Item . which runs the selected object using the default method. For paths (like .) the default is explorer, so ii . opens the current directory using explorer.)
  • ft (short for Format-Table formats piped input as a table.)
  • fl (short for format-like. Used like ft but for lists.)
  • Where-Object
  • Select-Object
20

There are dozens of us.

Also, I'll add:

  • Get-Help
  • Get-Command
  • Get-Member
8

pv (Pipe Viewer) is a command line tool to view verbose information about data streamed/piped through it. The data can be of any source like files, block devices, network streams etc. It shows the amount of data passed through, time running, progress bar, percentage and the estimated completion time.

19
lemm.ee

Oh. I know. But you don't understand - I'm compelled to type it out. I must.

24
suppo.fi

Someone who doesn't know the benefits of dedicated, unlimited scrollback buffers. This command is useful but has a bad effect (when unintentionally).

1

No, I do. Clearing the scrollback because I want a new, very clear start point for a new activity and don't care about the rest is frequently the goal.

If I do care about the history I'll do something else or possibly been saving history to disk, although that's far more rare.

2

i’d like to introduce you to your new best friend: reset… it doesn’t everything clear does and a LOT more

1

haha, I can relate to that :). formerly I had the compulsion to execute sync frequently. Now my compulsion is to push Ctrl-C like 4 times every time I need it. I read somewhere that's common because of ^C has a lack of feedback to the user, so, a script showing an alert that the clipboard received some information helped with this compulsion.

1

The amount of times i tried to dir in linux and ls in windows is mire than I like to admit

5
lemmy.world

CTR + u will delete the whole command. I use that a lot so I don't have to backspace. It's saved me a ton of time

15

How about ctrl+c to cancel and clear the command you are typing? It's much easier because you only need 1 hand, and does not impact your shell's history.

4

xdg-open FILE - opens a file with the default GUI app. I use it for example to open PDFs and PNG. I have a one letter alias for that. It can also open a file explorer in the current directory xdg-open . . Should work on any compliant desktop environment (gnome/kde).

13

diff -y -W 200 file1 file2

Shows a side by side diff of 2 files with enough column width to see most of what I need usually.

I have actually aliased this command as diffy

ctrl-r

searching bash history

du -sh * | sort -h

shows size of all files and dirs in the current dir and sorts them in ascending order so you can easily see the largest files or dirt ant the end of the list

ls -ltr

Shows the most recently modified files at the end of the listing.

9
Albbireply
lemmy.ca

I recently found btop and am trying to use that instead of htop.

10
Sneezycatreply
sopuli.xyz

I just aliased "sudo pacman -Syu && yay -Syu --aur" to "update" cause I got tired of writing it every day.

3
MinFapperreply
startrek.website

You can just run yay with no arguments and it does exactly what your update script does.

14
Noxiousreply
fedia.io

Wouldn't it make more sense to just yay -Syu to update everything, normal packages and AUR packages?

1
Sneezycatreply
sopuli.xyz

The reason I did it like this is because:

  • I didn't know yay could invoke pacman
  • I didn't want yay "upgrading" my pacman packages with AUR packages.

But I was just misunderstanding yay. As another comment said before you, one can just run yay without any arguments and it accomplishes the same thing.

1

Yay doesn't replace normal packages with AUR packages. Btw It's not just an AUR helper, it's a wrapper for Pacman with AUR support built-in. Check out paru btw, it's a more modern version of yay that basically works the same way: https://github.com/Morganamilo/paru

2
  • man
  • fd
  • entr
  • rg
  • gitui
  • nvim
  • tee
  • cd
  • mv
  • rm
  • ls
  • tmux
  • btop
  • yazi
  • du
  • xargs
  • cat
  • less
9

Never heard of it, looks cool but not as pretty as btop. Also has a ton of information I don't personally care about so for me it doesn't seem great.

1
sh.itjust.works

I recently learned to use a for loop on the command line to organize hundreds of files in a few seconds.

8
sh.itjust.works

Let's say, for example, you have a directory of files named x01-001; x01-002; x02-001; x02-002; x03-001... and so on.

I want to create subdirectories for each 'x' iteration and move each set to the corresponding subdirectory. My loop would look like this:

for i in {1..3}; do mkdir Data_x0$i && mv x0$i* Data_x0$i; done

I've also been using it if I need to rename large batches of files quickly.

9
lemmy.world

Check out rename

$ touch foo{1..5}.txt
$ rename -v 's/foo/bar/' foo*
foo1.txt renamed as bar1.txt
foo2.txt renamed as bar2.txt
foo3.txt renamed as bar3.txt
foo4.txt renamed as bar4.txt
foo5.txt renamed as bar5.txt
$ rename -v 's/\.txt/.text/' *.txt
bar1.txt renamed as bar1.text
bar2.txt renamed as bar2.text
bar3.txt renamed as bar3.text
bar4.txt renamed as bar4.text
bar5.txt renamed as bar5.text
$ rename -v 's/(.*)\.text/1234-$1.txt/' *.text
bar1.text renamed as 1234-bar1.txt
bar2.text renamed as 1234-bar2.txt
bar3.text renamed as 1234-bar3.txt
bar4.text renamed as 1234-bar4.txt
bar5.text renamed as 1234-bar5.txt
9
lemmy.ml

In your second example, it looks like you have an escape character before the first 'dot', but not the second one. Is this a typo, or am I misunderstanding the command?

1

It's not a typo. The first section of the regex is a matching section, where a dot means "match any character", and an escaped dot is a literal dot character. The second section is the replacement section, and you don't have to escape the dot there because that section isn't matching anything. You can escape it though if it makes the code easier to read.

rename is written in Perl so all Perl regular expression syntaxes are valid.

However, your comment did make me realize that I hadn't escaped a dot in the third example! So I fixed that.

4

xargs is also fun, and assuming your for loop doesn't update anything out of the loop, is highly parallelizable

The equivalent of the same command, that handles 10 tasks concurrently, using %% as a variable placeholder.

seq 1 100 | xargs -I'%%' -P 10 sh -c 'mkdir Data_X0%% && mv x0%%* Data_X0%%;'

But for mass renaming files, dired along with rectangle-select and multicursors within Emacs is my goto.

4
sh.itjust.works

Not a command as much as I press the up arrow a lot. I'm.pretty lazy and hitting the up arrow 12 times is easier then retyping a complex rsync command.

8

If you know it was the most recent rsync command: just type !rsync.

4

I actually use fish on my personal machine. But the servers I manage are pretty basic to save space and all just use stock bash.

2
lemmy.ml

For Debian based/descended distros:

sudo apt-get update && sudo apt-get upgrade

And technically I also regularly use

redshift -O 3000

all of the blue light filter programs try to align themselves with a user's geographic location and time, but I don't keep normal hours

7

I would but much like somebody else's recent post I have in the past nuked my install by blindly agreeing to some recommended software removals before. These days I like to double check what packages are being updated and replaced.

6
lemmy.world

g-push which is alias for

git push origin `git branch --show`

Which I'm writing on my phone without testing or looking

6

git push origin HEAD is a slightly shorter way of doing the same thing, even though you have an alias anyway lol

2
JackbyDevreply
programming.dev

git config --global alias.pusho 'push --set-upstream origin HEAD'

You're welcome.

2
Cavemanreply
lemmy.world

So that's making git push always push to the current branch?

1

When you're pushing a new branch you've never pushed before you need the -u command. That's what this alias is for.

As long as the config's push.default isn't matching, git push without arguments will only push the current branch.

1
waldenreply
sub.wetshaving.social

I learned you can edit .bashrc (in your home dir) and update the alias for ls to include what I like. It has saved me lots of keystrokes. Mine is ls -lha in addition to whatever color coding stuff is there by default.

6
waldenreply

Aha. Well, I guess I'm not the target audience because I can't be bothered to go through the installation steps. It's not in the LMDE repository, but I wish it were!

1

It's pretty easy. You either get it from Cargo (the Rust package manager) or add a custom repo to apt.

Cargo is the easier and safer option: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh && cargo install eza

Custom apt repo:

sudo apt update
sudo apt install -y gpg

sudo mkdir -p /etc/apt/keyrings
wget -qO- https://raw.githubusercontent.com/eza-community/eza/main/deb.asc | sudo gpg --dearmor -o /etc/apt/keyrings/gierens.gpg
echo "deb [signed-by=/etc/apt/keyrings/gierens.gpg] http://deb.gierens.de stable main" | sudo tee /etc/apt/sources.list.d/gierens.list
sudo chmod 644 /etc/apt/keyrings/gierens.gpg /etc/apt/sources.list.d/gierens.list
sudo apt update
sudo apt install -y eza

In my opinion though, you should also try lsd. It's even better than eza. You can also get it from Cargo, just a simple cargo install lsd.

1
jbrainsreply
sh.itjust.works

No, eza is one of those modern Rust replacement programs. It replaces ls.

3
embreply
lemmy.world

There's a whole bunch of cool modern replacements. Here's a handy list: altbox.dev

I personally use bat and rg all the time, and find them much more suitable for everyday tasks.

Edit: And to clarify, I didn't create either list, they're just ones I'd bookmarked at some point.

1
4amreply

A lot of distros include a .bashrc with common ls aliases commented out, just waiting for you to activate them if you like.

2

Another ls alias I'm a fan of is ls -latr which I alias to lt. It gives you a time sorted directory listing with the most recent next to your cursor (helpful for large directories).

3

du -sh /too/bar to get size of files/folders. sudo !! inserts sudo into previous command when forgotten. yay for full system update if yay is installed. cat reads files.

5

How slow, because I neved noticed

Trust me, it's noticeable. Or at least it was for me. Numbers wise, it doesn't sound like much, but the difference between 0.05s and 0.5s (which are roughly the times I was experiencing) is very noticeable, at least for me. One is done before you've even fully lifted the finger off the key, while with the other you're preparing to press (or maybe you're already pressing pressing) the next key, by the time you see a reaction.

Your mileage may vary.

1

Getting cheatsheets via curl cheat.sh/INSERT_COMMAND_HERE

No install necessary, Also, you can quickly search within the cheatsheets via ~. For example if you copy curl cheat.sh/ls~find will show all the examples of ls that use find. If you remove ~find, then it shows all examples of ls.

I have a function in my bash alias for it (also piped into more for readability):

function cht() { curl cheat.sh/"$1"?style=igor|more }

5
Bob
feddit.nl

I use "ping" every time I suspect my internet might be going a bit slow.

5
LordCromreply
lemmy.world

Try mtr . It'll run kinda like a trace route but will show you where the delay is happening. Still relys in icmp not being blocked

1
Bobreply
feddit.nl

'mtr' is not recognized as an internal or external command, operable program or batch file.

I assume I'm on the wrong OS.

1

Fzf is so useful its ridiculous. I recreated the functionality of sshs with fzf and a small bash script.

2

Check out the fzf docs. It ships with helpers that offer better shell integration than you're getting here.

1

Agree. Don’t just copy and paste CLI commands you find on the internet, suggested by a stranger

13

The most deceptive joke I've seen on this is sudo rm -fr / to remove the French language pack

9

I really like how nushell can parse output into it's native structures called tables using the detect command.

Unlike string outputs, tables allow for easy data manipulation through pipes like select foo will select foo key and you can filter and even reshape the datasets.

This is great if you need to work with large data pipes like kuberneters so you can do something like:

kubectl get pods --all-namespaces | detect columns | where $it.STATUS !~ "Running|Completed" | par-each { |it| kubectl -n $it.NAMESPACE delete pod $it.NAME }

This looks complex but it parses kubectl table string to table object -> filters rows only where status is not running or completed -> executes pod delete task for each row in parallel.

Nushell take a while to learn but having real data objects in your terminal pipes is incredible! Especially with the detect command.

There's are few more shells that do that though nu is the most mature one I've seen so far.

4
lemmy.ml

qmv -f do ${dir}

... for quickly moving and renaming files. The default 'qmv' opens up your preferred text editor with a list of the source and destination name of the directory of files you want to move/rename. The '-f do' tells the command we only want to see/edit the [d]estination [o]nly. If you need to rename/move a bunch of files, it's much quicker to do it in vim (at least for me).

4

locate, from the mlocate package. So useful. Honorable mention goes out to tldr.

4

Zoxide, dust, fd, rg, btm, tokei. So many newer Rust tools that are way better than the old stuff.

3
lemmy.world

Have you heard of our lord and saviour rclone?

I haven't gone back to rsync in a long time.

2

I use it to mount cloud storage as network drives... I'll have to look into your implication though!

1
sh.itjust.works

not sure if it counts as a command, but i use the up arrow to scroll through previous commands like, almost every time I open a terminal.

3

If you change the colours to night mode, the song "Sister Christian" starts playing

3
lemmy.ml

$ z

A great cd alternative

Also $ sudo paru -Syu

2
feddit.uk

I think you can run paru without sudo and it should still do privilege escalation no problem

3
kangreply

oh yes, i was thinking about pacman when writing haha

1

I often play an old DOS game in DOSBox, and when I exit it doesn't reset the screen resolution. So I reset it manually by typing

xrandr -output e-DP1 -auto

2

More of a shortcut, CTRL + A + D to exit the current session (exits a sudo su first, then a ssh, then the actual terminal)

2
feddit.uk

It's been abandoned btw. People recommend to switch to alternatives. Fastfetch and hyfetch seem to be the best ones rn.

Though I can't confirm as I wrote my own minimal fetch

1
lemmy.world

[2J[H

Is one of my favorites. Of course, most of you are too young to know what that means.

-1
JackbyDevreply
programming.dev

Is that easier than typing clear? Also, not sure why you'd say something like that about people's age. Anyone using terminals today is often going to run into weird quirks of them being around for decades even if they're young.

3