Hot answers tagged

15

The difference may be seen via strace: $ strace -ff -o bq watch sh -c 'ls\ /tmp/|wc -l' ^C $ strace -ff -o nobq watch sh -c 'ls /tmp/|wc -l' ^C $ grep exec bq* | grep sh bq.29218:execve("/usr/bin/watch", ["watch", "sh", "-c", "ls\\ /tmp/|wc -l"], [/* 54 vars */]) = 0 bq.29219:execve("/bin/sh", ["sh", "-c", "sh -c ls\\ /tmp/|wc -l"], [/* 56 vars */]) = 0 ...


8

With ls, though you may not always be able to get the time, you should be able to derive the date (year, month and day of the month). In the C locale, the date output in ls -l should either be Mmm dd HH:MM for recent files (and you should be able to derive the year (either this year or the previous one) or Mmm dd YYYY for older files or files with a ...


8

One of my favourite utilties is namei, part of util-linux and hence generally present only on Linux: $ namei /usr/share/foo/bar f: /usr/share/foo/bar d / d usr d share foo - No such file or directory But its output is not very parseable. So, if you just wish to point out something is missing, namei might be useful. It's useful for troubleshooting ...


8

A few points about the change. It only happens when outputting to terminals It disambiguates the output for users Output can be pasted back in the shell for further processing Users can get back to the old format by adding -N to their ls alias


7

There are two main categories of watch commands (of the ones that are to run commands periodically, watch is not a standard command, there are even systems where watch does something completely different like snooping on another tty line on FreeBSD). One that already passes the concatenation of its arguments with spaces to a shell (it does in effect call sh ...


6

--color adds escape sequences for the color. You can see this if you redirect the output (of ls --color) to a file. This is what it looks like: drwxr-xr-x 6 root root 4.0K Jan 9 08:23 ^[[01;34m.cabal^[[0m/ To account for this, try this instead: ls -lhAF1 --color | grep -E '^d.*[0-9]{2}:[0-9]{2} .*\.'


6

You're using the -s option to ls. A file's size and the amount of disk space it takes up may differ. Consider for example, if you open new file, seek 1G into it, and write something, the OS doesn't allocate 1G (plus the space for something) on disk, it allocates only the same for something -- this is called a "sparse file". I wrote a small C program to ...


6

You can chose quoting style: ls --quoting-style=escape The same as: ls -b or: QUOTING_STYLE=escape ls Make it an alias, or set QUOTING_STYLE in your .bashrc.


6

Parsing ls is often a bad idea. Often, but not always. Here's another suggestion for you, which collects the required directories together before passing the set to ls. find .* -maxdepth 0 -type d \( -name '.[^.]' -o -name '.??*' \) -exec ls -ld --color=always {} + It's been pointed out that the original code actually limits the list of directories to ...


3

Mac uses BSD ls. See man ls for details. The format of LS_COLORS is different. The variable name isn't even LS_COLORS, it's LSCOLORS. The links I found that were most helpful in figuring this out were this blog post, and this article which was linked to from the blog post. The default value for LSCOLORS is exfxcxdxbxegedabagacad. To leave everything at ...


3

you can have the ls filter for only .jpg files ls *.jpg > all.txt


3

Your filesystem is mounted with relatime by default. Access times will not be updated, if they're already newer than or equal to the modified time of the file. This is an (POSIX-violating) optimization, to avoid every single file read causing a write to the disk. Apparent atime updates are not affected by buffering. (lazyatime will buffer atime for up to ...


2

thanks for the response, but I wanted to list everything, not just the .jpg. I realized there is a simple solution to that which is ls * > all.txt instead of ls > all.txt Does anyone know why adding the wildcard would prevent the output file to include itself?


2

A more general solution than the one proposed by @ShayneManning: ls | grep -v '^all.txt$' > all.txt grep is used to filter lines by content. The option -v inverses the filter. So, all.txt will be excluded from the output of ls. All the other names will be printed to all.txt.


2

find . -type f | awk -F / '{a[$2]=a[$2] " " $0}; END {for(b in a){print a[b]}}'


2

/sys/class/gpio/gpio60 is a symbolic link. That's a special type of file that points to another file. When accessing the file contents, symbolic links are transparent: they act like their target (the file they point to). But when listing directories, symbolic links appear as themselves; ls -l shows them with l in the leftmost column, and shows their target ...


2

The reason is that ls always colorizes its output even if it is connected to a terminal. From man ls: --color[=WHEN] colorize the output. WHEN defaults to 'always' or can be 'never' or 'auto'. More info below Many other tools such as grep do not retain colors when standard output is terminal but for some reasons ls was ...


1

There is absolutely no reason to parse ls output when the expansion is actually done by the shell. Instead of this code: for data in `ls a*| sort -n` You may use this: for data in a* Even if you need to sort it, use this (which will fail for filenames with new lines): for data in $( echo a* | sort -n ) And the best way to work with several values ...


1

Something like this (accounting for pathnames with embedded blanks): #!/bin/sh explain() { if [ -d "$1" ] then printf "\t%s: is a directory\n" "$1" elif [ -e "$1" ] then printf "\t%s: is not a directory\n" "$1" else printf "\t%s: does not exist\n" "$1" fi } for item in "$@" do last= test="$item" ...


1

If you have GNU ls: ls --time-style=long-iso -l or ls --time-style=+FMT -l where FMT follows date command format. In your case: ls --time-style=+%Y/%m/%d -l


1

With zsh: for d (Sample*) print -r $d/*/* Or to print all non-directory files: for d (Sample*) print -r $d/**/*(^/) (note that if there are file names that contain newline characters, that won't be on one line). Or with any Bourne-like shell: for d in Sample*; do find "$d" ! -type d | paste -sd ' ' - done


1

You are seeing a file named ~ in the Downloads directory; that symbol also happens to refer to your home directory, when used at the beginning of an unquoted string (here is what bash does). To remove the file, you have many options; here are two of the simplest: rm ~/Downloads/~ cd ~/Downloads && rm ./~ Add the -i flag to any of the rm commands ...



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