Voice of the Masses: What’s your favourite CLI tool/trick?
|A new Linux Voice podcast looms upon the world like a giant, looming thing. For this episode, we want you to tell us: what’s your favourite trick, tip or shortcut at the command line? Maybe it’s a mega time-saver key-combo that has changed your life, or some nifty little app you found on GitHub. Whatever the case, post in the comments below and we’ll read out the best. Yes!
32 Comments
alias rsync_cp=’rsync -WavP $1 $2′
alias rsync_ssh=’rsync -avz -e “ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null” –progress ${1} ${2}’
I have a .bash_aliases file that’s sourced at startup. Any complex commands I type more than a few times get their own alias.
`alias aka=’vim ~/.config/zsh/aliases.zsh’`
is one of my favorite `aliases`. This makes it easy to add new aliases very quickly.
Ctrl+R. I use it for almost every command.
Yes! ‘Ctrl + R’ is great. You might also want to define HISTIGNORE:
export HISTIGNORE=”&:ls:exit”
With this, consecutively duplicate commands, the ‘ls’ command and the ‘exit’ command will never be entered into the command line history (i.e., they will not be searched for when you press ‘Ctrl + R’, available when you press ↑ or displayed when you type ‘history’).
Note that ‘ls subdir’, ‘ls -lha’ and other ‘ls’ commands with options/arguments will still be stored in the history. If you don’t want that, add an asterisk:
export HISTIGNORE=”&:ls:ls *:exit”
When you have a series of commands to repeat, use CTRL-R to find the first one then, rather than pressing ENTER to execute it, press CTRL-O. It will execute the command and load the next line from the history buffer into the command line.
Sudo !! ( Shame on me ) And the !$ to recall last commands arguments. So I usually cat a file then vim !$ if I found what I need.
And if you’re wondering how come I use vim please check the great tutorial of our beloved host.
Regarding the ‘sudo !!’ command, you might like this little utility: https://github.com/nvbn/thefuck
Try “alt + .” (alt plus dot) for insert last parameters.
Good one! I use it all the time. Almost as useful as up arrow a hundred times to find a ls -a
Linux is now in the windows store, see this article from Windows central: https://m.windowscentral.com/ubuntu-comes-windows-store-insiders
Probably won’t go down well with the purists…
TonyB
My favourite tip is prefixing a command with ‘: ’ (colon space, without the quotes), which make the command do … *nothing*. But it still saves it in the command history. I use this all the time!
For example, say I’m about to commit a some changes to a SVN (or Git) repository. I write a long, detailed commit message, and prepare the commit command: svn commit -m “Long, detailed message.”
But then I found out that I forgot to make some changes I want to commit, or need to check exactly what my changes were (‘svn diff’) before committing. Instead of cancelling the command (having to rewrite it later), I just hit [Home] : [Space] [Enter] (incidentally, no, the cursor does *not* have to be at the end of the line when you hit [Enter] for the whole line to be executed). The command is stored in the command history, but not actually executed. Then I run the needed commands, press the [Up] key a few times to retrieve the old command, and press [Home] [Delete] [Enter] to execute it. Saves a lot of time.
A custom command prompt is an oldie but goodie. I use
export PS1=”\[\e]2;\u@\H \w\a\e[0;32m\]\u@\h \[\e[0;35m\]\W\[\e[m\]$ ”
in my .bashrc file. This changes the prompt to ‘username@hostname’ in a dark green colour, followed by the name (not path) of the current subdirectory, in a dark magenta colour, followed by a grey $ sign, followed by a space. If I (username ‘tux’) were in the ‘/home/user/Documents’ folder on the ‘iceberg’ host, it would like like this (only with each part in a different colour):
tux@iceberg Documents$
Use different colours for different users, so that you’re always aware of which user you’re currently logged in as. (Use bright red for ‘root’!)
If you have some script in your $PATH you want to edit but you forget where exactly, you can always `vim $(which )`. I have the following two lines in my .zsh_aliases (which is sourced from .zshrc):
“`
# edwh: edit which; uses $EDITOR
function edwh(){$EDITOR $(which “$@”)}
# Completions for zsh
compdef _which edwh=’which’
“`
It’s possible to define aliases with the *same name* as existing commands/executables. This can be used to change the default options. I use the following to get ‘human-readable’ units for ‘df’ (I find 27G much easier to read than 27217472 …) and human-readable clock time for ‘dmesg’ (instead of the number of seconds since the kernel started):
alias df=”df -h”
alias dmesg=”dmesg -T”
(It’s really too bad that ‘human-readable’ units are *not* the default for commands that are meant to be run by humans!)
For commands that need to be run as root, i.e. prefixed with ‘sudo’, I define aliases that automatically prefix the ‘sudo’ part:
alias iotop=”sudo iotop”
alias zypper=”sudo zypper”
I also have a simple ‘ls’ substitute for detailed, one-column directory listing with human-readable units:
alias ll=”ls -lh”
(Consider adding -F to this one if you don’t have colours enabled for your ‘ls’ command.)
my fave shell trick is auto complete. This means that I can call my desktop ~/Des I also like actually typing directory names into a graphical file browser. Its surprising what ~/.kde contains, even when you think that you have deleted it.
Now for the thinkers what does this do:-
xterm -e”which soma ; sleep 6″
or this
xterm -e”which sql ; sleep 6″
and if you don’t like a silly message on a text login this should help
xterm -e”which fortune ; sleep 6″
Then chmod something afterwards
My latest, favourite, is `ag`, also known as “The Silver Searcher”. It’s essentially grep on steroids.
PATH’s lesser known cousin, the CDPATH environment variable, is very useful. If you often ‘cd’ into subdirectories of certain directories, e.g. you have a ‘devel’ directory with various projects, put this into your .bashrc:
export CDPATH=”.:~:~/devel”
Now, *wherever* you are, you can type ‘cd project’ to move to the ‘project’ directory if it is in either 1) the current directory, 2) your home directory or 3) the ‘devel’ subdirectory of your home directory (in order of priority).
It also works with subdirectories, eg. ‘cd project/src’ will change the directory to, e.g., ‘/home/username/devel/project/src’. And it even works with bash completion, e.g. pressing [Tab] after writing ‘cd pro’ will autocomplete to ‘cd project/’, and pressing [Tab] again will show a list of subdirectories of ‘/home/username/devel/project/’.
Oooh I didn’t know about this one! Awesome! Thanks!
(goes to put a bunch of stuff in CDPATH)
mind blown!
Using the CLI for converting Youtube music videos into MP3 files in a single command line with youtube-dl is my favourite:
youtube-dl -x –audio-format mp3 –audio-quality 0 “http://www.youtube.com/YOUR_VIDEO_URL”
Yes, very useful. If you don’t need the recoding into the MP3 format (with the corresponding loss in quality (and here, probably *increased* file size!)), skip the ‘–audio-format mp3 –audio-quality 0’ options. (The en-dashes in the command are supposed to be two hyphen-minus characters. LV’s comment system tries to be clever by automatically converting the hyphens to en-dashes.) Then you will get the audio in the original format (often Opus) , with no loss of quality (and generally a small file size). And you might want to add ‘–add-metadata’, to also store the metadata (audio description) in the audio file. This will be useful if you use a file indexer (e.g. KDE’s Baloo or GNOME’S Tracker). Then you can search for audio files by content (e.g. title, author or other information stored in the song’s ‘description’ on YouTube), not just filename. Define an alias and store it in .bashrc:
alias getsong='youtube-dl -x --add-metadata'
Then you can just write
getsong URL
to download the song from URL.Also note that, despite its name, youtube-dl also works with a lot of other video Web sites, not just YouTube.
try these handy shell scrips for you tube
https://paste.xinu.at/m-Xjb0F/
the wonderful get-iplayer for getting BBC TV and radio programs.
To save having to remember any and all switches for youtube-dl, I’ve been using a helper script called youtube-helper. It lists all the available download formats for the desired video and you just pick the relevant number.
Living in SSH all day being able to chain hosts together in ~/.ssh/config
Host jumpbox
HostName 194.10.33.257
Host webserver
ProxyCommand ssh -q -W %h:%p me@jumpbox
HostName 10.43.0.13
Then I can just
ssh felim@webserver
Without need int to know ports etc. hoping all about the place. SSH For The Win! 😉
tee and pv when doing a long pipeline of commands tee can give you a view into the data at that point in the command. pv just copies the input to output, but gives progress bar and stats on the error file, useful for a long running command to show progress.
My quick search of alias alias
aq='() {alias | grep $1}’
setting this shell script as your terminal emulators url handler (browser) will allow all youtube URL’s (and other youtube-dl supported sites) to auto open in mpv, all image urls ending with .png, .jpegwtc. will auto open in feh, all others will open in firefox, edit to suit.
heres some handy shell functions
http://arza.us/paste/funkytions
http://arza.us/paste/piper
some shell scripts i have written for video streaming.
i put them in a github repo
https://github.com/cirrusUK/bashtube
Same can be said for ‘#’ with or without the space. Bash interprets it as a comment but it stays in the history.
*sigh* (Javascript was not enabled, reply was not put into proper place). In response to using “: ” at the font of the command to not execute but stay in history:
Same can be said for ‘#’ with or without the space. Bash interprets it as a comment but it stays in the history.