Hot answers tagged

200

More precisely, a double dash (--) is used in bash built-in commands and many other commands to signify the end of command options, after which only positional parameters are accepted. Example use: lets say you want to grep a file for the string -v - normally -v will be considered the option to reverse the matching meaning (only show lines that do not match)...


91

In the simplest calling of sed, it has one line of text in the pattern space, ie. 1 line of \n delimited text from the input. The single line in the pattern space has no \n... That's why your regex is not finding anything. You can read multiple lines into the pattern-space and manipulate things surprisingly well, but with a more than normal effort.. Sed ...


70

The main differences are: wget's major strong side compared to curl is its ability to download recursively. wget is command line only. There's no lib or anything, but curl features and is powered by libcurl. curl supports FTP, FTPS, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, DICT, LDAP, LDAPS, FILE, POP3, IMAP, SMTP, RTMP and RTSP. wget supports HTTP, HTTPS and ...


61

From your comments, you seem to be confused about exactly what a shell is. The kernel is responsible for managing the system. It's the part that actually loads and runs programs, accesses files, allocates memory, etc. But the kernel has no user interface; you can only communicate with it by using another program as an intermediary. A shell is a program ...


60

Use perl instead of sed: $ perl -0777 -i.original -pe 's/a test\nPlease do not/not a test\nBe/igs' alpha.txt $ diff alpha.txt{,.original} 2,3c2,3 < not a test < Be --- > a test > Please do not -pie is your standard "replace in place" command-line sequence, and -0777 causes perl to slurp files whole. See perldoc perlrun to find out more about ...


59

Set Membership $ grep -xc 'element' set # outputs 1 if element is in set # outputs >1 if set is a multi-set # outputs 0 if element is not in set $ grep -xq 'element' set # returns 0 (true) if element is in set # returns 1 (false) if element is not in set $ awk '$...


59

In linux there are many tools for that, for example lsblk, fdisk -l or parted -l. Example $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 238.5G 0 disk ├─sda1 8:1 0 200M 0 part /boot/efi ├─sda2 8:2 0 500M 0 part /boot └─sda3 8:3 0 237.8G 0 part ├─...


57

Using - as a filename to mean stdin/stdout is a convention that a lot of programs use. It is not a special property of the filename. The kernel does not recognise - as special so any system calls referring to - as a filename will use - literally as the filename. With bash redirection, - is not recognised as a special filename, so bash will use that as the ...


56

The following is what I ended up using to reliably create a temporary directory that works on both Linux and Darwin (Mac OS X), without hardcoding $TMPDIR or /tmp: mytmpdir=`mktemp -d 2>/dev/null || mktemp -d -t 'mytmpdir'` Background: The GNU mktemp command requires no arguments. Plain mktemp will create a temporary file in the system temporary ...


49

It's usually used as a quick and dirty way to provide answers to an interactive script: yes | rm -r large_directory will not prompt you about any file being removed. Of course in the case of rm, you can always supply -f to make it steamroll the directory removal, but not all tools are so forgiving. Update A more relevant example of this that I recently ...


38

I know there is already a selected answer, but you can get the requested behavior with just ls: ls -ld -- */ (Note that the '--' marks the end of parameters, preventing folder names beginning with a hyphen from being interpreted as further command options.) This will list all the directories in the current working directory where it is run. To get all ...


33

mknod was originally used to create the character and block devices that populate /dev/. Nowadays software like udev automatically creates and removes device nodes on the virtual filesystem when the corresponding hardware is detected by the kernel, but originally /dev was just a directory in / that was populated during install. So yes, in case of a near ...


30

I think, it's better to replace \n symbol with some other symbol, and then work as usual: e.g. not-worked source code: cat alpha.txt | sed -e 's/a test\nPlease do not/not a test\nBe/' can be changed to: cat alpha.txt | tr '\n' '\r' | sed -e 's/a test\rPlease do not/not a test\rBe/' | tr '\r' '\n' If anybody doesn't know, \n is unix line ending, \r\n -...


30

As of v. 8.21, coreutils includes numfmt: numfmt reads numbers in various representations and reformats them as requested. The most common usage is converting numbers to / from human representation. e.g. printf %s\\n 5607598768908 | numfmt --to=iec-i 5.2Ti Various other examples (including filtering, input/output processing etc) are presented ...


