What's a handy terminal command you use often?

cybercitizen4@lemm.ee to Asklemmy@lemmy.ml – 97 points –
128

I really like that cd command. :P

You'll love zoxide then.

It's six letters. Can't they just call it zd or something? Yeah sure, I can use aliases, but why complicate in the first place?

The command is 'z'

This is most probably a distro-specific aliasing. Tried it on Guix, it does not work:

$ z
bash: z: command not found

$ zoxide
zoxide 0.9.2
....

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.

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.

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

sudo !! to rerun last command as sudo.

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

Fifth as in fifth most recent command or fifth oldest?

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.

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.

I would know this as tmux, is there a difference?

no, tmux is a newer screen. some of us havent switched cos we're too lazy i guess? i think the common wisdom is that it's better. i havent tried cos i already know enough of screen and it's fine for me

gnu screen is just a different program than tmux. they do the same thing though

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

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

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.

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

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

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

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

man terminal_of_choice, look for (start) command.

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?

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

Even better when cd automatically invokes pushd.

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

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

sudo udevadm monitor

Figuring out which usb device went on holiday.

Wow, super useful command. Starring this comment

clear. Constantly, and for no reason.

Ctrl-L

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

I used to, but the terminal clear is better, so I don't.

CMD/CTRL-K for me.

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

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.

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.

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.

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

Example of said Black Magik?

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.

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

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.

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

Related: Alt + ., to cycle through arguments used in previous commands

control+R

in bash, it lets you quickly search for previously executed commands.

its very useful and makes things much quicker, i recommend you give it a try.

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

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.

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

sudo rm -rf /

Very powerful yet helpful command :-)

Someone has to say this. Don't do it anyone

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

doesnt actually do anything on gnu rm, and hasnt in like a decade. but yeah, dont do it.

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

history | fzf

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

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

ls -al

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.

You might like eza even more!

Hmm, that's not working for me. You mean use those as options? 'ls -eza'?

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

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

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).

  • atools, which includes als, aunpack, apack. so you can stop caring about the kind of archive and just unpack it. it also saves you from shit archives that have multiple files/dirs in their root.
  • perl -e / perl -lne / ...
  • units
  • bc - a calculator that's actually good
  • pass - the only non-shit password store tool i've found so far. no gui, uses gpg and git to do the encrypting and storage/sharing
  • alias lr='ls -lrth' - so you can easily find the newest file, cos that's frequently what you want
  • unip - my script to look up things in the unicode db
  • find -type f -exec xzgrep 're' {} + - because xzgrep cant do -r

oh yeah, and for the shell readline, alt-b, alt-f, ctrl-w, ctrl-u, ctrl-k, ctrl-a, ctrl-e

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

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 }

Sudo !!

It reruns the last command as sudo.

Pretty useful since I'm always forgetting.

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.