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 ...
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/...
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 ...
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@...
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.
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 ...
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 ...
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 < ...
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}(?=.*\...
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....
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.
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
shell-script × 16786bash × 6967
shell × 2438
linux × 1940
scripting × 1180
text-processing × 1125
awk × 1005
sed × 856
files × 499
grep × 482
command-line × 420
ssh × 383
find × 377
variable × 339
ubuntu × 310
zsh × 303
cron × 287
regular-expression × 248
quoting × 231
ksh × 230
pipe × 225
io-redirection × 218
terminal × 205
date × 198
rename × 181