Absolute favourite is | the pipe command.
Linux
From Wikipedia, the free encyclopedia
Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991 by Linus Torvalds. Linux is typically packaged in a Linux distribution (or distro for short).
Distributions include the Linux kernel and supporting system software and libraries, many of which are provided by the GNU Project. Many Linux distributions use the word "Linux" in their name, but the Free Software Foundation uses the name GNU/Linux to emphasize the importance of GNU software, causing some controversy.
Rules
- Posts must be relevant to operating systems running the Linux kernel. GNU/Linux or otherwise.
- No misinformation
- No NSFW content
- No hate speech, bigotry, etc
Related Communities
Community icon by Alpár-Etele Méder, licensed under CC BY 3.0
I think vipe is underrated; it takes whatever is on its stdin, shoves it in a temp file, opens your favorite text editor (EDITOR environment variable) and waits in the background until you finish editing the file and close it. Then it outputs the edited text to its stdout.
It's useful in all kinds of pipes, but personally I use it tons of times a day in combination with xclip, in something like this:
xclip -o -selection primary | vipe | xclip -i -selection clipboard
(I actually have a bit fancier version of this pipe wrapped in a Bash function named xvxx.)
On my setup, this takes my current text selection, opens it in vim, and lets me edit it before it sends it to the "traditional" Ctrl+C clipboard. It's super handy for editing comments like this one.
If you often find yourself writing complex Bash pipelines involving generating some output and then running set of commands per line (perhaps in a while loop), sometimes replacing the "selection part" with vipe can be easier than coming up with right filter.
find_or_ls_or_grep_something | vipe | for while read -r foo; do some_action "$foo"; done
And if you are really confident with Bash, you can go even a step further and do:
you might find something like this useful sometimes:
find_or_ls_or_grep_something | vipe | bash
and just create a large dumb one-off script, manually curating what's exactly done. Remember that editing large lists in vim can be made much easier by utilizing vim's ability to invoke unix filter commands (those greps and uniqs and seds et al.) on the buffer, and /or block editing mode using Ctrl+V (that last one method goes really well with column -t).
Neat! FYI for anoyone else who does not find this, it is part of moreutils.
redshift -O 5000: 'redshift' is a utility that adjusts the color temperature of your display, '-O' allows you to set a specific temperature, and '5000' is what I like.
Edit: I also like xkill. xkill lets you click on a window or program and kills it. I need to do this frequently every time exit Kodi; the program stops, but the window is still there.
sudo shutdown 0
Prevents 99% of bugs and mistakes
I use $_ a lot, it allows you to use the last parameter of the previous command in your current command
mkdir something && cd $_
nano file
chmod +x $_
As a simple example.
If you want to create nested folders, you can do it in one go by adding -p to mkdir
mkdir -p bunch/of/nested/folders
Good explanation here:
https://koenwoortman.com/bash-mkdir-multiple-subdirectories/q
Sometimes starting a service takes a while and you're sitting there waiting for the terminal to be available again. Just add --no-block to systemctl and it will do it on the background without keeping the terminal occupied.
systemctl start --no-block myservice
For interactive editing, the keybind alt+. inserts the last argument from the previous command. Using this instead of $_ has the potential to make your shell history a little more explicit. (vim $_ isn't as likely to work a few commands later, but vim actual_file.sh might)
You can also press alt+. multiple times to cycle through all recent arguments
Yes, definitely and I do run into that when I search my history
when I forget to include sudo in my command:
sudo !!
If you use fish, you just need to press Alt + S.
To add to this one, it also supports more than just the previous command (which is what !! means), you can do like sudo !453 to run command 453 from your history, also supports relative like !-5. You can also use without sudo if you want which is handy to do things like !ls for the last ls command etc. Okay one more, you can add :p to the end to print the command before running it just in case like !systemctl:p which can be handy!
Absolutely wild stuff, there. Thanks for knowledge sharing!
Hah I am glad it was helpful! Glad to share, I always felt like half the point of learning is to share what you learned. That is one of my favorite “hidden gems” for lack of a better term that can be a real time saver.
Bonus just for more fun: you can use cd - to switch back to the directory you were last in after changing directories, it toggles the top two paths in the stack. It is similar to how pushd/popd work if you have you used those. I use that one a ton, there are fancier tools now but that one works everywhere.
Oh also, anyone on a Mac needs to know about pbcopy, Linux has xclip and I don’t remember what the Wayland analog is.
Also if you make a typo you can quickly fix it with ^, e.g.
ls /var/logs/apache
^logs^log
The watch command is very useful, for those who don't know, it starts an automated loop with a default of two seconds and executes whatever commands you place after it.
It allows you to actively monitor systems without having to manually re-run your command.
So for instance, if you wanted to see all storage block devices and monitor what a new storage device shows up as when you plug it in, you could do:
watch lsblk
And see in real time the drive mount. Technically not "real time" because the default refresh is 2 seconds, but you can specify shorter or longer intervals.
Obviously my example is kind of silly, but you can combine this with other commands or even whole bash scripts to do some cool stuff.
Ooooh cool, I think this explains how they have our raid monitor set up at work! I keep forgetting to poke through the script
find /path/to/starting/dir -type f -regextype egrep -regex 'some[[:space:]]*regex[[:space:]]*(goes|here)' -exec mv {} /path/to/new/directory/ \;
I routinely have to find a bunch of files that match a particular pattern and then do something with those files, and as a result, find with -exec is one of my top commands.
If you're someone who doesn't know wtf that above command does, here's a breakdown piece by piece:
find- cli tool to find files based on lots of different parameters/path/to/starting/dir- the directory at which find will start looking for files recursively moving down the file tree-type f- specifies I only wantfindto find files.-regextype egrep- In this example I'm using regex to pattern match filenames, and this tellsfindwhat flavor of regex to use-regex 'regex.here'- The regex to be used to pattern match against the filenames-exec-execis a way to redirect output in bash and use that output as a parameter in the subsequent command.mv {} /path/to/new/directory/-mvis just an example, you can use almost any command here. The important bit is{}, which is the placeholder for the parameter coming fromfind, in this case, a full file path. So this would read when expanded,mv /full/path/of/file/that/matches/the/regex.file /path/to/new/directory/\;- This terminates the command. The semi-colon is the actual termination, but it must be escaped so that the current shell doesn't see it and try to use it as a command separator.
I'm a big enjoyer of pushd and popd
so if youre in a working dir and need to go work in a different dir, you can pushd ./, cd to the new dir and do your thing, then popd to go back to the old dir without typing in the path again
I love these.
pushd can also take a path so that you don't have to do a cd after
Nice! I didn't know that one.
You can also cd to a directory and then do cd - to go to the last directory you were in.
pkill journalctl -b nvtop tail are great but I like:
LANGUAGE=en_GB LC_ALL=en_GB.UTF-8 LANG=en_GB.UTF-8 <your GUI program> to run a GUI program in English for more universal compatibility for helping newbies and creating/reading non-terminal based documentation
ctrl+r on bash will let you quickly search and execute previous commands by typing the first few characters usually.
it's much more of a game changer than it first meets the eye.
List open files
sudo lsof -i -P
Network traffic by hardware
sudo tcpdump -i en1 -nn -s0
Current processes
top -l 1
Ctrl-z to suspend the running program.
bg to make it continue running in the background.
jobs to get an overview of background programs.
fg to bring a program to the foreground.
and
disown
to keep background jobs alive when you close the terminal.
Search for github repos of dotfiles and read through people's shell profiles, aliases, and functions. You'll learn a lot.
It isn't a command but an application. I cannot do my work without it.
screen
I prefer tmux, but yes. Both do a great job in helping me manage my terminal sessions.
parallel, easy multithreading right in the command line. This is what I wish was included in every programming language's standard library, a dead simple parallelization function that takes a collection, an operation to be performed on the members of that collection, and optionally the max number of threads (should be the number of hardware threads available on the system by default), and just does it without needing to manually set up threads and handlers.
inotifywait, for seeing what files are being accessed/modified.
tail -F, for a live feed of a log file.
script, for recording a terminal session complete with control and formatting characters and your inputs. You can then cat the generated file to get the exact output back in your terminal.
screen, starts a terminal session that keeps running after you close the window/SSH and can be re-accessed with screen -x.
Finally, a more complex command I often find myself repeatedly hitting the up arrow to get:
find . -type f -name '*' -print0 | parallel --null 'echo {}'
Recursively lists every file in the current directory and uses parallel to perform some operation on them. The {} in the parallel string will be replaced with the path to a given file. The '*' part can be replaced with a more specific filter for the file name, like '*.txt'.
I only recently started using C-r to search in the command history. Game changer!
nc is useful. For example: if you have a disk image downloaded on computer A but want to write it to an SD card on computer B, you can run something like
user@B: nc -l 1234 | pv > /dev/$sdcard
And
user@A: nc B.local 1234 < /path/to/image.img
(I may have syntax messed up--also don't transfer sensitive information this way!)
Similarly, no need to store a compressed file if you're going to uncompress it as soon as you download it---just pipe wget or curl to tar or xz or whatever.
I once burnt a CD of a Linux ISO by wgeting directly to cdrecord. It was actually kinda useful because it was on a laptop that was running out of HD space. Luckily the University Internet was fast and the CD was successfully burnt :)
echo 'dXIgbW9tCmhhaGEgZ290dGVtCg==' | base64 -d
Or
base64 -d <<< 'dXIgbW9tCmhhaGEgZ290dGVtCg=='
Can't remember if <<< is POSIX or not, but pretty sure it works in bash and zsh.
FTR, <<< is a bashism. it's a nice one, though.