Hot answers tagged command-line
47
How do you feel about using awk instead of grep?
chopper:~> ps aux | awk 'NR == 1 || /syslogd/'
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND
root 19 0.0 0.0 2518684 1160 ?? Ss 26Aug12 1:00.22 /usr/sbin/syslogd
mrb 574 0.0 0.0 2432852 696 s006 R+ 8:04am 0:00.00 ...
42
Just with sed, without any pipes :
sed '1d;$d' file.txt
NOTE
1 mean first line
d mean delete
; is the separator for 2 commands
$ mean last line
34
Good way
Normally you can't do this with grep but you can use other tools. AWK was already mentioned but you can also use sed, like this:
sed -e '1p' -e '/youpattern/!d'
How it works:
Sed utility works on each line individually, running specified commands on each of them. You can have multiple commands, specifying several -e options. We can prepend ...
24
ps aux | { read line;echo "$line";grep someApp;}
EDIT: after comments
ps aux | { head -1;grep someApp;}
I though head -1 would read all input, but after testing it, it works too.
{ head -1;grep ok;} <<END
this is a test
this line should be ok
not this one
END
output is
this is a test
this line should be ok
23
Yes it can, and there are a few already. Such as /usr/bin/X :)
dennis@lightning:~$ ls {/usr{/local,},}/{s,}bin | grep '[A-Z]'
MAKEDEV
amuFormat.sh
GET
HEAD
Mail
POST
X
X11
Xephyr
Xnest
Xorg
NetworkManager
dennis@lightning:~$ zcat ~/.cache/apt-file /archive.ubuntu.com_ubuntu_dists_precise_Contents-i386.gz | tail -n +33 | cut -f1 | grep -P ...
21
There are many ways to calculate.
For simple expressions you can use bash itself:
echo $((20+5))
or expr:
expr 20 + 5
And for complex cases there is great tool bc:
echo "20+5" | bc
Btw, bc can calculate even very complex expression with roots, logarithms, cos, sin and so on.
21
You can use false (/bin/false, /usr/bin/false, or shell builtin):
$ false || echo It failed.
It failed.
$
You can also use exit 1 from a subshell:
$ (exit 1) || echo Gosh, it failed too.
Gosh, it failed too.
$
21
By default, when you run a command on the remote machine using ssh, a TTY is not allocated for the remote session. This lets you transfer binary data, etc. without having to deal with TTY quirks. This is the environment provided for the command executed on computerone.
However, when you run ssh without a remote command, it DOES allocate a TTY, because you ...
20
Seems the easiest way is to write it yourself. At the first look I found pretty good website, that can give us all information we need. Thus all we need to do is to write a function that will parse it. So five minutes with bash and voila:
$ function verbteacher() {
wget -qO - http://conjugator.reverso.net/conjugation-english-verb-$1.html | \
sed ...
20
help source says:
source: source filename [arguments]
Execute commands from a file in the current shell.
Read and execute commands from FILENAME in the current shell. The
entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.
...
19
Your shell is meant to evaluate that shell code output by ssh-agent. Run this instead:
eval "$(ssh-agent)"
Or if you've started ssh-agent already, copy paste it to your shell prompt (assuming you're running a Bourne-like shell).
ssh commands need to know how to talk to the ssh-agent, they know that from the SSH_AUTH_SOCK environment variable.
16
The mentioned solutions are fine for very simple calculations, but very error-prone. Examples:
# without spaces 20+5 literally produces 20+5
expr 20+5
→ 20+5
# bc's result does't give the fractional part by default
bc <<<
9.0/2.0
→ 4
# expr does only integer
expr 9 / 2
→ 4
# same for echo
echo $((9/2))
→ 4
# echo chokes on floats
echo ...
16
You can run python scripts by making them executable (chmod +x test.py) and making #!/usr/bin/env python the first line. Once you do that, running test.py args will invoke python to run your script. Read about shebang's if you want to learn more.
15
The following seems to work for me
grep --color -E -- "$(ls -rtl | tail -n3)|$" <(ls -l)
It uses grep with highlight on input ls -l and uses a regular expression to search for either of the inputs for the three oldest command. It also search for the end-of-line $ in order to print the whole file.
You can also put it in a function, such that you can ...
14
That's pretty much the most common way of finding "N most common things", except you're missing a sort, and you've got a gratuitious cat:
tr -c '[:alnum:]' '[\n*]' < test.txt | sort | uniq -c | sort -nr | head -10
If you don't put in a sort before the uniq -c you'll probably get a lot of false singleton words. uniq only does unique runs of lines, ...
14
The POSIX 2008 standard has a section describing "Shell and Utilities". Generally, if you stick to that your scripts should be fairly future-proof, except possibly for deprecations, but those hardly happen overnight so you should have plenty of time to update your scripts.
In some cases where output format for a single utility varies widely across ...
13
From the documentation it seems that there is no option to create a copy of the file.
You can define a shell function
function gzipkeep {
if [ -f "$1" ] ; then
gzip -c -- "$1" > "$1.gz"
fi
}
and then
gzipkeep file.txt
13
I usually use the column program for this, it's in a package called bsdmainutils on Debian:
column -t foo
Output:
case elems meshing nlsys
uniform 2350 0.076662 2.78
non-conformal 348 0.013332 0.55
scale 318 0.013333 0.44
smarter 504 0.016666 0.64
submodel 360 .009999 0.40
unstruct-quad 640 ...
13
In short: no.
You'll need to restore from a backup. (Some backup tools might have options to only restore only permission, others can list backed-up files with their permissions and you can use that to fix your system.)
If you don't have a backup, you'll need to fix all that manually.
13
Some keys on your keyboard don't correspond to actual characters. For example, A corresponds to the character a but the Up and F1 keys do not have their own dedicated characters. When those special keys are pressed, instead of getting a single character corresponding to the key, the terminal translates the keypress to a special sequence of multiple ...
12
There's no restriction on command names on Unix. Any file can be a command. And a filename can be any sequence of one or more (up to a limit though) of characters other than ASCII NUL or ASCII /. zsh even lifts that limitation for functions where you can have any string as the function name.
A few notes though:
you'll have a hard time creating a command ...
11
Not with mv.
The core function of mv (despite its name) is to rename an object. One of the guarantees that UNIX makes is that renames are atomic -- you're never allowed to see a partially completed rename. This guarantee can be very useful if you want to change a file (/etc/passwd, for example) that other programs may be looking at, and you want them to ...
11
rsync -va -n /oldisk/a/ /newdisk/a/
The -n will do a dry run, showing you what it would do without actually doing anything.
If it looks ok, run the rsync without the -n option.
This will be a copy, not a move, which isn't quite what you're doing, but is safer.
11
You can use xargs:
locate filename123 | xargs vi
By default xargs will execute as few instances of the specified command as possible, passing as many parameters as possible according to the system's ARG_MAX. To limit the number of parameters passed to an instance of vi, use xargs' -n option.
To handle file names containing spaces use xargs' -d option:
...
11
The -- is commonly used in command to indicate the end of options. This is useful if your filename begins with a "-" or your input is unknown. Here is an example of its use:
git diff --stat -- --file1 --file2
--file1 is treated as a filename rather than another option.
11
find -type f -name "*.mp4" -print0
Recursively search the current directory for normal files whose names end with .mp4 and print their relative path names, separated by null bytes. -print0 is safer than -print here, because newlines are valid characters in a filename. find(1)
| xargs -0
Use the input as arguments to the next command. Input is ...
11
In Unix, you could use command-line editor for such a thing. I typically run with set -o emacs so I can use the following Emacs keys directly on the command line:
display your command line
Ctrl-A - go to the beginning of the command line
Ctrl-K - cut the entire command line into an internal buffer
... do all needed work here...
Ctrl-Y - paste command from ...
11
You can background a task by adding a & after it.
For instance tail -f /var/log/messages & will background the task immediately.
As always you can see what tasks you backgrounded with the jobs command.
This of course assumes you have not yet run the command.
10
You could use bc. E.g.,
$ echo "25 + 5" | bc
30
Alternatively bc <<< 25+5 will also work.
Or interactively, if you want to do more than just a single simple calculation:
$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'. ...
10
To answer your specific question with your set of commands, use:
kill `pidof <name>`
Since pidof <name> gives you the PID of the process you are trying to kill you can use it with command line switches such as -9 etc too.
Tested with bash and tcsh.
Only top voted, non community-wiki answers of a minimum length are eligible

