I have a shell script to check if Firefox is running in my Linux machine like;
ps -ef|grep firefox
this will list all the instances of firefox running in my machine, showing their PIDs, so that I can manually kill them. My question is, is it possible to display the machine name also in this list? If there are multiple instances, each line should contain the machine name (or IP) also. In my shellscript, i did something like;
hostname
ps -ef|grep firefox
which returns the hostname once, and multiple instances are listed below that one by one. How can I print machine name (or IP) also along with each line?
Like this:
ps -ef | egrep '[/ ]firefox' | sed "s/^/$(hostname -s) : /"
This will do it:
ps -ef | grep [f]irefox | xargs -I{} echo "$(hostname) {}"
Notice the brackets around 'f' in firefox. This prevents your grep command from showing up in the results.
In my program I need to output to user which shell he is using. So in file /etc/udate-motd.d/00-header I wrote printf "$SHELL" but the problem is that even when I switching my shell to zsh, $SHELL is still equal to /bin/bash. I searched through the internet and found that I can bo it by using MyShell='ps -hp $$', and here is again a problem. When I use it MyShell is a string with number of processes (/etc/update-motd.d/00-header is also there) but there no word zsh.
So how can I understand which shell use the logging in person.
"the internet" gave you one kind of ps syntax. You've tagged this linux, so don't use BSD syntax. Try this:
ps hp $$ -o cmd
no dash
The users shell is determined in /etc/passwd. Why not take the information from there? You could
grep $USER /etc/passwd | cut -f7 -d:
to get the shell.
I run grep searches often but sometimes it takes so long I write to a file instead as a background task but it still has output for apparently errors (I think). For example:
grep -rl word . > grep-word.txt &
So this runs it in the background and lets me input new commands while it's running but then it randomly pops up messages like this:
grep: ./var/run/dbus/system_bus_socket: No such device or address
How do I get it to suppress or redirect these errors?
Assuming bash or sh (you don't say what shell you're calling this from):
grep -rl word . > grep-word.txt 2> /dev/null &
Do you know of any alternatives to wmctrl? A program that lets you manipulate windows and window management from the command line.
One drawback with wmctrl is that whilst you can manipulate the current window, you cannot get wmctrl to list information about the current window (it ignores -r).
To find the id of the currently active window, use:
xprop -root -f _NET_ACTIVE_WINDOW 0x " \$0\\n" _NET_ACTIVE_WINDOW | awk "{print \$2}"
Using this id, you can then get a lot of information about the currently active window:
xprop -id $(xprop -root -f _NET_ACTIVE_WINDOW 0x " \$0\\n" _NET_ACTIVE_WINDOW | awk "{print \$2}")
From there, you can grep what you need, or make it show just the desired field the same way I extracted _NET_ACTIVE_WINDOW above. So, to find the PID of the currently active window, you would append -f _NET_WM_PID 0c " \$0\\n" _NET_WM_PID to the command above, making it:
xprop -id $(xprop -root -f _NET_ACTIVE_WINDOW 0x " \$0\\n" _NET_ACTIVE_WINDOW | awk "{print \$2}") -f _NET_WM_PID 0c " \$0\\n" _NET_WM_PID | awk "{print \$2}"
Note that wmctrl also accepts the same kind of id in combination with the -i flag.
You can trick wmctrl into outputting the ID number of the active window by turning on verbose mode and telling it to move the active window with an invalid parameter:
wmctrl -v -r :ACTIVE: -e dummy
While this does exit with an error status, it also outputs the ID number of the active window:
envir_utf8: 1
Using window: 0x08400004
The -e option expects a list of comma separated integers: "gravity,X,Y,width,height"
Once you have the ID number of the active window, you can list all the windows and search for that ID number:
wmctrl -l
It's awkward to get information about the active window with wmctrl, but it's possible.
xdotool is a reasonable alternative (github project here), although unfortunately the author doesn't seem to care about it much any more.
My experience with wmctrl version 1.07 under RH Linux 5.5 and 5.6 64-bits is that
wmctrl gets completely lost at times. For instance, when looking for a firefox window
the following returns nothing:
% wmctrl -lpGx | grep -i 'Firefox'
My suspicion is that wmcntl cannot find firefox windows due to the nature of the window manager, in this case, metacity. This manager seems to reparent windows and perhaps this causes wmctrl not to list firefox windows. 'xwininfo' does list the firefox windows.
If you have access to xdotool (my version is 2.20110530.1) then you can try:
% mywin=`xwininfo -root -tree | awk '/- Mozilla Firefox/ { printf $1; exit}'`
% xdotool windowactivate --sync $mywin mousemove --window $mywin 0 0
This makes firefox active, makes it the top window in the stack on your desktop, and puts the mouse over it (as is sometimes needed when a user's environment sets focus to a window under the mouse without requiring a click.) If you don't want the mouse to move simply
remove 'mousemove --window $mywin 0 0' from the above.
Note: I had the same problem with finding Konqueror windows on the same Linux systems.
window id
wmctrl -a :ACTIVE: -v 2>&1 | grep "Using window: " | awk "{print \$3}"
You can check xdo which can do a decent job with minimal ressources.
Is there a Linux command that will list all available commands and aliases for this terminal session?
As if you typed 'a' and pressed tab, but for every letter of the alphabet.
Or running 'alias' but also returning commands.
Why? I'd like to run the following and see if a command is available:
ListAllCommands | grep searchstr
You can use the bash(1) built-in compgen
compgen -c will list all the commands you could run.
compgen -a will list all the aliases you could run.
compgen -b will list all the built-ins you could run.
compgen -k will list all the keywords you could run.
compgen -A function will list all the functions you could run.
compgen -A function -abck will list all the above in one go.
Check the man page for other completions you can generate.
To directly answer your question:
compgen -ac | grep searchstr
should do what you want.
Add to .bashrc
function ListAllCommands
{
echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 \
-executable -type f -printf '%P\n' | sort -u
}
If you also want aliases, then:
function ListAllCommands
{
COMMANDS=`echo -n $PATH | xargs -d : -I {} find {} -maxdepth 1 \
-executable -type f -printf '%P\n'`
ALIASES=`alias | cut -d '=' -f 1`
echo "$COMMANDS"$'\n'"$ALIASES" | sort -u
}
There is the
type -a mycommand
command which lists all aliases and commands in $PATH where mycommand is used. Can be used to check if the command exists in several variants. Other than that... There's probably some script around that parses $PATH and all aliases, but don't know about any such script.
The others command didn't work for me on embedded systems, because they require bash or a more complete version of xargs (busybox was limited).
The following commands should work on any Unix-like system.
List by folder :
ls $(echo $PATH | tr ':' ' ')
List all commands by name
ls $(echo $PATH | tr ':' ' ') | grep -v '/' | grep . | sort
Use "which searchstr". Returns either the path of the binary or the alias setup if it's an alias
Edit:
If you're looking for a list of aliases, you can use:
alias -p | cut -d= -f1 | cut -d' ' -f2
Add that in to whichever PATH searching answer you like. Assumes you're using bash..
Try this script:
#!/bin/bash
echo $PATH | tr : '\n' |
while read e; do
for i in $e/*; do
if [[ -x "$i" && -f "$i" ]]; then
echo $i
fi
done
done
For Mac users (find doesn't have -executable and xargs doesn't have -d):
echo $PATH | tr ':' '\n' | xargs -I {} find {} -maxdepth 1 -type f -perm '++x'
Alternatively, you can get a convenient list of commands coupled with quick descriptions (as long as the command has a man page, which most do):
apropos -s 1 ''
-s 1 returns only "section 1" manpages which are entries for executable programs.
'' is a search for anything. (If you use an asterisk, on my system, bash throws in a search for all the files and folders in your current working directory.)
Then you just grep it like you want.
apropos -s 1 '' | grep xdg
yields:
xdg-desktop-icon (1) - command line tool for (un)installing icons to the desktop
xdg-desktop-menu (1) - command line tool for (un)installing desktop menu items
xdg-email (1) - command line tool for sending mail using the user's preferred e-mail composer
xdg-icon-resource (1) - command line tool for (un)installing icon resources
xdg-mime (1) - command line tool for querying information about file type handling and adding descriptions for new file types
xdg-open (1) - opens a file or URL in the user's preferred application
xdg-screensaver (1) - command line tool for controlling the screensaver
xdg-settings (1) - get various settings from the desktop environment
xdg-user-dir (1) - Find an XDG user dir
xdg-user-dirs-update (1) - Update XDG user dir configuration
The results don't appear to be sorted, so if you're looking for a long list, you can throw a | sort | into the middle, and then pipe that to a pager like less/more/most. ala:
apropos -s 1 '' | sort | grep zip | less
Which returns a sorted list of all commands that have "zip" in their name or their short description, and pumps that the "less" pager. (You could also replace "less" with $PAGER and use the default pager.)
Try to press ALT-? (alt and question mark at the same time). Give it a second or two to build the list. It should work in bash.
Here's a solution that gives you a list of all executables and aliases. It's also portable to systems without xargs -d (e.g. Mac OS X), and properly handles paths with spaces in them.
#!/bin/bash
(echo -n $PATH | tr : '\0' | xargs -0 -n 1 ls; alias | sed 's/alias \([^=]*\)=.*/\1/') | sort -u | grep "$#"
Usage: myscript.sh [grep-options] pattern, e.g. to find all commands that begin with ls, case-insensitive, do:
myscript -i ^ls
It's useful to list the commands based on the keywords associated with the command.
Use: man -k "your keyword"
feel free to combine with:| grep "another word"
for example, to find a text editor:
man -k editor | grep text
shortcut method to list out all commands.
Open terminal and press two times "tab" button.
Thats show all commands in terminal
You can always to the following:
1. Hold the $PATH environment variable value.
2. Split by ":"
3. For earch entry:
ls * $entry
4. grep your command in that output.
The shell will execute command only if they are listed in the path env var anyway.
it depends, by that I mean it depends on what shell you are using. here are the constraints I see:
must run in the same process as your shell, to catch aliases and functions and variables that would effect the commands you can find, think PATH or EDITOR although EDITOR might be out of scope. You can have unexported variables that can effect things.
it is shell specific or your going off into the kernel, /proc/pid/enviorn and friends do not have enough information
I use ZSH so here is a zsh answer, it does the following 3 things:
dumps path
dumps alias names
dumps functions that are in the env
sorts them
here it is:
feed_me() {
(alias | cut -f1 -d= ; hash -f; hash -v | cut -f 1 -d= ; typeset +f) | sort
}
If you use zsh this should do it.
The problem is that the tab-completion is searching your path, but all commands are not in your path.
To find the commands in your path using bash you could do something like :
for x in echo $PATH | cut -d":" -f1; do ls $x; done
Here's a function you can put in your bashrc file:
function command-search
{
oldIFS=${IFS}
IFS=":"
for p in ${PATH}
do
ls $p | grep $1
done
export IFS=${oldIFS}
}
Example usage:
$ command-search gnome
gnome-audio-profiles-properties*
gnome-eject#
gnome-keyring*
gnome-keyring-daemon*
gnome-mount*
gnome-open*
gnome-sound-recorder*
gnome-text-editor#
gnome-umount#
gnome-volume-control*
polkit-gnome-authorization*
vim.gnome*
$
FYI: IFS is a variable that bash uses to split strings.
Certainly there could be some better ways to do this.
maybe i'm misunderstanding but what if you press Escape until you got the Display All X possibilities ?
compgen -c > list.txt && wc list.txt
Why don't you just type:
seachstr
In the terminal.
The shell will say somehing like
seacrhstr: command not found
EDIT:
Ok, I take the downvote, because the answer is stupid, I just want to know: What's wrong with this answer!!! The asker said:
and see if a command is available.
Typing the command will tell you if it is available!.
Probably he/she meant "with out executing the command" or "to include it in a script" but I cannot read his mind ( is not that I can't regularly it is just that he's wearing a
mind reading deflector )
in debian: ls /bin/ | grep "whatImSearchingFor"