LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 10-19-2020, 06:06 PM   #181
Tonus
Senior Member
 
Registered: Jan 2007
Location: Paris, France
Distribution: Slackware-15.0
Posts: 1,405
Blog Entries: 3

Rep: Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514Reputation: 514

Quote:
Originally Posted by linpi View Post
find . -name "*.php" | xargs grep 'search-string'

or find path -name "*.extension" | xargs grep 'search-string'
Great ! A little improved

Code:
find path -name '*.extension' -print0 2>/dev/null | xargs -0 grep 'searched string'
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 !

Last edited by Tonus; 10-19-2020 at 06:08 PM. Reason: broken pipe and typos
 
Old 10-19-2020, 06:19 PM   #182
joscer
LQ Newbie
 
Registered: Dec 2009
Location: Mexico City
Distribution: lubuntu, ubuntu, debian
Posts: 4

Rep: Reputation: 0
using OMZ
 
Old 10-19-2020, 11:32 PM   #183
JDoes
LQ Newbie
 
Registered: Mar 2020
Location: Via Lactea
Distribution: Ubuntu, Kali, CentOS, Mac OSX, Windows 10
Posts: 3

Rep: Reputation: 1
RHEL 8

ss -tuna

It just sounds funny! And it is quite informative.
 
1 members found this post helpful.
Old 10-20-2020, 02:57 AM   #184
fraxflax
LQ Newbie
 
Registered: Jan 2018
Location: Sweden
Distribution: Debian
Posts: 24

Rep: Reputation: 5
the path alias in correct order

removed contents of this post as it was posted in error, how to remove it?

Last edited by fraxflax; 10-20-2020 at 03:45 AM. Reason: error posting it
 
Old 10-20-2020, 03:37 AM   #185
fraxflax
LQ Newbie
 
Registered: Jan 2018
Location: Sweden
Distribution: Debian
Posts: 24

Rep: Reputation: 5
path-alias for all shells showing search order and handling white-spaces

Quote:
Originally Posted by clamstrip View Post
I like this!

Quote:
Originally Posted by bigearsbilly View Post
Code:
alias path='(IFS=:;ls -1d $PATH |  nl)'

% path
ls: cannot access '/home/billy/R/usr/bin': No such file or directory
ls: cannot access '/home/billy/R/bin': No such file or directory
     1  /bin
     2  /home/billy/bin
     3  /home/billy/go
     4  /home/billy/usr/bin
     5  /sbin
     6  /usr/bin
     7  /usr/games
     8  /usr/local/bin
     9  /usr/local/games
    10  /usr/sbin
so here's the variant

alias path 'ls -1d `echo $PATH | tr ':' "\n"` | nl'
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
 
Old 10-20-2020, 04:32 AM   #186
fraxflax
LQ Newbie
 
Registered: Jan 2018
Location: Sweden
Distribution: Debian
Posts: 24

Rep: Reputation: 5
Quote:
Originally Posted by Tonus View Post
Great ! A little improved

Code:
find path -name '*.extension' -print0 2>/dev/null | xargs -0 grep 'searched string'
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:

Code:
 
find path -type f -name '*.extension' -exec grep 'searched string' {} \;
I find using 'find' very useful with

Code:
 
-type f -exec grep -il 'searched string' {} \;
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
 
Old 10-20-2020, 07:52 AM   #187
heinjh
LQ Newbie
 
Registered: Apr 2015
Posts: 1

Rep: Reputation: Disabled
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
mkcd () { mkdir -p -- "$1" && cd -P -- "$1" }
 
Old 10-20-2020, 11:07 AM   #188
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
Quote:
Originally Posted by mark.schutte View Post
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).

Code:
set -o vi
shopt -s dotglob extglob
 
Old 10-20-2020, 12:10 PM   #189
resolv_25
Member
 
Registered: Aug 2011
Location: Croatia
Distribution: Debian 10/Ubuntu 20.04
Posts: 64

Rep: Reputation: 15
Good thread this one :-)

Simple checking if previous command is successful, it can be within the script or chained command (command1; echo $?):
$?

Example:

Code:
mysql $db < db_backup_tmp.sql
if [ $? -eq 0 ] 
then
    echo "Db import successfull."
else
    echo "Mysql import not successfull."
    echo "Exit..."
    exit;			
fi
 
Old 10-20-2020, 01:07 PM   #190
dr_agon
Member
 
Registered: Sep 2007
Location: Poland
Distribution: Ubuntu LTS
Posts: 105
Blog Entries: 12

Rep: Reputation: 26
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:
Code:
PS1='${debian_chroot:+($debian_chroot)}\[\033[0;31m\]\u\[\e[0;32m\]@\h\[\033[00m\]:\[\033[01;36m\]\w\[\033[00m\]\$'
(all colors for black background!)
 
Old 10-20-2020, 06:27 PM   #191
djk44883
Member
 
Registered: Aug 2008
Location: Ohio
Distribution: debian
Posts: 141

Rep: Reputation: 29
Quote:
Originally Posted by Septuagerian View Post
watch -n 2 sensors.
Although I suppose there are more, sensors is the only practical application I've ever found for watch as well!

Quote:
Originally Posted by Ourck19 View Post
poweroff !!!
Best & nice day
Fred.
agreed, Like the quick and easy
 
Old 10-21-2020, 03:49 AM   #192
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by resolv_25 View Post
Simple checking if previous command is successful, it can be within the script or chained command (command1; echo $?):
$?
that's actually part of my prompt:
Code:
PS1="...\${?#0}..."
i.e., only show the status if it's not 0.
 
1 members found this post helpful.
Old 10-21-2020, 04:07 AM   #193
josephj
Member
 
Registered: Nov 2007
Location: Northeastern USA
Distribution: kubuntu
Posts: 214

Rep: Reputation: 112Reputation: 112
Quote:
Originally Posted by resolv_25 View Post
Good thread this one :-)

Simple checking if previous command is successful, it can be within the script or chained command (command1; echo $?):
$?

Example:

Code:
mysql $db < db_backup_tmp.sql
if [ $? -eq 0 ] 
then
    echo "Db import successfull."
else
    echo "Mysql import not successfull."
    echo "Exit..."
    exit;			
fi
If you're using a recent version of bash, this is a cool alternative:

Code:
mysql $db < db_backup_tmp.sql
rc=$?
if (( rc )) 
then
    echo "Mysql import failed with code [$rc]"
    exit "$rc"			
fi

echo "Db import successful"
 
1 members found this post helpful.
Old 10-26-2020, 10:13 PM   #194
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
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:
Code:
tabs -2
 
Old 10-30-2020, 09:53 AM   #195
DarrenDrapkin
Member
 
Registered: Aug 2014
Location: Leeds, England
Distribution: Slackware x86 64 version 15.0
Posts: 127
Blog Entries: 3

Rep: Reputation: 27
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: What is your favorite Linux terminal trick? LXer Syndicated Linux News 0 08-01-2018 12:41 PM
Poll: (Without the poll) - How is Linux used in your workplace? SlowCoder General 13 09-11-2007 11:03 PM
vim :gui trick and undo-trick dazdaz Linux - Software 3 09-10-2007 02:45 PM
Poll: What is your favorite brand of Motherboard? dstrbd1 Linux - Hardware 2 02-16-2006 05:40 PM
Weekly Hardware Poll, Dec 14th: What's your favorite notebook? finegan Linux - Hardware 18 12-03-2004 12:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration