What's a handy terminal command you use often?
What's a handy terminal command you use often?
What's a handy terminal command you use often?
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.
@papertowels@lemmy.one 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!!
We're all learning tricks in this thread! Grateful for all y'all nerds.
Love these, I used a terminal select from history with fuzzy finding to do the !5 as redo
cd
then ls
then cd
then ls
maybe I’ll throw a ls -a
I use -A instead, which doesn't show "." and ".."
Nah you gotta alias ls -a to la for more efficiency.
l
Don't forget your pwd
thrown in to get back your bearings!
Done be silly, that's part of my prompt.
I really like that cd
command. :P
You'll love zoxide
then.
Hm I wonder, is it really a command? I thought it is just a function of the shell to change the working directory.
A command is anything you execute in the shell.
cd
is just a built-in command
tldr
because I am too impatient to read through man pages or google the exact syntax for what I want to do.
There are exactly three kinds of manpages:
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.
tldr is the first of 4 ways I rtfm. Then -h, man, and then the arch wiki
I went a little overboard and wrote a one-liner to accurately answer this question
bash
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)
undefined
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:
bash
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.
Helix?
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.
Holy shit, you're a madman
history -i
sudo udevadm monitor
Figuring out which usb device went on holiday.
Wow, super useful command. Starring this comment
tldr
is great. Basically a crowd-sourced alternative to man
with much more concise entries. Example:
undefined
$ 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
Well....slap my ass and call me Mary.....
Thanks kind internet stranger!
:O
Woah, that's dope as heck. Thank you!
Sudo !!
It reruns the last command as sudo.
Pretty useful since I'm always forgetting.
Most commands soon followed by sudo !!
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.
I use this constantly
i rely on this in my job. if i really need it, i'll be using it often enough that it'll always be in ^R distance :)
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
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.
Try zellij. Not as popular as tmux, but very intuitive to use.
Maybe someone reading wants to now about prefix+s
. This doubles your excitement.
In a similar vein, nohup
lets you send tasks to the background and seems to be everywhere.
You can't mention nohup
without at least mentioning kill -9
or pkill
to slay the monster you created you madman
Also, screen can connect to an UART device or serial or anything that offers up a TTY
Don’t use screen
, but I do use tmux
pretty heavily.
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?
You can find out by running screen and executing pstree, that way you can see how the screen process is run.
clear
because apparently I am too scatterbrained to comprehend more than one full page of text in the terminal
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.
I almost never use clear because i'm afraid if i will need the text later.(just like infinity tab number on firefox)
<enter>
* 20Use script instead, you can even have it in your .*shrc to run automatically whenever a shell is invoked (make sure to add a check that the shell wasn't invoked by script, so you don't inadvertently forkbomb yourself)
Alternatively, just use Terminator as yout terminal emulator and enable the logger anytime you need it to record the shell session.
Also, use bookmarks. That's what they're there for. 100 tabs is a great way to clutter your brain, but terrible for productivity. If you forget about it after bookmarking, it wasn't important to begin with.
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 think the standard is ~/.local/bin, for the people that like standards.
or add it to path
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 -
what's your alias?
After using too much WINE,
I type pwd
, whoami
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.
CMD/CTRL-K for me.
I like it so much I alised it to c
.
i’d like to introduce you to your new best friend: reset… it doesn’t everything clear does and a LOT more
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.
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.
Uhhh...sudo su
Don't be like me
sudo -i
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
Fucking hell Lol 😂
There are dozens of us.
Also, I'll add:
cd
every single day.
You haven't discovered exa
? Noob
/s
ls and cd
I sometimes hit ls and then need to type dir.
The amount of times i tried to dir in linux and ls in windows is mire than I like to admit
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
I just use control C, is there a difference other than whether the line shows up or not?
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.
This is great for when you type in your root password incorrectly!
And then ctrl+y to paste it back and recover that text.
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).
undefined
sl
Choo choo!
A lemming of taste
ls
ls -ltrc
Show most recently modified files.
sudo pacman -Syu
I just aliased "sudo pacman -Syu && yay -Syu --aur" to "update" cause I got tired of writing it every day.
You can just run yay
with no arguments and it does exactly what your update script does.
Wouldn't it make more sense to just yay -Syu to update everything, normal packages and AUR packages?
sudo
Make me a sandwich.
LambdaRX is not in the sudoers file. This incident will be reported
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
Instructions unclear, dick now stuck in computer.
For reference: https://en.m.wikipedia.org/wiki/Rm_(Unix)
The rm -rf / variant of the command, if run by a superuser, would cause every file accessible from the present file system to be deleted from the machine.
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
I did this on my personal computer just to prove a point.
nmtui. But that's because my router is trash.
On my desktop:
df -h
to check disk usage
htop
to see resource usage
ll
list directory contents
I recently found btop and am trying to use that instead of htop.
looks up btop ooooo
ll
df -h
du -sch
Ctrl+r
ll
Is an alias for
undefined
ls -al
yea?
I set mine to
bash
ls -lAh
I have it as ls -alFh
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.
you ever use ctrl+r ?
...I do now!
If you know it was the most recent rsync command: just type !rsync.
Laughs in fish
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.
Btop is an amazing resource monitor
Have you tried glances?
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.
Not a command but bang expansions. For example !?
is the args of last command useful for stuff like mkdir foo ; cd !?
https://www.redhat.com/sysadmin/bash-bang-commands learn these. you suck at using your computer if you don't know them.
Is there something similar in fish shell?
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.
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!
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).
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.
jq
ncdu
Seems like an appropriate place to share https://github.com/agarrharr/awesome-cli-apps
I'm a fan of ripgrep and lsd in particular.
Neofetch
let me guess, you either use arch or gentoo
You caught me. (I use arch btw)
For Debian based/descended distros:
undefined
sudo apt-get update && sudo apt-get upgrade
And technically I also regularly use
undefined
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
Chuck the -y in there for extra lazy mode
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.
topgrade
does this and and a lot more
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 }
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.
nano
The one downvote is from the Vim user
The one downvote is from the Vim user
touch
😏
I remember touch
g-push
which is alias for
undefined
git push origin `git branch --show`
Which I'm writing on my phone without testing or looking
git config --global alias.pusho 'push --set-upstream origin HEAD'
You're welcome.
So that's making git push
always push to the current branch?
git push origin HEAD
is a slightly shorter way of doing the same thing, even though you have an alias anyway lol
I've recently started using tmux
when starting a new SSH session to try to build the habit.
bash
history | fzf
Awesome, didn't know fzf yet
Fzf is so useful its ridiculous. I recreated the functionality of sshs with fzf and a small bash script.
Check out the fzf docs. It ships with helpers that offer better shell integration than you're getting here.
I use "ping" every time I suspect my internet might be going a bit slow.
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
'mtr' is not recognized as an internal or external command, operable program or batch file.
I assume I'm on the wrong OS.