147

Preface: While it may be quite satisfying to upvote an answer such as this and call it a day, please be assured that the GNU coreutils maintainers do not care about SO answer votes, & that if you actually want to encourage them to change, you need to email them as this answer describes. Update 2019: Sometime this past year the maintainers have doubled-...


127

You can chose quoting style: ls --quoting-style=literal The same as: ls -N or: QUOTING_STYLE=literal ls Make it an alias, or set export QUOTING_STYLE=literal in your .bashrc to achieve pre-8.25 behavior.


77

With symlinks, tools have two things they can do: Treat the symlink as a symlink ("preserving its nature"), or Treat the symlink as the type of file that it points to. Saying that -H "preserves its nature" is not a contradiction. Consider the alternative. If you use -L, any symlinks cp finds will be opened, and their contents copied to the target file ...


68

nutshell: yes exhibits similar behavior to most other standard utilities which typically write to a FILE STREAM with output buffered by the libC via stdio. These only do the syscall write() every some 4kb (16kb or 64kb) or whatever the output block BUFSIZ is . echo is a write() per GNU. That's a lot of mode-switching (which is not, apparently, as costly as ...


57

It's not the default since for robustness reasons one may want a copy to take place to protect against data corruption. Also for performance reasons you may want the writes to happen at copy time rather than some latency sensitive process working on a CoW file and being delayed by the writes possibly to a different part of a mechanical disk. Note that from ...


52

A few points about the change. It was introduced in coreutils v8.25, and alignment improved in v8.26 It only happens when outputting to terminals so doesn't break scripts It disambiguates the output for users for files containing whitespace It sanitizes output so it is safe to copy and paste Output is now always valid to copy and paste back to shell Users ...


48

