Get the latest tech news

Why you shouldn't parse the output of ls(1)


Contents The ls(1) command is pretty good at showing you the attributes of a single file (at least in some cases), but when you ask it for a list of files, there's a huge problem: Unix allows almost any character in a filename, including whitespace, newlines, commas, pipe symbols, and pretty much anything else you'd ever try to use as a delimiter except NUL. There are proposals to try and "fix" this within POSIX, but they won't help in dealing with the current situation (see also how to deal with filenames correctly).

If you want the oldest or newest file in a directory, don't use ls -t | head -1-- read Bash FAQ 99 instead. There is usually no need to write the filenames out in a straight line and then rely on some other program to read the stream and separate the names back out. On Debian unstable ( circa 2009), with a contemporary version of GNU coreutils, ls showed the timestamps in two fields, with the first being Y-M-D and the second being H:M, no matter how old the file is.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of output

output

Photo of ls(1

ls(1

Related news:

News photo

Exactly.ai secures $4M to help artists use AI to scale up their output

News photo

Korean Lithium Refiner Ecopro Cuts Output on Weakening EV Demand

News photo

Tesla Trims Car Output in China as EV Sales Growth Slows