Skip to main content
28votes
Accepted

Why does cd '' succeed in bash?

The most recent POSIX specification for the cd utility (Issue 8, from 2024) has this in the OPERANDS section: [...] If directory is an empty string, cd shall write a diagnostic message to standard ...
Kusalananda's user avatar
17votes

Idiomatic way of generating a unique filename?

Bit of a conflicting requirement! Idiomatically, you put unique files either into $TMPDIR (or /tmp by default), or they are state-carrying files, in which case they belong in $XDG_STATE_HOME/...
Marcus Müller's user avatar
7votes
Accepted

Idiomatic way of generating a unique filename?

Well, do you want a guaranteed unique name, or do you want a probabilistically unique name? If the latter, then yes, by all means just generate a random name using what ever means, use that and get on ...
ilkkachu's user avatar
6votes

Idiomatic way of generating a unique filename?

mktemp does have the following option: -u, --dry-run do not create anything; merely print a name (unsafe) E.g. got just a temporary pathname without creation using: [mr_halfword@...
Chester Gillon's user avatar
6votes

Idiomatic way of generating a unique filename?

From the mktemp man page... -u, --dry-run do not create anything; merely print a name (unsafe) And you cannot guarantee the future.
Bob Goddard's user avatar
4votes

Counting unique IP addresses per hour in existing log files

With awk: awk ' { hits[$1 " " substr($4,1,16) "00]"]++ } END { for (k in hits) printf "%s - %d hits\n", k, hits[k] } ' access*.log This will: Create an array ...
jesse_b's user avatar
  • 40.4k
3votes

Idiomatic way of generating a unique filename?

The true idomatic way (in Unix) to generate a unique temporary filename is to use the current process ID to form the name. For example in a shell script: touch tmpfile.$$ There can only ever be one ...
Greg A. Woods's user avatar
3votes
Accepted

Creating in Linux files in GB or MB range by various size by read a file

It sounds like you might want to do something like this: $ cat ./tst.sh #!/usr/bin/env bash while read -r idx sfx size; do echo fallocate "file${idx}.${sfx}" "$size" done < ...
Ed Morton's user avatar
2votes

Counting unique IP addresses per hour in existing log files

To handle the cases where there may be more than one IP address on the line and not necessarily in the first field: perl -lne ' $hits{$&}->{"$1:00$2"}++ while m{\d+(?:\.\d+){3}(?=.*\...
Stéphane Chazelas's user avatar
1vote

Idiomatic way of generating a unique filename?

I don't understand why a temporary file would not be in /tmp or the system's temporary directory, but your script could look like: #! /usr/bin/env bash r_dir=`mktemp -d` file="${r_dir}/filename....
user7215's user avatar
1vote

Idiomatic way of generating a unique filename?

No bash one liners?? unbelievable! echo $(head /dev/urandom | tr -dc A-Za-z0-9 | head -c6) -c6 6 letters long or 6^62 possibilities.
j0h's user avatar
  • 3,911

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

close