LQ Poll: What's your favorite Linux terminal trick?
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I also like it :-) and good not to be dependent of shell-specific env vars ... but clamstrip's variation above will not handle that you may have white-spaces in the directory names in the path....
also it makes more sense to me to force 'ls' not to sort the output, then you actually get the real search order of the existing paths, which nor bigearsbilly's bash specific alias handles (it will if you just add '-f' to 'ls' though).
So my variation of the alias covering for those instances follows:
Code:
alias path='eval ls -1df `echo \"$PATH\" | sed -e"s/:/\" \"/g"` | nl'
Setting up an example using a stupid (but valid) search path with non-existing and existing dirs also with spaces in the names :-)
Code:
% echo $PATH
/tmp/first_in_path:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/local/sbin:/usr/sbin:/sbin:/tmp/with white spaces:/non/existing/path:/non/existing/with white spaces:/tmp/last_in_path
% path
ls: cannot access '/non/existing/path': No such file or directory
ls: cannot access '/non/existing/with white spaces': No such file or directory
1 /tmp/first_in_path
2 /usr/local/bin
3 /usr/bin
4 /bin
5 /usr/local/games
6 /usr/games
7 /usr/local/sbin
8 /usr/sbin
9 /sbin
10 /tmp/with white spaces
11 /tmp/last_in_path
Last edited by fraxflax; 10-20-2020 at 04:17 AM.
Reason: added quote to bigearsbilly as well
The "-print0" and -0 options to deal with spaces in filenames, the redirect to get rid of access errors for some files.
Indeed find and xargs, redirections and pipes are very useful !
Yupp, you can also utilize the find '-type' arg to only get files not having to redirect to /dev/null and '-exec' arg to handle white spaces doing the grep not having to pipe it to xargs:
to locate files containing certain data ... I use it all the time :-)
If you want to have both the filenames and the matched line containing the 'searched string' in the same output, the way to go would be 'find -print0' in combination with 'xargs -0' as suggested (that way grep gets several filenames as argument and will print which file it matched the string) and if you want to get rid on errors of files that cannot be read, the redirect of stderr is also required ... but '-type f' is still useful as there is no need to feed grep with directories :-)
Last edited by fraxflax; 10-20-2020 at 04:51 AM.
Reason: interpunctation
Because I am lazy... I added a bunch to my .bashrc
Code:
# Use "gh" to search through history for a term
# Type in the search term after the gh like "gh bash"
# This will search through the history for anything with "bash" in it
alias gh='history|grep'
# list only hidden directories and files
alias l.="ls -d .* --color=auto'
# pipe out directory listing for viewing with the long format
alias lsl='ls -lhFA|less'
# use "c" to clear the screen
alias c='clear'
# use "h" to bring up history
alias h='history'
#use ".." to quickly move to parent directory.
alias ..='cd ..'
# combined two commands that opens .bashrc to edit, and reloads it after the editor exits to implement changes.
alias editbashrc='vim ~/.bashrc ; source ~/.bashrc'
# function to make directory and then change into it
What's my favorite Linux trick? Not really a trick, but it is my favorite command line short-cut of all time... it's the "!$". Nothing saves more typing that !$. I can't live without it. If you've never heard of it, typing !$ on the command line repeats the last white-space separated <ANYTHING> from your previous command line. I love using !$ whenever the last thing I typed was so super long path or long filename. Why type it again? Simply hit !$. :-)
Thanks for this tip, but you don't have to type existing long paths or filenames if you use mc (midnight commander) file manager. It has commands "Ctrl-x p" and "Ctrl-x t" to put them on the command line for you.
And if you use the text editor vim, these lines in your .bashrc let you edit command lines as if you were in vim, instead of having to learn corresponding bash line edit commands (similar for emacs users too, I think).
I like the colored command prompt with information about current git branch. The colors differ for clean repo, unstaged changes and staged changes. I have this in my .bashrc :
Code:
#---
# git color prompt
# Configure colors, if available.
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
c_reset='\[\e[0m\]'
c_user='\[\e[0;32m\]'
c_path='\[\e[1;36m\]'
c_git_clean='\[\e[0;37m\]'
c_git_staged='\[\e[0;32m\]'
c_git_unstaged='\[\e[0;31m\]'
else
c_reset=
c_user=
c_path=
c_git_clean=
c_git_staged=
c_git_unstaged=
fi
# Function to assemble the Git parsingart of our prompt.
git_prompt ()
{
GIT_DIR=`git rev-parse --git-dir 2>/dev/null`
if [ -z "$GIT_DIR" ]; then
return 0
fi
GIT_HEAD=`cat $GIT_DIR/HEAD`
GIT_BRANCH=${GIT_HEAD##*/}
if [ ${#GIT_BRANCH} -eq 40 ]; then
GIT_BRANCH="(no branch)"
fi
STATUS=`git status --porcelain`
if [ -z "$STATUS" ]; then
git_color="${c_git_clean}"
else
echo -e "$STATUS" | grep -E -q '^.[A-Z\?]'
if [ $? -eq 0 ]; then
git_color="${c_git_unstaged}"
else
git_color="${c_git_staged}"
fi
fi
echo "$git_color[$GIT_BRANCH]$c_reset"
}
PROMPT_COMMAND="PS1=\"${c_user}\u${c_reset}:${c_path}\w${c_reset}\$(git_prompt)\$ \" ;"
#---
For machines I connect to remotely I put colored hostname in the prompt, and for root I have user in red:
So I see a lot of my faves already listed, but one I hadn't seen which I do on most servers these days is add a little colour to man:
Code:
# Less Colors for Man Pages
export LESS_TERMCAP_mb=$'\E[1;33m' # begin blinking
export LESS_TERMCAP_md=$'\E[1;33m' # begin bold
export LESS_TERMCAP_me=$'\E[0m' # end mode
export LESS_TERMCAP_so=$'\E[1;32;5;246m' # begin standout-mode - info box
export LESS_TERMCAP_se=$'\E[0m' # end standout-mode
export LESS_TERMCAP_us=$'\E[04;1;36m' # begin underline
export LESS_TERMCAP_ue=$'\E[0m' # end underline
A few of my standard aliases:
Code:
# useful aliases
alias ls='ls --group-directories-first --color=auto -F'
alias ll='ls -l'
alias la='ls -A'
alias lh='ll -h'
alias grep='grep --color=tty -d skip'
alias less='less -XRFmx2'
Lastly, so my terminal is the same as my preferred indenting style, add the following to .bashrc:
I know you will call me a boreing old so and so, but here's a second vote for tab completion. Like most trickery it does not always work, and like most trickery, can get you out of trouble very quickly, when it does work.
At one stage I had bash so well trained that I could do some very sophisticated coding, without even touching an editor to write scripts in.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.