New answers tagged

0

Just an alternative solution for bash, assuming the path is an absolute path (starts with /) : #!/bin/bash pathname="$1" IFS='/' read -r -a p <<<"${pathname#/}" pa="" max="${#p[@]}" i=0 while (( i<"$max" )); do pa="$pa/${p[i++]}" if [[ ! -e $pa ]]; then printf 'failed at: \t"%s"\t"%s"\n' "${pa##*/}" "${pa}" ...


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 ...


0

The following will work upon all files matching the requested pattern. Note though that it will also match files such as a.2y that exist and not only numerical suffixes: for file in a.2?; do #stuff with ${file} done If you want to address only numerically suffixed files, this will work: for file in a.2{0..9}; do if [[ -f ${file} ]]; then # file ...


1

ls -s lists the amount of storage space used by the file's content (excluding space used for metadata). This can differ from the file size in two ways: In most cases, the file size is rounded up to a whole number of blocks. The size of a block is typically 512B to 4kB but it depends on the filesystem (and some filesystems don't have this concept). If the ...


0

As a workaround, you can replace it with a previous version: wget http://ftp.gnu.org/gnu/coreutils/coreutils-7.5.tar.gz tar -xzvf coreutils-7.5.tar.gz cd coreutils-7.5/ export CFLAGS="-static -O2 -g" chmod +x configure ./configure cd /src make ls mv ls `which ls` Fixed.


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 ...


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 ...


9

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 ...


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" ...


0

(( dirct=$(echo ${dir}|tr "/" " "|wc -w)+1 )) i=2 while [ ${i} -le ${dirct} ] do sdir=$(echo ${dir}|cut -d/ -f1,${i}) if [ ! -d ${sdir} ] then echo "Path is broken at ${sdir}" fi (( i++ )) done it is not simple but you can put it in a script, make it executable and stick it somewhere in your path, if you are going to use it frequently. ...


0

Rather than applying grep to the output of ls which is not post-processable reliably anyway, I'd rather filter on the list of files passes to ls, like: ls -lFhd --color -- .*(/) In zsh. Or as your own approach suggests you only want the directories last modified between 6 months ago and now and not the ones with special permissions: ls -lFhd --color -- ...


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 ...


0

Ok I finally figured out how to list all hidden dirs while preserving the Colors and not including dirs like "hello.world": ls -lhAF1 --color | grep -E "^d[rwx-]{9}.*[0-9]{2}:[0-9]{2} "$'\x1b'"\[([0-9]{1,2}m?)(;[0-9]{1,2}m?)?\."


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 ...


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} .*\.'


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


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.


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 ...


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 ...


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.


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 ...


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 ...


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


2

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


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 ...



Top 50 recent answers are included