29

Sort of. The shell has no idea what the commands you are running will do, it just connects the output of one to the input of the other. If grep finds more than 10 lines that say "hello world" then head will have all 10 lines it wants, and close the pipe. This will cause grep to be killed with a SIGPIPE, so it does not need to continue scanning a very ...


28

There are several different patterns for options that have been used historically in UNIX applications. Several old ones, like tar, use a positional scheme: command options arguments as for example tar uses tar *something*f "file operated on" *"paths of files to manipulate"* In a first attempt to avoid the confusion, tar and a few other programs ...


28

There is already a command for this: seq 100 104 will print these numbers on separate lines: 100 101 102 103 104 So just direct this output into a file: seq 100 104 > my_file.txt and seq 100 2 104 will print in increments of two, namely: 100, 102, 104


27

In most cases, [ is a shell builtin and is equivalent to test. However, like test, it also exists as a standalone executable: that's the /bin/[ you saw. You can test this with type -a [ (on an Arch Linux system, running bash): $ type -a [ [ is a shell builtin [ is /bin/[ So, on my system, I have two [: my shell's builtin and the executable in /bin. The ...


26

No, there is no such standard tool. Since GNU coreutils 8.21 (Feb 2013, so not yet present in all distributions), on non-embedded Linux and Cygwin, you can use numfmt. It doesn't produce exactly the same output format (as of coreutils 8.23, I don't think you can get 2 digits after the decimal points). $ numfmt --to=iec-i --suffix=B --padding=7 1 177152 ...


24

There are three levels of built-in utilities: Some utilities are really part of the shell as a programming language, even though they are not reserved words. They are control flow utilities (., :, break, continue, return, trap, exit, exec, eval), parameter-related utilities (set, unset, shift, export, readonly, local¹, typeset¹), alias utilities (alias², ...


20

the file command makes "best-guesses" about the encoding. Use the -i parameter to force file to print information about the encoding. I created two files containing german umlauts. one encoded in utf-8 and one encoded in iso-8859-1. $ file -i * file1: text/plain; charset=utf-8 file2: text/plain; charset=iso-8859-1


19

sed has three commands to manage multi-line operations: N, D and P (compare them to normal n, d and p). In this case, you can match the first line of your pattern, use N to append the second line to pattern space and then use s to do your substitution. Something like: /a test$/{ N s/a test\nPlease do not/not a test\nBe/ }


18

This marks end of parameter (option) list. http://linux.about.com/library/cmd/blcmdl1_compgen.htm


18

First, from your experience with the second card, it seems that your reader is damaged and now damages the cards you insert into it. Stop using that reader immediately, and try to recover the card with another reader. If your data is at all valuable, try to get a brand-name reader with better quality than a bottom-price one. If the card is merely partly ...


17

-9 is the signal number (in this case SIGKILL), so kill -9 sends a SIGKILL to the process in question. This signal causes the process to terminate immediately (unless it's waiting in a kernel function). The signal can neither be ignored nor can the receiving process perform any clean up action after receiving the signal (i.e. a signal handler for SIGKILL ...


17

Edit 2015 as of util-linux 2.25, the fallocate utility on Linux has a -d/--dig-hole option for that. fallocate -d the-file Would dig a hole for every block full of zeros in the file On older systems, you can do it by hand: Linux has a FALLOC_FL_PUNCH_HOLE option to fallocate that can do this. I found a script on github with an example: Using ...


16

xdotool exposes the pointer location (xdotool getmouselocation), and recent versions (since 2.20110530.1) indicate which window is at that location as well. None of xwininfo, wmctrl or older versions of xdotool appear to have a way to match a window by a screen position where it's visible. The underlying X library call is XQueryPointer (corresponding to a ...


16

You can use file tool: $ file file.png file.png: PNG image data, 734 x 73, 8-bit/color RGB, non-interlaced $ mv file.png file.txt $ file file.txt file.txt: PNG image data, 734 x 73, 8-bit/color RGB, non-interlaced


15

Beside the main point mentioned in the previous answer the yes command can also be used to test high loads of CPU on a system. yes creates a process which acts as a dummy CPU loader and results in 100% processor usage. http://en.wikipedia.org/wiki/Yes_(Unix)



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