Hot answers tagged

7

With zsh: rm -f pre*(OL[2,-1]) OL: reverse order by size [2,-1]: second to last only The equivalent with bash and GNU utilities would be something like: eval "files=($(LC_ALL=C ls --quoting-style=shell-always -dS ./pre*))" rm -f "${files[@]:1}" You may want to limit it to regular files, as the size for non-regular files has generally not much ...


7

You can use a combination of few utilities: stat -c '%s %n' pre_* | sort -k1,1rn | tail -n +2 | cut -d' ' -f2 | xargs rm Assuming GNU system and no unusual filenames. stat gets the filesize and name separated by space for all pre_* files sort sorts the file according to the file size, with highest sized one goes to top tail -n +2 gets the rest of the ...


6

grep -E '\b6\b' \b is a "word boundary" Edit: After pointing @nobar in the right direction, he found/pointed_out the shortcut-option -w (word-regexp) in the manpage, which simplifies the above to: grep -w 6 If used a lot, you could use a function similar to wgrp(){ grep -w "$1" "$2"; } Note (to @glenn-jackman): If you don't quote "$2" here, you can ...


5

Yes, you can. Using grep with PCRE(-P): ip addr | grep -Po '^\d+:\s+\K[^:]+' ^\d+:\s+ matched the portion before interface name at the start, \K discards the match [^:]+ gets the portion upto the next : i.e. the interface name Similar logic using sed: ip addr | sed -nE 's/^[[:digit:]]+:[[:blank:]]+([^:]+).*/\1/p' On my system: % ip addr | grep -Po ...


5

Never known about the command intel_gpu_time interesting, thank you. This command is part of the package intel-gpu-tools (apt-get install intel-gpu-tools) under Ubuntu and by the look of it the package under RedHat (CentOS) is called exactly the same. So, give yum install intel-gpu-tools a try.


5

apt-get install whois Requires a network connection that can see the kali repo at http://http.kali.org/


5

You can use !!:* to refer to all the words but the zeroth of the last command line. !! refers to the previous command, : separates the event specification from the word designator, * refers to all the words but the zeroth. This is from the HISTORY EXPANSION section of bash(1). wieland@host in ~» cat foo | grep bar bar wieland@host in ~» tail -f !!:* tail -...


4