For GNU utilities, the full documentation is in the info page, where you can read: -f Ignored; for compatibility with BSD versions of `touch'. See historic BSD man pages for touch, where -f was to force the touch. If you look at the source of those old BSDs, there was no utimes() system call, so touch would open the file in read+write mode, read one ...


48

“Clobber” in the context of data manipulation means destroying data by overwriting it. In the context of files in a Unix environment, the word was used at least as far back as the early 1980s, possibly earlier. Csh had set noclobber to configure > to refuse to overwrite an existing file (later set -o noclobber in ksh93 and other sh-style shells). When GNU ...


47

It's not a bug. The use case is for when you want to link a file to the same basename but in a different directory: cd /tmp ln -s /etc/passwd ls -l passwd lrwxrwxrwx 1 xxx xxx 11 Jul 29 09:10 passwd -> /etc/passwd It's true that when you do this with a filename that is in the same directory it creates a link to itself which does not do a whole lot of ...


41

In ksh, Bash, Zsh, Yash or BusyBox sh: [ "$RANDOM" -lt 3277 ] && do_stuff The RANDOM special variable of the Korn, Bash, Yash, Z and BusyBox shells produces a pseudo-random decimal integer value between 0 and 32767 every time it’s evaluated, so the above gives (close to) a one-in-ten chance. You can use this to produce a function which behaves as ...


37

The [ binary residing under the /bin tree in many GNU/Linux distributions is not something to be alarmed off. At least in my Fedora 19 it is a part of the coreutils package, as demonstrated below: $ rpm -qf /bin/[ coreutils-8.21-13.fc19.x86_64 and is a synonym for test to allow for expressions like [ expression ] to be written in shell scripts or even ...


33

This is documented, for Coreutils’ md5sum: If file contains a backslash or newline, the line is started with a backslash, and each problematic character in the file name is escaped with a backslash, making the output unambiguous even in the presence of arbitrary file names. (file is the filename, not the file’s contents). b2sum, sha1sum, and the various ...


31

- 1 month will subtract one from the month number, and then if the resulting date is not valid (February 30, for example), adjust it so that it is valid. So December 31 - 1 month is December 1, not a day in November, and March 31 - 1 month is March 3 (unless executed in a leap year). Here's quote from the info page for Gnu date (which is the date version ...


31

Your . trick can only be used when you're copying a directory, not a file. The -T option works with both directories and files. If you do: cp srcfile destfile and there's already a directory named destfile it will copy to destfile/srcfile, which may not be intended. So you use cp -T srcfile destfile and you correctly get the error: cp: cannot overwrite ...


27

Because this is actually a standard term. As explained in Wikipedia: In software engineering, clobbering a file or computer memory is overwriting its contents. The Jargon File defines clobbering as To overwrite, usually unintentionally: "I walked off the end of the array and clobbered the stack." Compare mung, scribble, trash, and ...


25

Izkata's comment revealed the answer: locale-specific comparisons. The sort command uses the locale indicated by the environment, whereas Python defaults to a byte order comparison. Comparing UTF-8 strings is harder than comparing byte strings. $ time (LC_ALL=C sort <numbers.txt >s2.txt) real 0m5.485s user 0m14.028s sys 0m0.404s How about ...


25

Non-standard solution: [ $(date +%1N) == 1 ] && do_stuff Check if the last digit of the current time in nanoseconds is 1!


23

The difference in behavior between -L and -H comes when -r is specified as well. cp won't create symlinks in subdirectories with -L -r but it will if you use -H -r.


23

du without an output format specifier gives disk usage in blocks of 512 bytes, not kilobytes. You can use the option -k to display in kilobytes instead. On OS X (or macOS, or MacOS, or Macos; whichever you like), you can customize the default unit by setting the environment variable BLOCKSIZE (this affects other commands as well).


23

tac works with records and their separators, attached, by default after the corresponding record. This is somewhat counter-intuitive compared to other record-based tools (such as AWK) where separators are detached. With -b, the records, with their newline attached, are as follows (in original order): Hello \nNew \nWorld \n! \n Output in reverse, this ...


22

The problem with cp/mv/ln as they were originally designed is that they're two commands in one (copy to and copy into). cp A B is either copy A to B or copy A into B (copy A to B/A) depending on whether B exists and is a directory or not (and more variations if B is a symlink to a directory). That's bad because it's ambiguous. So the GNU implementations ...


21

A better question would be why is your shell writing the file so slowly. Any self-contained compiled program that uses file writing syscalls responsibly (not flushing every character at a time) would do it reasonably quicky. What you are doing, is writing lines in an interpreted language (the shell), and in addition you do a lot of unnecessary input output ...


21

An alternative to using $RANDOM is the shuf command: [[ $(shuf -i 1-10 -n 1) == 1 ]] && do_stuff will do the job. Also useful for randomly selecting lines from a file, eg. for a music playlist.


19

By default, cp tests if its last argument is an existing directory. If this happens, cp creates a link inside that directory, with the base name of the source. That is, given the command cp foo/bar wibble if wibble is an existing directory then cp copies the source to wibble/bar. If wibble does not exist then cp links the source to wibble. If you want to ...


19

You can put the grep in an if condition, or if you don't care about the exit status, add || true. Example: grep kills the shell $ bash $ set -e $ echo $$ 33913 $ grep foo /etc/motd $ echo $$ 9233 solution 1: throw away the non-zero exit status $ bash $ set -e $ echo $$ 34074 $ grep foo /etc/motd || true $ echo $$ 34074 solution 2: explicitly test the ...


19

It's straightforward in shells that support process substitution, e.g. bash $ echo foo | tee >(xsel) foo $ xsel -o foo Otherwise, you could use a FIFO (although it lacks convenience) $ mkfifo _myfifo $ xsel < _myfifo & $ echo bar | tee _myfifo bar $ xsel -o bar [1] + Done xsel 0<_myfifo $


18

As @samiam has stated the list is returned to you in a semi-random order via readdir(). I'll just add the following. The list returned is what I would call the directory order. On older filesystems, the order is often the creation order that the file entries in the directory's table were added. There is of course a caveat to this, when a directory entry is ...


17

Stephen Kitt's answer covers the what and I will try to cover why this change was implemented. First, someone observed that a filename containing newlines1 could result in ambiguous output. For example, consider this output: d41d8cd98f00b204e9800998ecf8427e foo 25af89c92254a806b2e93fffd8ac1814 bar Does this mean there were two files foo and bar, or only ...


16

The command dircolors is specific to GNU coreutils, so you'll find it on non-embedded Linux and on Cygwin but not on other unix systems such as OSX. The generated settings in your .zshrc aren't portable to OSX. Since you're using the default colors, you can pass an empty string to the list-colors to get colors in file completions. For colors with the ...


14

Since FreeBSD has no command dircolor and OS X has the base of FreeBSD you can't use it. The simplest thing is to use export CLICOLOR=YES in your .zshrc and .bashrc and remove eval "$(dircolors -b)". To change the colors you can use the environment variable LSCOLORS. For example: export LSCOLORS="Gxfxcxdxbxegedabagacad" You can find out more about it ...


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