Two changes to your current script: don't parse ls; instead rely on the shell's globbing because the files are in a subdirectory, either cd there first and run the loop, or use basename and dirname to pull out the directory and filename portions of the file before adding the prefix. (Note: I also changed your "/Path" to "./Path" as I didn't want to ...


4

I was able to put the answer together with help from this question. The program "wc" program counts newlines, words and byte counts. The "-l" option specifies that the number of lines is desired. For my application, the following worked nicely to count the number of instances of "somePattern": $grep -r "somePattern" | wc -l


3

You can use history expansion $ echo test !#:^ echo test test test test $ echo a/b/test.py proj_copy/!#:^ echo a/b/test.py proj_copy/a/b/test.py a/b/test.py proj_copy/a/b/test.py !# The entire command line typed so far. :^ The first argument You could also use brace expansion $echo test{,} test test $echo {,proj_copy}/a/b/test.py /a/b/...


3

The sqlite3 interface uses the ReadLine library for command line editing. You can erase a full line with Ctrl-U but once you have pressed Enter, the line has been accepted and is no longer editable (well, it is, but not as part of the current statement). As you've noticed, sending end-of-file with Ctrl-D will prompt the client to exit, which is not what you ...


3

If you're on Linux or otherwise have access to GNU tools, you can do this: last=-1; find . -type f -name '*.png' -printf '%f\0' | sort -nz | while read -d '' i; do s=$(stat -c '%s' "$i"); [[ $s = $last ]] && rm "$i"; last=$s; done Explanation last=-1 : set the variable $last to -1. find . -type f -name '*.png' -...


2

POSIX implementations of ls support a -S option to provide a listing ordered by size (and it appears, sorted by name within equal sizes). You could pipe a listing into a shell loop, remembering the size for the preceding item and removing "duplicates". That has the drawback of getting the filename (if you happen to like using spaces in your filenames). ...


2

You may have some incorrect expectation of what the uuendcode ... | mailx ... command is supposed to do. In the world that uuencode was designed to operate in, there is no such thing as an attachment, because MIME doesn't exist yet. A person who receives a message with uuencoded stuff in it is supposed to see it like this: From: Alice <alice@example.edu&...


2

You can search for a specific integer with a regular expression. Search for the digits preceded by either the beginning of the data or a non-digit character, and likewise after the digits. grep -e '^6$' -e '^6[^0-9]' -e '[^0-9]6$' -e '[^0-9]6[^0-9]' # BRE syntax grep -E '(^|[^0-9])6($|[^0-9])' # ERE syntax An entire-word ...


2

+1 to what @jos said above ("google it"). In this particular case you should remove the ".rela_" and "_gpl" parts. You'll find that these are kernel symbols. "rela" is an acronym for "relocation" and has to do with the ELF file format. These are all interesting topics and I encourage you to look into them further.


1

These look very much like section names from the Linux kernel. The ones prefixed by .rela contain relocation information for the named section, e.g. .rela.text is the relocation information for the text section (where kernel object code is stored). Other sections of interest are: .modinfo - kernel module information .rela.__ksymtab - kernel symbol table ...


1

You can do that with bash itself, using command substitution and then parameter expansion. First take the output of the command in a variable by using command substitution $(), and then use parameter expansion to replace all newlines with spaces ${variable//$'\n'/ }: $ myVariable=$(grep "type" /root/myFile | cut-d'=' -f2) $ myVariable=${myVariable//$'\n'/ ...


1

myVariable=`grep "type" /root/myFile | cut-d'=' -f2` What is between back-ticks (`) is run and the output is assigned to myVariable. If your current output is separated by line feeds (\n), then you may want to replace them with spaces with tr such as: myVariable=`grep "type" /root/myFile | cut-d'=' -f2`|tr '\n' ' '` Note: Some people prefer using the $...


1

You can override the builtin compgen for simple directory completions (as for cd and ls). For example, put this function in your ~/.bashrc compgen(){ local IFS=$'\n' local x tmp x=$( builtin compgen "$@" ) && while read -r tmp; do [[ "$tmp" =~ ^/home/e && ! "$tmp" =~ ^/home/elsherbini$ ]] || echo "$tmp" ...


1

I'm using zsh which allows completion to be customized very thoroughly. This option prevents certain patterns from completion: autoload -Uz compinit; compinit # ignore some common patterns that we usually don't want as completions zstyle ':completion:*' ignored-patterns "/usr/share/iscan" "/usr/lib/iscan" \ "iscan" "/usr/lib/ispell" "ispell" "/usr/...


1

One way I'd suggest is to have an alias if we are just concerned about the home directory path, adding the below to your profile would help here, whatever unique alias you made should autocomplete with even a first character. alias myHome="cd /home/elsherbini" . .profile my\T pwd /home/elsherbini EDIT: The other way could be binding a key but even this ...


1

That's a lot of commands doing the same things only one command could do. I don't know how "listener.ora" is formatted, but the below line should work fine, at most with some minor tweaking. sed -rn '/PORT/ s/.*([0-9]{4}).*/\1/p' /ora$sid/dbs/listener.ora -r for regex -n for suppressing automatic printing /PORT/ to find line containing PORT s/ for replace ...


1

I guess this wasn't such an obscure need. My regular programming editor, pluma supports this via the "find" dialog using the option "Match entire word only". I'm sure many other editors have this as well -- although at the moment, apparently not pluma's ancestor, gedit.


1

You can do it like this: $ MYVAR='<li class="rainbow-list-item southern disrupted " data-line-id="lul-southern" data-severity-codes="4,6,9" data-mode="national-rail">' $ echo $MYVAR | tr ' ' '\n' | awk -F= '/data-severity-codes/ {print $2}' "4,6,9" You can also use sed to remove " and replace , by a blank if you need it: $ echo $MYVAR | tr ' ' '\n' ...


1

You can use the matching operator =~: #! /bin/bash var='<li class="rainbow-list-item southern disrupted " data-line-id="lul-southern" data-severity-codes="4,6,9" data-mode="national-rail">' if [[ $var =~ data-severity-codes=\"([^\"]+)\" ]] ; then echo ${BASH_REMATCH[1]} fi Output: 4,6,9 Or use an XML-aware tool: xmllint --xpath li/@data-...


1

A quick look at the manual page shows: password=arg specifies the CIFS password. If this option is not given then the environment variable PASSWD is used. If the password is not specified directly or indirectly via an argument to mount, mount.cifs will prompt for a password, unless the guest option is specified. So, ...


1

Press Up or Ctrl+P to recall the previous line. Repeat if you want the line before, etc. Press Home or Ctrl+A to go to the beginning of the line. Press Alt+D to delete the word under the cursor, i.e. the command you want to replace. Type the command you want to run now. Press Enter to run the command.



Only top voted, non community-wiki answers of a minimum length are eligible